Simon Glass | 4f04d54 | 2020-04-08 08:32:55 -0600 | [diff] [blame] | 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."; |
Simon Glass | 4d3177d | 2021-07-24 09:03:33 -0600 | [diff] [blame] | 18 | static const char str4[] = "1234567890123 I lost closer friends"; |
| 19 | static const char str5[] = "0x9876543210the last time I was deloused"; |
Simon Glass | ab833ef | 2021-07-24 09:03:34 -0600 | [diff] [blame] | 20 | static const char str6[] = "0778octal is seldom used"; |
| 21 | static const char str7[] = "707it is a piece of computing history"; |
Simon Glass | 4f04d54 | 2020-04-08 08:32:55 -0600 | [diff] [blame] | 22 | |
| 23 | /* Declare a new str test */ |
| 24 | #define STR_TEST(_name, _flags) UNIT_TEST(_name, _flags, str_test) |
| 25 | |
Patrick Delaunay | 76fde13 | 2020-11-19 10:08:43 +0100 | [diff] [blame] | 26 | static int str_upper(struct unit_test_state *uts) |
Simon Glass | 4f04d54 | 2020-04-08 08:32:55 -0600 | [diff] [blame] | 27 | { |
Simon Glass | fdc79a6 | 2020-04-08 08:32:56 -0600 | [diff] [blame] | 28 | char out[TEST_STR_SIZE]; |
| 29 | |
| 30 | /* Make sure it adds a terminator */ |
| 31 | out[strlen(str1)] = 'a'; |
| 32 | str_to_upper(str1, out, SIZE_MAX); |
| 33 | ut_asserteq_str("I'M SORRY I'M LATE.", out); |
| 34 | |
| 35 | /* In-place operation */ |
| 36 | strcpy(out, str2); |
| 37 | str_to_upper(out, out, SIZE_MAX); |
| 38 | ut_asserteq_str("1099ABNO, DON'T BOTHER APOLOGISING.", out); |
| 39 | |
| 40 | /* Limited length */ |
| 41 | str_to_upper(str1, out, 7); |
| 42 | ut_asserteq_str("I'M SORO, DON'T BOTHER APOLOGISING.", out); |
| 43 | |
| 44 | /* In-place with limited length */ |
| 45 | strcpy(out, str2); |
| 46 | str_to_upper(out, out, 7); |
| 47 | ut_asserteq_str("1099ABNo, don't bother apologising.", out); |
| 48 | |
| 49 | /* Copy an empty string to a buffer with space*/ |
| 50 | out[1] = 0x7f; |
| 51 | str_to_upper("", out, SIZE_MAX); |
| 52 | ut_asserteq('\0', *out); |
| 53 | ut_asserteq(0x7f, out[1]); |
| 54 | |
| 55 | /* Copy an empty string to a buffer with no space*/ |
| 56 | out[0] = 0x7f; |
| 57 | str_to_upper("", out, 0); |
| 58 | ut_asserteq(0x7f, out[0]); |
| 59 | |
| 60 | return 0; |
| 61 | } |
Patrick Delaunay | 76fde13 | 2020-11-19 10:08:43 +0100 | [diff] [blame] | 62 | STR_TEST(str_upper, 0); |
Simon Glass | fdc79a6 | 2020-04-08 08:32:56 -0600 | [diff] [blame] | 63 | |
| 64 | static int run_strtoul(struct unit_test_state *uts, const char *str, int base, |
| 65 | ulong expect_val, int expect_endp_offset, bool upper) |
| 66 | { |
| 67 | char out[TEST_STR_SIZE]; |
Simon Glass | 4f04d54 | 2020-04-08 08:32:55 -0600 | [diff] [blame] | 68 | char *endp; |
| 69 | ulong val; |
| 70 | |
Simon Glass | fdc79a6 | 2020-04-08 08:32:56 -0600 | [diff] [blame] | 71 | strcpy(out, str); |
| 72 | if (upper) |
| 73 | str_to_upper(out, out, -1); |
| 74 | |
| 75 | val = simple_strtoul(out, &endp, base); |
Simon Glass | 4f04d54 | 2020-04-08 08:32:55 -0600 | [diff] [blame] | 76 | ut_asserteq(expect_val, val); |
Simon Glass | fdc79a6 | 2020-04-08 08:32:56 -0600 | [diff] [blame] | 77 | ut_asserteq(expect_endp_offset, endp - out); |
Simon Glass | 4f04d54 | 2020-04-08 08:32:55 -0600 | [diff] [blame] | 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | static int str_simple_strtoul(struct unit_test_state *uts) |
| 83 | { |
Simon Glass | fdc79a6 | 2020-04-08 08:32:56 -0600 | [diff] [blame] | 84 | int upper; |
Simon Glass | 4f04d54 | 2020-04-08 08:32:55 -0600 | [diff] [blame] | 85 | |
Simon Glass | fdc79a6 | 2020-04-08 08:32:56 -0600 | [diff] [blame] | 86 | /* Check that it is case-insentive */ |
| 87 | for (upper = 0; upper < 2; upper++) { |
| 88 | /* Base 10 and base 16 */ |
| 89 | ut_assertok(run_strtoul(uts, str2, 10, 1099, 4, upper)); |
| 90 | ut_assertok(run_strtoul(uts, str2, 16, 0x1099ab, 6, upper)); |
Simon Glass | 96b2344 | 2021-07-24 09:03:32 -0600 | [diff] [blame] | 91 | ut_assertok(run_strtoul(uts, str3, 16, 0xb, 3, upper)); |
Simon Glass | e695113 | 2021-07-24 09:03:38 -0600 | [diff] [blame] | 92 | ut_assertok(run_strtoul(uts, str3, 10, 0xb, 3, upper)); |
Simon Glass | 4f04d54 | 2020-04-08 08:32:55 -0600 | [diff] [blame] | 93 | |
Simon Glass | ab833ef | 2021-07-24 09:03:34 -0600 | [diff] [blame] | 94 | /* Octal */ |
| 95 | ut_assertok(run_strtoul(uts, str6, 0, 63, 3, upper)); |
| 96 | ut_assertok(run_strtoul(uts, str7, 8, 0x1c7, 3, upper)); |
| 97 | |
Simon Glass | fdc79a6 | 2020-04-08 08:32:56 -0600 | [diff] [blame] | 98 | /* Invalid string */ |
| 99 | ut_assertok(run_strtoul(uts, str1, 10, 0, 0, upper)); |
Simon Glass | 4f04d54 | 2020-04-08 08:32:55 -0600 | [diff] [blame] | 100 | |
Simon Glass | fdc79a6 | 2020-04-08 08:32:56 -0600 | [diff] [blame] | 101 | /* Base 0 */ |
| 102 | ut_assertok(run_strtoul(uts, str1, 0, 0, 0, upper)); |
| 103 | ut_assertok(run_strtoul(uts, str2, 0, 1099, 4, upper)); |
| 104 | ut_assertok(run_strtoul(uts, str3, 0, 0xb, 3, upper)); |
| 105 | |
| 106 | /* Base 2 */ |
| 107 | ut_assertok(run_strtoul(uts, str1, 2, 0, 0, upper)); |
| 108 | ut_assertok(run_strtoul(uts, str2, 2, 2, 2, upper)); |
| 109 | } |
Simon Glass | 4f04d54 | 2020-04-08 08:32:55 -0600 | [diff] [blame] | 110 | |
| 111 | /* Check endp being NULL */ |
| 112 | ut_asserteq(1099, simple_strtoul(str2, NULL, 0)); |
| 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | STR_TEST(str_simple_strtoul, 0); |
| 117 | |
Simon Glass | 4d3177d | 2021-07-24 09:03:33 -0600 | [diff] [blame] | 118 | static int run_strtoull(struct unit_test_state *uts, const char *str, int base, |
| 119 | unsigned long long expect_val, int expect_endp_offset, |
| 120 | bool upper) |
| 121 | { |
| 122 | char out[TEST_STR_SIZE]; |
| 123 | char *endp; |
| 124 | unsigned long long val; |
| 125 | |
| 126 | strcpy(out, str); |
| 127 | if (upper) |
| 128 | str_to_upper(out, out, -1); |
| 129 | |
| 130 | val = simple_strtoull(out, &endp, base); |
| 131 | ut_asserteq(expect_val, val); |
| 132 | ut_asserteq(expect_endp_offset, endp - out); |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | static int str_simple_strtoull(struct unit_test_state *uts) |
| 138 | { |
| 139 | int upper; |
| 140 | |
| 141 | /* Check that it is case-insentive */ |
| 142 | for (upper = 0; upper < 2; upper++) { |
| 143 | /* Base 10 and base 16 */ |
| 144 | ut_assertok(run_strtoull(uts, str2, 10, 1099, 4, upper)); |
| 145 | ut_assertok(run_strtoull(uts, str2, 16, 0x1099ab, 6, upper)); |
| 146 | ut_assertok(run_strtoull(uts, str3, 16, 0xb, 3, upper)); |
Simon Glass | e695113 | 2021-07-24 09:03:38 -0600 | [diff] [blame] | 147 | ut_assertok(run_strtoull(uts, str3, 10, 0xb, 3, upper)); |
Simon Glass | 4d3177d | 2021-07-24 09:03:33 -0600 | [diff] [blame] | 148 | |
Simon Glass | ab833ef | 2021-07-24 09:03:34 -0600 | [diff] [blame] | 149 | /* Octal */ |
| 150 | ut_assertok(run_strtoull(uts, str6, 0, 63, 3, upper)); |
| 151 | ut_assertok(run_strtoull(uts, str7, 8, 0x1c7, 3, upper)); |
| 152 | |
Simon Glass | 4d3177d | 2021-07-24 09:03:33 -0600 | [diff] [blame] | 153 | /* Large values */ |
| 154 | ut_assertok(run_strtoull(uts, str4, 10, 1234567890123, 13, |
| 155 | upper)); |
| 156 | ut_assertok(run_strtoull(uts, str4, 16, 0x1234567890123, 13, |
| 157 | upper)); |
| 158 | ut_assertok(run_strtoull(uts, str5, 0, 0x9876543210, 12, |
| 159 | upper)); |
| 160 | |
| 161 | /* Invalid string */ |
| 162 | ut_assertok(run_strtoull(uts, str1, 10, 0, 0, upper)); |
| 163 | |
| 164 | /* Base 0 */ |
| 165 | ut_assertok(run_strtoull(uts, str1, 0, 0, 0, upper)); |
| 166 | ut_assertok(run_strtoull(uts, str2, 0, 1099, 4, upper)); |
| 167 | ut_assertok(run_strtoull(uts, str3, 0, 0xb, 3, upper)); |
| 168 | |
| 169 | /* Base 2 */ |
| 170 | ut_assertok(run_strtoull(uts, str1, 2, 0, 0, upper)); |
| 171 | ut_assertok(run_strtoull(uts, str2, 2, 2, 2, upper)); |
| 172 | } |
| 173 | |
| 174 | /* Check endp being NULL */ |
| 175 | ut_asserteq(1099, simple_strtoull(str2, NULL, 0)); |
| 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | STR_TEST(str_simple_strtoull, 0); |
| 180 | |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 181 | static int str_hextoul(struct unit_test_state *uts) |
| 182 | { |
| 183 | char *endp; |
| 184 | |
| 185 | /* Just a simple test, since we know this uses simple_strtoul() */ |
| 186 | ut_asserteq(0x1099ab, hextoul(str2, &endp)); |
| 187 | ut_asserteq(6, endp - str2); |
| 188 | |
| 189 | return 0; |
| 190 | } |
| 191 | STR_TEST(str_hextoul, 0); |
| 192 | |
Simon Glass | 0b1284e | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 193 | static int str_dectoul(struct unit_test_state *uts) |
| 194 | { |
| 195 | char *endp; |
| 196 | |
| 197 | /* Just a simple test, since we know this uses simple_strtoul() */ |
| 198 | ut_asserteq(1099, dectoul(str2, &endp)); |
| 199 | ut_asserteq(4, endp - str2); |
| 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | STR_TEST(str_dectoul, 0); |
| 204 | |
Simon Glass | 2fa4756 | 2022-04-24 23:30:54 -0600 | [diff] [blame] | 205 | static int str_itoa(struct unit_test_state *uts) |
| 206 | { |
| 207 | ut_asserteq_str("123", simple_itoa(123)); |
| 208 | ut_asserteq_str("0", simple_itoa(0)); |
| 209 | ut_asserteq_str("2147483647", simple_itoa(0x7fffffff)); |
| 210 | ut_asserteq_str("4294967295", simple_itoa(0xffffffff)); |
| 211 | |
| 212 | /* Use #ifdef here to avoid a compiler warning on 32-bit machines */ |
| 213 | #ifdef CONFIG_PHYS_64BIT |
| 214 | if (sizeof(ulong) == 8) { |
| 215 | ut_asserteq_str("9223372036854775807", |
| 216 | simple_itoa((1UL << 63) - 1)); |
| 217 | ut_asserteq_str("18446744073709551615", simple_itoa(-1)); |
| 218 | } |
| 219 | #endif /* CONFIG_PHYS_64BIT */ |
| 220 | |
| 221 | return 0; |
| 222 | } |
| 223 | STR_TEST(str_itoa, 0); |
| 224 | |
| 225 | static int str_xtoa(struct unit_test_state *uts) |
| 226 | { |
| 227 | ut_asserteq_str("7f", simple_xtoa(127)); |
| 228 | ut_asserteq_str("00", simple_xtoa(0)); |
| 229 | ut_asserteq_str("7fffffff", simple_xtoa(0x7fffffff)); |
| 230 | ut_asserteq_str("ffffffff", simple_xtoa(0xffffffff)); |
| 231 | |
| 232 | /* Use #ifdef here to avoid a compiler warning on 32-bit machines */ |
| 233 | #ifdef CONFIG_PHYS_64BIT |
| 234 | if (sizeof(ulong) == 8) { |
| 235 | ut_asserteq_str("7fffffffffffffff", |
| 236 | simple_xtoa((1UL << 63) - 1)); |
| 237 | ut_asserteq_str("ffffffffffffffff", simple_xtoa(-1)); |
| 238 | } |
| 239 | #endif /* CONFIG_PHYS_64BIT */ |
| 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | STR_TEST(str_xtoa, 0); |
| 244 | |
Simon Glass | 18436c7 | 2022-04-24 23:30:55 -0600 | [diff] [blame] | 245 | static int str_trailing(struct unit_test_state *uts) |
| 246 | { |
Simon Glass | 8565efd | 2022-04-24 23:30:58 -0600 | [diff] [blame] | 247 | const char str1[] = "abc123def"; |
| 248 | const char str2[] = "abc123def456"; |
| 249 | const char *end; |
Simon Glass | 18436c7 | 2022-04-24 23:30:55 -0600 | [diff] [blame] | 250 | |
| 251 | ut_asserteq(-1, trailing_strtol("")); |
| 252 | ut_asserteq(-1, trailing_strtol("123")); |
| 253 | ut_asserteq(123, trailing_strtol("abc123")); |
| 254 | ut_asserteq(4, trailing_strtol("12c4")); |
| 255 | ut_asserteq(-1, trailing_strtol("abd")); |
| 256 | ut_asserteq(-1, trailing_strtol("abc123def")); |
| 257 | |
| 258 | ut_asserteq(-1, trailing_strtoln(str1, NULL)); |
| 259 | ut_asserteq(123, trailing_strtoln(str1, str1 + 6)); |
| 260 | ut_asserteq(-1, trailing_strtoln(str1, str1 + 9)); |
| 261 | |
Simon Glass | d667a0d | 2022-04-24 23:30:57 -0600 | [diff] [blame] | 262 | ut_asserteq(3, trailing_strtol("a3")); |
| 263 | |
Simon Glass | 8565efd | 2022-04-24 23:30:58 -0600 | [diff] [blame] | 264 | ut_asserteq(123, trailing_strtoln_end(str1, str1 + 6, &end)); |
| 265 | ut_asserteq(3, end - str1); |
| 266 | |
| 267 | ut_asserteq(-1, trailing_strtoln_end(str1, str1 + 7, &end)); |
| 268 | ut_asserteq(7, end - str1); |
| 269 | |
| 270 | ut_asserteq(456, trailing_strtoln_end(str2, NULL, &end)); |
| 271 | ut_asserteq(9, end - str2); |
| 272 | |
Simon Glass | 18436c7 | 2022-04-24 23:30:55 -0600 | [diff] [blame] | 273 | return 0; |
| 274 | } |
| 275 | STR_TEST(str_trailing, 0); |
| 276 | |
Simon Glass | 3e96ed4 | 2023-01-17 10:47:14 -0700 | [diff] [blame] | 277 | static int test_str_to_list(struct unit_test_state *uts) |
| 278 | { |
| 279 | const char **ptr; |
| 280 | ulong start; |
| 281 | |
| 282 | /* check out of memory */ |
| 283 | start = ut_check_delta(0); |
| 284 | malloc_enable_testing(0); |
| 285 | ut_assertnull(str_to_list("")); |
| 286 | ut_assertok(ut_check_delta(start)); |
| 287 | |
| 288 | ut_assertnull(str_to_list("this is a test")); |
| 289 | ut_assertok(ut_check_delta(start)); |
| 290 | |
| 291 | malloc_enable_testing(1); |
| 292 | ut_assertnull(str_to_list("this is a test")); |
| 293 | ut_assertok(ut_check_delta(start)); |
| 294 | |
| 295 | /* for an empty string, only one nalloc is needed */ |
| 296 | malloc_enable_testing(1); |
| 297 | ptr = str_to_list(""); |
| 298 | ut_assertnonnull(ptr); |
| 299 | ut_assertnull(ptr[0]); |
| 300 | str_free_list(ptr); |
| 301 | ut_assertok(ut_check_delta(start)); |
| 302 | |
| 303 | malloc_disable_testing(); |
| 304 | |
| 305 | /* test the same again, without any nalloc restrictions */ |
| 306 | ptr = str_to_list(""); |
| 307 | ut_assertnonnull(ptr); |
| 308 | ut_assertnull(ptr[0]); |
| 309 | str_free_list(ptr); |
| 310 | ut_assertok(ut_check_delta(start)); |
| 311 | |
| 312 | /* test a single string */ |
| 313 | start = ut_check_delta(0); |
| 314 | ptr = str_to_list("hi"); |
| 315 | ut_assertnonnull(ptr); |
| 316 | ut_assertnonnull(ptr[0]); |
| 317 | ut_asserteq_str("hi", ptr[0]); |
| 318 | ut_assertnull(ptr[1]); |
| 319 | str_free_list(ptr); |
| 320 | ut_assertok(ut_check_delta(start)); |
| 321 | |
| 322 | /* test two strings */ |
| 323 | ptr = str_to_list("hi there"); |
| 324 | ut_assertnonnull(ptr); |
| 325 | ut_assertnonnull(ptr[0]); |
| 326 | ut_asserteq_str("hi", ptr[0]); |
| 327 | ut_assertnonnull(ptr[1]); |
| 328 | ut_asserteq_str("there", ptr[1]); |
| 329 | ut_assertnull(ptr[2]); |
| 330 | str_free_list(ptr); |
| 331 | ut_assertok(ut_check_delta(start)); |
| 332 | |
| 333 | /* test leading, trailing and multiple spaces */ |
| 334 | ptr = str_to_list(" more space "); |
| 335 | ut_assertnonnull(ptr); |
| 336 | ut_assertnonnull(ptr[0]); |
| 337 | ut_asserteq_str("", ptr[0]); |
| 338 | ut_assertnonnull(ptr[1]); |
| 339 | ut_asserteq_str("more", ptr[1]); |
| 340 | ut_assertnonnull(ptr[2]); |
| 341 | ut_asserteq_str("", ptr[2]); |
| 342 | ut_assertnonnull(ptr[3]); |
| 343 | ut_asserteq_str("space", ptr[3]); |
| 344 | ut_assertnonnull(ptr[4]); |
| 345 | ut_asserteq_str("", ptr[4]); |
| 346 | ut_assertnonnull(ptr[5]); |
| 347 | ut_asserteq_str("", ptr[5]); |
| 348 | ut_assertnull(ptr[6]); |
| 349 | str_free_list(ptr); |
| 350 | ut_assertok(ut_check_delta(start)); |
| 351 | |
| 352 | /* test freeing a NULL pointer */ |
| 353 | str_free_list(NULL); |
| 354 | |
| 355 | return 0; |
| 356 | } |
| 357 | STR_TEST(test_str_to_list, 0); |
| 358 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 359 | int do_ut_str(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Simon Glass | 4f04d54 | 2020-04-08 08:32:55 -0600 | [diff] [blame] | 360 | { |
Simon Glass | a7a9875 | 2021-03-07 17:35:10 -0700 | [diff] [blame] | 361 | struct unit_test *tests = UNIT_TEST_SUITE_START(str_test); |
| 362 | const int n_ents = UNIT_TEST_SUITE_COUNT(str_test); |
Simon Glass | 4f04d54 | 2020-04-08 08:32:55 -0600 | [diff] [blame] | 363 | |
| 364 | return cmd_ut_category("str", "str_", tests, n_ents, argc, argv); |
| 365 | } |