blob: 880cc928ec8d9d2a2c1563d804d0a93bd39be966 [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
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
15static const char str1[] = "I'm sorry I'm late.";
16static const char str2[] = "1099abNo, don't bother apologising.";
17static const char str3[] = "0xbI'm sorry you're alive.";
Simon Glass4d3177d2021-07-24 09:03:33 -060018static const char str4[] = "1234567890123 I lost closer friends";
19static const char str5[] = "0x9876543210the last time I was deloused";
Simon Glass4f04d542020-04-08 08:32:55 -060020
21/* Declare a new str test */
22#define STR_TEST(_name, _flags) UNIT_TEST(_name, _flags, str_test)
23
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}
Patrick Delaunay76fde132020-11-19 10:08:43 +010060STR_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));
90 ut_assertok(run_strtoul(uts, str3, 10, 0, 1, upper));
Simon Glass4f04d542020-04-08 08:32:55 -060091
Simon Glassfdc79a62020-04-08 08:32:56 -060092 /* Invalid string */
93 ut_assertok(run_strtoul(uts, str1, 10, 0, 0, upper));
Simon Glass4f04d542020-04-08 08:32:55 -060094
Simon Glassfdc79a62020-04-08 08:32:56 -060095 /* 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 }
Simon Glass4f04d542020-04-08 08:32:55 -0600104
105 /* Check endp being NULL */
106 ut_asserteq(1099, simple_strtoul(str2, NULL, 0));
107
108 return 0;
109}
110STR_TEST(str_simple_strtoul, 0);
111
Simon Glass4d3177d2021-07-24 09:03:33 -0600112static 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
131static 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}
169STR_TEST(str_simple_strtoull, 0);
170
Simon Glass7e5f4602021-07-24 09:03:29 -0600171static 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}
181STR_TEST(str_hextoul, 0);
182
Simon Glass0b1284e2021-07-24 09:03:30 -0600183static 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}
193STR_TEST(str_dectoul, 0);
194
Simon Glass09140112020-05-10 11:40:03 -0600195int do_ut_str(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Simon Glass4f04d542020-04-08 08:32:55 -0600196{
Simon Glassa7a98752021-03-07 17:35:10 -0700197 struct unit_test *tests = UNIT_TEST_SUITE_START(str_test);
198 const int n_ents = UNIT_TEST_SUITE_COUNT(str_test);
Simon Glass4f04d542020-04-08 08:32:55 -0600199
200 return cmd_ut_category("str", "str_", tests, n_ents, argc, argv);
201}