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; |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 28 | struct memiter it; |
David Brazdil | 136f294 | 2019-09-23 14:11:03 +0100 | [diff] [blame] | 29 | constexpr const char data[] = "test"; |
| 30 | |
| 31 | string_init_empty(&str); |
| 32 | ASSERT_TRUE(string_is_empty(&str)); |
| 33 | ASSERT_STREQ(string_data(&str), ""); |
| 34 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 35 | memiter_init(&it, data, sizeof(data)); |
| 36 | ASSERT_EQ(string_init(&str, &it), STRING_SUCCESS); |
David Brazdil | 136f294 | 2019-09-23 14:11:03 +0100 | [diff] [blame] | 37 | ASSERT_FALSE(string_is_empty(&str)); |
| 38 | ASSERT_STRNE(string_data(&str), ""); |
| 39 | ASSERT_STREQ(string_data(&str), "test"); |
| 40 | } |
| 41 | |
| 42 | TEST(string, data_zero_size) |
| 43 | { |
| 44 | struct string str; |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 45 | struct memiter it; |
David Brazdil | 136f294 | 2019-09-23 14:11:03 +0100 | [diff] [blame] | 46 | constexpr const char data[] = "test"; |
| 47 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 48 | memiter_init(&it, data, 0); |
| 49 | ASSERT_EQ(string_init(&str, &it), STRING_ERROR_INVALID_INPUT); |
David Brazdil | 136f294 | 2019-09-23 14:11:03 +0100 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | TEST(string, data_no_null_terminator) |
| 53 | { |
| 54 | struct string str; |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 55 | struct memiter it; |
David Brazdil | 136f294 | 2019-09-23 14:11:03 +0100 | [diff] [blame] | 56 | constexpr const char data[] = {'t', 'e', 's', 't'}; |
| 57 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 58 | memiter_init(&it, data, sizeof(data)); |
| 59 | ASSERT_EQ(string_init(&str, &it), STRING_ERROR_INVALID_INPUT); |
David Brazdil | 136f294 | 2019-09-23 14:11:03 +0100 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | TEST(string, data_two_null_terminators) |
| 63 | { |
| 64 | struct string str; |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 65 | struct memiter it; |
David Brazdil | 136f294 | 2019-09-23 14:11:03 +0100 | [diff] [blame] | 66 | constexpr const char data[] = {'\0', 't', 'e', 's', 't', '\0'}; |
| 67 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 68 | memiter_init(&it, data, sizeof(data)); |
| 69 | ASSERT_EQ(string_init(&str, &it), STRING_ERROR_INVALID_INPUT); |
David Brazdil | 136f294 | 2019-09-23 14:11:03 +0100 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | } /* namespace */ |