test-kstrtox.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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. {"-0", 10, 0LL},
  250. {"-1", 10, -1LL},
  251. {"-2", 10, -2LL},
  252. {"-9223372036854775808", 10, LLONG_MIN},
  253. };
  254. TEST_OK(kstrtoll, long long, "%lld", test_ll_ok);
  255. }
  256. static void __init test_kstrtoll_fail(void)
  257. {
  258. static DEFINE_TEST_FAIL(test_ll_fail) = {
  259. {"9223372036854775808", 10},
  260. {"9223372036854775809", 10},
  261. {"18446744073709551614", 10},
  262. {"18446744073709551615", 10},
  263. {"-9223372036854775809", 10},
  264. {"-18446744073709551614", 10},
  265. {"-18446744073709551615", 10},
  266. /* sign is first character if any */
  267. {"-+1", 0},
  268. {"-+1", 8},
  269. {"-+1", 10},
  270. {"-+1", 16},
  271. };
  272. TEST_FAIL(kstrtoll, long long, "%lld", test_ll_fail);
  273. }
  274. static void __init test_kstrtou64_ok(void)
  275. {
  276. DECLARE_TEST_OK(u64, struct test_u64);
  277. static DEFINE_TEST_OK(struct test_u64, test_u64_ok) = {
  278. {"0", 10, 0},
  279. {"1", 10, 1},
  280. {"126", 10, 126},
  281. {"127", 10, 127},
  282. {"128", 10, 128},
  283. {"129", 10, 129},
  284. {"254", 10, 254},
  285. {"255", 10, 255},
  286. {"256", 10, 256},
  287. {"257", 10, 257},
  288. {"32766", 10, 32766},
  289. {"32767", 10, 32767},
  290. {"32768", 10, 32768},
  291. {"32769", 10, 32769},
  292. {"65534", 10, 65534},
  293. {"65535", 10, 65535},
  294. {"65536", 10, 65536},
  295. {"65537", 10, 65537},
  296. {"2147483646", 10, 2147483646},
  297. {"2147483647", 10, 2147483647},
  298. {"2147483648", 10, 2147483648ULL},
  299. {"2147483649", 10, 2147483649ULL},
  300. {"4294967294", 10, 4294967294ULL},
  301. {"4294967295", 10, 4294967295ULL},
  302. {"4294967296", 10, 4294967296ULL},
  303. {"4294967297", 10, 4294967297ULL},
  304. {"9223372036854775806", 10, 9223372036854775806ULL},
  305. {"9223372036854775807", 10, 9223372036854775807ULL},
  306. {"9223372036854775808", 10, 9223372036854775808ULL},
  307. {"9223372036854775809", 10, 9223372036854775809ULL},
  308. {"18446744073709551614", 10, 18446744073709551614ULL},
  309. {"18446744073709551615", 10, 18446744073709551615ULL},
  310. };
  311. TEST_OK(kstrtou64, u64, "%llu", test_u64_ok);
  312. }
  313. static void __init test_kstrtou64_fail(void)
  314. {
  315. static DEFINE_TEST_FAIL(test_u64_fail) = {
  316. {"-2", 10},
  317. {"-1", 10},
  318. {"18446744073709551616", 10},
  319. {"18446744073709551617", 10},
  320. };
  321. TEST_FAIL(kstrtou64, u64, "%llu", test_u64_fail);
  322. }
  323. static void __init test_kstrtos64_ok(void)
  324. {
  325. DECLARE_TEST_OK(s64, struct test_s64);
  326. static DEFINE_TEST_OK(struct test_s64, test_s64_ok) = {
  327. {"-128", 10, -128},
  328. {"-127", 10, -127},
  329. {"-1", 10, -1},
  330. {"0", 10, 0},
  331. {"1", 10, 1},
  332. {"126", 10, 126},
  333. {"127", 10, 127},
  334. {"128", 10, 128},
  335. {"129", 10, 129},
  336. {"254", 10, 254},
  337. {"255", 10, 255},
  338. {"256", 10, 256},
  339. {"257", 10, 257},
  340. {"32766", 10, 32766},
  341. {"32767", 10, 32767},
  342. {"32768", 10, 32768},
  343. {"32769", 10, 32769},
  344. {"65534", 10, 65534},
  345. {"65535", 10, 65535},
  346. {"65536", 10, 65536},
  347. {"65537", 10, 65537},
  348. {"2147483646", 10, 2147483646},
  349. {"2147483647", 10, 2147483647},
  350. {"2147483648", 10, 2147483648LL},
  351. {"2147483649", 10, 2147483649LL},
  352. {"4294967294", 10, 4294967294LL},
  353. {"4294967295", 10, 4294967295LL},
  354. {"4294967296", 10, 4294967296LL},
  355. {"4294967297", 10, 4294967297LL},
  356. {"9223372036854775806", 10, 9223372036854775806LL},
  357. {"9223372036854775807", 10, 9223372036854775807LL},
  358. };
  359. TEST_OK(kstrtos64, s64, "%lld", test_s64_ok);
  360. }
  361. static void __init test_kstrtos64_fail(void)
  362. {
  363. static DEFINE_TEST_FAIL(test_s64_fail) = {
  364. {"9223372036854775808", 10},
  365. {"9223372036854775809", 10},
  366. {"18446744073709551614", 10},
  367. {"18446744073709551615", 10},
  368. {"18446744073709551616", 10},
  369. {"18446744073709551617", 10},
  370. };
  371. TEST_FAIL(kstrtos64, s64, "%lld", test_s64_fail);
  372. }
  373. static void __init test_kstrtou32_ok(void)
  374. {
  375. DECLARE_TEST_OK(u32, struct test_u32);
  376. static DEFINE_TEST_OK(struct test_u32, test_u32_ok) = {
  377. {"0", 10, 0},
  378. {"1", 10, 1},
  379. {"126", 10, 126},
  380. {"127", 10, 127},
  381. {"128", 10, 128},
  382. {"129", 10, 129},
  383. {"254", 10, 254},
  384. {"255", 10, 255},
  385. {"256", 10, 256},
  386. {"257", 10, 257},
  387. {"32766", 10, 32766},
  388. {"32767", 10, 32767},
  389. {"32768", 10, 32768},
  390. {"32769", 10, 32769},
  391. {"65534", 10, 65534},
  392. {"65535", 10, 65535},
  393. {"65536", 10, 65536},
  394. {"65537", 10, 65537},
  395. {"2147483646", 10, 2147483646},
  396. {"2147483647", 10, 2147483647},
  397. {"2147483648", 10, 2147483648U},
  398. {"2147483649", 10, 2147483649U},
  399. {"4294967294", 10, 4294967294U},
  400. {"4294967295", 10, 4294967295U},
  401. };
  402. TEST_OK(kstrtou32, u32, "%u", test_u32_ok);
  403. }
  404. static void __init test_kstrtou32_fail(void)
  405. {
  406. static DEFINE_TEST_FAIL(test_u32_fail) = {
  407. {"-2", 10},
  408. {"-1", 10},
  409. {"4294967296", 10},
  410. {"4294967297", 10},
  411. {"9223372036854775806", 10},
  412. {"9223372036854775807", 10},
  413. {"9223372036854775808", 10},
  414. {"9223372036854775809", 10},
  415. {"18446744073709551614", 10},
  416. {"18446744073709551615", 10},
  417. {"18446744073709551616", 10},
  418. {"18446744073709551617", 10},
  419. };
  420. TEST_FAIL(kstrtou32, u32, "%u", test_u32_fail);
  421. }
  422. static void __init test_kstrtos32_ok(void)
  423. {
  424. DECLARE_TEST_OK(s32, struct test_s32);
  425. static DEFINE_TEST_OK(struct test_s32, test_s32_ok) = {
  426. {"-128", 10, -128},
  427. {"-127", 10, -127},
  428. {"-1", 10, -1},
  429. {"0", 10, 0},
  430. {"1", 10, 1},
  431. {"126", 10, 126},
  432. {"127", 10, 127},
  433. {"128", 10, 128},
  434. {"129", 10, 129},
  435. {"254", 10, 254},
  436. {"255", 10, 255},
  437. {"256", 10, 256},
  438. {"257", 10, 257},
  439. {"32766", 10, 32766},
  440. {"32767", 10, 32767},
  441. {"32768", 10, 32768},
  442. {"32769", 10, 32769},
  443. {"65534", 10, 65534},
  444. {"65535", 10, 65535},
  445. {"65536", 10, 65536},
  446. {"65537", 10, 65537},
  447. {"2147483646", 10, 2147483646},
  448. {"2147483647", 10, 2147483647},
  449. };
  450. TEST_OK(kstrtos32, s32, "%d", test_s32_ok);
  451. }
  452. static void __init test_kstrtos32_fail(void)
  453. {
  454. static DEFINE_TEST_FAIL(test_s32_fail) = {
  455. {"2147483648", 10},
  456. {"2147483649", 10},
  457. {"4294967294", 10},
  458. {"4294967295", 10},
  459. {"4294967296", 10},
  460. {"4294967297", 10},
  461. {"9223372036854775806", 10},
  462. {"9223372036854775807", 10},
  463. {"9223372036854775808", 10},
  464. {"9223372036854775809", 10},
  465. {"18446744073709551614", 10},
  466. {"18446744073709551615", 10},
  467. {"18446744073709551616", 10},
  468. {"18446744073709551617", 10},
  469. };
  470. TEST_FAIL(kstrtos32, s32, "%d", test_s32_fail);
  471. }
  472. static void __init test_kstrtou16_ok(void)
  473. {
  474. DECLARE_TEST_OK(u16, struct test_u16);
  475. static DEFINE_TEST_OK(struct test_u16, test_u16_ok) = {
  476. {"0", 10, 0},
  477. {"1", 10, 1},
  478. {"126", 10, 126},
  479. {"127", 10, 127},
  480. {"128", 10, 128},
  481. {"129", 10, 129},
  482. {"254", 10, 254},
  483. {"255", 10, 255},
  484. {"256", 10, 256},
  485. {"257", 10, 257},
  486. {"32766", 10, 32766},
  487. {"32767", 10, 32767},
  488. {"32768", 10, 32768},
  489. {"32769", 10, 32769},
  490. {"65534", 10, 65534},
  491. {"65535", 10, 65535},
  492. };
  493. TEST_OK(kstrtou16, u16, "%hu", test_u16_ok);
  494. }
  495. static void __init test_kstrtou16_fail(void)
  496. {
  497. static DEFINE_TEST_FAIL(test_u16_fail) = {
  498. {"-2", 10},
  499. {"-1", 10},
  500. {"65536", 10},
  501. {"65537", 10},
  502. {"2147483646", 10},
  503. {"2147483647", 10},
  504. {"2147483648", 10},
  505. {"2147483649", 10},
  506. {"4294967294", 10},
  507. {"4294967295", 10},
  508. {"4294967296", 10},
  509. {"4294967297", 10},
  510. {"9223372036854775806", 10},
  511. {"9223372036854775807", 10},
  512. {"9223372036854775808", 10},
  513. {"9223372036854775809", 10},
  514. {"18446744073709551614", 10},
  515. {"18446744073709551615", 10},
  516. {"18446744073709551616", 10},
  517. {"18446744073709551617", 10},
  518. };
  519. TEST_FAIL(kstrtou16, u16, "%hu", test_u16_fail);
  520. }
  521. static void __init test_kstrtos16_ok(void)
  522. {
  523. DECLARE_TEST_OK(s16, struct test_s16);
  524. static DEFINE_TEST_OK(struct test_s16, test_s16_ok) = {
  525. {"-130", 10, -130},
  526. {"-129", 10, -129},
  527. {"-128", 10, -128},
  528. {"-127", 10, -127},
  529. {"-1", 10, -1},
  530. {"0", 10, 0},
  531. {"1", 10, 1},
  532. {"126", 10, 126},
  533. {"127", 10, 127},
  534. {"128", 10, 128},
  535. {"129", 10, 129},
  536. {"254", 10, 254},
  537. {"255", 10, 255},
  538. {"256", 10, 256},
  539. {"257", 10, 257},
  540. {"32766", 10, 32766},
  541. {"32767", 10, 32767},
  542. };
  543. TEST_OK(kstrtos16, s16, "%hd", test_s16_ok);
  544. }
  545. static void __init test_kstrtos16_fail(void)
  546. {
  547. static DEFINE_TEST_FAIL(test_s16_fail) = {
  548. {"32768", 10},
  549. {"32769", 10},
  550. {"65534", 10},
  551. {"65535", 10},
  552. {"65536", 10},
  553. {"65537", 10},
  554. {"2147483646", 10},
  555. {"2147483647", 10},
  556. {"2147483648", 10},
  557. {"2147483649", 10},
  558. {"4294967294", 10},
  559. {"4294967295", 10},
  560. {"4294967296", 10},
  561. {"4294967297", 10},
  562. {"9223372036854775806", 10},
  563. {"9223372036854775807", 10},
  564. {"9223372036854775808", 10},
  565. {"9223372036854775809", 10},
  566. {"18446744073709551614", 10},
  567. {"18446744073709551615", 10},
  568. {"18446744073709551616", 10},
  569. {"18446744073709551617", 10},
  570. };
  571. TEST_FAIL(kstrtos16, s16, "%hd", test_s16_fail);
  572. }
  573. static void __init test_kstrtou8_ok(void)
  574. {
  575. DECLARE_TEST_OK(u8, struct test_u8);
  576. static DEFINE_TEST_OK(struct test_u8, test_u8_ok) = {
  577. {"0", 10, 0},
  578. {"1", 10, 1},
  579. {"126", 10, 126},
  580. {"127", 10, 127},
  581. {"128", 10, 128},
  582. {"129", 10, 129},
  583. {"254", 10, 254},
  584. {"255", 10, 255},
  585. };
  586. TEST_OK(kstrtou8, u8, "%hhu", test_u8_ok);
  587. }
  588. static void __init test_kstrtou8_fail(void)
  589. {
  590. static DEFINE_TEST_FAIL(test_u8_fail) = {
  591. {"-2", 10},
  592. {"-1", 10},
  593. {"256", 10},
  594. {"257", 10},
  595. {"32766", 10},
  596. {"32767", 10},
  597. {"32768", 10},
  598. {"32769", 10},
  599. {"65534", 10},
  600. {"65535", 10},
  601. {"65536", 10},
  602. {"65537", 10},
  603. {"2147483646", 10},
  604. {"2147483647", 10},
  605. {"2147483648", 10},
  606. {"2147483649", 10},
  607. {"4294967294", 10},
  608. {"4294967295", 10},
  609. {"4294967296", 10},
  610. {"4294967297", 10},
  611. {"9223372036854775806", 10},
  612. {"9223372036854775807", 10},
  613. {"9223372036854775808", 10},
  614. {"9223372036854775809", 10},
  615. {"18446744073709551614", 10},
  616. {"18446744073709551615", 10},
  617. {"18446744073709551616", 10},
  618. {"18446744073709551617", 10},
  619. };
  620. TEST_FAIL(kstrtou8, u8, "%hhu", test_u8_fail);
  621. }
  622. static void __init test_kstrtos8_ok(void)
  623. {
  624. DECLARE_TEST_OK(s8, struct test_s8);
  625. static DEFINE_TEST_OK(struct test_s8, test_s8_ok) = {
  626. {"-128", 10, -128},
  627. {"-127", 10, -127},
  628. {"-1", 10, -1},
  629. {"0", 10, 0},
  630. {"1", 10, 1},
  631. {"126", 10, 126},
  632. {"127", 10, 127},
  633. };
  634. TEST_OK(kstrtos8, s8, "%hhd", test_s8_ok);
  635. }
  636. static void __init test_kstrtos8_fail(void)
  637. {
  638. static DEFINE_TEST_FAIL(test_s8_fail) = {
  639. {"-130", 10},
  640. {"-129", 10},
  641. {"128", 10},
  642. {"129", 10},
  643. {"254", 10},
  644. {"255", 10},
  645. {"256", 10},
  646. {"257", 10},
  647. {"32766", 10},
  648. {"32767", 10},
  649. {"32768", 10},
  650. {"32769", 10},
  651. {"65534", 10},
  652. {"65535", 10},
  653. {"65536", 10},
  654. {"65537", 10},
  655. {"2147483646", 10},
  656. {"2147483647", 10},
  657. {"2147483648", 10},
  658. {"2147483649", 10},
  659. {"4294967294", 10},
  660. {"4294967295", 10},
  661. {"4294967296", 10},
  662. {"4294967297", 10},
  663. {"9223372036854775806", 10},
  664. {"9223372036854775807", 10},
  665. {"9223372036854775808", 10},
  666. {"9223372036854775809", 10},
  667. {"18446744073709551614", 10},
  668. {"18446744073709551615", 10},
  669. {"18446744073709551616", 10},
  670. {"18446744073709551617", 10},
  671. };
  672. TEST_FAIL(kstrtos8, s8, "%hhd", test_s8_fail);
  673. }
  674. static int __init test_kstrtox_init(void)
  675. {
  676. test_kstrtoull_ok();
  677. test_kstrtoull_fail();
  678. test_kstrtoll_ok();
  679. test_kstrtoll_fail();
  680. test_kstrtou64_ok();
  681. test_kstrtou64_fail();
  682. test_kstrtos64_ok();
  683. test_kstrtos64_fail();
  684. test_kstrtou32_ok();
  685. test_kstrtou32_fail();
  686. test_kstrtos32_ok();
  687. test_kstrtos32_fail();
  688. test_kstrtou16_ok();
  689. test_kstrtou16_fail();
  690. test_kstrtos16_ok();
  691. test_kstrtos16_fail();
  692. test_kstrtou8_ok();
  693. test_kstrtou8_fail();
  694. test_kstrtos8_ok();
  695. test_kstrtos8_fail();
  696. return -EINVAL;
  697. }
  698. module_init(test_kstrtox_init);
  699. MODULE_LICENSE("Dual BSD/GPL");