projects/linux·blob master · refs

924 lines · 22.0 KiB

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 {"569202370375329612767", 10},
202 {"10000000000000000", 16},
203 /* negative */
204 {"-0", 0},
205 {"-0", 8},
206 {"-0", 10},
207 {"-0", 16},
208 {"-1", 0},
209 {"-1", 8},
210 {"-1", 10},
211 {"-1", 16},
212 /* sign is first character if any */
213 {"-+1", 0},
214 {"-+1", 8},
215 {"-+1", 10},
216 {"-+1", 16},
217 /* nothing after \n */
218 {"0\n0", 0},
219 {"0\n0", 8},
220 {"0\n0", 10},
221 {"0\n0", 16},
222 {"0\n+", 0},
223 {"0\n+", 8},
224 {"0\n+", 10},
225 {"0\n+", 16},
226 {"0\n-", 0},
227 {"0\n-", 8},
228 {"0\n-", 10},
229 {"0\n-", 16},
230 {"0\n ", 0},
231 {"0\n ", 8},
232 {"0\n ", 10},
233 {"0\n ", 16},
234 };
235 TEST_FAIL(kstrtoull, unsigned long long, "%llu", test_ull_fail);
236}
237
238static void __init test_kstrtoll_ok(void)
239{
240 DECLARE_TEST_OK(long long, struct test_ll);
241 static DEFINE_TEST_OK(struct test_ll, test_ll_ok) = {
242 {"0", 10, 0LL},
243 {"1", 10, 1LL},
244 {"127", 10, 127LL},
245 {"128", 10, 128LL},
246 {"129", 10, 129LL},
247 {"255", 10, 255LL},
248 {"256", 10, 256LL},
249 {"257", 10, 257LL},
250 {"32767", 10, 32767LL},
251 {"32768", 10, 32768LL},
252 {"32769", 10, 32769LL},
253 {"65535", 10, 65535LL},
254 {"65536", 10, 65536LL},
255 {"65537", 10, 65537LL},
256 {"2147483647", 10, 2147483647LL},
257 {"2147483648", 10, 2147483648LL},
258 {"2147483649", 10, 2147483649LL},
259 {"4294967295", 10, 4294967295LL},
260 {"4294967296", 10, 4294967296LL},
261 {"4294967297", 10, 4294967297LL},
262 {"9223372036854775807", 10, 9223372036854775807LL},
263
264 {"-0", 10, 0LL},
265 {"-1", 10, -1LL},
266 {"-2", 10, -2LL},
267 {"-9223372036854775808", 10, LLONG_MIN},
268 };
269 TEST_OK(kstrtoll, long long, "%lld", test_ll_ok);
270}
271
272static void __init test_kstrtoll_fail(void)
273{
274 static DEFINE_TEST_FAIL(test_ll_fail) = {
275 {"9223372036854775808", 10},
276 {"9223372036854775809", 10},
277 {"18446744073709551614", 10},
278 {"18446744073709551615", 10},
279 {"569202370375329612767", 10},
280 {"-9223372036854775809", 10},
281 {"-18446744073709551614", 10},
282 {"-18446744073709551615", 10},
283 {"-569202370375329612767", 10},
284 /* sign is first character if any */
285 {"-+1", 0},
286 {"-+1", 8},
287 {"-+1", 10},
288 {"-+1", 16},
289 };
290 TEST_FAIL(kstrtoll, long long, "%lld", test_ll_fail);
291}
292
293static void __init test_kstrtou64_ok(void)
294{
295 DECLARE_TEST_OK(u64, struct test_u64);
296 static DEFINE_TEST_OK(struct test_u64, test_u64_ok) = {
297 {"0", 10, 0},
298 {"1", 10, 1},
299 {"126", 10, 126},
300 {"127", 10, 127},
301 {"128", 10, 128},
302 {"129", 10, 129},
303 {"254", 10, 254},
304 {"255", 10, 255},
305 {"256", 10, 256},
306 {"257", 10, 257},
307 {"32766", 10, 32766},
308 {"32767", 10, 32767},
309 {"32768", 10, 32768},
310 {"32769", 10, 32769},
311 {"65534", 10, 65534},
312 {"65535", 10, 65535},
313 {"65536", 10, 65536},
314 {"65537", 10, 65537},
315 {"2147483646", 10, 2147483646},
316 {"2147483647", 10, 2147483647},
317 {"2147483648", 10, 2147483648ULL},
318 {"2147483649", 10, 2147483649ULL},
319 {"4294967294", 10, 4294967294ULL},
320 {"4294967295", 10, 4294967295ULL},
321 {"4294967296", 10, 4294967296ULL},
322 {"4294967297", 10, 4294967297ULL},
323 {"9223372036854775806", 10, 9223372036854775806ULL},
324 {"9223372036854775807", 10, 9223372036854775807ULL},
325 {"9223372036854775808", 10, 9223372036854775808ULL},
326 {"9223372036854775809", 10, 9223372036854775809ULL},
327 {"18446744073709551614", 10, 18446744073709551614ULL},
328 {"18446744073709551615", 10, 18446744073709551615ULL},
329 };
330 TEST_OK(kstrtou64, u64, "%llu", test_u64_ok);
331}
332
333static void __init test_kstrtou64_fail(void)
334{
335 static DEFINE_TEST_FAIL(test_u64_fail) = {
336 {"-2", 10},
337 {"-1", 10},
338 {"18446744073709551616", 10},
339 {"18446744073709551617", 10},
340 {"569202370375329612767", 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 {"569202370375329612767", 10},
394 {"-569202370375329612767", 10},
395 };
396 TEST_FAIL(kstrtos64, s64, "%lld", test_s64_fail);
397}
398
399static void __init test_kstrtou32_ok(void)
400{
401 DECLARE_TEST_OK(u32, struct test_u32);
402 static DEFINE_TEST_OK(struct test_u32, test_u32_ok) = {
403 {"0", 10, 0},
404 {"1", 10, 1},
405 {"126", 10, 126},
406 {"127", 10, 127},
407 {"128", 10, 128},
408 {"129", 10, 129},
409 {"254", 10, 254},
410 {"255", 10, 255},
411 {"256", 10, 256},
412 {"257", 10, 257},
413 {"32766", 10, 32766},
414 {"32767", 10, 32767},
415 {"32768", 10, 32768},
416 {"32769", 10, 32769},
417 {"65534", 10, 65534},
418 {"65535", 10, 65535},
419 {"65536", 10, 65536},
420 {"65537", 10, 65537},
421 {"2147483646", 10, 2147483646},
422 {"2147483647", 10, 2147483647},
423 {"2147483648", 10, 2147483648U},
424 {"2147483649", 10, 2147483649U},
425 {"4294967294", 10, 4294967294U},
426 {"4294967295", 10, 4294967295U},
427 };
428 TEST_OK(kstrtou32, u32, "%u", test_u32_ok);
429}
430
431static void __init test_kstrtou32_fail(void)
432{
433 static DEFINE_TEST_FAIL(test_u32_fail) = {
434 {"-2", 10},
435 {"-1", 10},
436 {"4294967296", 10},
437 {"4294967297", 10},
438 {"9223372036854775806", 10},
439 {"9223372036854775807", 10},
440 {"9223372036854775808", 10},
441 {"9223372036854775809", 10},
442 {"18446744073709551614", 10},
443 {"18446744073709551615", 10},
444 {"18446744073709551616", 10},
445 {"18446744073709551617", 10},
446 };
447 TEST_FAIL(kstrtou32, u32, "%u", test_u32_fail);
448}
449
450static void __init test_kstrtos32_ok(void)
451{
452 DECLARE_TEST_OK(s32, struct test_s32);
453 static DEFINE_TEST_OK(struct test_s32, test_s32_ok) = {
454 {"-128", 10, -128},
455 {"-127", 10, -127},
456 {"-1", 10, -1},
457 {"0", 10, 0},
458 {"1", 10, 1},
459 {"126", 10, 126},
460 {"127", 10, 127},
461 {"128", 10, 128},
462 {"129", 10, 129},
463 {"254", 10, 254},
464 {"255", 10, 255},
465 {"256", 10, 256},
466 {"257", 10, 257},
467 {"32766", 10, 32766},
468 {"32767", 10, 32767},
469 {"32768", 10, 32768},
470 {"32769", 10, 32769},
471 {"65534", 10, 65534},
472 {"65535", 10, 65535},
473 {"65536", 10, 65536},
474 {"65537", 10, 65537},
475 {"2147483646", 10, 2147483646},
476 {"2147483647", 10, 2147483647},
477 };
478 TEST_OK(kstrtos32, s32, "%d", test_s32_ok);
479}
480
481static void __init test_kstrtos32_fail(void)
482{
483 static DEFINE_TEST_FAIL(test_s32_fail) = {
484 {"2147483648", 10},
485 {"2147483649", 10},
486 {"4294967294", 10},
487 {"4294967295", 10},
488 {"4294967296", 10},
489 {"4294967297", 10},
490 {"9223372036854775806", 10},
491 {"9223372036854775807", 10},
492 {"9223372036854775808", 10},
493 {"9223372036854775809", 10},
494 {"18446744073709551614", 10},
495 {"18446744073709551615", 10},
496 {"18446744073709551616", 10},
497 {"18446744073709551617", 10},
498 };
499 TEST_FAIL(kstrtos32, s32, "%d", test_s32_fail);
500}
501
502static void __init test_kstrtou16_ok(void)
503{
504 DECLARE_TEST_OK(u16, struct test_u16);
505 static DEFINE_TEST_OK(struct test_u16, test_u16_ok) = {
506 {"0", 10, 0},
507 {"1", 10, 1},
508 {"126", 10, 126},
509 {"127", 10, 127},
510 {"128", 10, 128},
511 {"129", 10, 129},
512 {"254", 10, 254},
513 {"255", 10, 255},
514 {"256", 10, 256},
515 {"257", 10, 257},
516 {"32766", 10, 32766},
517 {"32767", 10, 32767},
518 {"32768", 10, 32768},
519 {"32769", 10, 32769},
520 {"65534", 10, 65534},
521 {"65535", 10, 65535},
522 };
523 TEST_OK(kstrtou16, u16, "%hu", test_u16_ok);
524}
525
526static void __init test_kstrtou16_fail(void)
527{
528 static DEFINE_TEST_FAIL(test_u16_fail) = {
529 {"-2", 10},
530 {"-1", 10},
531 {"65536", 10},
532 {"65537", 10},
533 {"2147483646", 10},
534 {"2147483647", 10},
535 {"2147483648", 10},
536 {"2147483649", 10},
537 {"4294967294", 10},
538 {"4294967295", 10},
539 {"4294967296", 10},
540 {"4294967297", 10},
541 {"9223372036854775806", 10},
542 {"9223372036854775807", 10},
543 {"9223372036854775808", 10},
544 {"9223372036854775809", 10},
545 {"18446744073709551614", 10},
546 {"18446744073709551615", 10},
547 {"18446744073709551616", 10},
548 {"18446744073709551617", 10},
549 };
550 TEST_FAIL(kstrtou16, u16, "%hu", test_u16_fail);
551}
552
553static void __init test_kstrtos16_ok(void)
554{
555 DECLARE_TEST_OK(s16, struct test_s16);
556 static DEFINE_TEST_OK(struct test_s16, test_s16_ok) = {
557 {"-130", 10, -130},
558 {"-129", 10, -129},
559 {"-128", 10, -128},
560 {"-127", 10, -127},
561 {"-1", 10, -1},
562 {"0", 10, 0},
563 {"1", 10, 1},
564 {"126", 10, 126},
565 {"127", 10, 127},
566 {"128", 10, 128},
567 {"129", 10, 129},
568 {"254", 10, 254},
569 {"255", 10, 255},
570 {"256", 10, 256},
571 {"257", 10, 257},
572 {"32766", 10, 32766},
573 {"32767", 10, 32767},
574 };
575 TEST_OK(kstrtos16, s16, "%hd", test_s16_ok);
576}
577
578static void __init test_kstrtos16_fail(void)
579{
580 static DEFINE_TEST_FAIL(test_s16_fail) = {
581 {"32768", 10},
582 {"32769", 10},
583 {"65534", 10},
584 {"65535", 10},
585 {"65536", 10},
586 {"65537", 10},
587 {"2147483646", 10},
588 {"2147483647", 10},
589 {"2147483648", 10},
590 {"2147483649", 10},
591 {"4294967294", 10},
592 {"4294967295", 10},
593 {"4294967296", 10},
594 {"4294967297", 10},
595 {"9223372036854775806", 10},
596 {"9223372036854775807", 10},
597 {"9223372036854775808", 10},
598 {"9223372036854775809", 10},
599 {"18446744073709551614", 10},
600 {"18446744073709551615", 10},
601 {"18446744073709551616", 10},
602 {"18446744073709551617", 10},
603 };
604 TEST_FAIL(kstrtos16, s16, "%hd", test_s16_fail);
605}
606
607static void __init test_kstrtou8_ok(void)
608{
609 DECLARE_TEST_OK(u8, struct test_u8);
610 static DEFINE_TEST_OK(struct test_u8, test_u8_ok) = {
611 {"0", 10, 0},
612 {"1", 10, 1},
613 {"126", 10, 126},
614 {"127", 10, 127},
615 {"128", 10, 128},
616 {"129", 10, 129},
617 {"254", 10, 254},
618 {"255", 10, 255},
619 };
620 TEST_OK(kstrtou8, u8, "%hhu", test_u8_ok);
621}
622
623static void __init test_kstrtou8_fail(void)
624{
625 static DEFINE_TEST_FAIL(test_u8_fail) = {
626 {"-2", 10},
627 {"-1", 10},
628 {"256", 10},
629 {"257", 10},
630 {"32766", 10},
631 {"32767", 10},
632 {"32768", 10},
633 {"32769", 10},
634 {"65534", 10},
635 {"65535", 10},
636 {"65536", 10},
637 {"65537", 10},
638 {"2147483646", 10},
639 {"2147483647", 10},
640 {"2147483648", 10},
641 {"2147483649", 10},
642 {"4294967294", 10},
643 {"4294967295", 10},
644 {"4294967296", 10},
645 {"4294967297", 10},
646 {"9223372036854775806", 10},
647 {"9223372036854775807", 10},
648 {"9223372036854775808", 10},
649 {"9223372036854775809", 10},
650 {"18446744073709551614", 10},
651 {"18446744073709551615", 10},
652 {"18446744073709551616", 10},
653 {"18446744073709551617", 10},
654 };
655 TEST_FAIL(kstrtou8, u8, "%hhu", test_u8_fail);
656}
657
658static void __init test_kstrtos8_ok(void)
659{
660 DECLARE_TEST_OK(s8, struct test_s8);
661 static DEFINE_TEST_OK(struct test_s8, test_s8_ok) = {
662 {"-128", 10, -128},
663 {"-127", 10, -127},
664 {"-1", 10, -1},
665 {"0", 10, 0},
666 {"1", 10, 1},
667 {"126", 10, 126},
668 {"127", 10, 127},
669 };
670 TEST_OK(kstrtos8, s8, "%hhd", test_s8_ok);
671}
672
673static void __init test_kstrtos8_fail(void)
674{
675 static DEFINE_TEST_FAIL(test_s8_fail) = {
676 {"-130", 10},
677 {"-129", 10},
678 {"128", 10},
679 {"129", 10},
680 {"254", 10},
681 {"255", 10},
682 {"256", 10},
683 {"257", 10},
684 {"32766", 10},
685 {"32767", 10},
686 {"32768", 10},
687 {"32769", 10},
688 {"65534", 10},
689 {"65535", 10},
690 {"65536", 10},
691 {"65537", 10},
692 {"2147483646", 10},
693 {"2147483647", 10},
694 {"2147483648", 10},
695 {"2147483649", 10},
696 {"4294967294", 10},
697 {"4294967295", 10},
698 {"4294967296", 10},
699 {"4294967297", 10},
700 {"9223372036854775806", 10},
701 {"9223372036854775807", 10},
702 {"9223372036854775808", 10},
703 {"9223372036854775809", 10},
704 {"18446744073709551614", 10},
705 {"18446744073709551615", 10},
706 {"18446744073709551616", 10},
707 {"18446744073709551617", 10},
708 };
709 TEST_FAIL(kstrtos8, s8, "%hhd", test_s8_fail);
710}
711
712static void __init test_kstrtoudec64_ok(void)
713{
714 DECLARE_TEST_OK(u64, struct test_udec64);
715 static DEFINE_TEST_OK(struct test_udec64, test_udec64_ok) = {
716 /* basic: integer.fraction, exact digits */
717 {"0.0", 1, 0},
718 {"1.5", 1, 15},
719 {"1.234", 3, 1234},
720 {"42.0", 1, 420},
721 /* zero */
722 {"0.0", 1, 0},
723 {"0.000", 3, 0},
724 /* integer only */
725 {"0", 1, 0},
726 {"42", 3, 42000},
727 {"123.", 2, 12300},
728 {"1", 1, 10},
729 /* fractional only (leading dot) */
730 {".5", 1, 5},
731 {".5", 0, 0},
732 {".123", 3, 123},
733 {".001", 3, 1},
734 /* zero padding: fewer fractional digits than scale */
735 {"1.2", 3, 1200},
736 {"1.2", 6, 1200000},
737 {"0.01", 3, 10},
738 {"0.1", 9, 100000000ULL},
739 {"0.01", 9, 10000000},
740 /* truncation: more fractional digits than scale */
741 {"1.23456", 3, 1234},
742 {"3.1415926535", 6, 3141592},
743 {"0.999999999", 3, 999},
744 {"1.99", 1, 19},
745 {"1.234", 0, 1},
746 /* trailing newline */
747 {"1.5\n", 1, 15},
748 {"42\n", 3, 42000},
749 /* plus sign */
750 {"+1.5", 1, 15},
751 {"+.5", 1, 5},
752 /* scale progression */
753 {"1.", 0, 1},
754 {"1.0", 1, 10},
755 {"1.00", 2, 100},
756 {"1.000", 3, 1000},
757 {"1.000000", 6, 1000000},
758 {"1.000000000", 9, 1000000000ULL},
759 /* max limit check */
760 {"18446744073.709551615", 9, ULLONG_MAX},
761 {"18446744073709.551615", 6, ULLONG_MAX},
762 {"0.18446744073709551615", 20, ULLONG_MAX},
763 /* scale > 19: representable when integer part is small */
764 {"0.00000000000000000001", 20, 1},
765 {"0.1", 20, 10000000000000000000ULL},
766 {"0.00000000000000000000001", 23, 1},
767 /* truncation with scale > 19 */
768 {"0.0000000000000000000012345", 23, 123},
769 /* truncation with many excess digits */
770 {"0.00000000000000000000000000000000423", 34, 42},
771 {"1.99999999999999999999999999999999999", 3, 1999},
772 };
773 TEST_OK(kstrtoudec64, u64, "%llu", test_udec64_ok);
774}
775
776static void __init test_kstrtoudec64_fail(void)
777{
778 static DEFINE_TEST_FAIL(test_udec64_fail) = {
779 /* empty / whitespace */
780 {"", 3},
781 {"\n", 3},
782 /* minus sign (unsigned) */
783 {"-1.5", 1},
784 {"-0.5", 1},
785 /* only a decimal point */
786 {".", 3},
787 {".", 0},
788 /* only a sign */
789 {"+", 3},
790 /* non-digit characters */
791 {"abc", 3},
792 {"1.2x", 3},
793 /* leading/trailing space */
794 {" 1.5", 1},
795 {"1.5 ", 1},
796 /* overflow */
797 {"18446744073710.551615", 6},
798 {"99999999999999999999", 1},
799 /* overflow with scale > 19 */
800 {"1.0", 21},
801 {"0.2", 20},
802 {"0.18446744073709551616", 20},
803 {"1", 20},
804 };
805 TEST_FAIL(kstrtoudec64, u64, "%llu", test_udec64_fail);
806}
807
808static void __init test_kstrtodec64_ok(void)
809{
810 DECLARE_TEST_OK(s64, struct test_dec64);
811 static DEFINE_TEST_OK(struct test_dec64, test_dec64_ok) = {
812 /* basic positive */
813 {"0.0", 1, 0},
814 {"1.5", 1, 15},
815 {"1.234", 3, 1234},
816 /* basic negative */
817 {"-1.5", 1, -15},
818 {"-1.234", 3, -1234},
819 {"-0.5", 1, -5},
820 {"-0.001", 3, -1},
821 /* zero (signed) */
822 {"-0", 1, 0},
823 {"-0.0", 1, 0},
824 {"0.000", 3, 0},
825 /* integer only */
826 {"42", 3, 42000},
827 {"-42", 3, -42000},
828 /* fractional only */
829 {".5", 1, 5},
830 {"-.5", 1, -5},
831 /* zero padding */
832 {"1.2", 3, 1200},
833 {"-1.2", 3, -1200},
834 {"0.01", 3, 10},
835 {"-0.01", 3, -10},
836 /* truncation */
837 {"1.23456", 3, 1234},
838 {"-1.23456", 3, -1234},
839 {"0.999999999", 3, 999},
840 {"-0.999999999", 3, -999},
841 /* trailing newline */
842 {"1.5\n", 1, 15},
843 {"-1.5\n", 1, -15},
844 /* plus sign */
845 {"+1.5", 1, 15},
846 /* limits */
847 {"9223372036.854775807", 9, LLONG_MAX},
848 {"-9223372036.854775808", 9, LLONG_MIN},
849 /* scale > 19 */
850 {"0.0", 20, 0},
851 {"-0.0", 20, 0},
852 {"0.00000000000000000001", 20, 1},
853 {"-0.00000000000000000001", 20, -1},
854 {"0.009223372036854775807", 21, LLONG_MAX},
855 {"-0.009223372036854775808", 21, LLONG_MIN},
856 };
857 TEST_OK(kstrtodec64, s64, "%lld", test_dec64_ok);
858}
859
860static void __init test_kstrtodec64_fail(void)
861{
862 static DEFINE_TEST_FAIL(test_dec64_fail) = {
863 /* empty / whitespace */
864 {"", 3},
865 {"\n", 3},
866 /* no digits after dot */
867 {".", 3},
868 {"-.", 3},
869 /* no digits at all */
870 {"+", 3},
871 {"-", 3},
872 /* non-digit characters */
873 {"abc", 3},
874 {"-1.2x", 3},
875 /* signed overflow */
876 {"9223372036.854775808", 9},
877 {"-9223372036.854775809", 9},
878 {"99999999999999999999", 1},
879 /* signed overflow with scale > 19 */
880 {"0.1", 20},
881 {"-0.1", 20},
882 {"0.09223372036854775808", 20},
883 {"-0.09223372036854775809", 20},
884 };
885 TEST_FAIL(kstrtodec64, s64, "%lld", test_dec64_fail);
886}
887
888static int __init test_kstrtox_init(void)
889{
890 test_kstrtoull_ok();
891 test_kstrtoull_fail();
892 test_kstrtoll_ok();
893 test_kstrtoll_fail();
894
895 test_kstrtou64_ok();
896 test_kstrtou64_fail();
897 test_kstrtos64_ok();
898 test_kstrtos64_fail();
899
900 test_kstrtou32_ok();
901 test_kstrtou32_fail();
902 test_kstrtos32_ok();
903 test_kstrtos32_fail();
904
905 test_kstrtou16_ok();
906 test_kstrtou16_fail();
907 test_kstrtos16_ok();
908 test_kstrtos16_fail();
909
910 test_kstrtou8_ok();
911 test_kstrtou8_fail();
912 test_kstrtos8_ok();
913 test_kstrtos8_fail();
914
915 test_kstrtoudec64_ok();
916 test_kstrtoudec64_fail();
917 test_kstrtodec64_ok();
918 test_kstrtodec64_fail();
919
920 return -EINVAL;
921}
922module_init(test_kstrtox_init);
923MODULE_DESCRIPTION("Module test for kstrto*() APIs");
924MODULE_LICENSE("Dual BSD/GPL");