blob: e62045318c091db2b426b1ee2e38836dd776c9c6 [file] [log] [blame]
Simon Glass4f04d542020-04-08 08:32:55 -06001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2020 Google LLC
4 */
5
Simon Glass4f04d542020-04-08 08:32:55 -06006#include <vsprintf.h>
Simon Glass4563fb42024-11-02 13:36:58 -06007#include <test/lib.h>
Simon Glass4f04d542020-04-08 08:32:55 -06008#include <test/test.h>
9#include <test/ut.h>
10
11/* This is large enough for any of the test strings */
12#define TEST_STR_SIZE 200
13
14static const char str1[] = "I'm sorry I'm late.";
15static const char str2[] = "1099abNo, don't bother apologising.";
16static const char str3[] = "0xbI'm sorry you're alive.";
Simon Glass4d3177d2021-07-24 09:03:33 -060017static const char str4[] = "1234567890123 I lost closer friends";
18static const char str5[] = "0x9876543210the last time I was deloused";
Simon Glassab833ef2021-07-24 09:03:34 -060019static const char str6[] = "0778octal is seldom used";
20static const char str7[] = "707it is a piece of computing history";
Heinrich Schuchardt54441892024-11-13 22:15:12 +010021static const char str8[] = "0x887e2561352d80fa";
22static const char str9[] = "614FF7EAA63009DA";
Simon Glass4f04d542020-04-08 08:32:55 -060023
Patrick Delaunay76fde132020-11-19 10:08:43 +010024static int str_upper(struct unit_test_state *uts)
Simon Glass4f04d542020-04-08 08:32:55 -060025{
Simon Glassfdc79a62020-04-08 08:32:56 -060026 char out[TEST_STR_SIZE];
27
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);
32
33 /* In-place operation */
34 strcpy(out, str2);
35 str_to_upper(out, out, SIZE_MAX);
36 ut_asserteq_str("1099ABNO, DON'T BOTHER APOLOGISING.", out);
37
38 /* Limited length */
39 str_to_upper(str1, out, 7);
40 ut_asserteq_str("I'M SORO, DON'T BOTHER APOLOGISING.", out);
41
42 /* In-place with limited length */
43 strcpy(out, str2);
44 str_to_upper(out, out, 7);
45 ut_asserteq_str("1099ABNo, don't bother apologising.", out);
46
47 /* Copy an empty string to a buffer with space*/
48 out[1] = 0x7f;
49 str_to_upper("", out, SIZE_MAX);
50 ut_asserteq('\0', *out);
51 ut_asserteq(0x7f, out[1]);
52
53 /* Copy an empty string to a buffer with no space*/
54 out[0] = 0x7f;
55 str_to_upper("", out, 0);
56 ut_asserteq(0x7f, out[0]);
57
58 return 0;
59}
Simon Glass4563fb42024-11-02 13:36:58 -060060LIB_TEST(str_upper, 0);
Simon Glassfdc79a62020-04-08 08:32:56 -060061
62static int run_strtoul(struct unit_test_state *uts, const char *str, int base,
63 ulong expect_val, int expect_endp_offset, bool upper)
64{
65 char out[TEST_STR_SIZE];
Simon Glass4f04d542020-04-08 08:32:55 -060066 char *endp;
67 ulong val;
68
Simon Glassfdc79a62020-04-08 08:32:56 -060069 strcpy(out, str);
70 if (upper)
71 str_to_upper(out, out, -1);
72
73 val = simple_strtoul(out, &endp, base);
Simon Glass4f04d542020-04-08 08:32:55 -060074 ut_asserteq(expect_val, val);
Simon Glassfdc79a62020-04-08 08:32:56 -060075 ut_asserteq(expect_endp_offset, endp - out);
Simon Glass4f04d542020-04-08 08:32:55 -060076
77 return 0;
78}
79
80static int str_simple_strtoul(struct unit_test_state *uts)
81{
Simon Glassfdc79a62020-04-08 08:32:56 -060082 int upper;
Simon Glass4f04d542020-04-08 08:32:55 -060083
Simon Glassfdc79a62020-04-08 08:32:56 -060084 /* 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));
Simon Glass96b23442021-07-24 09:03:32 -060089 ut_assertok(run_strtoul(uts, str3, 16, 0xb, 3, upper));
Simon Glasse6951132021-07-24 09:03:38 -060090 ut_assertok(run_strtoul(uts, str3, 10, 0xb, 3, upper));
Simon Glass4f04d542020-04-08 08:32:55 -060091
Simon Glassab833ef2021-07-24 09:03:34 -060092 /* Octal */
93 ut_assertok(run_strtoul(uts, str6, 0, 63, 3, upper));
94 ut_assertok(run_strtoul(uts, str7, 8, 0x1c7, 3, upper));
95
Simon Glassfdc79a62020-04-08 08:32:56 -060096 /* Invalid string */
97 ut_assertok(run_strtoul(uts, str1, 10, 0, 0, upper));
Simon Glass4f04d542020-04-08 08:32:55 -060098
Simon Glassfdc79a62020-04-08 08:32:56 -060099 /* Base 0 */
100 ut_assertok(run_strtoul(uts, str1, 0, 0, 0, upper));
101 ut_assertok(run_strtoul(uts, str2, 0, 1099, 4, upper));
102 ut_assertok(run_strtoul(uts, str3, 0, 0xb, 3, upper));
103
104 /* Base 2 */
105 ut_assertok(run_strtoul(uts, str1, 2, 0, 0, upper));
106 ut_assertok(run_strtoul(uts, str2, 2, 2, 2, upper));
107 }
Simon Glass4f04d542020-04-08 08:32:55 -0600108
109 /* Check endp being NULL */
110 ut_asserteq(1099, simple_strtoul(str2, NULL, 0));
111
112 return 0;
113}
Simon Glass4563fb42024-11-02 13:36:58 -0600114LIB_TEST(str_simple_strtoul, 0);
Simon Glass4f04d542020-04-08 08:32:55 -0600115
Simon Glass4d3177d2021-07-24 09:03:33 -0600116static int run_strtoull(struct unit_test_state *uts, const char *str, int base,
117 unsigned long long expect_val, int expect_endp_offset,
118 bool upper)
119{
120 char out[TEST_STR_SIZE];
121 char *endp;
122 unsigned long long val;
123
124 strcpy(out, str);
125 if (upper)
126 str_to_upper(out, out, -1);
127
128 val = simple_strtoull(out, &endp, base);
129 ut_asserteq(expect_val, val);
130 ut_asserteq(expect_endp_offset, endp - out);
131
132 return 0;
133}
134
135static int str_simple_strtoull(struct unit_test_state *uts)
136{
137 int upper;
138
139 /* Check that it is case-insentive */
140 for (upper = 0; upper < 2; upper++) {
141 /* Base 10 and base 16 */
142 ut_assertok(run_strtoull(uts, str2, 10, 1099, 4, upper));
143 ut_assertok(run_strtoull(uts, str2, 16, 0x1099ab, 6, upper));
144 ut_assertok(run_strtoull(uts, str3, 16, 0xb, 3, upper));
Simon Glasse6951132021-07-24 09:03:38 -0600145 ut_assertok(run_strtoull(uts, str3, 10, 0xb, 3, upper));
Simon Glass4d3177d2021-07-24 09:03:33 -0600146
Simon Glassab833ef2021-07-24 09:03:34 -0600147 /* Octal */
148 ut_assertok(run_strtoull(uts, str6, 0, 63, 3, upper));
149 ut_assertok(run_strtoull(uts, str7, 8, 0x1c7, 3, upper));
150
Simon Glass4d3177d2021-07-24 09:03:33 -0600151 /* Large values */
152 ut_assertok(run_strtoull(uts, str4, 10, 1234567890123, 13,
153 upper));
154 ut_assertok(run_strtoull(uts, str4, 16, 0x1234567890123, 13,
155 upper));
156 ut_assertok(run_strtoull(uts, str5, 0, 0x9876543210, 12,
157 upper));
158
159 /* Invalid string */
160 ut_assertok(run_strtoull(uts, str1, 10, 0, 0, upper));
161
162 /* Base 0 */
163 ut_assertok(run_strtoull(uts, str1, 0, 0, 0, upper));
164 ut_assertok(run_strtoull(uts, str2, 0, 1099, 4, upper));
165 ut_assertok(run_strtoull(uts, str3, 0, 0xb, 3, upper));
166
167 /* Base 2 */
168 ut_assertok(run_strtoull(uts, str1, 2, 0, 0, upper));
169 ut_assertok(run_strtoull(uts, str2, 2, 2, 2, upper));
170 }
171
172 /* Check endp being NULL */
173 ut_asserteq(1099, simple_strtoull(str2, NULL, 0));
174
175 return 0;
176}
Simon Glass4563fb42024-11-02 13:36:58 -0600177LIB_TEST(str_simple_strtoull, 0);
Simon Glass4d3177d2021-07-24 09:03:33 -0600178
Simon Glass7e5f4602021-07-24 09:03:29 -0600179static int str_hextoul(struct unit_test_state *uts)
180{
181 char *endp;
182
183 /* Just a simple test, since we know this uses simple_strtoul() */
184 ut_asserteq(0x1099ab, hextoul(str2, &endp));
185 ut_asserteq(6, endp - str2);
186
187 return 0;
188}
Simon Glass4563fb42024-11-02 13:36:58 -0600189LIB_TEST(str_hextoul, 0);
Simon Glass7e5f4602021-07-24 09:03:29 -0600190
Heinrich Schuchardt54441892024-11-13 22:15:12 +0100191static int str_hextoull(struct unit_test_state *uts)
192{
193 char *endp;
194
195 /* Just a simple test, since we know this uses simple_strtoull() */
196 ut_asserteq_64(0x887e2561352d80faULL, hextoull(str8, &endp));
197 ut_asserteq_64(0x12, endp - str8);
198 ut_asserteq_64(0x614ff7eaa63009daULL, hextoull(str9, &endp));
199 ut_asserteq_64(0x10, endp - str9);
200 ut_asserteq_64(0x887e2561352d80faULL, hextoull(str8, NULL));
201 ut_asserteq_64(0x614ff7eaa63009daULL, hextoull(str9, NULL));
202
203 return 0;
204}
205LIB_TEST(str_hextoull, 0);
206
Simon Glass0b1284e2021-07-24 09:03:30 -0600207static int str_dectoul(struct unit_test_state *uts)
208{
209 char *endp;
210
211 /* Just a simple test, since we know this uses simple_strtoul() */
212 ut_asserteq(1099, dectoul(str2, &endp));
213 ut_asserteq(4, endp - str2);
214
215 return 0;
216}
Simon Glass4563fb42024-11-02 13:36:58 -0600217LIB_TEST(str_dectoul, 0);
Simon Glass0b1284e2021-07-24 09:03:30 -0600218
Simon Glass2fa47562022-04-24 23:30:54 -0600219static int str_itoa(struct unit_test_state *uts)
220{
221 ut_asserteq_str("123", simple_itoa(123));
222 ut_asserteq_str("0", simple_itoa(0));
223 ut_asserteq_str("2147483647", simple_itoa(0x7fffffff));
224 ut_asserteq_str("4294967295", simple_itoa(0xffffffff));
225
226 /* Use #ifdef here to avoid a compiler warning on 32-bit machines */
227#ifdef CONFIG_PHYS_64BIT
228 if (sizeof(ulong) == 8) {
229 ut_asserteq_str("9223372036854775807",
230 simple_itoa((1UL << 63) - 1));
231 ut_asserteq_str("18446744073709551615", simple_itoa(-1));
232 }
233#endif /* CONFIG_PHYS_64BIT */
234
235 return 0;
236}
Simon Glass4563fb42024-11-02 13:36:58 -0600237LIB_TEST(str_itoa, 0);
Simon Glass2fa47562022-04-24 23:30:54 -0600238
239static int str_xtoa(struct unit_test_state *uts)
240{
241 ut_asserteq_str("7f", simple_xtoa(127));
242 ut_asserteq_str("00", simple_xtoa(0));
243 ut_asserteq_str("7fffffff", simple_xtoa(0x7fffffff));
244 ut_asserteq_str("ffffffff", simple_xtoa(0xffffffff));
245
246 /* Use #ifdef here to avoid a compiler warning on 32-bit machines */
247#ifdef CONFIG_PHYS_64BIT
248 if (sizeof(ulong) == 8) {
249 ut_asserteq_str("7fffffffffffffff",
250 simple_xtoa((1UL << 63) - 1));
251 ut_asserteq_str("ffffffffffffffff", simple_xtoa(-1));
252 }
253#endif /* CONFIG_PHYS_64BIT */
254
255 return 0;
256}
Simon Glass4563fb42024-11-02 13:36:58 -0600257LIB_TEST(str_xtoa, 0);
Simon Glass2fa47562022-04-24 23:30:54 -0600258
Simon Glass18436c72022-04-24 23:30:55 -0600259static int str_trailing(struct unit_test_state *uts)
260{
Simon Glass8565efd2022-04-24 23:30:58 -0600261 const char str1[] = "abc123def";
262 const char str2[] = "abc123def456";
263 const char *end;
Simon Glass18436c72022-04-24 23:30:55 -0600264
265 ut_asserteq(-1, trailing_strtol(""));
266 ut_asserteq(-1, trailing_strtol("123"));
267 ut_asserteq(123, trailing_strtol("abc123"));
268 ut_asserteq(4, trailing_strtol("12c4"));
269 ut_asserteq(-1, trailing_strtol("abd"));
270 ut_asserteq(-1, trailing_strtol("abc123def"));
271
272 ut_asserteq(-1, trailing_strtoln(str1, NULL));
273 ut_asserteq(123, trailing_strtoln(str1, str1 + 6));
274 ut_asserteq(-1, trailing_strtoln(str1, str1 + 9));
275
Simon Glassd667a0d2022-04-24 23:30:57 -0600276 ut_asserteq(3, trailing_strtol("a3"));
277
Simon Glass8565efd2022-04-24 23:30:58 -0600278 ut_asserteq(123, trailing_strtoln_end(str1, str1 + 6, &end));
279 ut_asserteq(3, end - str1);
280
281 ut_asserteq(-1, trailing_strtoln_end(str1, str1 + 7, &end));
282 ut_asserteq(7, end - str1);
283
284 ut_asserteq(456, trailing_strtoln_end(str2, NULL, &end));
285 ut_asserteq(9, end - str2);
286
Simon Glass18436c72022-04-24 23:30:55 -0600287 return 0;
288}
Simon Glass4563fb42024-11-02 13:36:58 -0600289LIB_TEST(str_trailing, 0);
Simon Glass18436c72022-04-24 23:30:55 -0600290
Simon Glass3e96ed42023-01-17 10:47:14 -0700291static int test_str_to_list(struct unit_test_state *uts)
292{
293 const char **ptr;
294 ulong start;
295
296 /* check out of memory */
297 start = ut_check_delta(0);
298 malloc_enable_testing(0);
299 ut_assertnull(str_to_list(""));
300 ut_assertok(ut_check_delta(start));
301
302 ut_assertnull(str_to_list("this is a test"));
303 ut_assertok(ut_check_delta(start));
304
305 malloc_enable_testing(1);
306 ut_assertnull(str_to_list("this is a test"));
307 ut_assertok(ut_check_delta(start));
308
309 /* for an empty string, only one nalloc is needed */
310 malloc_enable_testing(1);
311 ptr = str_to_list("");
312 ut_assertnonnull(ptr);
313 ut_assertnull(ptr[0]);
314 str_free_list(ptr);
315 ut_assertok(ut_check_delta(start));
316
317 malloc_disable_testing();
318
319 /* test the same again, without any nalloc restrictions */
320 ptr = str_to_list("");
321 ut_assertnonnull(ptr);
322 ut_assertnull(ptr[0]);
323 str_free_list(ptr);
324 ut_assertok(ut_check_delta(start));
325
326 /* test a single string */
327 start = ut_check_delta(0);
328 ptr = str_to_list("hi");
329 ut_assertnonnull(ptr);
330 ut_assertnonnull(ptr[0]);
331 ut_asserteq_str("hi", ptr[0]);
332 ut_assertnull(ptr[1]);
333 str_free_list(ptr);
334 ut_assertok(ut_check_delta(start));
335
336 /* test two strings */
337 ptr = str_to_list("hi there");
338 ut_assertnonnull(ptr);
339 ut_assertnonnull(ptr[0]);
340 ut_asserteq_str("hi", ptr[0]);
341 ut_assertnonnull(ptr[1]);
342 ut_asserteq_str("there", ptr[1]);
343 ut_assertnull(ptr[2]);
344 str_free_list(ptr);
345 ut_assertok(ut_check_delta(start));
346
347 /* test leading, trailing and multiple spaces */
348 ptr = str_to_list(" more space ");
349 ut_assertnonnull(ptr);
350 ut_assertnonnull(ptr[0]);
351 ut_asserteq_str("", ptr[0]);
352 ut_assertnonnull(ptr[1]);
353 ut_asserteq_str("more", ptr[1]);
354 ut_assertnonnull(ptr[2]);
355 ut_asserteq_str("", ptr[2]);
356 ut_assertnonnull(ptr[3]);
357 ut_asserteq_str("space", ptr[3]);
358 ut_assertnonnull(ptr[4]);
359 ut_asserteq_str("", ptr[4]);
Simon Glass947aafd2024-07-30 08:39:36 -0600360 ut_assertnull(ptr[5]);
Simon Glass3e96ed42023-01-17 10:47:14 -0700361 str_free_list(ptr);
362 ut_assertok(ut_check_delta(start));
363
364 /* test freeing a NULL pointer */
365 str_free_list(NULL);
366
367 return 0;
368}
Simon Glass4563fb42024-11-02 13:36:58 -0600369LIB_TEST(test_str_to_list, 0);