Simon Glass | d050928 | 2020-04-08 08:32:55 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright 2020 Google LLC |
| 4 | */ |
| 5 | |
Simon Glass | d050928 | 2020-04-08 08:32:55 -0600 | [diff] [blame] | 6 | #include <vsprintf.h> |
Simon Glass | 2dcfdf1 | 2024-11-02 13:36:58 -0600 | [diff] [blame^] | 7 | #include <test/lib.h> |
Simon Glass | d050928 | 2020-04-08 08:32:55 -0600 | [diff] [blame] | 8 | #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 | |
| 14 | static const char str1[] = "I'm sorry I'm late."; |
| 15 | static const char str2[] = "1099abNo, don't bother apologising."; |
| 16 | static const char str3[] = "0xbI'm sorry you're alive."; |
Simon Glass | 238efc2 | 2021-07-24 09:03:33 -0600 | [diff] [blame] | 17 | static const char str4[] = "1234567890123 I lost closer friends"; |
| 18 | static const char str5[] = "0x9876543210the last time I was deloused"; |
Simon Glass | b9efdf5 | 2021-07-24 09:03:34 -0600 | [diff] [blame] | 19 | static const char str6[] = "0778octal is seldom used"; |
| 20 | static const char str7[] = "707it is a piece of computing history"; |
Simon Glass | d050928 | 2020-04-08 08:32:55 -0600 | [diff] [blame] | 21 | |
Patrick Delaunay | c8cdd49 | 2020-11-19 10:08:43 +0100 | [diff] [blame] | 22 | static int str_upper(struct unit_test_state *uts) |
Simon Glass | 811a260 | 2020-04-08 08:32:56 -0600 | [diff] [blame] | 23 | { |
| 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 Glass | 2dcfdf1 | 2024-11-02 13:36:58 -0600 | [diff] [blame^] | 58 | LIB_TEST(str_upper, 0); |
Simon Glass | 811a260 | 2020-04-08 08:32:56 -0600 | [diff] [blame] | 59 | |
Simon Glass | d050928 | 2020-04-08 08:32:55 -0600 | [diff] [blame] | 60 | static int run_strtoul(struct unit_test_state *uts, const char *str, int base, |
Simon Glass | 811a260 | 2020-04-08 08:32:56 -0600 | [diff] [blame] | 61 | ulong expect_val, int expect_endp_offset, bool upper) |
Simon Glass | d050928 | 2020-04-08 08:32:55 -0600 | [diff] [blame] | 62 | { |
Simon Glass | 811a260 | 2020-04-08 08:32:56 -0600 | [diff] [blame] | 63 | char out[TEST_STR_SIZE]; |
Simon Glass | d050928 | 2020-04-08 08:32:55 -0600 | [diff] [blame] | 64 | char *endp; |
| 65 | ulong val; |
| 66 | |
Simon Glass | 811a260 | 2020-04-08 08:32:56 -0600 | [diff] [blame] | 67 | strcpy(out, str); |
| 68 | if (upper) |
| 69 | str_to_upper(out, out, -1); |
| 70 | |
| 71 | val = simple_strtoul(out, &endp, base); |
Simon Glass | d050928 | 2020-04-08 08:32:55 -0600 | [diff] [blame] | 72 | ut_asserteq(expect_val, val); |
Simon Glass | 811a260 | 2020-04-08 08:32:56 -0600 | [diff] [blame] | 73 | ut_asserteq(expect_endp_offset, endp - out); |
Simon Glass | d050928 | 2020-04-08 08:32:55 -0600 | [diff] [blame] | 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | static int str_simple_strtoul(struct unit_test_state *uts) |
| 79 | { |
Simon Glass | 811a260 | 2020-04-08 08:32:56 -0600 | [diff] [blame] | 80 | int upper; |
Simon Glass | d050928 | 2020-04-08 08:32:55 -0600 | [diff] [blame] | 81 | |
Simon Glass | 811a260 | 2020-04-08 08:32:56 -0600 | [diff] [blame] | 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)); |
Simon Glass | 8be8eab | 2021-07-24 09:03:32 -0600 | [diff] [blame] | 87 | ut_assertok(run_strtoul(uts, str3, 16, 0xb, 3, upper)); |
Simon Glass | 90877bb | 2021-07-24 09:03:38 -0600 | [diff] [blame] | 88 | ut_assertok(run_strtoul(uts, str3, 10, 0xb, 3, upper)); |
Simon Glass | d050928 | 2020-04-08 08:32:55 -0600 | [diff] [blame] | 89 | |
Simon Glass | b9efdf5 | 2021-07-24 09:03:34 -0600 | [diff] [blame] | 90 | /* 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 Glass | 811a260 | 2020-04-08 08:32:56 -0600 | [diff] [blame] | 94 | /* 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 Glass | d050928 | 2020-04-08 08:32:55 -0600 | [diff] [blame] | 101 | |
Simon Glass | 811a260 | 2020-04-08 08:32:56 -0600 | [diff] [blame] | 102 | /* 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 Glass | d050928 | 2020-04-08 08:32:55 -0600 | [diff] [blame] | 106 | |
| 107 | /* Check endp being NULL */ |
| 108 | ut_asserteq(1099, simple_strtoul(str2, NULL, 0)); |
| 109 | |
| 110 | return 0; |
| 111 | } |
Simon Glass | 2dcfdf1 | 2024-11-02 13:36:58 -0600 | [diff] [blame^] | 112 | LIB_TEST(str_simple_strtoul, 0); |
Simon Glass | d050928 | 2020-04-08 08:32:55 -0600 | [diff] [blame] | 113 | |
Simon Glass | 238efc2 | 2021-07-24 09:03:33 -0600 | [diff] [blame] | 114 | static 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 | |
| 133 | static 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 Glass | 90877bb | 2021-07-24 09:03:38 -0600 | [diff] [blame] | 143 | ut_assertok(run_strtoull(uts, str3, 10, 0xb, 3, upper)); |
Simon Glass | 238efc2 | 2021-07-24 09:03:33 -0600 | [diff] [blame] | 144 | |
Simon Glass | b9efdf5 | 2021-07-24 09:03:34 -0600 | [diff] [blame] | 145 | /* 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 Glass | 238efc2 | 2021-07-24 09:03:33 -0600 | [diff] [blame] | 149 | /* 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 Glass | 2dcfdf1 | 2024-11-02 13:36:58 -0600 | [diff] [blame^] | 175 | LIB_TEST(str_simple_strtoull, 0); |
Simon Glass | 238efc2 | 2021-07-24 09:03:33 -0600 | [diff] [blame] | 176 | |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 177 | static 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 Glass | 2dcfdf1 | 2024-11-02 13:36:58 -0600 | [diff] [blame^] | 187 | LIB_TEST(str_hextoul, 0); |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 188 | |
Simon Glass | ff9b903 | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 189 | static 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 Glass | 2dcfdf1 | 2024-11-02 13:36:58 -0600 | [diff] [blame^] | 199 | LIB_TEST(str_dectoul, 0); |
Simon Glass | ff9b903 | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 200 | |
Simon Glass | 8c2d639 | 2022-04-24 23:30:54 -0600 | [diff] [blame] | 201 | static 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 Glass | 2dcfdf1 | 2024-11-02 13:36:58 -0600 | [diff] [blame^] | 219 | LIB_TEST(str_itoa, 0); |
Simon Glass | 8c2d639 | 2022-04-24 23:30:54 -0600 | [diff] [blame] | 220 | |
| 221 | static 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 Glass | 2dcfdf1 | 2024-11-02 13:36:58 -0600 | [diff] [blame^] | 239 | LIB_TEST(str_xtoa, 0); |
Simon Glass | 8c2d639 | 2022-04-24 23:30:54 -0600 | [diff] [blame] | 240 | |
Simon Glass | e74efcd | 2022-04-24 23:30:55 -0600 | [diff] [blame] | 241 | static int str_trailing(struct unit_test_state *uts) |
| 242 | { |
Simon Glass | 4bf2fc1 | 2022-04-24 23:30:58 -0600 | [diff] [blame] | 243 | const char str1[] = "abc123def"; |
| 244 | const char str2[] = "abc123def456"; |
| 245 | const char *end; |
Simon Glass | e74efcd | 2022-04-24 23:30:55 -0600 | [diff] [blame] | 246 | |
| 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 Glass | 20888f3 | 2022-04-24 23:30:57 -0600 | [diff] [blame] | 258 | ut_asserteq(3, trailing_strtol("a3")); |
| 259 | |
Simon Glass | 4bf2fc1 | 2022-04-24 23:30:58 -0600 | [diff] [blame] | 260 | 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 Glass | e74efcd | 2022-04-24 23:30:55 -0600 | [diff] [blame] | 269 | return 0; |
| 270 | } |
Simon Glass | 2dcfdf1 | 2024-11-02 13:36:58 -0600 | [diff] [blame^] | 271 | LIB_TEST(str_trailing, 0); |
Simon Glass | e74efcd | 2022-04-24 23:30:55 -0600 | [diff] [blame] | 272 | |
Simon Glass | 6a9de55 | 2023-01-17 10:47:14 -0700 | [diff] [blame] | 273 | static 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 Glass | 6667c01 | 2024-07-30 08:39:36 -0600 | [diff] [blame] | 342 | ut_assertnull(ptr[5]); |
Simon Glass | 6a9de55 | 2023-01-17 10:47:14 -0700 | [diff] [blame] | 343 | 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 Glass | 2dcfdf1 | 2024-11-02 13:36:58 -0600 | [diff] [blame^] | 351 | LIB_TEST(test_str_to_list, 0); |