David Brazdil | 136f294 | 2019-09-23 14:11:03 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 The Hafnium Authors. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <gmock/gmock.h> |
| 18 | |
| 19 | extern "C" { |
| 20 | #include "hf/string.h" |
| 21 | } |
| 22 | |
| 23 | namespace |
| 24 | { |
| 25 | TEST(string, valid) |
| 26 | { |
| 27 | struct string str; |
| 28 | constexpr const char data[] = "test"; |
| 29 | |
| 30 | string_init_empty(&str); |
| 31 | ASSERT_TRUE(string_is_empty(&str)); |
| 32 | ASSERT_STREQ(string_data(&str), ""); |
| 33 | |
| 34 | ASSERT_EQ(string_init(&str, data, sizeof(data)), STRING_SUCCESS); |
| 35 | ASSERT_FALSE(string_is_empty(&str)); |
| 36 | ASSERT_STRNE(string_data(&str), ""); |
| 37 | ASSERT_STREQ(string_data(&str), "test"); |
| 38 | } |
| 39 | |
| 40 | TEST(string, data_zero_size) |
| 41 | { |
| 42 | struct string str; |
| 43 | constexpr const char data[] = "test"; |
| 44 | |
| 45 | ASSERT_EQ(string_init(&str, data, 0), STRING_ERROR_INVALID_INPUT); |
| 46 | } |
| 47 | |
| 48 | TEST(string, data_no_null_terminator) |
| 49 | { |
| 50 | struct string str; |
| 51 | constexpr const char data[] = {'t', 'e', 's', 't'}; |
| 52 | |
| 53 | ASSERT_EQ(string_init(&str, data, sizeof(data)), |
| 54 | STRING_ERROR_INVALID_INPUT); |
| 55 | } |
| 56 | |
| 57 | TEST(string, data_two_null_terminators) |
| 58 | { |
| 59 | struct string str; |
| 60 | constexpr const char data[] = {'\0', 't', 'e', 's', 't', '\0'}; |
| 61 | |
| 62 | ASSERT_EQ(string_init(&str, data, sizeof(data)), |
| 63 | STRING_ERROR_INVALID_INPUT); |
| 64 | } |
| 65 | |
| 66 | } /* namespace */ |