1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <commonlib/bsd/string.h>
6 #include <tests/test.h>
8 static void test_strlen(void **state
)
13 assert_int_equal(__builtin_strlen(str
), strlen(str
));
16 assert_int_equal(__builtin_strlen(str
), strlen(str
));
19 assert_int_equal(__builtin_strlen(str
), strlen(str
));
21 assert_int_equal(0, strlen(""));
24 static void test_strnlen(void **state
)
26 /* maxlen is SIZE_MAX */
27 assert_int_equal(8, strnlen("coreboot", SIZE_MAX
));
29 /* maxlen larger than string len */
30 assert_int_equal(8, strnlen("coreboot", 100));
32 /* maxlen equal to string len */
33 assert_int_equal(8, strnlen("coreboot", 8));
35 /* maxlen smaller than string len */
36 assert_int_equal(5, strnlen("coreboot", 5));
39 assert_int_equal(0, strnlen("coreboot", 0));
42 assert_int_equal(0, strnlen("", 3));
45 static void test_strcat(void **state
)
47 static const char str
[] = "Hello ";
48 size_t len
= __builtin_strlen(str
);
49 static const char src
[] = "World";
50 static const char expected
[] = "Hello World";
51 size_t expected_len
= __builtin_strlen(expected
);
57 memset(dst
+ 1, 0xee, sizeof(dst
) - 1);
58 ret
= strcat(dst
, "");
59 assert_ptr_equal(dst
, ret
);
60 assert_int_equal('\0', dst
[0]);
63 memcpy(dst
, str
, len
+ 1);
64 memset(dst
+ len
+ 1, 0xee, sizeof(dst
) - len
- 1);
65 ret
= strcat(dst
, "");
66 assert_memory_equal(str
, dst
, len
+ 1);
69 memset(dst
, 0x0, sizeof(dst
));
70 memset(dst
+ 1, 0xee, sizeof(dst
) - 1);
71 ret
= strcat(dst
, src
);
72 assert_ptr_equal(dst
, ret
);
73 assert_memory_equal(src
, dst
, __builtin_strlen(src
) + 1);
75 /* Non-empty str & dst */
76 memcpy(dst
, str
, len
+ 1);
77 memset(dst
+ len
+ 1, 0xee, sizeof(dst
) - len
- 1);
78 ret
= strcat(dst
, src
);
79 assert_ptr_equal(dst
, ret
);
80 assert_memory_equal(expected
, dst
, expected_len
+ 1);
83 static void test_strncat(void **state
)
85 static const char str
[] = "Hello ";
86 size_t len
= __builtin_strlen(str
);
87 static const char src
[] = "World";
88 size_t src_len
= __builtin_strlen(src
);
89 static const char expected
[] = "Hello World";
90 size_t expected_len
= __builtin_strlen(expected
);
94 /* n larger than src len */
95 memcpy(dst
, str
, len
+ 1);
96 memset(dst
+ len
+ 1, 0xee, sizeof(dst
) - len
- 1);
97 ret
= strncat(dst
, src
, src_len
+ 5);
98 assert_ptr_equal(dst
, ret
);
99 assert_memory_equal(expected
, dst
, expected_len
+ 1);
101 /* n smaller than src len */
102 memcpy(dst
, str
, len
+ 1);
103 memset(dst
+ len
+ 1, 0xee, sizeof(dst
) - len
- 1);
104 ret
= strncat(dst
, src
, src_len
- 2);
105 assert_ptr_equal(dst
, ret
);
106 assert_memory_equal("Hello Wor", dst
, expected_len
- 2 + 1);
109 memcpy(dst
, str
, len
+ 1);
110 memset(dst
+ len
+ 1, 0xee, sizeof(dst
) - len
- 1);
111 ret
= strncat(dst
, src
, 0);
112 assert_ptr_equal(dst
, ret
);
113 assert_memory_equal(str
, dst
, len
+ 1);
116 static void test_skip_atoi(void **state
)
120 const struct str_with_u_val_t
{
124 } str_with_u_val
[] = {
131 for (i
= 0; i
< ARRAY_SIZE(str_with_u_val
); i
++) {
132 ptr
= str_with_u_val
[i
].str
;
134 assert_true(str_with_u_val
[i
].value
== skip_atoi(&ptr
));
135 assert_int_equal(str_with_u_val
[i
].offset
, ptr
- copy
);
141 const struct CMUnitTest tests
[] = {
142 cmocka_unit_test(test_strlen
),
143 cmocka_unit_test(test_strnlen
),
144 cmocka_unit_test(test_strcat
),
145 cmocka_unit_test(test_strncat
),
146 cmocka_unit_test(test_skip_atoi
),
149 return cb_run_group_tests(tests
, NULL
, NULL
);