blob: 222f5d85d41109b4a7b948240665fd444d1c4b5d [file] [log] [blame]
David Brazdil136f2942019-09-23 14:11:03 +01001/*
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
19extern "C" {
20#include "hf/string.h"
21}
22
23namespace
24{
25TEST(string, valid)
26{
27 struct string str;
David Brazdilb856be62020-03-25 10:14:55 +000028 struct memiter it;
David Brazdil136f2942019-09-23 14:11:03 +010029 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 Brazdilb856be62020-03-25 10:14:55 +000035 memiter_init(&it, data, sizeof(data));
36 ASSERT_EQ(string_init(&str, &it), STRING_SUCCESS);
David Brazdil136f2942019-09-23 14:11:03 +010037 ASSERT_FALSE(string_is_empty(&str));
38 ASSERT_STRNE(string_data(&str), "");
39 ASSERT_STREQ(string_data(&str), "test");
40}
41
42TEST(string, data_zero_size)
43{
44 struct string str;
David Brazdilb856be62020-03-25 10:14:55 +000045 struct memiter it;
David Brazdil136f2942019-09-23 14:11:03 +010046 constexpr const char data[] = "test";
47
David Brazdilb856be62020-03-25 10:14:55 +000048 memiter_init(&it, data, 0);
49 ASSERT_EQ(string_init(&str, &it), STRING_ERROR_INVALID_INPUT);
David Brazdil136f2942019-09-23 14:11:03 +010050}
51
52TEST(string, data_no_null_terminator)
53{
54 struct string str;
David Brazdilb856be62020-03-25 10:14:55 +000055 struct memiter it;
David Brazdil136f2942019-09-23 14:11:03 +010056 constexpr const char data[] = {'t', 'e', 's', 't'};
57
David Brazdilb856be62020-03-25 10:14:55 +000058 memiter_init(&it, data, sizeof(data));
59 ASSERT_EQ(string_init(&str, &it), STRING_ERROR_INVALID_INPUT);
David Brazdil136f2942019-09-23 14:11:03 +010060}
61
62TEST(string, data_two_null_terminators)
63{
64 struct string str;
David Brazdilb856be62020-03-25 10:14:55 +000065 struct memiter it;
David Brazdil136f2942019-09-23 14:11:03 +010066 constexpr const char data[] = {'\0', 't', 'e', 's', 't', '\0'};
67
David Brazdilb856be62020-03-25 10:14:55 +000068 memiter_init(&it, data, sizeof(data));
69 ASSERT_EQ(string_init(&str, &it), STRING_ERROR_INVALID_INPUT);
David Brazdil136f2942019-09-23 14:11:03 +010070}
71
72} /* namespace */