test-kstrtox.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. #include <linux/init.h>
  2. #include <linux/kernel.h>
  3. #include <linux/module.h>
  4. #define for_each_test(i, test) \
  5. for (i = 0; i < ARRAY_SIZE(test); i++)
  6. struct test_fail {
  7. const char *str;
  8. unsigned int base;
  9. };
  10. #define DEFINE_TEST_FAIL(test) \
  11. const struct test_fail test[] __initconst
  12. #define DECLARE_TEST_OK(type, test_type) \
  13. test_type { \
  14. const char *str; \
  15. unsigned int base; \
  16. type expected_res; \
  17. }
  18. #define DEFINE_TEST_OK(type, test) \
  19. const type test[] __initconst
  20. #define TEST_FAIL(fn, type, fmt, test) \
  21. { \
  22. unsigned int i; \
  23. \
  24. for_each_test(i, test) { \
  25. const struct test_fail *t = &test[i]; \
  26. type tmp; \
  27. int rv; \
  28. \
  29. tmp = 0; \
  30. rv = fn(t->str, t->base, &tmp); \
  31. if (rv >= 0) { \
  32. WARN(1, "str '%s', base %u, expected -E, got %d/" fmt "\n", \
  33. t->str, t->base, rv, tmp); \
  34. continue; \
  35. } \
  36. } \
  37. }
  38. #define TEST_OK(fn, type, fmt, test) \
  39. { \
  40. unsigned int i; \
  41. \
  42. for_each_test(i, test) { \
  43. const typeof(test[0]) *t = &test[i]; \
  44. type res; \
  45. int rv; \
  46. \
  47. rv = fn(t->str, t->base, &res); \
  48. if (rv != 0) { \
  49. WARN(1, "str '%s', base %u, expected 0/" fmt ", got %d\n", \
  50. t->str, t->base, t->expected_res, rv); \
  51. continue; \
  52. } \
  53. if (res != t->expected_res) { \
  54. WARN(1, "str '%s', base %u, expected " fmt ", got " fmt "\n", \
  55. t->str, t->base, t->expected_res, res); \
  56. continue; \
  57. } \
  58. } \
  59. }
  60. static void __init test_kstrtoull_ok(void)
  61. {
  62. DECLARE_TEST_OK(unsigned long long, struct test_ull);
  63. static DEFINE_TEST_OK(struct test_ull, test_ull_ok) = {
  64. {"0", 10, 0ULL},
  65. {"1", 10, 1ULL},
  66. {"127", 10, 127ULL},
  67. {"128", 10, 128ULL},
  68. {"129", 10, 129ULL},
  69. {"255", 10, 255ULL},
  70. {"256", 10, 256ULL},
  71. {"257", 10, 257ULL},
  72. {"32767", 10, 32767ULL},
  73. {"32768", 10, 32768ULL},
  74. {"32769", 10, 32769ULL},
  75. {"65535", 10, 65535ULL},
  76. {"65536", 10, 65536ULL},
  77. {"65537", 10, 65537ULL},
  78. {"2147483647", 10, 2147483647ULL},
  79. {"2147483648", 10, 2147483648ULL},
  80. {"2147483649", 10, 2147483649ULL},
  81. {"4294967295", 10, 4294967295ULL},
  82. {"4294967296", 10, 4294967296ULL},
  83. {"4294967297", 10, 4294967297ULL},
  84. {"9223372036854775807", 10, 9223372036854775807ULL},
  85. {"9223372036854775808", 10, 9223372036854775808ULL},
  86. {"9223372036854775809", 10, 9223372036854775809ULL},
  87. {"18446744073709551614", 10, 18446744073709551614ULL},
  88. {"18446744073709551615", 10, 18446744073709551615ULL},
  89. {"00", 8, 00ULL},
  90. {"01", 8, 01ULL},
  91. {"0177", 8, 0177ULL},
  92. {"0200", 8, 0200ULL},
  93. {"0201", 8, 0201ULL},
  94. {"0377", 8, 0377ULL},
  95. {"0400", 8, 0400ULL},
  96. {"0401", 8, 0401ULL},
  97. {"077777", 8, 077777ULL},
  98. {"0100000", 8, 0100000ULL},
  99. {"0100001", 8, 0100001ULL},
  100. {"0177777", 8, 0177777ULL},
  101. {"0200000", 8, 0200000ULL},
  102. {"0200001", 8, 0200001ULL},
  103. {"017777777777", 8, 017777777777ULL},
  104. {"020000000000", 8, 020000000000ULL},
  105. {"020000000001", 8, 020000000001ULL},
  106. {"037777777777", 8, 037777777777ULL},
  107. {"040000000000", 8, 040000000000ULL},
  108. {"040000000001", 8, 040000000001ULL},
  109. {"0777777777777777777777", 8, 0777777777777777777777ULL},
  110. {"01000000000000000000000", 8, 01000000000000000000000ULL},
  111. {"01000000000000000000001", 8, 01000000000000000000001ULL},
  112. {"01777777777777777777776", 8, 01777777777777777777776ULL},
  113. {"01777777777777777777777", 8, 01777777777777777777777ULL},
  114. {"0x0", 16, 0x0ULL},
  115. {"0x1", 16, 0x1ULL},
  116. {"0x7f", 16, 0x7fULL},
  117. {"0x80", 16, 0x80ULL},
  118. {"0x81", 16, 0x81ULL},
  119. {"0xff", 16, 0xffULL},
  120. {"0x100", 16, 0x100ULL},
  121. {"0x101", 16, 0x101ULL},
  122. {"0x7fff", 16, 0x7fffULL},
  123. {"0x8000", 16, 0x8000ULL},
  124. {"0x8001", 16, 0x8001ULL},
  125. {"0xffff", 16, 0xffffULL},
  126. {"0x10000", 16, 0x10000ULL},
  127. {"0x10001", 16, 0x10001ULL},
  128. {"0x7fffffff", 16, 0x7fffffffULL},
  129. {"0x80000000", 16, 0x80000000ULL},
  130. {"0x80000001", 16, 0x80000001ULL},
  131. {"0xffffffff", 16, 0xffffffffULL},
  132. {"0x100000000", 16, 0x100000000ULL},
  133. {"0x100000001", 16, 0x100000001ULL},
  134. {"0x7fffffffffffffff", 16, 0x7fffffffffffffffULL},
  135. {"0x8000000000000000", 16, 0x8000000000000000ULL},
  136. {"0x8000000000000001", 16, 0x8000000000000001ULL},
  137. {"0xfffffffffffffffe", 16, 0xfffffffffffffffeULL},
  138. {"0xffffffffffffffff", 16, 0xffffffffffffffffULL},
  139. {"0\n", 0, 0ULL},
  140. };
  141. TEST_OK(kstrtoull, unsigned long long, "%llu", test_ull_ok);
  142. }
  143. static void __init test_kstrtoull_fail(void)
  144. {
  145. static DEFINE_TEST_FAIL(test_ull_fail) = {
  146. {"", 0},
  147. {"", 8},
  148. {"", 10},
  149. {"", 16},
  150. {"\n", 0},
  151. {"\n", 8},
  152. {"\n", 10},
  153. {"\n", 16},
  154. {"\n0", 0},
  155. {"\n0", 8},
  156. {"\n0", 10},
  157. {"\n0", 16},
  158. {"+", 0},
  159. {"+", 8},
  160. {"+", 10},
  161. {"+", 16},
  162. {"-", 0},
  163. {"-", 8},
  164. {"-", 10},
  165. {"-", 16},
  166. {"0x", 0},
  167. {"0x", 16},
  168. {"0X", 0},
  169. {"0X", 16},
  170. {"0 ", 0},
  171. {"1+", 0},
  172. {"1-", 0},
  173. {" 2", 0},
  174. /* base autodetection */
  175. {"0x0z", 0},
  176. {"0z", 0},
  177. {"a", 0},
  178. /* digit >= base */
  179. {"2", 2},
  180. {"8", 8},
  181. {"a", 10},
  182. {"A", 10},
  183. {"g", 16},
  184. {"G", 16},
  185. /* overflow */
  186. {"10000000000000000000000000000000000000000000000000000000000000000", 2},
  187. {"2000000000000000000000", 8},
  188. {"18446744073709551616", 10},
  189. {"10000000000000000", 16},
  190. /* negative */
  191. {"-0", 0},
  192. {"-0", 8},
  193. {"-0", 10},
  194. {"-0", 16},
  195. {"-1", 0},
  196. {"-1", 8},
  197. {"-1", 10},
  198. {"-1", 16},
  199. /* sign is first character if any */
  200. {"-+1", 0},
  201. {"-+1", 8},
  202. {"-+1", 10},
  203. {"-+1", 16},
  204. /* nothing after \n */
  205. {"0\n0", 0},
  206. {"0\n0", 8},
  207. {"0\n0", 10},
  208. {"0\n0", 16},
  209. {"0\n+", 0},
  210. {"0\n+", 8},
  211. {"0\n+", 10},
  212. {"0\n+", 16},
  213. {"0\n-", 0},
  214. {"0\n-", 8},
  215. {"0\n-", 10},
  216. {"0\n-", 16},
  217. {"0\n ", 0},
  218. {"0\n ", 8},
  219. {"0\n ", 10},
  220. {"0\n ", 16},
  221. };
  222. TEST_FAIL(kstrtoull, unsigned long long, "%llu", test_ull_fail);
  223. }
  224. static void __init test_kstrtoll_ok(void)
  225. {
  226. DECLARE_TEST_OK(long long, struct test_ll);
  227. static DEFINE_TEST_OK(struct test_ll, test_ll_ok) = {
  228. {"0", 10, 0LL},
  229. {"1", 10, 1LL},
  230. {"127", 10, 127LL},
  231. {"128", 10, 128LL},
  232. {"129", 10, 129LL},
  233. {"255", 10, 255LL},
  234. {"256", 10, 256LL},
  235. {"257", 10, 257LL},
  236. {"32767", 10, 32767LL},
  237. {"32768", 10, 32768LL},
  238. {"32769", 10, 32769LL},
  239. {"65535", 10, 65535LL},
  240. {"65536", 10, 65536LL},
  241. {"65537", 10, 65537LL},
  242. {"2147483647", 10, 2147483647LL},
  243. {"2147483648", 10, 2147483648LL},
  244. {"2147483649", 10, 2147483649LL},
  245. {"4294967295", 10, 4294967295LL},
  246. {"4294967296", 10, 4294967296LL},
  247. {"4294967297", 10, 4294967297LL},
  248. {"9223372036854775807", 10, 9223372036854775807LL},
  249. {"-1", 10, -1LL},
  250. {"-2", 10, -2LL},
  251. {"-9223372036854775808", 10, LLONG_MIN},
  252. };
  253. TEST_OK(kstrtoll, long long, "%lld", test_ll_ok);
  254. }
  255. static void __init test_kstrtoll_fail(void)
  256. {
  257. static DEFINE_TEST_FAIL(test_ll_fail) = {
  258. {"9223372036854775808", 10},
  259. {"9223372036854775809", 10},
  260. {"18446744073709551614", 10},
  261. {"18446744073709551615", 10},
  262. {"-9223372036854775809", 10},
  263. {"-18446744073709551614", 10},
  264. {"-18446744073709551615", 10},
  265. /* negative zero isn't an integer in Linux */
  266. {"-0", 0},
  267. {"-0", 8},
  268. {"-0", 10},
  269. {"-0", 16},
  270. /* sign is first character if any */
  271. {"-+1", 0},
  272. {"-+1", 8},
  273. {"-+1", 10},
  274. {"-+1", 16},
  275. };
  276. TEST_FAIL(kstrtoll, long long, "%lld", test_ll_fail);
  277. }
  278. static void __init test_kstrtou64_ok(void)
  279. {
  280. DECLARE_TEST_OK(u64, struct test_u64);
  281. static DEFINE_TEST_OK(struct test_u64, test_u64_ok) = {
  282. {"0", 10, 0},
  283. {"1", 10, 1},
  284. {"126", 10, 126},
  285. {"127", 10, 127},
  286. {"128", 10, 128},
  287. {"129", 10, 129},
  288. {"254", 10, 254},
  289. {"255", 10, 255},
  290. {"256", 10, 256},
  291. {"257", 10, 257},
  292. {"32766", 10, 32766},
  293. {"32767", 10, 32767},
  294. {"32768", 10, 32768},
  295. {"32769", 10, 32769},
  296. {"65534", 10, 65534},
  297. {"65535", 10, 65535},
  298. {"65536", 10, 65536},
  299. {"65537", 10, 65537},
  300. {"2147483646", 10, 2147483646},
  301. {"2147483647", 10, 2147483647},
  302. {"2147483648", 10, 2147483648ULL},
  303. {"2147483649", 10, 2147483649ULL},
  304. {"4294967294", 10, 4294967294ULL},
  305. {"4294967295", 10, 4294967295ULL},
  306. {"4294967296", 10, 4294967296ULL},
  307. {"4294967297", 10, 4294967297ULL},
  308. {"9223372036854775806", 10, 9223372036854775806ULL},
  309. {"9223372036854775807", 10, 9223372036854775807ULL},
  310. {"9223372036854775808", 10, 9223372036854775808ULL},
  311. {"9223372036854775809", 10, 9223372036854775809ULL},
  312. {"18446744073709551614", 10, 18446744073709551614ULL},
  313. {"18446744073709551615", 10, 18446744073709551615ULL},
  314. };
  315. TEST_OK(kstrtou64, u64, "%llu", test_u64_ok);
  316. }
  317. static void __init test_kstrtou64_fail(void)
  318. {
  319. static DEFINE_TEST_FAIL(test_u64_fail) = {
  320. {"-2", 10},
  321. {"-1", 10},
  322. {"18446744073709551616", 10},
  323. {"18446744073709551617", 10},
  324. };
  325. TEST_FAIL(kstrtou64, u64, "%llu", test_u64_fail);
  326. }
  327. static void __init test_kstrtos64_ok(void)
  328. {
  329. DECLARE_TEST_OK(s64, struct test_s64);
  330. static DEFINE_TEST_OK(struct test_s64, test_s64_ok) = {
  331. {"-128", 10, -128},
  332. {"-127", 10, -127},
  333. {"-1", 10, -1},
  334. {"0", 10, 0},
  335. {"1", 10, 1},
  336. {"126", 10, 126},
  337. {"127", 10, 127},
  338. {"128", 10, 128},
  339. {"129", 10, 129},
  340. {"254", 10, 254},
  341. {"255", 10, 255},
  342. {"256", 10, 256},
  343. {"257", 10, 257},
  344. {"32766", 10, 32766},
  345. {"32767", 10, 32767},
  346. {"32768", 10, 32768},
  347. {"32769", 10, 32769},
  348. {"65534", 10, 65534},
  349. {"65535", 10, 65535},
  350. {"65536", 10, 65536},
  351. {"65537", 10, 65537},
  352. {"2147483646", 10, 2147483646},
  353. {"2147483647", 10, 2147483647},
  354. {"2147483648", 10, 2147483648LL},
  355. {"2147483649", 10, 2147483649LL},
  356. {"4294967294", 10, 4294967294LL},
  357. {"4294967295", 10, 4294967295LL},
  358. {"4294967296", 10, 4294967296LL},
  359. {"4294967297", 10, 4294967297LL},
  360. {"9223372036854775806", 10, 9223372036854775806LL},
  361. {"9223372036854775807", 10, 9223372036854775807LL},
  362. };
  363. TEST_OK(kstrtos64, s64, "%lld", test_s64_ok);
  364. }
  365. static void __init test_kstrtos64_fail(void)
  366. {
  367. static DEFINE_TEST_FAIL(test_s64_fail) = {
  368. {"9223372036854775808", 10},
  369. {"9223372036854775809", 10},
  370. {"18446744073709551614", 10},
  371. {"18446744073709551615", 10},
  372. {"18446744073709551616", 10},
  373. {"18446744073709551617", 10},
  374. };
  375. TEST_FAIL(kstrtos64, s64, "%lld", test_s64_fail);
  376. }
  377. static void __init test_kstrtou32_ok(void)
  378. {
  379. DECLARE_TEST_OK(u32, struct test_u32);
  380. static DEFINE_TEST_OK(struct test_u32, test_u32_ok) = {
  381. {"0", 10, 0},
  382. {"1", 10, 1},
  383. {"126", 10, 126},
  384. {"127", 10, 127},
  385. {"128", 10, 128},
  386. {"129", 10, 129},
  387. {"254", 10, 254},
  388. {"255", 10, 255},
  389. {"256", 10, 256},
  390. {"257", 10, 257},
  391. {"32766", 10, 32766},
  392. {"32767", 10, 32767},
  393. {"32768", 10, 32768},
  394. {"32769", 10, 32769},
  395. {"65534", 10, 65534},
  396. {"65535", 10, 65535},
  397. {"65536", 10, 65536},
  398. {"65537", 10, 65537},
  399. {"2147483646", 10, 2147483646},
  400. {"2147483647", 10, 2147483647},
  401. {"2147483648", 10, 2147483648U},
  402. {"2147483649", 10, 2147483649U},
  403. {"4294967294", 10, 4294967294U},
  404. {"4294967295", 10, 4294967295U},
  405. };
  406. TEST_OK(kstrtou32, u32, "%u", test_u32_ok);
  407. }
  408. static void __init test_kstrtou32_fail(void)
  409. {
  410. static DEFINE_TEST_FAIL(test_u32_fail) = {
  411. {"-2", 10},
  412. {"-1", 10},
  413. {"4294967296", 10},
  414. {"4294967297", 10},
  415. {"9223372036854775806", 10},
  416. {"9223372036854775807", 10},
  417. {"9223372036854775808", 10},
  418. {"9223372036854775809", 10},
  419. {"18446744073709551614", 10},
  420. {"18446744073709551615", 10},
  421. {"18446744073709551616", 10},
  422. {"18446744073709551617", 10},
  423. };
  424. TEST_FAIL(kstrtou32, u32, "%u", test_u32_fail);
  425. }
  426. static void __init test_kstrtos32_ok(void)
  427. {
  428. DECLARE_TEST_OK(s32, struct test_s32);
  429. static DEFINE_TEST_OK(struct test_s32, test_s32_ok) = {
  430. {"-128", 10, -128},
  431. {"-127", 10, -127},
  432. {"-1", 10, -1},
  433. {"0", 10, 0},
  434. {"1", 10, 1},
  435. {"126", 10, 126},
  436. {"127", 10, 127},
  437. {"128", 10, 128},
  438. {"129", 10, 129},
  439. {"254", 10, 254},
  440. {"255", 10, 255},
  441. {"256", 10, 256},
  442. {"257", 10, 257},
  443. {"32766", 10, 32766},
  444. {"32767", 10, 32767},
  445. {"32768", 10, 32768},
  446. {"32769", 10, 32769},
  447. {"65534", 10, 65534},
  448. {"65535", 10, 65535},
  449. {"65536", 10, 65536},
  450. {"65537", 10, 65537},
  451. {"2147483646", 10, 2147483646},
  452. {"2147483647", 10, 2147483647},
  453. };
  454. TEST_OK(kstrtos32, s32, "%d", test_s32_ok);
  455. }
  456. static void __init test_kstrtos32_fail(void)
  457. {
  458. static DEFINE_TEST_FAIL(test_s32_fail) = {
  459. {"2147483648", 10},
  460. {"2147483649", 10},
  461. {"4294967294", 10},
  462. {"4294967295", 10},
  463. {"4294967296", 10},
  464. {"4294967297", 10},
  465. {"9223372036854775806", 10},
  466. {"9223372036854775807", 10},
  467. {"9223372036854775808", 10},
  468. {"9223372036854775809", 10},
  469. {"18446744073709551614", 10},
  470. {"18446744073709551615", 10},
  471. {"18446744073709551616", 10},
  472. {"18446744073709551617", 10},
  473. };
  474. TEST_FAIL(kstrtos32, s32, "%d", test_s32_fail);
  475. }
  476. static void __init test_kstrtou16_ok(void)
  477. {
  478. DECLARE_TEST_OK(u16, struct test_u16);
  479. static DEFINE_TEST_OK(struct test_u16, test_u16_ok) = {
  480. {"0", 10, 0},
  481. {"1", 10, 1},
  482. {"126", 10, 126},
  483. {"127", 10, 127},
  484. {"128", 10, 128},
  485. {"129", 10, 129},
  486. {"254", 10, 254},
  487. {"255", 10, 255},
  488. {"256", 10, 256},
  489. {"257", 10, 257},
  490. {"32766", 10, 32766},
  491. {"32767", 10, 32767},
  492. {"32768", 10, 32768},
  493. {"32769", 10, 32769},
  494. {"65534", 10, 65534},
  495. {"65535", 10, 65535},
  496. };
  497. TEST_OK(kstrtou16, u16, "%hu", test_u16_ok);
  498. }
  499. static void __init test_kstrtou16_fail(void)
  500. {
  501. static DEFINE_TEST_FAIL(test_u16_fail) = {
  502. {"-2", 10},
  503. {"-1", 10},
  504. {"65536", 10},
  505. {"65537", 10},
  506. {"2147483646", 10},
  507. {"2147483647", 10},
  508. {"2147483648", 10},
  509. {"2147483649", 10},
  510. {"4294967294", 10},
  511. {"4294967295", 10},
  512. {"4294967296", 10},
  513. {"4294967297", 10},
  514. {"9223372036854775806", 10},
  515. {"9223372036854775807", 10},
  516. {"9223372036854775808", 10},
  517. {"9223372036854775809", 10},
  518. {"18446744073709551614", 10},
  519. {"18446744073709551615", 10},
  520. {"18446744073709551616", 10},
  521. {"18446744073709551617", 10},
  522. };
  523. TEST_FAIL(kstrtou16, u16, "%hu", test_u16_fail);
  524. }
  525. static void __init test_kstrtos16_ok(void)
  526. {
  527. DECLARE_TEST_OK(s16, struct test_s16);
  528. static DEFINE_TEST_OK(struct test_s16, test_s16_ok) = {
  529. {"-130", 10, -130},
  530. {"-129", 10, -129},
  531. {"-128", 10, -128},
  532. {"-127", 10, -127},
  533. {"-1", 10, -1},
  534. {"0", 10, 0},
  535. {"1", 10, 1},
  536. {"126", 10, 126},
  537. {"127", 10, 127},
  538. {"128", 10, 128},
  539. {"129", 10, 129},
  540. {"254", 10, 254},
  541. {"255", 10, 255},
  542. {"256", 10, 256},
  543. {"257", 10, 257},
  544. {"32766", 10, 32766},
  545. {"32767", 10, 32767},
  546. };
  547. TEST_OK(kstrtos16, s16, "%hd", test_s16_ok);
  548. }
  549. static void __init test_kstrtos16_fail(void)
  550. {
  551. static DEFINE_TEST_FAIL(test_s16_fail) = {
  552. {"32768", 10},
  553. {"32769", 10},
  554. {"65534", 10},
  555. {"65535", 10},
  556. {"65536", 10},
  557. {"65537", 10},
  558. {"2147483646", 10},
  559. {"2147483647", 10},
  560. {"2147483648", 10},
  561. {"2147483649", 10},
  562. {"4294967294", 10},
  563. {"4294967295", 10},
  564. {"4294967296", 10},
  565. {"4294967297", 10},
  566. {"9223372036854775806", 10},
  567. {"9223372036854775807", 10},
  568. {"9223372036854775808", 10},
  569. {"9223372036854775809", 10},
  570. {"18446744073709551614", 10},
  571. {"18446744073709551615", 10},
  572. {"18446744073709551616", 10},
  573. {"18446744073709551617", 10},
  574. };
  575. TEST_FAIL(kstrtos16, s16, "%hd", test_s16_fail);
  576. }
  577. static void __init test_kstrtou8_ok(void)
  578. {
  579. DECLARE_TEST_OK(u8, struct test_u8);
  580. static DEFINE_TEST_OK(struct test_u8, test_u8_ok) = {
  581. {"0", 10, 0},
  582. {"1", 10, 1},
  583. {"126", 10, 126},
  584. {"127", 10, 127},
  585. {"128", 10, 128},
  586. {"129", 10, 129},
  587. {"254", 10, 254},
  588. {"255", 10, 255},
  589. };
  590. TEST_OK(kstrtou8, u8, "%hhu", test_u8_ok);
  591. }
  592. static void __init test_kstrtou8_fail(void)
  593. {
  594. static DEFINE_TEST_FAIL(test_u8_fail) = {
  595. {"-2", 10},
  596. {"-1", 10},
  597. {"256", 10},
  598. {"257", 10},
  599. {"32766", 10},
  600. {"32767", 10},
  601. {"32768", 10},
  602. {"32769", 10},
  603. {"65534", 10},
  604. {"65535", 10},
  605. {"65536", 10},
  606. {"65537", 10},
  607. {"2147483646", 10},
  608. {"2147483647", 10},
  609. {"2147483648", 10},
  610. {"2147483649", 10},
  611. {"4294967294", 10},
  612. {"4294967295", 10},
  613. {"4294967296", 10},
  614. {"4294967297", 10},
  615. {"9223372036854775806", 10},
  616. {"9223372036854775807", 10},
  617. {"9223372036854775808", 10},
  618. {"9223372036854775809", 10},
  619. {"18446744073709551614", 10},
  620. {"18446744073709551615", 10},
  621. {"18446744073709551616", 10},
  622. {"18446744073709551617", 10},
  623. };
  624. TEST_FAIL(kstrtou8, u8, "%hhu", test_u8_fail);
  625. }
  626. static void __init test_kstrtos8_ok(void)
  627. {
  628. DECLARE_TEST_OK(s8, struct test_s8);
  629. static DEFINE_TEST_OK(struct test_s8, test_s8_ok) = {
  630. {"-128", 10, -128},
  631. {"-127", 10, -127},
  632. {"-1", 10, -1},
  633. {"0", 10, 0},
  634. {"1", 10, 1},
  635. {"126", 10, 126},
  636. {"127", 10, 127},
  637. };
  638. TEST_OK(kstrtos8, s8, "%hhd", test_s8_ok);
  639. }
  640. static void __init test_kstrtos8_fail(void)
  641. {
  642. static DEFINE_TEST_FAIL(test_s8_fail) = {
  643. {"-130", 10},
  644. {"-129", 10},
  645. {"128", 10},
  646. {"129", 10},
  647. {"254", 10},
  648. {"255", 10},
  649. {"256", 10},
  650. {"257", 10},
  651. {"32766", 10},
  652. {"32767", 10},
  653. {"32768", 10},
  654. {"32769", 10},
  655. {"65534", 10},
  656. {"65535", 10},
  657. {"65536", 10},
  658. {"65537", 10},
  659. {"2147483646", 10},
  660. {"2147483647", 10},
  661. {"2147483648", 10},
  662. {"2147483649", 10},
  663. {"4294967294", 10},
  664. {"4294967295", 10},
  665. {"4294967296", 10},
  666. {"4294967297", 10},
  667. {"9223372036854775806", 10},
  668. {"9223372036854775807", 10},
  669. {"9223372036854775808", 10},
  670. {"9223372036854775809", 10},
  671. {"18446744073709551614", 10},
  672. {"18446744073709551615", 10},
  673. {"18446744073709551616", 10},
  674. {"18446744073709551617", 10},
  675. };
  676. TEST_FAIL(kstrtos8, s8, "%hhd", test_s8_fail);
  677. }
  678. static int __init test_kstrtox_init(void)
  679. {
  680. test_kstrtoull_ok();
  681. test_kstrtoull_fail();
  682. test_kstrtoll_ok();
  683. test_kstrtoll_fail();
  684. test_kstrtou64_ok();
  685. test_kstrtou64_fail();
  686. test_kstrtos64_ok();
  687. test_kstrtos64_fail();
  688. test_kstrtou32_ok();
  689. test_kstrtou32_fail();
  690. test_kstrtos32_ok();
  691. test_kstrtos32_fail();
  692. test_kstrtou16_ok();
  693. test_kstrtou16_fail();
  694. test_kstrtos16_ok();
  695. test_kstrtos16_fail();
  696. test_kstrtou8_ok();
  697. test_kstrtou8_fail();
  698. test_kstrtos8_ok();
  699. test_kstrtos8_fail();
  700. return -EINVAL;
  701. }
  702. module_init(test_kstrtox_init);
  703. MODULE_LICENSE("Dual BSD/GPL");