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.";
19 /* Declare a new str test */
20 #define STR_TEST(_name, _flags) UNIT_TEST(_name, _flags, str_test)
22 static int str_test_upper(struct unit_test_state *uts)
24 char out[TEST_STR_SIZE];
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);
31 /* In-place operation */
33 str_to_upper(out, out, SIZE_MAX);
34 ut_asserteq_str("1099ABNO, DON'T BOTHER APOLOGISING.", out);
37 str_to_upper(str1, out, 7);
38 ut_asserteq_str("I'M SORO, DON'T BOTHER APOLOGISING.", out);
40 /* In-place with limited length */
42 str_to_upper(out, out, 7);
43 ut_asserteq_str("1099ABNo, don't bother apologising.", out);
45 /* Copy an empty string to a buffer with space*/
47 str_to_upper("", out, SIZE_MAX);
48 ut_asserteq('\0', *out);
49 ut_asserteq(0x7f, out[1]);
51 /* Copy an empty string to a buffer with no space*/
53 str_to_upper("", out, 0);
54 ut_asserteq(0x7f, out[0]);
58 STR_TEST(str_test_upper, 0);
60 static int run_strtoul(struct unit_test_state *uts, const char *str, int base,
61 ulong expect_val, int expect_endp_offset, bool upper)
63 char out[TEST_STR_SIZE];
69 str_to_upper(out, out, -1);
71 val = simple_strtoul(out, &endp, base);
72 ut_asserteq(expect_val, val);
73 ut_asserteq(expect_endp_offset, endp - out);
78 static int str_simple_strtoul(struct unit_test_state *uts)
82 /* 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));
89 ut_assertok(run_strtoul(uts, str1, 10, 0, 0, upper));
92 ut_assertok(run_strtoul(uts, str1, 0, 0, 0, upper));
93 ut_assertok(run_strtoul(uts, str2, 0, 1099, 4, upper));
94 ut_assertok(run_strtoul(uts, str3, 0, 0xb, 3, upper));
97 ut_assertok(run_strtoul(uts, str1, 2, 0, 0, upper));
98 ut_assertok(run_strtoul(uts, str2, 2, 2, 2, upper));
101 /* Check endp being NULL */
102 ut_asserteq(1099, simple_strtoul(str2, NULL, 0));
106 STR_TEST(str_simple_strtoul, 0);
108 int do_ut_str(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
110 struct unit_test *tests = ll_entry_start(struct unit_test,
112 const int n_ents = ll_entry_count(struct unit_test, str_test);
114 return cmd_ut_category("str", "str_", tests, n_ents, argc, argv);