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";
20 static const char str6[] = "0778octal is seldom used";
21 static const char str7[] = "707it is a piece of computing history";
23 /* Declare a new str test */
24 #define STR_TEST(_name, _flags) UNIT_TEST(_name, _flags, str_test)
26 static int str_upper(struct unit_test_state *uts)
28 char out[TEST_STR_SIZE];
30 /* Make sure it adds a terminator */
31 out[strlen(str1)] = 'a';
32 str_to_upper(str1, out, SIZE_MAX);
33 ut_asserteq_str("I'M SORRY I'M LATE.", out);
35 /* In-place operation */
37 str_to_upper(out, out, SIZE_MAX);
38 ut_asserteq_str("1099ABNO, DON'T BOTHER APOLOGISING.", out);
41 str_to_upper(str1, out, 7);
42 ut_asserteq_str("I'M SORO, DON'T BOTHER APOLOGISING.", out);
44 /* In-place with limited length */
46 str_to_upper(out, out, 7);
47 ut_asserteq_str("1099ABNo, don't bother apologising.", out);
49 /* Copy an empty string to a buffer with space*/
51 str_to_upper("", out, SIZE_MAX);
52 ut_asserteq('\0', *out);
53 ut_asserteq(0x7f, out[1]);
55 /* Copy an empty string to a buffer with no space*/
57 str_to_upper("", out, 0);
58 ut_asserteq(0x7f, out[0]);
62 STR_TEST(str_upper, 0);
64 static int run_strtoul(struct unit_test_state *uts, const char *str, int base,
65 ulong expect_val, int expect_endp_offset, bool upper)
67 char out[TEST_STR_SIZE];
73 str_to_upper(out, out, -1);
75 val = simple_strtoul(out, &endp, base);
76 ut_asserteq(expect_val, val);
77 ut_asserteq(expect_endp_offset, endp - out);
82 static int str_simple_strtoul(struct unit_test_state *uts)
86 /* Check that it is case-insentive */
87 for (upper = 0; upper < 2; upper++) {
88 /* Base 10 and base 16 */
89 ut_assertok(run_strtoul(uts, str2, 10, 1099, 4, upper));
90 ut_assertok(run_strtoul(uts, str2, 16, 0x1099ab, 6, upper));
91 ut_assertok(run_strtoul(uts, str3, 16, 0xb, 3, upper));
92 ut_assertok(run_strtoul(uts, str3, 10, 0xb, 3, upper));
95 ut_assertok(run_strtoul(uts, str6, 0, 63, 3, upper));
96 ut_assertok(run_strtoul(uts, str7, 8, 0x1c7, 3, upper));
99 ut_assertok(run_strtoul(uts, str1, 10, 0, 0, upper));
102 ut_assertok(run_strtoul(uts, str1, 0, 0, 0, upper));
103 ut_assertok(run_strtoul(uts, str2, 0, 1099, 4, upper));
104 ut_assertok(run_strtoul(uts, str3, 0, 0xb, 3, upper));
107 ut_assertok(run_strtoul(uts, str1, 2, 0, 0, upper));
108 ut_assertok(run_strtoul(uts, str2, 2, 2, 2, upper));
111 /* Check endp being NULL */
112 ut_asserteq(1099, simple_strtoul(str2, NULL, 0));
116 STR_TEST(str_simple_strtoul, 0);
118 static int run_strtoull(struct unit_test_state *uts, const char *str, int base,
119 unsigned long long expect_val, int expect_endp_offset,
122 char out[TEST_STR_SIZE];
124 unsigned long long val;
128 str_to_upper(out, out, -1);
130 val = simple_strtoull(out, &endp, base);
131 ut_asserteq(expect_val, val);
132 ut_asserteq(expect_endp_offset, endp - out);
137 static int str_simple_strtoull(struct unit_test_state *uts)
141 /* Check that it is case-insentive */
142 for (upper = 0; upper < 2; upper++) {
143 /* Base 10 and base 16 */
144 ut_assertok(run_strtoull(uts, str2, 10, 1099, 4, upper));
145 ut_assertok(run_strtoull(uts, str2, 16, 0x1099ab, 6, upper));
146 ut_assertok(run_strtoull(uts, str3, 16, 0xb, 3, upper));
147 ut_assertok(run_strtoull(uts, str3, 10, 0xb, 3, upper));
150 ut_assertok(run_strtoull(uts, str6, 0, 63, 3, upper));
151 ut_assertok(run_strtoull(uts, str7, 8, 0x1c7, 3, upper));
154 ut_assertok(run_strtoull(uts, str4, 10, 1234567890123, 13,
156 ut_assertok(run_strtoull(uts, str4, 16, 0x1234567890123, 13,
158 ut_assertok(run_strtoull(uts, str5, 0, 0x9876543210, 12,
162 ut_assertok(run_strtoull(uts, str1, 10, 0, 0, upper));
165 ut_assertok(run_strtoull(uts, str1, 0, 0, 0, upper));
166 ut_assertok(run_strtoull(uts, str2, 0, 1099, 4, upper));
167 ut_assertok(run_strtoull(uts, str3, 0, 0xb, 3, upper));
170 ut_assertok(run_strtoull(uts, str1, 2, 0, 0, upper));
171 ut_assertok(run_strtoull(uts, str2, 2, 2, 2, upper));
174 /* Check endp being NULL */
175 ut_asserteq(1099, simple_strtoull(str2, NULL, 0));
179 STR_TEST(str_simple_strtoull, 0);
181 static int str_hextoul(struct unit_test_state *uts)
185 /* Just a simple test, since we know this uses simple_strtoul() */
186 ut_asserteq(0x1099ab, hextoul(str2, &endp));
187 ut_asserteq(6, endp - str2);
191 STR_TEST(str_hextoul, 0);
193 static int str_dectoul(struct unit_test_state *uts)
197 /* Just a simple test, since we know this uses simple_strtoul() */
198 ut_asserteq(1099, dectoul(str2, &endp));
199 ut_asserteq(4, endp - str2);
203 STR_TEST(str_dectoul, 0);
205 int do_ut_str(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
207 struct unit_test *tests = UNIT_TEST_SUITE_START(str_test);
208 const int n_ents = UNIT_TEST_SUITE_COUNT(str_test);
210 return cmd_ut_category("str", "str_", tests, n_ents, argc, argv);