2 #include <linux/kernel.h>
3 #include <linux/module.h>
5 #define for_each_test(i, test) \
6 for (i = 0; i < sizeof(test) / sizeof(test[0]); i++)
13 #define DEFINE_TEST_FAIL(test) \
14 const struct test_fail test[] __initconst
16 #define DECLARE_TEST_OK(type, test_type) \
23 #define DEFINE_TEST_OK(type, test) \
24 const type test[] __initconst
26 #define TEST_FAIL(fn, type, fmt, test) \
30 for_each_test(i, test) { \
31 const struct test_fail *t = &test[i]; \
36 rv = fn(t->str, t->base, &tmp); \
38 WARN(1, "str '%s', base %u, expected -E, got %d/" fmt "\n", \
39 t->str, t->base, rv, tmp); \
45 #define TEST_OK(fn, type, fmt, test) \
49 for_each_test(i, test) { \
50 const typeof(test[0]) *t = &test[i]; \
54 rv = fn(t->str, t->base, &res); \
56 WARN(1, "str '%s', base %u, expected 0/" fmt ", got %d\n", \
57 t->str, t->base, t->expected_res, rv); \
60 if (res != t->expected_res) { \
61 WARN(1, "str '%s', base %u, expected " fmt ", got " fmt "\n", \
62 t->str, t->base, t->expected_res, res); \
68 static void __init test_kstrtoull_ok(
void)
80 {
"32767", 10, 32767ULL},
81 {
"32768", 10, 32768ULL},
82 {
"32769", 10, 32769ULL},
83 {
"65535", 10, 65535ULL},
84 {
"65536", 10, 65536ULL},
85 {
"65537", 10, 65537ULL},
86 {
"2147483647", 10, 2147483647ULL},
87 {
"2147483648", 10, 2147483648ULL},
88 {
"2147483649", 10, 2147483649ULL},
89 {
"4294967295", 10, 4294967295ULL},
90 {
"4294967296", 10, 4294967296ULL},
91 {
"4294967297", 10, 4294967297ULL},
92 {
"9223372036854775807", 10, 9223372036854775807ULL},
93 {
"9223372036854775808", 10, 9223372036854775808ULL},
94 {
"9223372036854775809", 10, 9223372036854775809ULL},
95 {
"18446744073709551614", 10, 18446744073709551614ULL},
96 {
"18446744073709551615", 10, 18446744073709551615ULL},
100 {
"0177", 8, 0177ULL},
101 {
"0200", 8, 0200ULL},
102 {
"0201", 8, 0201ULL},
103 {
"0377", 8, 0377ULL},
104 {
"0400", 8, 0400ULL},
105 {
"0401", 8, 0401ULL},
106 {
"077777", 8, 077777ULL},
107 {
"0100000", 8, 0100000ULL},
108 {
"0100001", 8, 0100001ULL},
109 {
"0177777", 8, 0177777ULL},
110 {
"0200000", 8, 0200000ULL},
111 {
"0200001", 8, 0200001ULL},
112 {
"017777777777", 8, 017777777777ULL},
113 {
"020000000000", 8, 020000000000ULL},
114 {
"020000000001", 8, 020000000001ULL},
115 {
"037777777777", 8, 037777777777ULL},
116 {
"040000000000", 8, 040000000000ULL},
117 {
"040000000001", 8, 040000000001ULL},
118 {
"0777777777777777777777", 8, 0777777777777777777777ULL},
119 {
"01000000000000000000000", 8, 01000000000000000000000ULL},
120 {
"01000000000000000000001", 8, 01000000000000000000001ULL},
121 {
"01777777777777777777776", 8, 01777777777777777777776ULL},
122 {
"01777777777777777777777", 8, 01777777777777777777777ULL},
126 {
"0x7f", 16, 0x7fULL},
127 {
"0x80", 16, 0x80ULL},
128 {
"0x81", 16, 0x81ULL},
129 {
"0xff", 16, 0xffULL},
130 {
"0x100", 16, 0x100ULL},
131 {
"0x101", 16, 0x101ULL},
132 {
"0x7fff", 16, 0x7fffULL},
133 {
"0x8000", 16, 0x8000ULL},
134 {
"0x8001", 16, 0x8001ULL},
135 {
"0xffff", 16, 0xffffULL},
136 {
"0x10000", 16, 0x10000ULL},
137 {
"0x10001", 16, 0x10001ULL},
138 {
"0x7fffffff", 16, 0x7fffffffULL},
139 {
"0x80000000", 16, 0x80000000ULL},
140 {
"0x80000001", 16, 0x80000001ULL},
141 {
"0xffffffff", 16, 0xffffffffULL},
142 {
"0x100000000", 16, 0x100000000ULL},
143 {
"0x100000001", 16, 0x100000001ULL},
144 {
"0x7fffffffffffffff", 16, 0x7fffffffffffffffULL},
145 {
"0x8000000000000000", 16, 0x8000000000000000ULL},
146 {
"0x8000000000000001", 16, 0x8000000000000001ULL},
147 {
"0xfffffffffffffffe", 16, 0xfffffffffffffffeULL},
148 {
"0xffffffffffffffff", 16, 0xffffffffffffffffULL},
155 static void __init test_kstrtoull_fail(
void)
198 {
"10000000000000000000000000000000000000000000000000000000000000000", 2},
199 {
"2000000000000000000000", 8},
200 {
"18446744073709551616", 10},
201 {
"10000000000000000", 16},
237 static void __init test_kstrtoll_ok(
void)
249 {
"32767", 10, 32767
LL},
250 {
"32768", 10, 32768
LL},
251 {
"32769", 10, 32769
LL},
252 {
"65535", 10, 65535
LL},
253 {
"65536", 10, 65536
LL},
254 {
"65537", 10, 65537
LL},
255 {
"2147483647", 10, 2147483647
LL},
256 {
"2147483648", 10, 2147483648
LL},
257 {
"2147483649", 10, 2147483649
LL},
258 {
"4294967295", 10, 4294967295
LL},
259 {
"4294967296", 10, 4294967296
LL},
260 {
"4294967297", 10, 4294967297
LL},
261 {
"9223372036854775807", 10, 9223372036854775807
LL},
270 static void __init test_kstrtoll_fail(
void)
273 {
"9223372036854775808", 10},
274 {
"9223372036854775809", 10},
275 {
"18446744073709551614", 10},
276 {
"18446744073709551615", 10},
277 {
"-9223372036854775809", 10},
278 {
"-18446744073709551614", 10},
279 {
"-18446744073709551615", 10},
294 static void __init test_kstrtou64_ok(
void)
308 {
"32766", 10, 32766},
309 {
"32767", 10, 32767},
310 {
"32768", 10, 32768},
311 {
"32769", 10, 32769},
312 {
"65534", 10, 65534},
313 {
"65535", 10, 65535},
314 {
"65536", 10, 65536},
315 {
"65537", 10, 65537},
316 {
"2147483646", 10, 2147483646},
317 {
"2147483647", 10, 2147483647},
318 {
"2147483648", 10, 2147483648ULL},
319 {
"2147483649", 10, 2147483649ULL},
320 {
"4294967294", 10, 4294967294ULL},
321 {
"4294967295", 10, 4294967295ULL},
322 {
"4294967296", 10, 4294967296ULL},
323 {
"4294967297", 10, 4294967297ULL},
324 {
"9223372036854775806", 10, 9223372036854775806ULL},
325 {
"9223372036854775807", 10, 9223372036854775807ULL},
326 {
"9223372036854775808", 10, 9223372036854775808ULL},
327 {
"9223372036854775809", 10, 9223372036854775809ULL},
328 {
"18446744073709551614", 10, 18446744073709551614ULL},
329 {
"18446744073709551615", 10, 18446744073709551615ULL},
334 static void __init test_kstrtou64_fail(
void)
339 {
"18446744073709551616", 10},
340 {
"18446744073709551617", 10},
345 static void __init test_kstrtos64_ok(
void)
362 {
"32766", 10, 32766},
363 {
"32767", 10, 32767},
364 {
"32768", 10, 32768},
365 {
"32769", 10, 32769},
366 {
"65534", 10, 65534},
367 {
"65535", 10, 65535},
368 {
"65536", 10, 65536},
369 {
"65537", 10, 65537},
370 {
"2147483646", 10, 2147483646},
371 {
"2147483647", 10, 2147483647},
372 {
"2147483648", 10, 2147483648
LL},
373 {
"2147483649", 10, 2147483649
LL},
374 {
"4294967294", 10, 4294967294
LL},
375 {
"4294967295", 10, 4294967295
LL},
376 {
"4294967296", 10, 4294967296
LL},
377 {
"4294967297", 10, 4294967297
LL},
378 {
"9223372036854775806", 10, 9223372036854775806
LL},
379 {
"9223372036854775807", 10, 9223372036854775807
LL},
384 static void __init test_kstrtos64_fail(
void)
387 {
"9223372036854775808", 10},
388 {
"9223372036854775809", 10},
389 {
"18446744073709551614", 10},
390 {
"18446744073709551615", 10},
391 {
"18446744073709551616", 10},
392 {
"18446744073709551617", 10},
397 static void __init test_kstrtou32_ok(
void)
411 {
"32766", 10, 32766},
412 {
"32767", 10, 32767},
413 {
"32768", 10, 32768},
414 {
"32769", 10, 32769},
415 {
"65534", 10, 65534},
416 {
"65535", 10, 65535},
417 {
"65536", 10, 65536},
418 {
"65537", 10, 65537},
419 {
"2147483646", 10, 2147483646},
420 {
"2147483647", 10, 2147483647},
421 {
"2147483648", 10, 2147483648
U},
422 {
"2147483649", 10, 2147483649
U},
423 {
"4294967294", 10, 4294967294
U},
424 {
"4294967295", 10, 4294967295
U},
429 static void __init test_kstrtou32_fail(
void)
436 {
"9223372036854775806", 10},
437 {
"9223372036854775807", 10},
438 {
"9223372036854775808", 10},
439 {
"9223372036854775809", 10},
440 {
"18446744073709551614", 10},
441 {
"18446744073709551615", 10},
442 {
"18446744073709551616", 10},
443 {
"18446744073709551617", 10},
448 static void __init test_kstrtos32_ok(
void)
465 {
"32766", 10, 32766},
466 {
"32767", 10, 32767},
467 {
"32768", 10, 32768},
468 {
"32769", 10, 32769},
469 {
"65534", 10, 65534},
470 {
"65535", 10, 65535},
471 {
"65536", 10, 65536},
472 {
"65537", 10, 65537},
473 {
"2147483646", 10, 2147483646},
474 {
"2147483647", 10, 2147483647},
479 static void __init test_kstrtos32_fail(
void)
488 {
"9223372036854775806", 10},
489 {
"9223372036854775807", 10},
490 {
"9223372036854775808", 10},
491 {
"9223372036854775809", 10},
492 {
"18446744073709551614", 10},
493 {
"18446744073709551615", 10},
494 {
"18446744073709551616", 10},
495 {
"18446744073709551617", 10},
500 static void __init test_kstrtou16_ok(
void)
514 {
"32766", 10, 32766},
515 {
"32767", 10, 32767},
516 {
"32768", 10, 32768},
517 {
"32769", 10, 32769},
518 {
"65534", 10, 65534},
519 {
"65535", 10, 65535},
524 static void __init test_kstrtou16_fail(
void)
539 {
"9223372036854775806", 10},
540 {
"9223372036854775807", 10},
541 {
"9223372036854775808", 10},
542 {
"9223372036854775809", 10},
543 {
"18446744073709551614", 10},
544 {
"18446744073709551615", 10},
545 {
"18446744073709551616", 10},
546 {
"18446744073709551617", 10},
551 static void __init test_kstrtos16_ok(
void)
570 {
"32766", 10, 32766},
571 {
"32767", 10, 32767},
576 static void __init test_kstrtos16_fail(
void)
593 {
"9223372036854775806", 10},
594 {
"9223372036854775807", 10},
595 {
"9223372036854775808", 10},
596 {
"9223372036854775809", 10},
597 {
"18446744073709551614", 10},
598 {
"18446744073709551615", 10},
599 {
"18446744073709551616", 10},
600 {
"18446744073709551617", 10},
605 static void __init test_kstrtou8_ok(
void)
621 static void __init test_kstrtou8_fail(
void)
644 {
"9223372036854775806", 10},
645 {
"9223372036854775807", 10},
646 {
"9223372036854775808", 10},
647 {
"9223372036854775809", 10},
648 {
"18446744073709551614", 10},
649 {
"18446744073709551615", 10},
650 {
"18446744073709551616", 10},
651 {
"18446744073709551617", 10},
656 static void __init test_kstrtos8_ok(
void)
671 static void __init test_kstrtos8_fail(
void)
698 {
"9223372036854775806", 10},
699 {
"9223372036854775807", 10},
700 {
"9223372036854775808", 10},
701 {
"9223372036854775809", 10},
702 {
"18446744073709551614", 10},
703 {
"18446744073709551615", 10},
704 {
"18446744073709551616", 10},
705 {
"18446744073709551617", 10},
710 static int __init test_kstrtox_init(
void)
713 test_kstrtoull_fail();
715 test_kstrtoll_fail();
718 test_kstrtou64_fail();
720 test_kstrtos64_fail();
723 test_kstrtou32_fail();
725 test_kstrtos32_fail();
728 test_kstrtou16_fail();
730 test_kstrtos16_fail();
733 test_kstrtou8_fail();
735 test_kstrtos8_fail();