lib: Add tests for simple_strtoull()
[platform/kernel/u-boot.git] / test / str_ut.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2020 Google LLC
4  */
5
6 #include <common.h>
7 #include <vsprintf.h>
8 #include <test/suites.h>
9 #include <test/test.h>
10 #include <test/ut.h>
11
12 /* This is large enough for any of the test strings */
13 #define TEST_STR_SIZE   200
14
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
21 /* Declare a new str test */
22 #define STR_TEST(_name, _flags)         UNIT_TEST(_name, _flags, str_test)
23
24 static int str_upper(struct unit_test_state *uts)
25 {
26         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 }
60 STR_TEST(str_upper, 0);
61
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)
64 {
65         char out[TEST_STR_SIZE];
66         char *endp;
67         ulong val;
68
69         strcpy(out, str);
70         if (upper)
71                 str_to_upper(out, out, -1);
72
73         val = simple_strtoul(out, &endp, base);
74         ut_asserteq(expect_val, val);
75         ut_asserteq(expect_endp_offset, endp - out);
76
77         return 0;
78 }
79
80 static int str_simple_strtoul(struct unit_test_state *uts)
81 {
82         int upper;
83
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));
91
92                 /* Invalid string */
93                 ut_assertok(run_strtoul(uts, str1, 10, 0, 0, upper));
94
95                 /* Base 0 */
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));
99
100                 /* Base 2 */
101                 ut_assertok(run_strtoul(uts, str1, 2, 0, 0, upper));
102                 ut_assertok(run_strtoul(uts, str2, 2, 2, 2, upper));
103         }
104
105         /* Check endp being NULL */
106         ut_asserteq(1099, simple_strtoul(str2, NULL, 0));
107
108         return 0;
109 }
110 STR_TEST(str_simple_strtoul, 0);
111
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,
114                         bool upper)
115 {
116         char out[TEST_STR_SIZE];
117         char *endp;
118         unsigned long long val;
119
120         strcpy(out, str);
121         if (upper)
122                 str_to_upper(out, out, -1);
123
124         val = simple_strtoull(out, &endp, base);
125         ut_asserteq(expect_val, val);
126         ut_asserteq(expect_endp_offset, endp - out);
127
128         return 0;
129 }
130
131 static int str_simple_strtoull(struct unit_test_state *uts)
132 {
133         int upper;
134
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));
142
143                 /* Large values */
144                 ut_assertok(run_strtoull(uts, str4, 10, 1234567890123, 13,
145                                          upper));
146                 ut_assertok(run_strtoull(uts, str4, 16, 0x1234567890123, 13,
147                                          upper));
148                 ut_assertok(run_strtoull(uts, str5, 0, 0x9876543210, 12,
149                                          upper));
150
151                 /* Invalid string */
152                 ut_assertok(run_strtoull(uts, str1, 10, 0, 0, upper));
153
154                 /* Base 0 */
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));
158
159                 /* Base 2 */
160                 ut_assertok(run_strtoull(uts, str1, 2, 0, 0, upper));
161                 ut_assertok(run_strtoull(uts, str2, 2, 2, 2, upper));
162         }
163
164         /* Check endp being NULL */
165         ut_asserteq(1099, simple_strtoull(str2, NULL, 0));
166
167         return 0;
168 }
169 STR_TEST(str_simple_strtoull, 0);
170
171 static int str_hextoul(struct unit_test_state *uts)
172 {
173         char *endp;
174
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);
178
179         return 0;
180 }
181 STR_TEST(str_hextoul, 0);
182
183 static int str_dectoul(struct unit_test_state *uts)
184 {
185         char *endp;
186
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);
190
191         return 0;
192 }
193 STR_TEST(str_dectoul, 0);
194
195 int do_ut_str(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
196 {
197         struct unit_test *tests = UNIT_TEST_SUITE_START(str_test);
198         const int n_ents = UNIT_TEST_SUITE_COUNT(str_test);
199
200         return cmd_ut_category("str", "str_", tests, n_ents, argc, argv);
201 }