sconfig: Move (WEAK_)DEV_PTR from device.h to static.h
[coreboot.git] / tests / commonlib / bsd / string-test.c
blob194177abbbbdffd3c6dbee2b0c07ae093a7f173a
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <commonlib/bsd/string.h>
4 #include <stddef.h>
5 #include <string.h>
6 #include <tests/test.h>
8 static void test_strlen(void **state)
10 const char *str;
12 str = "coreboot";
13 assert_int_equal(__builtin_strlen(str), strlen(str));
15 str = "is\0very";
16 assert_int_equal(__builtin_strlen(str), strlen(str));
18 str = "nice\n";
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));
38 /* maxlen is 0 */
39 assert_int_equal(0, strnlen("coreboot", 0));
41 /* Empty string */
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);
52 char dst[100];
53 char *ret;
55 /* Empty src & dst */
56 dst[0] = '\0';
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]);
62 /* Empty src */
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);
68 /* Empty dst */
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);
91 char dst[100];
92 char *ret;
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);
108 /* n is 0 */
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)
118 int i;
119 char *ptr, *copy;
120 const struct str_with_u_val_t {
121 char *str;
122 uint32_t value;
123 uint32_t offset;
124 } str_with_u_val[] = {
125 {"42aa", 42, 2},
126 {"a", 0, 0},
127 {"0", 0, 1},
128 {"4a2", 4, 1},
131 for (i = 0; i < ARRAY_SIZE(str_with_u_val); i++) {
132 ptr = str_with_u_val[i].str;
133 copy = ptr;
134 assert_true(str_with_u_val[i].value == skip_atoi(&ptr));
135 assert_int_equal(str_with_u_val[i].offset, ptr - copy);
139 int main(void)
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);