1 #include <linux/init.h>
2 #include <linux/kernel.h>
3 #include <linux/module.h>
5 #define for_each_test(i, test) \
6 for (i = 0; i < ARRAY_SIZE(test); 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)
70 DECLARE_TEST_OK(unsigned long long, struct test_ull
);
71 static DEFINE_TEST_OK(struct test_ull
, test_ull_ok
) = {
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
},
152 TEST_OK(kstrtoull
, unsigned long long, "%llu", test_ull_ok
);
155 static void __init
test_kstrtoull_fail(void)
157 static DEFINE_TEST_FAIL(test_ull_fail
) = {
186 /* base autodetection */
198 {"10000000000000000000000000000000000000000000000000000000000000000", 2},
199 {"2000000000000000000000", 8},
200 {"18446744073709551616", 10},
201 {"10000000000000000", 16},
211 /* sign is first character if any */
216 /* nothing after \n */
234 TEST_FAIL(kstrtoull
, unsigned long long, "%llu", test_ull_fail
);
237 static void __init
test_kstrtoll_ok(void)
239 DECLARE_TEST_OK(long long, struct test_ll
);
240 static DEFINE_TEST_OK(struct test_ll
, test_ll_ok
) = {
249 {"32767", 10, 32767LL},
250 {"32768", 10, 32768LL},
251 {"32769", 10, 32769LL},
252 {"65535", 10, 65535LL},
253 {"65536", 10, 65536LL},
254 {"65537", 10, 65537LL},
255 {"2147483647", 10, 2147483647LL},
256 {"2147483648", 10, 2147483648LL},
257 {"2147483649", 10, 2147483649LL},
258 {"4294967295", 10, 4294967295LL},
259 {"4294967296", 10, 4294967296LL},
260 {"4294967297", 10, 4294967297LL},
261 {"9223372036854775807", 10, 9223372036854775807LL},
266 {"-9223372036854775808", 10, LLONG_MIN
},
268 TEST_OK(kstrtoll
, long long, "%lld", test_ll_ok
);
271 static void __init
test_kstrtoll_fail(void)
273 static DEFINE_TEST_FAIL(test_ll_fail
) = {
274 {"9223372036854775808", 10},
275 {"9223372036854775809", 10},
276 {"18446744073709551614", 10},
277 {"18446744073709551615", 10},
278 {"-9223372036854775809", 10},
279 {"-18446744073709551614", 10},
280 {"-18446744073709551615", 10},
281 /* sign is first character if any */
287 TEST_FAIL(kstrtoll
, long long, "%lld", test_ll_fail
);
290 static void __init
test_kstrtou64_ok(void)
292 DECLARE_TEST_OK(u64
, struct test_u64
);
293 static DEFINE_TEST_OK(struct test_u64
, test_u64_ok
) = {
304 {"32766", 10, 32766},
305 {"32767", 10, 32767},
306 {"32768", 10, 32768},
307 {"32769", 10, 32769},
308 {"65534", 10, 65534},
309 {"65535", 10, 65535},
310 {"65536", 10, 65536},
311 {"65537", 10, 65537},
312 {"2147483646", 10, 2147483646},
313 {"2147483647", 10, 2147483647},
314 {"2147483648", 10, 2147483648ULL},
315 {"2147483649", 10, 2147483649ULL},
316 {"4294967294", 10, 4294967294ULL},
317 {"4294967295", 10, 4294967295ULL},
318 {"4294967296", 10, 4294967296ULL},
319 {"4294967297", 10, 4294967297ULL},
320 {"9223372036854775806", 10, 9223372036854775806ULL},
321 {"9223372036854775807", 10, 9223372036854775807ULL},
322 {"9223372036854775808", 10, 9223372036854775808ULL},
323 {"9223372036854775809", 10, 9223372036854775809ULL},
324 {"18446744073709551614", 10, 18446744073709551614ULL},
325 {"18446744073709551615", 10, 18446744073709551615ULL},
327 TEST_OK(kstrtou64
, u64
, "%llu", test_u64_ok
);
330 static void __init
test_kstrtou64_fail(void)
332 static DEFINE_TEST_FAIL(test_u64_fail
) = {
335 {"18446744073709551616", 10},
336 {"18446744073709551617", 10},
338 TEST_FAIL(kstrtou64
, u64
, "%llu", test_u64_fail
);
341 static void __init
test_kstrtos64_ok(void)
343 DECLARE_TEST_OK(s64
, struct test_s64
);
344 static DEFINE_TEST_OK(struct test_s64
, test_s64_ok
) = {
358 {"32766", 10, 32766},
359 {"32767", 10, 32767},
360 {"32768", 10, 32768},
361 {"32769", 10, 32769},
362 {"65534", 10, 65534},
363 {"65535", 10, 65535},
364 {"65536", 10, 65536},
365 {"65537", 10, 65537},
366 {"2147483646", 10, 2147483646},
367 {"2147483647", 10, 2147483647},
368 {"2147483648", 10, 2147483648LL},
369 {"2147483649", 10, 2147483649LL},
370 {"4294967294", 10, 4294967294LL},
371 {"4294967295", 10, 4294967295LL},
372 {"4294967296", 10, 4294967296LL},
373 {"4294967297", 10, 4294967297LL},
374 {"9223372036854775806", 10, 9223372036854775806LL},
375 {"9223372036854775807", 10, 9223372036854775807LL},
377 TEST_OK(kstrtos64
, s64
, "%lld", test_s64_ok
);
380 static void __init
test_kstrtos64_fail(void)
382 static DEFINE_TEST_FAIL(test_s64_fail
) = {
383 {"9223372036854775808", 10},
384 {"9223372036854775809", 10},
385 {"18446744073709551614", 10},
386 {"18446744073709551615", 10},
387 {"18446744073709551616", 10},
388 {"18446744073709551617", 10},
390 TEST_FAIL(kstrtos64
, s64
, "%lld", test_s64_fail
);
393 static void __init
test_kstrtou32_ok(void)
395 DECLARE_TEST_OK(u32
, struct test_u32
);
396 static DEFINE_TEST_OK(struct test_u32
, test_u32_ok
) = {
407 {"32766", 10, 32766},
408 {"32767", 10, 32767},
409 {"32768", 10, 32768},
410 {"32769", 10, 32769},
411 {"65534", 10, 65534},
412 {"65535", 10, 65535},
413 {"65536", 10, 65536},
414 {"65537", 10, 65537},
415 {"2147483646", 10, 2147483646},
416 {"2147483647", 10, 2147483647},
417 {"2147483648", 10, 2147483648U},
418 {"2147483649", 10, 2147483649U},
419 {"4294967294", 10, 4294967294U},
420 {"4294967295", 10, 4294967295U},
422 TEST_OK(kstrtou32
, u32
, "%u", test_u32_ok
);
425 static void __init
test_kstrtou32_fail(void)
427 static DEFINE_TEST_FAIL(test_u32_fail
) = {
432 {"9223372036854775806", 10},
433 {"9223372036854775807", 10},
434 {"9223372036854775808", 10},
435 {"9223372036854775809", 10},
436 {"18446744073709551614", 10},
437 {"18446744073709551615", 10},
438 {"18446744073709551616", 10},
439 {"18446744073709551617", 10},
441 TEST_FAIL(kstrtou32
, u32
, "%u", test_u32_fail
);
444 static void __init
test_kstrtos32_ok(void)
446 DECLARE_TEST_OK(s32
, struct test_s32
);
447 static DEFINE_TEST_OK(struct test_s32
, test_s32_ok
) = {
461 {"32766", 10, 32766},
462 {"32767", 10, 32767},
463 {"32768", 10, 32768},
464 {"32769", 10, 32769},
465 {"65534", 10, 65534},
466 {"65535", 10, 65535},
467 {"65536", 10, 65536},
468 {"65537", 10, 65537},
469 {"2147483646", 10, 2147483646},
470 {"2147483647", 10, 2147483647},
472 TEST_OK(kstrtos32
, s32
, "%d", test_s32_ok
);
475 static void __init
test_kstrtos32_fail(void)
477 static DEFINE_TEST_FAIL(test_s32_fail
) = {
484 {"9223372036854775806", 10},
485 {"9223372036854775807", 10},
486 {"9223372036854775808", 10},
487 {"9223372036854775809", 10},
488 {"18446744073709551614", 10},
489 {"18446744073709551615", 10},
490 {"18446744073709551616", 10},
491 {"18446744073709551617", 10},
493 TEST_FAIL(kstrtos32
, s32
, "%d", test_s32_fail
);
496 static void __init
test_kstrtou16_ok(void)
498 DECLARE_TEST_OK(u16
, struct test_u16
);
499 static DEFINE_TEST_OK(struct test_u16
, test_u16_ok
) = {
510 {"32766", 10, 32766},
511 {"32767", 10, 32767},
512 {"32768", 10, 32768},
513 {"32769", 10, 32769},
514 {"65534", 10, 65534},
515 {"65535", 10, 65535},
517 TEST_OK(kstrtou16
, u16
, "%hu", test_u16_ok
);
520 static void __init
test_kstrtou16_fail(void)
522 static DEFINE_TEST_FAIL(test_u16_fail
) = {
535 {"9223372036854775806", 10},
536 {"9223372036854775807", 10},
537 {"9223372036854775808", 10},
538 {"9223372036854775809", 10},
539 {"18446744073709551614", 10},
540 {"18446744073709551615", 10},
541 {"18446744073709551616", 10},
542 {"18446744073709551617", 10},
544 TEST_FAIL(kstrtou16
, u16
, "%hu", test_u16_fail
);
547 static void __init
test_kstrtos16_ok(void)
549 DECLARE_TEST_OK(s16
, struct test_s16
);
550 static DEFINE_TEST_OK(struct test_s16
, test_s16_ok
) = {
566 {"32766", 10, 32766},
567 {"32767", 10, 32767},
569 TEST_OK(kstrtos16
, s16
, "%hd", test_s16_ok
);
572 static void __init
test_kstrtos16_fail(void)
574 static DEFINE_TEST_FAIL(test_s16_fail
) = {
589 {"9223372036854775806", 10},
590 {"9223372036854775807", 10},
591 {"9223372036854775808", 10},
592 {"9223372036854775809", 10},
593 {"18446744073709551614", 10},
594 {"18446744073709551615", 10},
595 {"18446744073709551616", 10},
596 {"18446744073709551617", 10},
598 TEST_FAIL(kstrtos16
, s16
, "%hd", test_s16_fail
);
601 static void __init
test_kstrtou8_ok(void)
603 DECLARE_TEST_OK(u8
, struct test_u8
);
604 static DEFINE_TEST_OK(struct test_u8
, test_u8_ok
) = {
614 TEST_OK(kstrtou8
, u8
, "%hhu", test_u8_ok
);
617 static void __init
test_kstrtou8_fail(void)
619 static DEFINE_TEST_FAIL(test_u8_fail
) = {
640 {"9223372036854775806", 10},
641 {"9223372036854775807", 10},
642 {"9223372036854775808", 10},
643 {"9223372036854775809", 10},
644 {"18446744073709551614", 10},
645 {"18446744073709551615", 10},
646 {"18446744073709551616", 10},
647 {"18446744073709551617", 10},
649 TEST_FAIL(kstrtou8
, u8
, "%hhu", test_u8_fail
);
652 static void __init
test_kstrtos8_ok(void)
654 DECLARE_TEST_OK(s8
, struct test_s8
);
655 static DEFINE_TEST_OK(struct test_s8
, test_s8_ok
) = {
664 TEST_OK(kstrtos8
, s8
, "%hhd", test_s8_ok
);
667 static void __init
test_kstrtos8_fail(void)
669 static DEFINE_TEST_FAIL(test_s8_fail
) = {
694 {"9223372036854775806", 10},
695 {"9223372036854775807", 10},
696 {"9223372036854775808", 10},
697 {"9223372036854775809", 10},
698 {"18446744073709551614", 10},
699 {"18446744073709551615", 10},
700 {"18446744073709551616", 10},
701 {"18446744073709551617", 10},
703 TEST_FAIL(kstrtos8
, s8
, "%hhd", test_s8_fail
);
706 static int __init
test_kstrtox_init(void)
709 test_kstrtoull_fail();
711 test_kstrtoll_fail();
714 test_kstrtou64_fail();
716 test_kstrtos64_fail();
719 test_kstrtou32_fail();
721 test_kstrtos32_fail();
724 test_kstrtou16_fail();
726 test_kstrtos16_fail();
729 test_kstrtou8_fail();
731 test_kstrtos8_fail();
734 module_init(test_kstrtox_init
);
735 MODULE_LICENSE("Dual BSD/GPL");