blob: 3cc9dfea6aa19101e695f66337c6f71126c6eead [file] [log] [blame]
Simon Glassd0509282020-04-08 08:32:55 -06001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2020 Google LLC
4 */
5
Simon Glassd0509282020-04-08 08:32:55 -06006#include <vsprintf.h>
Simon Glass2dcfdf12024-11-02 13:36:58 -06007#include <test/lib.h>
Simon Glassd0509282020-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 Glass238efc22021-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 Glassb9efdf52021-07-24 09:03:34 -060019static const char str6[] = "0778octal is seldom used";
20static const char str7[] = "707it is a piece of computing history";
Simon Glassd0509282020-04-08 08:32:55 -060021
Patrick Delaunayc8cdd492020-11-19 10:08:43 +010022static int str_upper(struct unit_test_state *uts)
Simon Glass811a2602020-04-08 08:32:56 -060023{
24 char out[TEST_STR_SIZE];
25
26 /* Make sure it adds a terminator */
27 out[strlen(str1)] = 'a';
28 str_to_upper(str1, out, SIZE_MAX);
29 ut_asserteq_str("I'M SORRY I'M LATE.", out);
30
31 /* In-place operation */
32 strcpy(out, str2);
33 str_to_upper(out, out, SIZE_MAX);
34 ut_asserteq_str("1099ABNO, DON'T BOTHER APOLOGISING.", out);
35
36 /* Limited length */
37 str_to_upper(str1, out, 7);
38 ut_asserteq_str("I'M SORO, DON'T BOTHER APOLOGISING.", out);
39
40 /* In-place with limited length */
41 strcpy(out, str2);
42 str_to_upper(out, out, 7);
43 ut_asserteq_str("1099ABNo, don't bother apologising.", out);
44
45 /* Copy an empty string to a buffer with space*/
46 out[1] = 0x7f;
47 str_to_upper("", out, SIZE_MAX);
48 ut_asserteq('\0', *out);
49 ut_asserteq(0x7f, out[1]);
50
51 /* Copy an empty string to a buffer with no space*/
52 out[0] = 0x7f;
53 str_to_upper("", out, 0);
54 ut_asserteq(0x7f, out[0]);
55
56 return 0;
57}
Simon Glass2dcfdf12024-11-02 13:36:58 -060058LIB_TEST(str_upper, 0);
Simon Glass811a2602020-04-08 08:32:56 -060059
Simon Glassd0509282020-04-08 08:32:55 -060060static int run_strtoul(struct unit_test_state *uts, const char *str, int base,
Simon Glass811a2602020-04-08 08:32:56 -060061 ulong expect_val, int expect_endp_offset, bool upper)
Simon Glassd0509282020-04-08 08:32:55 -060062{
Simon Glass811a2602020-04-08 08:32:56 -060063 char out[TEST_STR_SIZE];
Simon Glassd0509282020-04-08 08:32:55 -060064 char *endp;
65 ulong val;
66
Simon Glass811a2602020-04-08 08:32:56 -060067 strcpy(out, str);
68 if (upper)
69 str_to_upper(out, out, -1);
70
71 val = simple_strtoul(out, &endp, base);
Simon Glassd0509282020-04-08 08:32:55 -060072 ut_asserteq(expect_val, val);
Simon Glass811a2602020-04-08 08:32:56 -060073 ut_asserteq(expect_endp_offset, endp - out);
Simon Glassd0509282020-04-08 08:32:55 -060074
75 return 0;
76}
77
78static int str_simple_strtoul(struct unit_test_state *uts)
79{
Simon Glass811a2602020-04-08 08:32:56 -060080 int upper;
Simon Glassd0509282020-04-08 08:32:55 -060081
Simon Glass811a2602020-04-08 08:32:56 -060082 /* Check that it is case-insentive */
83 for (upper = 0; upper < 2; upper++) {
84 /* Base 10 and base 16 */
85 ut_assertok(run_strtoul(uts, str2, 10, 1099, 4, upper));
86 ut_assertok(run_strtoul(uts, str2, 16, 0x1099ab, 6, upper));
Simon Glass8be8eab2021-07-24 09:03:32 -060087 ut_assertok(run_strtoul(uts, str3, 16, 0xb, 3, upper));
Simon Glass90877bb2021-07-24 09:03:38 -060088 ut_assertok(run_strtoul(uts, str3, 10, 0xb, 3, upper));
Simon Glassd0509282020-04-08 08:32:55 -060089
Simon Glassb9efdf52021-07-24 09:03:34 -060090 /* Octal */
91 ut_assertok(run_strtoul(uts, str6, 0, 63, 3, upper));
92 ut_assertok(run_strtoul(uts, str7, 8, 0x1c7, 3, upper));
93
Simon Glass811a2602020-04-08 08:32:56 -060094 /* Invalid string */
95 ut_assertok(run_strtoul(uts, str1, 10, 0, 0, upper));
96
97 /* Base 0 */
98 ut_assertok(run_strtoul(uts, str1, 0, 0, 0, upper));
99 ut_assertok(run_strtoul(uts, str2, 0, 1099, 4, upper));
100 ut_assertok(run_strtoul(uts, str3, 0, 0xb, 3, upper));
Simon Glassd0509282020-04-08 08:32:55 -0600101
Simon Glass811a2602020-04-08 08:32:56 -0600102 /* Base 2 */
103 ut_assertok(run_strtoul(uts, str1, 2, 0, 0, upper));
104 ut_assertok(run_strtoul(uts, str2, 2, 2, 2, upper));
105 }
Simon Glassd0509282020-04-08 08:32:55 -0600106
107 /* Check endp being NULL */
108 ut_asserteq(1099, simple_strtoul(str2, NULL, 0));
109
110 return 0;
111}
Simon Glass2dcfdf12024-11-02 13:36:58 -0600112LIB_TEST(str_simple_strtoul, 0);
Simon Glassd0509282020-04-08 08:32:55 -0600113
Simon Glass238efc22021-07-24 09:03:33 -0600114static int run_strtoull(struct unit_test_state *uts, const char *str, int base,
115 unsigned long long expect_val, int expect_endp_offset,
116 bool upper)
117{
118 char out[TEST_STR_SIZE];
119 char *endp;
120 unsigned long long val;
121
122 strcpy(out, str);
123 if (upper)
124 str_to_upper(out, out, -1);
125
126 val = simple_strtoull(out, &endp, base);
127 ut_asserteq(expect_val, val);
128 ut_asserteq(expect_endp_offset, endp - out);
129
130 return 0;
131}
132
133static int str_simple_strtoull(struct unit_test_state *uts)
134{
135 int upper;
136
137 /* Check that it is case-insentive */
138 for (upper = 0; upper < 2; upper++) {
139 /* Base 10 and base 16 */
140 ut_assertok(run_strtoull(uts, str2, 10, 1099, 4, upper));
141 ut_assertok(run_strtoull(uts, str2, 16, 0x1099ab, 6, upper));
142 ut_assertok(run_strtoull(uts, str3, 16, 0xb, 3, upper));
Simon Glass90877bb2021-07-24 09:03:38 -0600143 ut_assertok(run_strtoull(uts, str3, 10, 0xb, 3, upper));
Simon Glass238efc22021-07-24 09:03:33 -0600144
Simon Glassb9efdf52021-07-24 09:03:34 -0600145 /* Octal */
146 ut_assertok(run_strtoull(uts, str6, 0, 63, 3, upper));
147 ut_assertok(run_strtoull(uts, str7, 8, 0x1c7, 3, upper));
148
Simon Glass238efc22021-07-24 09:03:33 -0600149 /* Large values */
150 ut_assertok(run_strtoull(uts, str4, 10, 1234567890123, 13,
151 upper));
152 ut_assertok(run_strtoull(uts, str4, 16, 0x1234567890123, 13,
153 upper));
154 ut_assertok(run_strtoull(uts, str5, 0, 0x9876543210, 12,
155 upper));
156
157 /* Invalid string */
158 ut_assertok(run_strtoull(uts, str1, 10, 0, 0, upper));
159
160 /* Base 0 */
161 ut_assertok(run_strtoull(uts, str1, 0, 0, 0, upper));
162 ut_assertok(run_strtoull(uts, str2, 0, 1099, 4, upper));
163 ut_assertok(run_strtoull(uts, str3, 0, 0xb, 3, upper));
164
165 /* Base 2 */
166 ut_assertok(run_strtoull(uts, str1, 2, 0, 0, upper));
167 ut_assertok(run_strtoull(uts, str2, 2, 2, 2, upper));
168 }
169
170 /* Check endp being NULL */
171 ut_asserteq(1099, simple_strtoull(str2, NULL, 0));
172
173 return 0;
174}
Simon Glass2dcfdf12024-11-02 13:36:58 -0600175LIB_TEST(str_simple_strtoull, 0);
Simon Glass238efc22021-07-24 09:03:33 -0600176
Simon Glass3ff49ec2021-07-24 09:03:29 -0600177static int str_hextoul(struct unit_test_state *uts)
178{
179 char *endp;
180
181 /* Just a simple test, since we know this uses simple_strtoul() */
182 ut_asserteq(0x1099ab, hextoul(str2, &endp));
183 ut_asserteq(6, endp - str2);
184
185 return 0;
186}
Simon Glass2dcfdf12024-11-02 13:36:58 -0600187LIB_TEST(str_hextoul, 0);
Simon Glass3ff49ec2021-07-24 09:03:29 -0600188
Simon Glassff9b9032021-07-24 09:03:30 -0600189static int str_dectoul(struct unit_test_state *uts)
190{
191 char *endp;
192
193 /* Just a simple test, since we know this uses simple_strtoul() */
194 ut_asserteq(1099, dectoul(str2, &endp));
195 ut_asserteq(4, endp - str2);
196
197 return 0;
198}
Simon Glass2dcfdf12024-11-02 13:36:58 -0600199LIB_TEST(str_dectoul, 0);
Simon Glassff9b9032021-07-24 09:03:30 -0600200
Simon Glass8c2d6392022-04-24 23:30:54 -0600201static int str_itoa(struct unit_test_state *uts)
202{
203 ut_asserteq_str("123", simple_itoa(123));
204 ut_asserteq_str("0", simple_itoa(0));
205 ut_asserteq_str("2147483647", simple_itoa(0x7fffffff));
206 ut_asserteq_str("4294967295", simple_itoa(0xffffffff));
207
208 /* Use #ifdef here to avoid a compiler warning on 32-bit machines */
209#ifdef CONFIG_PHYS_64BIT
210 if (sizeof(ulong) == 8) {
211 ut_asserteq_str("9223372036854775807",
212 simple_itoa((1UL << 63) - 1));
213 ut_asserteq_str("18446744073709551615", simple_itoa(-1));
214 }
215#endif /* CONFIG_PHYS_64BIT */
216
217 return 0;
218}
Simon Glass2dcfdf12024-11-02 13:36:58 -0600219LIB_TEST(str_itoa, 0);
Simon Glass8c2d6392022-04-24 23:30:54 -0600220
221static int str_xtoa(struct unit_test_state *uts)
222{
223 ut_asserteq_str("7f", simple_xtoa(127));
224 ut_asserteq_str("00", simple_xtoa(0));
225 ut_asserteq_str("7fffffff", simple_xtoa(0x7fffffff));
226 ut_asserteq_str("ffffffff", simple_xtoa(0xffffffff));
227
228 /* Use #ifdef here to avoid a compiler warning on 32-bit machines */
229#ifdef CONFIG_PHYS_64BIT
230 if (sizeof(ulong) == 8) {
231 ut_asserteq_str("7fffffffffffffff",
232 simple_xtoa((1UL << 63) - 1));
233 ut_asserteq_str("ffffffffffffffff", simple_xtoa(-1));
234 }
235#endif /* CONFIG_PHYS_64BIT */
236
237 return 0;
238}
Simon Glass2dcfdf12024-11-02 13:36:58 -0600239LIB_TEST(str_xtoa, 0);
Simon Glass8c2d6392022-04-24 23:30:54 -0600240
Simon Glasse74efcd2022-04-24 23:30:55 -0600241static int str_trailing(struct unit_test_state *uts)
242{
Simon Glass4bf2fc12022-04-24 23:30:58 -0600243 const char str1[] = "abc123def";
244 const char str2[] = "abc123def456";
245 const char *end;
Simon Glasse74efcd2022-04-24 23:30:55 -0600246
247 ut_asserteq(-1, trailing_strtol(""));
248 ut_asserteq(-1, trailing_strtol("123"));
249 ut_asserteq(123, trailing_strtol("abc123"));
250 ut_asserteq(4, trailing_strtol("12c4"));
251 ut_asserteq(-1, trailing_strtol("abd"));
252 ut_asserteq(-1, trailing_strtol("abc123def"));
253
254 ut_asserteq(-1, trailing_strtoln(str1, NULL));
255 ut_asserteq(123, trailing_strtoln(str1, str1 + 6));
256 ut_asserteq(-1, trailing_strtoln(str1, str1 + 9));
257
Simon Glass20888f32022-04-24 23:30:57 -0600258 ut_asserteq(3, trailing_strtol("a3"));
259
Simon Glass4bf2fc12022-04-24 23:30:58 -0600260 ut_asserteq(123, trailing_strtoln_end(str1, str1 + 6, &end));
261 ut_asserteq(3, end - str1);
262
263 ut_asserteq(-1, trailing_strtoln_end(str1, str1 + 7, &end));
264 ut_asserteq(7, end - str1);
265
266 ut_asserteq(456, trailing_strtoln_end(str2, NULL, &end));
267 ut_asserteq(9, end - str2);
268
Simon Glasse74efcd2022-04-24 23:30:55 -0600269 return 0;
270}
Simon Glass2dcfdf12024-11-02 13:36:58 -0600271LIB_TEST(str_trailing, 0);
Simon Glasse74efcd2022-04-24 23:30:55 -0600272
Simon Glass6a9de552023-01-17 10:47:14 -0700273static int test_str_to_list(struct unit_test_state *uts)
274{
275 const char **ptr;
276 ulong start;
277
278 /* check out of memory */
279 start = ut_check_delta(0);
280 malloc_enable_testing(0);
281 ut_assertnull(str_to_list(""));
282 ut_assertok(ut_check_delta(start));
283
284 ut_assertnull(str_to_list("this is a test"));
285 ut_assertok(ut_check_delta(start));
286
287 malloc_enable_testing(1);
288 ut_assertnull(str_to_list("this is a test"));
289 ut_assertok(ut_check_delta(start));
290
291 /* for an empty string, only one nalloc is needed */
292 malloc_enable_testing(1);
293 ptr = str_to_list("");
294 ut_assertnonnull(ptr);
295 ut_assertnull(ptr[0]);
296 str_free_list(ptr);
297 ut_assertok(ut_check_delta(start));
298
299 malloc_disable_testing();
300
301 /* test the same again, without any nalloc restrictions */
302 ptr = str_to_list("");
303 ut_assertnonnull(ptr);
304 ut_assertnull(ptr[0]);
305 str_free_list(ptr);
306 ut_assertok(ut_check_delta(start));
307
308 /* test a single string */
309 start = ut_check_delta(0);
310 ptr = str_to_list("hi");
311 ut_assertnonnull(ptr);
312 ut_assertnonnull(ptr[0]);
313 ut_asserteq_str("hi", ptr[0]);
314 ut_assertnull(ptr[1]);
315 str_free_list(ptr);
316 ut_assertok(ut_check_delta(start));
317
318 /* test two strings */
319 ptr = str_to_list("hi there");
320 ut_assertnonnull(ptr);
321 ut_assertnonnull(ptr[0]);
322 ut_asserteq_str("hi", ptr[0]);
323 ut_assertnonnull(ptr[1]);
324 ut_asserteq_str("there", ptr[1]);
325 ut_assertnull(ptr[2]);
326 str_free_list(ptr);
327 ut_assertok(ut_check_delta(start));
328
329 /* test leading, trailing and multiple spaces */
330 ptr = str_to_list(" more space ");
331 ut_assertnonnull(ptr);
332 ut_assertnonnull(ptr[0]);
333 ut_asserteq_str("", ptr[0]);
334 ut_assertnonnull(ptr[1]);
335 ut_asserteq_str("more", ptr[1]);
336 ut_assertnonnull(ptr[2]);
337 ut_asserteq_str("", ptr[2]);
338 ut_assertnonnull(ptr[3]);
339 ut_asserteq_str("space", ptr[3]);
340 ut_assertnonnull(ptr[4]);
341 ut_asserteq_str("", ptr[4]);
Simon Glass6667c012024-07-30 08:39:36 -0600342 ut_assertnull(ptr[5]);
Simon Glass6a9de552023-01-17 10:47:14 -0700343 str_free_list(ptr);
344 ut_assertok(ut_check_delta(start));
345
346 /* test freeing a NULL pointer */
347 str_free_list(NULL);
348
349 return 0;
350}
Simon Glass2dcfdf12024-11-02 13:36:58 -0600351LIB_TEST(test_str_to_list, 0);