Loading...
1#include <linux/init.h>
2#include <linux/kernel.h>
3#include <linux/module.h>
4
5#define for_each_test(i, test) \
6 for (i = 0; i < ARRAY_SIZE(test); i++)
7
8struct test_fail {
9 const char *str;
10 unsigned int base;
11};
12
13#define DEFINE_TEST_FAIL(test) \
14 const struct test_fail test[] __initconst
15
16#define DECLARE_TEST_OK(type, test_type) \
17 test_type { \
18 const char *str; \
19 unsigned int base; \
20 type expected_res; \
21 }
22
23#define DEFINE_TEST_OK(type, test) \
24 const type test[] __initconst
25
26#define TEST_FAIL(fn, type, fmt, test) \
27{ \
28 unsigned int i; \
29 \
30 for_each_test(i, test) { \
31 const struct test_fail *t = &test[i]; \
32 type tmp; \
33 int rv; \
34 \
35 tmp = 0; \
36 rv = fn(t->str, t->base, &tmp); \
37 if (rv >= 0) { \
38 WARN(1, "str '%s', base %u, expected -E, got %d/" fmt "\n", \
39 t->str, t->base, rv, tmp); \
40 continue; \
41 } \
42 } \
43}
44
45#define TEST_OK(fn, type, fmt, test) \
46{ \
47 unsigned int i; \
48 \
49 for_each_test(i, test) { \
50 const typeof(test[0]) *t = &test[i]; \
51 type res; \
52 int rv; \
53 \
54 rv = fn(t->str, t->base, &res); \
55 if (rv != 0) { \
56 WARN(1, "str '%s', base %u, expected 0/" fmt ", got %d\n", \
57 t->str, t->base, t->expected_res, rv); \
58 continue; \
59 } \
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); \
63 continue; \
64 } \
65 } \
66}
67
68static void __init test_kstrtoull_ok(void)
69{
70 DECLARE_TEST_OK(unsigned long long, struct test_ull);
71 static DEFINE_TEST_OK(struct test_ull, test_ull_ok) = {
72 {"0", 10, 0ULL},
73 {"1", 10, 1ULL},
74 {"127", 10, 127ULL},
75 {"128", 10, 128ULL},
76 {"129", 10, 129ULL},
77 {"255", 10, 255ULL},
78 {"256", 10, 256ULL},
79 {"257", 10, 257ULL},
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},
97
98 {"00", 8, 00ULL},
99 {"01", 8, 01ULL},
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},
123
124 {"0x0", 16, 0x0ULL},
125 {"0x1", 16, 0x1ULL},
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},
149
150 {"0\n", 0, 0ULL},
151 };
152 TEST_OK(kstrtoull, unsigned long long, "%llu", test_ull_ok);
153}
154
155static void __init test_kstrtoull_fail(void)
156{
157 static DEFINE_TEST_FAIL(test_ull_fail) = {
158 {"", 0},
159 {"", 8},
160 {"", 10},
161 {"", 16},
162 {"\n", 0},
163 {"\n", 8},
164 {"\n", 10},
165 {"\n", 16},
166 {"\n0", 0},
167 {"\n0", 8},
168 {"\n0", 10},
169 {"\n0", 16},
170 {"+", 0},
171 {"+", 8},
172 {"+", 10},
173 {"+", 16},
174 {"-", 0},
175 {"-", 8},
176 {"-", 10},
177 {"-", 16},
178 {"0x", 0},
179 {"0x", 16},
180 {"0X", 0},
181 {"0X", 16},
182 {"0 ", 0},
183 {"1+", 0},
184 {"1-", 0},
185 {" 2", 0},
186 /* base autodetection */
187 {"0x0z", 0},
188 {"0z", 0},
189 {"a", 0},
190 /* digit >= base */
191 {"2", 2},
192 {"8", 8},
193 {"a", 10},
194 {"A", 10},
195 {"g", 16},
196 {"G", 16},
197 /* overflow */
198 {"10000000000000000000000000000000000000000000000000000000000000000", 2},
199 {"2000000000000000000000", 8},
200 {"18446744073709551616", 10},
201 {"10000000000000000", 16},
202 /* negative */
203 {"-0", 0},
204 {"-0", 8},
205 {"-0", 10},
206 {"-0", 16},
207 {"-1", 0},
208 {"-1", 8},
209 {"-1", 10},
210 {"-1", 16},
211 /* sign is first character if any */
212 {"-+1", 0},
213 {"-+1", 8},
214 {"-+1", 10},
215 {"-+1", 16},
216 /* nothing after \n */
217 {"0\n0", 0},
218 {"0\n0", 8},
219 {"0\n0", 10},
220 {"0\n0", 16},
221 {"0\n+", 0},
222 {"0\n+", 8},
223 {"0\n+", 10},
224 {"0\n+", 16},
225 {"0\n-", 0},
226 {"0\n-", 8},
227 {"0\n-", 10},
228 {"0\n-", 16},
229 {"0\n ", 0},
230 {"0\n ", 8},
231 {"0\n ", 10},
232 {"0\n ", 16},
233 };
234 TEST_FAIL(kstrtoull, unsigned long long, "%llu", test_ull_fail);
235}
236
237static void __init test_kstrtoll_ok(void)
238{
239 DECLARE_TEST_OK(long long, struct test_ll);
240 static DEFINE_TEST_OK(struct test_ll, test_ll_ok) = {
241 {"0", 10, 0LL},
242 {"1", 10, 1LL},
243 {"127", 10, 127LL},
244 {"128", 10, 128LL},
245 {"129", 10, 129LL},
246 {"255", 10, 255LL},
247 {"256", 10, 256LL},
248 {"257", 10, 257LL},
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},
262
263 {"-0", 10, 0LL},
264 {"-1", 10, -1LL},
265 {"-2", 10, -2LL},
266 {"-9223372036854775808", 10, LLONG_MIN},
267 };
268 TEST_OK(kstrtoll, long long, "%lld", test_ll_ok);
269}
270
271static void __init test_kstrtoll_fail(void)
272{
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 */
282 {"-+1", 0},
283 {"-+1", 8},
284 {"-+1", 10},
285 {"-+1", 16},
286 };
287 TEST_FAIL(kstrtoll, long long, "%lld", test_ll_fail);
288}
289
290static void __init test_kstrtou64_ok(void)
291{
292 DECLARE_TEST_OK(u64, struct test_u64);
293 static DEFINE_TEST_OK(struct test_u64, test_u64_ok) = {
294 {"0", 10, 0},
295 {"1", 10, 1},
296 {"126", 10, 126},
297 {"127", 10, 127},
298 {"128", 10, 128},
299 {"129", 10, 129},
300 {"254", 10, 254},
301 {"255", 10, 255},
302 {"256", 10, 256},
303 {"257", 10, 257},
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},
326 };
327 TEST_OK(kstrtou64, u64, "%llu", test_u64_ok);
328}
329
330static void __init test_kstrtou64_fail(void)
331{
332 static DEFINE_TEST_FAIL(test_u64_fail) = {
333 {"-2", 10},
334 {"-1", 10},
335 {"18446744073709551616", 10},
336 {"18446744073709551617", 10},
337 };
338 TEST_FAIL(kstrtou64, u64, "%llu", test_u64_fail);
339}
340
341static void __init test_kstrtos64_ok(void)
342{
343 DECLARE_TEST_OK(s64, struct test_s64);
344 static DEFINE_TEST_OK(struct test_s64, test_s64_ok) = {
345 {"-128", 10, -128},
346 {"-127", 10, -127},
347 {"-1", 10, -1},
348 {"0", 10, 0},
349 {"1", 10, 1},
350 {"126", 10, 126},
351 {"127", 10, 127},
352 {"128", 10, 128},
353 {"129", 10, 129},
354 {"254", 10, 254},
355 {"255", 10, 255},
356 {"256", 10, 256},
357 {"257", 10, 257},
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},
376 };
377 TEST_OK(kstrtos64, s64, "%lld", test_s64_ok);
378}
379
380static void __init test_kstrtos64_fail(void)
381{
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},
389 };
390 TEST_FAIL(kstrtos64, s64, "%lld", test_s64_fail);
391}
392
393static void __init test_kstrtou32_ok(void)
394{
395 DECLARE_TEST_OK(u32, struct test_u32);
396 static DEFINE_TEST_OK(struct test_u32, test_u32_ok) = {
397 {"0", 10, 0},
398 {"1", 10, 1},
399 {"126", 10, 126},
400 {"127", 10, 127},
401 {"128", 10, 128},
402 {"129", 10, 129},
403 {"254", 10, 254},
404 {"255", 10, 255},
405 {"256", 10, 256},
406 {"257", 10, 257},
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},
421 };
422 TEST_OK(kstrtou32, u32, "%u", test_u32_ok);
423}
424
425static void __init test_kstrtou32_fail(void)
426{
427 static DEFINE_TEST_FAIL(test_u32_fail) = {
428 {"-2", 10},
429 {"-1", 10},
430 {"4294967296", 10},
431 {"4294967297", 10},
432 {"9223372036854775806", 10},
433 {"9223372036854775807", 10},
434 {"9223372036854775808", 10},
435 {"9223372036854775809", 10},
436 {"18446744073709551614", 10},
437 {"18446744073709551615", 10},
438 {"18446744073709551616", 10},
439 {"18446744073709551617", 10},
440 };
441 TEST_FAIL(kstrtou32, u32, "%u", test_u32_fail);
442}
443
444static void __init test_kstrtos32_ok(void)
445{
446 DECLARE_TEST_OK(s32, struct test_s32);
447 static DEFINE_TEST_OK(struct test_s32, test_s32_ok) = {
448 {"-128", 10, -128},
449 {"-127", 10, -127},
450 {"-1", 10, -1},
451 {"0", 10, 0},
452 {"1", 10, 1},
453 {"126", 10, 126},
454 {"127", 10, 127},
455 {"128", 10, 128},
456 {"129", 10, 129},
457 {"254", 10, 254},
458 {"255", 10, 255},
459 {"256", 10, 256},
460 {"257", 10, 257},
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},
471 };
472 TEST_OK(kstrtos32, s32, "%d", test_s32_ok);
473}
474
475static void __init test_kstrtos32_fail(void)
476{
477 static DEFINE_TEST_FAIL(test_s32_fail) = {
478 {"2147483648", 10},
479 {"2147483649", 10},
480 {"4294967294", 10},
481 {"4294967295", 10},
482 {"4294967296", 10},
483 {"4294967297", 10},
484 {"9223372036854775806", 10},
485 {"9223372036854775807", 10},
486 {"9223372036854775808", 10},
487 {"9223372036854775809", 10},
488 {"18446744073709551614", 10},
489 {"18446744073709551615", 10},
490 {"18446744073709551616", 10},
491 {"18446744073709551617", 10},
492 };
493 TEST_FAIL(kstrtos32, s32, "%d", test_s32_fail);
494}
495
496static void __init test_kstrtou16_ok(void)
497{
498 DECLARE_TEST_OK(u16, struct test_u16);
499 static DEFINE_TEST_OK(struct test_u16, test_u16_ok) = {
500 {"0", 10, 0},
501 {"1", 10, 1},
502 {"126", 10, 126},
503 {"127", 10, 127},
504 {"128", 10, 128},
505 {"129", 10, 129},
506 {"254", 10, 254},
507 {"255", 10, 255},
508 {"256", 10, 256},
509 {"257", 10, 257},
510 {"32766", 10, 32766},
511 {"32767", 10, 32767},
512 {"32768", 10, 32768},
513 {"32769", 10, 32769},
514 {"65534", 10, 65534},
515 {"65535", 10, 65535},
516 };
517 TEST_OK(kstrtou16, u16, "%hu", test_u16_ok);
518}
519
520static void __init test_kstrtou16_fail(void)
521{
522 static DEFINE_TEST_FAIL(test_u16_fail) = {
523 {"-2", 10},
524 {"-1", 10},
525 {"65536", 10},
526 {"65537", 10},
527 {"2147483646", 10},
528 {"2147483647", 10},
529 {"2147483648", 10},
530 {"2147483649", 10},
531 {"4294967294", 10},
532 {"4294967295", 10},
533 {"4294967296", 10},
534 {"4294967297", 10},
535 {"9223372036854775806", 10},
536 {"9223372036854775807", 10},
537 {"9223372036854775808", 10},
538 {"9223372036854775809", 10},
539 {"18446744073709551614", 10},
540 {"18446744073709551615", 10},
541 {"18446744073709551616", 10},
542 {"18446744073709551617", 10},
543 };
544 TEST_FAIL(kstrtou16, u16, "%hu", test_u16_fail);
545}
546
547static void __init test_kstrtos16_ok(void)
548{
549 DECLARE_TEST_OK(s16, struct test_s16);
550 static DEFINE_TEST_OK(struct test_s16, test_s16_ok) = {
551 {"-130", 10, -130},
552 {"-129", 10, -129},
553 {"-128", 10, -128},
554 {"-127", 10, -127},
555 {"-1", 10, -1},
556 {"0", 10, 0},
557 {"1", 10, 1},
558 {"126", 10, 126},
559 {"127", 10, 127},
560 {"128", 10, 128},
561 {"129", 10, 129},
562 {"254", 10, 254},
563 {"255", 10, 255},
564 {"256", 10, 256},
565 {"257", 10, 257},
566 {"32766", 10, 32766},
567 {"32767", 10, 32767},
568 };
569 TEST_OK(kstrtos16, s16, "%hd", test_s16_ok);
570}
571
572static void __init test_kstrtos16_fail(void)
573{
574 static DEFINE_TEST_FAIL(test_s16_fail) = {
575 {"32768", 10},
576 {"32769", 10},
577 {"65534", 10},
578 {"65535", 10},
579 {"65536", 10},
580 {"65537", 10},
581 {"2147483646", 10},
582 {"2147483647", 10},
583 {"2147483648", 10},
584 {"2147483649", 10},
585 {"4294967294", 10},
586 {"4294967295", 10},
587 {"4294967296", 10},
588 {"4294967297", 10},
589 {"9223372036854775806", 10},
590 {"9223372036854775807", 10},
591 {"9223372036854775808", 10},
592 {"9223372036854775809", 10},
593 {"18446744073709551614", 10},
594 {"18446744073709551615", 10},
595 {"18446744073709551616", 10},
596 {"18446744073709551617", 10},
597 };
598 TEST_FAIL(kstrtos16, s16, "%hd", test_s16_fail);
599}
600
601static void __init test_kstrtou8_ok(void)
602{
603 DECLARE_TEST_OK(u8, struct test_u8);
604 static DEFINE_TEST_OK(struct test_u8, test_u8_ok) = {
605 {"0", 10, 0},
606 {"1", 10, 1},
607 {"126", 10, 126},
608 {"127", 10, 127},
609 {"128", 10, 128},
610 {"129", 10, 129},
611 {"254", 10, 254},
612 {"255", 10, 255},
613 };
614 TEST_OK(kstrtou8, u8, "%hhu", test_u8_ok);
615}
616
617static void __init test_kstrtou8_fail(void)
618{
619 static DEFINE_TEST_FAIL(test_u8_fail) = {
620 {"-2", 10},
621 {"-1", 10},
622 {"256", 10},
623 {"257", 10},
624 {"32766", 10},
625 {"32767", 10},
626 {"32768", 10},
627 {"32769", 10},
628 {"65534", 10},
629 {"65535", 10},
630 {"65536", 10},
631 {"65537", 10},
632 {"2147483646", 10},
633 {"2147483647", 10},
634 {"2147483648", 10},
635 {"2147483649", 10},
636 {"4294967294", 10},
637 {"4294967295", 10},
638 {"4294967296", 10},
639 {"4294967297", 10},
640 {"9223372036854775806", 10},
641 {"9223372036854775807", 10},
642 {"9223372036854775808", 10},
643 {"9223372036854775809", 10},
644 {"18446744073709551614", 10},
645 {"18446744073709551615", 10},
646 {"18446744073709551616", 10},
647 {"18446744073709551617", 10},
648 };
649 TEST_FAIL(kstrtou8, u8, "%hhu", test_u8_fail);
650}
651
652static void __init test_kstrtos8_ok(void)
653{
654 DECLARE_TEST_OK(s8, struct test_s8);
655 static DEFINE_TEST_OK(struct test_s8, test_s8_ok) = {
656 {"-128", 10, -128},
657 {"-127", 10, -127},
658 {"-1", 10, -1},
659 {"0", 10, 0},
660 {"1", 10, 1},
661 {"126", 10, 126},
662 {"127", 10, 127},
663 };
664 TEST_OK(kstrtos8, s8, "%hhd", test_s8_ok);
665}
666
667static void __init test_kstrtos8_fail(void)
668{
669 static DEFINE_TEST_FAIL(test_s8_fail) = {
670 {"-130", 10},
671 {"-129", 10},
672 {"128", 10},
673 {"129", 10},
674 {"254", 10},
675 {"255", 10},
676 {"256", 10},
677 {"257", 10},
678 {"32766", 10},
679 {"32767", 10},
680 {"32768", 10},
681 {"32769", 10},
682 {"65534", 10},
683 {"65535", 10},
684 {"65536", 10},
685 {"65537", 10},
686 {"2147483646", 10},
687 {"2147483647", 10},
688 {"2147483648", 10},
689 {"2147483649", 10},
690 {"4294967294", 10},
691 {"4294967295", 10},
692 {"4294967296", 10},
693 {"4294967297", 10},
694 {"9223372036854775806", 10},
695 {"9223372036854775807", 10},
696 {"9223372036854775808", 10},
697 {"9223372036854775809", 10},
698 {"18446744073709551614", 10},
699 {"18446744073709551615", 10},
700 {"18446744073709551616", 10},
701 {"18446744073709551617", 10},
702 };
703 TEST_FAIL(kstrtos8, s8, "%hhd", test_s8_fail);
704}
705
706static int __init test_kstrtox_init(void)
707{
708 test_kstrtoull_ok();
709 test_kstrtoull_fail();
710 test_kstrtoll_ok();
711 test_kstrtoll_fail();
712
713 test_kstrtou64_ok();
714 test_kstrtou64_fail();
715 test_kstrtos64_ok();
716 test_kstrtos64_fail();
717
718 test_kstrtou32_ok();
719 test_kstrtou32_fail();
720 test_kstrtos32_ok();
721 test_kstrtos32_fail();
722
723 test_kstrtou16_ok();
724 test_kstrtou16_fail();
725 test_kstrtos16_ok();
726 test_kstrtos16_fail();
727
728 test_kstrtou8_ok();
729 test_kstrtou8_fail();
730 test_kstrtos8_ok();
731 test_kstrtos8_fail();
732 return -EINVAL;
733}
734module_init(test_kstrtox_init);
735MODULE_LICENSE("Dual BSD/GPL");
1#include <linux/init.h>
2#include <linux/kernel.h>
3#include <linux/module.h>
4
5#define for_each_test(i, test) \
6 for (i = 0; i < sizeof(test) / sizeof(test[0]); i++)
7
8struct test_fail {
9 const char *str;
10 unsigned int base;
11};
12
13#define DEFINE_TEST_FAIL(test) \
14 const struct test_fail test[] __initconst
15
16#define DECLARE_TEST_OK(type, test_type) \
17 test_type { \
18 const char *str; \
19 unsigned int base; \
20 type expected_res; \
21 }
22
23#define DEFINE_TEST_OK(type, test) \
24 const type test[] __initconst
25
26#define TEST_FAIL(fn, type, fmt, test) \
27{ \
28 unsigned int i; \
29 \
30 for_each_test(i, test) { \
31 const struct test_fail *t = &test[i]; \
32 type tmp; \
33 int rv; \
34 \
35 tmp = 0; \
36 rv = fn(t->str, t->base, &tmp); \
37 if (rv >= 0) { \
38 WARN(1, "str '%s', base %u, expected -E, got %d/" fmt "\n", \
39 t->str, t->base, rv, tmp); \
40 continue; \
41 } \
42 } \
43}
44
45#define TEST_OK(fn, type, fmt, test) \
46{ \
47 unsigned int i; \
48 \
49 for_each_test(i, test) { \
50 const typeof(test[0]) *t = &test[i]; \
51 type res; \
52 int rv; \
53 \
54 rv = fn(t->str, t->base, &res); \
55 if (rv != 0) { \
56 WARN(1, "str '%s', base %u, expected 0/" fmt ", got %d\n", \
57 t->str, t->base, t->expected_res, rv); \
58 continue; \
59 } \
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); \
63 continue; \
64 } \
65 } \
66}
67
68static void __init test_kstrtoull_ok(void)
69{
70 DECLARE_TEST_OK(unsigned long long, struct test_ull);
71 static DEFINE_TEST_OK(struct test_ull, test_ull_ok) = {
72 {"0", 10, 0ULL},
73 {"1", 10, 1ULL},
74 {"127", 10, 127ULL},
75 {"128", 10, 128ULL},
76 {"129", 10, 129ULL},
77 {"255", 10, 255ULL},
78 {"256", 10, 256ULL},
79 {"257", 10, 257ULL},
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},
97
98 {"00", 8, 00ULL},
99 {"01", 8, 01ULL},
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},
123
124 {"0x0", 16, 0x0ULL},
125 {"0x1", 16, 0x1ULL},
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},
149
150 {"0\n", 0, 0ULL},
151 };
152 TEST_OK(kstrtoull, unsigned long long, "%llu", test_ull_ok);
153}
154
155static void __init test_kstrtoull_fail(void)
156{
157 static DEFINE_TEST_FAIL(test_ull_fail) = {
158 {"", 0},
159 {"", 8},
160 {"", 10},
161 {"", 16},
162 {"\n", 0},
163 {"\n", 8},
164 {"\n", 10},
165 {"\n", 16},
166 {"\n0", 0},
167 {"\n0", 8},
168 {"\n0", 10},
169 {"\n0", 16},
170 {"+", 0},
171 {"+", 8},
172 {"+", 10},
173 {"+", 16},
174 {"-", 0},
175 {"-", 8},
176 {"-", 10},
177 {"-", 16},
178 {"0x", 0},
179 {"0x", 16},
180 {"0X", 0},
181 {"0X", 16},
182 {"0 ", 0},
183 {"1+", 0},
184 {"1-", 0},
185 {" 2", 0},
186 /* base autodetection */
187 {"0x0z", 0},
188 {"0z", 0},
189 {"a", 0},
190 /* digit >= base */
191 {"2", 2},
192 {"8", 8},
193 {"a", 10},
194 {"A", 10},
195 {"g", 16},
196 {"G", 16},
197 /* overflow */
198 {"10000000000000000000000000000000000000000000000000000000000000000", 2},
199 {"2000000000000000000000", 8},
200 {"18446744073709551616", 10},
201 {"10000000000000000", 16},
202 /* negative */
203 {"-0", 0},
204 {"-0", 8},
205 {"-0", 10},
206 {"-0", 16},
207 {"-1", 0},
208 {"-1", 8},
209 {"-1", 10},
210 {"-1", 16},
211 /* sign is first character if any */
212 {"-+1", 0},
213 {"-+1", 8},
214 {"-+1", 10},
215 {"-+1", 16},
216 /* nothing after \n */
217 {"0\n0", 0},
218 {"0\n0", 8},
219 {"0\n0", 10},
220 {"0\n0", 16},
221 {"0\n+", 0},
222 {"0\n+", 8},
223 {"0\n+", 10},
224 {"0\n+", 16},
225 {"0\n-", 0},
226 {"0\n-", 8},
227 {"0\n-", 10},
228 {"0\n-", 16},
229 {"0\n ", 0},
230 {"0\n ", 8},
231 {"0\n ", 10},
232 {"0\n ", 16},
233 };
234 TEST_FAIL(kstrtoull, unsigned long long, "%llu", test_ull_fail);
235}
236
237static void __init test_kstrtoll_ok(void)
238{
239 DECLARE_TEST_OK(long long, struct test_ll);
240 static DEFINE_TEST_OK(struct test_ll, test_ll_ok) = {
241 {"0", 10, 0LL},
242 {"1", 10, 1LL},
243 {"127", 10, 127LL},
244 {"128", 10, 128LL},
245 {"129", 10, 129LL},
246 {"255", 10, 255LL},
247 {"256", 10, 256LL},
248 {"257", 10, 257LL},
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},
262
263 {"-1", 10, -1LL},
264 {"-2", 10, -2LL},
265 {"-9223372036854775808", 10, LLONG_MIN},
266 };
267 TEST_OK(kstrtoll, long long, "%lld", test_ll_ok);
268}
269
270static void __init test_kstrtoll_fail(void)
271{
272 static DEFINE_TEST_FAIL(test_ll_fail) = {
273 {"9223372036854775808", 10},
274 {"9223372036854775809", 10},
275 {"18446744073709551614", 10},
276 {"18446744073709551615", 10},
277 {"-9223372036854775809", 10},
278 {"-18446744073709551614", 10},
279 {"-18446744073709551615", 10},
280 /* negative zero isn't an integer in Linux */
281 {"-0", 0},
282 {"-0", 8},
283 {"-0", 10},
284 {"-0", 16},
285 /* sign is first character if any */
286 {"-+1", 0},
287 {"-+1", 8},
288 {"-+1", 10},
289 {"-+1", 16},
290 };
291 TEST_FAIL(kstrtoll, long long, "%lld", test_ll_fail);
292}
293
294static void __init test_kstrtou64_ok(void)
295{
296 DECLARE_TEST_OK(u64, struct test_u64);
297 static DEFINE_TEST_OK(struct test_u64, test_u64_ok) = {
298 {"0", 10, 0},
299 {"1", 10, 1},
300 {"126", 10, 126},
301 {"127", 10, 127},
302 {"128", 10, 128},
303 {"129", 10, 129},
304 {"254", 10, 254},
305 {"255", 10, 255},
306 {"256", 10, 256},
307 {"257", 10, 257},
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},
330 };
331 TEST_OK(kstrtou64, u64, "%llu", test_u64_ok);
332}
333
334static void __init test_kstrtou64_fail(void)
335{
336 static DEFINE_TEST_FAIL(test_u64_fail) = {
337 {"-2", 10},
338 {"-1", 10},
339 {"18446744073709551616", 10},
340 {"18446744073709551617", 10},
341 };
342 TEST_FAIL(kstrtou64, u64, "%llu", test_u64_fail);
343}
344
345static void __init test_kstrtos64_ok(void)
346{
347 DECLARE_TEST_OK(s64, struct test_s64);
348 static DEFINE_TEST_OK(struct test_s64, test_s64_ok) = {
349 {"-128", 10, -128},
350 {"-127", 10, -127},
351 {"-1", 10, -1},
352 {"0", 10, 0},
353 {"1", 10, 1},
354 {"126", 10, 126},
355 {"127", 10, 127},
356 {"128", 10, 128},
357 {"129", 10, 129},
358 {"254", 10, 254},
359 {"255", 10, 255},
360 {"256", 10, 256},
361 {"257", 10, 257},
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, 2147483648LL},
373 {"2147483649", 10, 2147483649LL},
374 {"4294967294", 10, 4294967294LL},
375 {"4294967295", 10, 4294967295LL},
376 {"4294967296", 10, 4294967296LL},
377 {"4294967297", 10, 4294967297LL},
378 {"9223372036854775806", 10, 9223372036854775806LL},
379 {"9223372036854775807", 10, 9223372036854775807LL},
380 };
381 TEST_OK(kstrtos64, s64, "%lld", test_s64_ok);
382}
383
384static void __init test_kstrtos64_fail(void)
385{
386 static DEFINE_TEST_FAIL(test_s64_fail) = {
387 {"9223372036854775808", 10},
388 {"9223372036854775809", 10},
389 {"18446744073709551614", 10},
390 {"18446744073709551615", 10},
391 {"18446744073709551616", 10},
392 {"18446744073709551617", 10},
393 };
394 TEST_FAIL(kstrtos64, s64, "%lld", test_s64_fail);
395}
396
397static void __init test_kstrtou32_ok(void)
398{
399 DECLARE_TEST_OK(u32, struct test_u32);
400 static DEFINE_TEST_OK(struct test_u32, test_u32_ok) = {
401 {"0", 10, 0},
402 {"1", 10, 1},
403 {"126", 10, 126},
404 {"127", 10, 127},
405 {"128", 10, 128},
406 {"129", 10, 129},
407 {"254", 10, 254},
408 {"255", 10, 255},
409 {"256", 10, 256},
410 {"257", 10, 257},
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, 2147483648U},
422 {"2147483649", 10, 2147483649U},
423 {"4294967294", 10, 4294967294U},
424 {"4294967295", 10, 4294967295U},
425 };
426 TEST_OK(kstrtou32, u32, "%u", test_u32_ok);
427}
428
429static void __init test_kstrtou32_fail(void)
430{
431 static DEFINE_TEST_FAIL(test_u32_fail) = {
432 {"-2", 10},
433 {"-1", 10},
434 {"4294967296", 10},
435 {"4294967297", 10},
436 {"9223372036854775806", 10},
437 {"9223372036854775807", 10},
438 {"9223372036854775808", 10},
439 {"9223372036854775809", 10},
440 {"18446744073709551614", 10},
441 {"18446744073709551615", 10},
442 {"18446744073709551616", 10},
443 {"18446744073709551617", 10},
444 };
445 TEST_FAIL(kstrtou32, u32, "%u", test_u32_fail);
446}
447
448static void __init test_kstrtos32_ok(void)
449{
450 DECLARE_TEST_OK(s32, struct test_s32);
451 static DEFINE_TEST_OK(struct test_s32, test_s32_ok) = {
452 {"-128", 10, -128},
453 {"-127", 10, -127},
454 {"-1", 10, -1},
455 {"0", 10, 0},
456 {"1", 10, 1},
457 {"126", 10, 126},
458 {"127", 10, 127},
459 {"128", 10, 128},
460 {"129", 10, 129},
461 {"254", 10, 254},
462 {"255", 10, 255},
463 {"256", 10, 256},
464 {"257", 10, 257},
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},
475 };
476 TEST_OK(kstrtos32, s32, "%d", test_s32_ok);
477}
478
479static void __init test_kstrtos32_fail(void)
480{
481 static DEFINE_TEST_FAIL(test_s32_fail) = {
482 {"2147483648", 10},
483 {"2147483649", 10},
484 {"4294967294", 10},
485 {"4294967295", 10},
486 {"4294967296", 10},
487 {"4294967297", 10},
488 {"9223372036854775806", 10},
489 {"9223372036854775807", 10},
490 {"9223372036854775808", 10},
491 {"9223372036854775809", 10},
492 {"18446744073709551614", 10},
493 {"18446744073709551615", 10},
494 {"18446744073709551616", 10},
495 {"18446744073709551617", 10},
496 };
497 TEST_FAIL(kstrtos32, s32, "%d", test_s32_fail);
498}
499
500static void __init test_kstrtou16_ok(void)
501{
502 DECLARE_TEST_OK(u16, struct test_u16);
503 static DEFINE_TEST_OK(struct test_u16, test_u16_ok) = {
504 {"0", 10, 0},
505 {"1", 10, 1},
506 {"126", 10, 126},
507 {"127", 10, 127},
508 {"128", 10, 128},
509 {"129", 10, 129},
510 {"254", 10, 254},
511 {"255", 10, 255},
512 {"256", 10, 256},
513 {"257", 10, 257},
514 {"32766", 10, 32766},
515 {"32767", 10, 32767},
516 {"32768", 10, 32768},
517 {"32769", 10, 32769},
518 {"65534", 10, 65534},
519 {"65535", 10, 65535},
520 };
521 TEST_OK(kstrtou16, u16, "%hu", test_u16_ok);
522}
523
524static void __init test_kstrtou16_fail(void)
525{
526 static DEFINE_TEST_FAIL(test_u16_fail) = {
527 {"-2", 10},
528 {"-1", 10},
529 {"65536", 10},
530 {"65537", 10},
531 {"2147483646", 10},
532 {"2147483647", 10},
533 {"2147483648", 10},
534 {"2147483649", 10},
535 {"4294967294", 10},
536 {"4294967295", 10},
537 {"4294967296", 10},
538 {"4294967297", 10},
539 {"9223372036854775806", 10},
540 {"9223372036854775807", 10},
541 {"9223372036854775808", 10},
542 {"9223372036854775809", 10},
543 {"18446744073709551614", 10},
544 {"18446744073709551615", 10},
545 {"18446744073709551616", 10},
546 {"18446744073709551617", 10},
547 };
548 TEST_FAIL(kstrtou16, u16, "%hu", test_u16_fail);
549}
550
551static void __init test_kstrtos16_ok(void)
552{
553 DECLARE_TEST_OK(s16, struct test_s16);
554 static DEFINE_TEST_OK(struct test_s16, test_s16_ok) = {
555 {"-130", 10, -130},
556 {"-129", 10, -129},
557 {"-128", 10, -128},
558 {"-127", 10, -127},
559 {"-1", 10, -1},
560 {"0", 10, 0},
561 {"1", 10, 1},
562 {"126", 10, 126},
563 {"127", 10, 127},
564 {"128", 10, 128},
565 {"129", 10, 129},
566 {"254", 10, 254},
567 {"255", 10, 255},
568 {"256", 10, 256},
569 {"257", 10, 257},
570 {"32766", 10, 32766},
571 {"32767", 10, 32767},
572 };
573 TEST_OK(kstrtos16, s16, "%hd", test_s16_ok);
574}
575
576static void __init test_kstrtos16_fail(void)
577{
578 static DEFINE_TEST_FAIL(test_s16_fail) = {
579 {"32768", 10},
580 {"32769", 10},
581 {"65534", 10},
582 {"65535", 10},
583 {"65536", 10},
584 {"65537", 10},
585 {"2147483646", 10},
586 {"2147483647", 10},
587 {"2147483648", 10},
588 {"2147483649", 10},
589 {"4294967294", 10},
590 {"4294967295", 10},
591 {"4294967296", 10},
592 {"4294967297", 10},
593 {"9223372036854775806", 10},
594 {"9223372036854775807", 10},
595 {"9223372036854775808", 10},
596 {"9223372036854775809", 10},
597 {"18446744073709551614", 10},
598 {"18446744073709551615", 10},
599 {"18446744073709551616", 10},
600 {"18446744073709551617", 10},
601 };
602 TEST_FAIL(kstrtos16, s16, "%hd", test_s16_fail);
603}
604
605static void __init test_kstrtou8_ok(void)
606{
607 DECLARE_TEST_OK(u8, struct test_u8);
608 static DEFINE_TEST_OK(struct test_u8, test_u8_ok) = {
609 {"0", 10, 0},
610 {"1", 10, 1},
611 {"126", 10, 126},
612 {"127", 10, 127},
613 {"128", 10, 128},
614 {"129", 10, 129},
615 {"254", 10, 254},
616 {"255", 10, 255},
617 };
618 TEST_OK(kstrtou8, u8, "%hhu", test_u8_ok);
619}
620
621static void __init test_kstrtou8_fail(void)
622{
623 static DEFINE_TEST_FAIL(test_u8_fail) = {
624 {"-2", 10},
625 {"-1", 10},
626 {"256", 10},
627 {"257", 10},
628 {"32766", 10},
629 {"32767", 10},
630 {"32768", 10},
631 {"32769", 10},
632 {"65534", 10},
633 {"65535", 10},
634 {"65536", 10},
635 {"65537", 10},
636 {"2147483646", 10},
637 {"2147483647", 10},
638 {"2147483648", 10},
639 {"2147483649", 10},
640 {"4294967294", 10},
641 {"4294967295", 10},
642 {"4294967296", 10},
643 {"4294967297", 10},
644 {"9223372036854775806", 10},
645 {"9223372036854775807", 10},
646 {"9223372036854775808", 10},
647 {"9223372036854775809", 10},
648 {"18446744073709551614", 10},
649 {"18446744073709551615", 10},
650 {"18446744073709551616", 10},
651 {"18446744073709551617", 10},
652 };
653 TEST_FAIL(kstrtou8, u8, "%hhu", test_u8_fail);
654}
655
656static void __init test_kstrtos8_ok(void)
657{
658 DECLARE_TEST_OK(s8, struct test_s8);
659 static DEFINE_TEST_OK(struct test_s8, test_s8_ok) = {
660 {"-128", 10, -128},
661 {"-127", 10, -127},
662 {"-1", 10, -1},
663 {"0", 10, 0},
664 {"1", 10, 1},
665 {"126", 10, 126},
666 {"127", 10, 127},
667 };
668 TEST_OK(kstrtos8, s8, "%hhd", test_s8_ok);
669}
670
671static void __init test_kstrtos8_fail(void)
672{
673 static DEFINE_TEST_FAIL(test_s8_fail) = {
674 {"-130", 10},
675 {"-129", 10},
676 {"128", 10},
677 {"129", 10},
678 {"254", 10},
679 {"255", 10},
680 {"256", 10},
681 {"257", 10},
682 {"32766", 10},
683 {"32767", 10},
684 {"32768", 10},
685 {"32769", 10},
686 {"65534", 10},
687 {"65535", 10},
688 {"65536", 10},
689 {"65537", 10},
690 {"2147483646", 10},
691 {"2147483647", 10},
692 {"2147483648", 10},
693 {"2147483649", 10},
694 {"4294967294", 10},
695 {"4294967295", 10},
696 {"4294967296", 10},
697 {"4294967297", 10},
698 {"9223372036854775806", 10},
699 {"9223372036854775807", 10},
700 {"9223372036854775808", 10},
701 {"9223372036854775809", 10},
702 {"18446744073709551614", 10},
703 {"18446744073709551615", 10},
704 {"18446744073709551616", 10},
705 {"18446744073709551617", 10},
706 };
707 TEST_FAIL(kstrtos8, s8, "%hhd", test_s8_fail);
708}
709
710static int __init test_kstrtox_init(void)
711{
712 test_kstrtoull_ok();
713 test_kstrtoull_fail();
714 test_kstrtoll_ok();
715 test_kstrtoll_fail();
716
717 test_kstrtou64_ok();
718 test_kstrtou64_fail();
719 test_kstrtos64_ok();
720 test_kstrtos64_fail();
721
722 test_kstrtou32_ok();
723 test_kstrtou32_fail();
724 test_kstrtos32_ok();
725 test_kstrtos32_fail();
726
727 test_kstrtou16_ok();
728 test_kstrtou16_fail();
729 test_kstrtos16_ok();
730 test_kstrtos16_fail();
731
732 test_kstrtou8_ok();
733 test_kstrtou8_fail();
734 test_kstrtos8_ok();
735 test_kstrtos8_fail();
736 return -EINVAL;
737}
738module_init(test_kstrtox_init);
739MODULE_LICENSE("Dual BSD/GPL");