1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2020 Google LLC
8 #include <test/suites.h>
12 /* This is large enough for any of the test strings */
13 #define TEST_STR_SIZE 200
15 static const char str1[] = "I'm sorry I'm late.";
16 static const char str2[] = "1099abNo, don't bother apologising.";
17 static const char str3[] = "0xbI'm sorry you're alive.";
18 static const char str4[] = "1234567890123 I lost closer friends";
19 static const char str5[] = "0x9876543210the last time I was deloused";
21 /* Declare a new str test */
22 #define STR_TEST(_name, _flags) UNIT_TEST(_name, _flags, str_test)
24 static int str_upper(struct unit_test_state *uts)
26 char out[TEST_STR_SIZE];
28 /* Make sure it adds a terminator */
29 out[strlen(str1)] = 'a';
30 str_to_upper(str1, out, SIZE_MAX);
31 ut_asserteq_str("I'M SORRY I'M LATE.", out);
33 /* In-place operation */
35 str_to_upper(out, out, SIZE_MAX);
36 ut_asserteq_str("1099ABNO, DON'T BOTHER APOLOGISING.", out);
39 str_to_upper(str1, out, 7);
40 ut_asserteq_str("I'M SORO, DON'T BOTHER APOLOGISING.", out);
42 /* In-place with limited length */
44 str_to_upper(out, out, 7);
45 ut_asserteq_str("1099ABNo, don't bother apologising.", out);
47 /* Copy an empty string to a buffer with space*/
49 str_to_upper("", out, SIZE_MAX);
50 ut_asserteq('\0', *out);
51 ut_asserteq(0x7f, out[1]);
53 /* Copy an empty string to a buffer with no space*/
55 str_to_upper("", out, 0);
56 ut_asserteq(0x7f, out[0]);
60 STR_TEST(str_upper, 0);
62 static int run_strtoul(struct unit_test_state *uts, const char *str, int base,
63 ulong expect_val, int expect_endp_offset, bool upper)
65 char out[TEST_STR_SIZE];
71 str_to_upper(out, out, -1);
73 val = simple_strtoul(out, &endp, base);
74 ut_asserteq(expect_val, val);
75 ut_asserteq(expect_endp_offset, endp - out);
80 static int str_simple_strtoul(struct unit_test_state *uts)
84 /* Check that it is case-insentive */
85 for (upper = 0; upper < 2; upper++) {
86 /* Base 10 and base 16 */
87 ut_assertok(run_strtoul(uts, str2, 10, 1099, 4, upper));
88 ut_assertok(run_strtoul(uts, str2, 16, 0x1099ab, 6, upper));
89 ut_assertok(run_strtoul(uts, str3, 16, 0xb, 3, upper));
90 ut_assertok(run_strtoul(uts, str3, 10, 0, 1, upper));
93 ut_assertok(run_strtoul(uts, str1, 10, 0, 0, upper));
96 ut_assertok(run_strtoul(uts, str1, 0, 0, 0, upper));
97 ut_assertok(run_strtoul(uts, str2, 0, 1099, 4, upper));
98 ut_assertok(run_strtoul(uts, str3, 0, 0xb, 3, upper));
101 ut_assertok(run_strtoul(uts, str1, 2, 0, 0, upper));
102 ut_assertok(run_strtoul(uts, str2, 2, 2, 2, upper));
105 /* Check endp being NULL */
106 ut_asserteq(1099, simple_strtoul(str2, NULL, 0));
110 STR_TEST(str_simple_strtoul, 0);
112 static int run_strtoull(struct unit_test_state *uts, const char *str, int base,
113 unsigned long long expect_val, int expect_endp_offset,
116 char out[TEST_STR_SIZE];
118 unsigned long long val;
122 str_to_upper(out, out, -1);
124 val = simple_strtoull(out, &endp, base);
125 ut_asserteq(expect_val, val);
126 ut_asserteq(expect_endp_offset, endp - out);
131 static int str_simple_strtoull(struct unit_test_state *uts)
135 /* Check that it is case-insentive */
136 for (upper = 0; upper < 2; upper++) {
137 /* Base 10 and base 16 */
138 ut_assertok(run_strtoull(uts, str2, 10, 1099, 4, upper));
139 ut_assertok(run_strtoull(uts, str2, 16, 0x1099ab, 6, upper));
140 ut_assertok(run_strtoull(uts, str3, 16, 0xb, 3, upper));
141 ut_assertok(run_strtoull(uts, str3, 10, 0, 1, upper));
144 ut_assertok(run_strtoull(uts, str4, 10, 1234567890123, 13,
146 ut_assertok(run_strtoull(uts, str4, 16, 0x1234567890123, 13,
148 ut_assertok(run_strtoull(uts, str5, 0, 0x9876543210, 12,
152 ut_assertok(run_strtoull(uts, str1, 10, 0, 0, upper));
155 ut_assertok(run_strtoull(uts, str1, 0, 0, 0, upper));
156 ut_assertok(run_strtoull(uts, str2, 0, 1099, 4, upper));
157 ut_assertok(run_strtoull(uts, str3, 0, 0xb, 3, upper));
160 ut_assertok(run_strtoull(uts, str1, 2, 0, 0, upper));
161 ut_assertok(run_strtoull(uts, str2, 2, 2, 2, upper));
164 /* Check endp being NULL */
165 ut_asserteq(1099, simple_strtoull(str2, NULL, 0));
169 STR_TEST(str_simple_strtoull, 0);
171 static int str_hextoul(struct unit_test_state *uts)
175 /* Just a simple test, since we know this uses simple_strtoul() */
176 ut_asserteq(0x1099ab, hextoul(str2, &endp));
177 ut_asserteq(6, endp - str2);
181 STR_TEST(str_hextoul, 0);
183 static int str_dectoul(struct unit_test_state *uts)
187 /* Just a simple test, since we know this uses simple_strtoul() */
188 ut_asserteq(1099, dectoul(str2, &endp));
189 ut_asserteq(4, endp - str2);
193 STR_TEST(str_dectoul, 0);
195 int do_ut_str(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
197 struct unit_test *tests = UNIT_TEST_SUITE_START(str_test);
198 const int n_ents = UNIT_TEST_SUITE_COUNT(str_test);
200 return cmd_ut_category("str", "str_", tests, n_ents, argc, argv);