test_bitmap.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /*
  2. * Test cases for printf facility.
  3. */
  4. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  5. #include <linux/bitmap.h>
  6. #include <linux/init.h>
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/printk.h>
  10. #include <linux/slab.h>
  11. #include <linux/string.h>
  12. static unsigned total_tests __initdata;
  13. static unsigned failed_tests __initdata;
  14. static char pbl_buffer[PAGE_SIZE] __initdata;
  15. static bool __init
  16. __check_eq_uint(const char *srcfile, unsigned int line,
  17. const unsigned int exp_uint, unsigned int x)
  18. {
  19. if (exp_uint != x) {
  20. pr_warn("[%s:%u] expected %u, got %u\n",
  21. srcfile, line, exp_uint, x);
  22. return false;
  23. }
  24. return true;
  25. }
  26. static bool __init
  27. __check_eq_bitmap(const char *srcfile, unsigned int line,
  28. const unsigned long *exp_bmap, unsigned int exp_nbits,
  29. const unsigned long *bmap, unsigned int nbits)
  30. {
  31. if (exp_nbits != nbits) {
  32. pr_warn("[%s:%u] bitmap length mismatch: expected %u, got %u\n",
  33. srcfile, line, exp_nbits, nbits);
  34. return false;
  35. }
  36. if (!bitmap_equal(exp_bmap, bmap, nbits)) {
  37. pr_warn("[%s:%u] bitmaps contents differ: expected \"%*pbl\", got \"%*pbl\"\n",
  38. srcfile, line,
  39. exp_nbits, exp_bmap, nbits, bmap);
  40. return false;
  41. }
  42. return true;
  43. }
  44. static bool __init
  45. __check_eq_pbl(const char *srcfile, unsigned int line,
  46. const char *expected_pbl,
  47. const unsigned long *bitmap, unsigned int nbits)
  48. {
  49. snprintf(pbl_buffer, sizeof(pbl_buffer), "%*pbl", nbits, bitmap);
  50. if (strcmp(expected_pbl, pbl_buffer)) {
  51. pr_warn("[%s:%u] expected \"%s\", got \"%s\"\n",
  52. srcfile, line,
  53. expected_pbl, pbl_buffer);
  54. return false;
  55. }
  56. return true;
  57. }
  58. static bool __init
  59. __check_eq_u32_array(const char *srcfile, unsigned int line,
  60. const u32 *exp_arr, unsigned int exp_len,
  61. const u32 *arr, unsigned int len)
  62. {
  63. if (exp_len != len) {
  64. pr_warn("[%s:%u] array length differ: expected %u, got %u\n",
  65. srcfile, line,
  66. exp_len, len);
  67. return false;
  68. }
  69. if (memcmp(exp_arr, arr, len*sizeof(*arr))) {
  70. pr_warn("[%s:%u] array contents differ\n", srcfile, line);
  71. print_hex_dump(KERN_WARNING, " exp: ", DUMP_PREFIX_OFFSET,
  72. 32, 4, exp_arr, exp_len*sizeof(*exp_arr), false);
  73. print_hex_dump(KERN_WARNING, " got: ", DUMP_PREFIX_OFFSET,
  74. 32, 4, arr, len*sizeof(*arr), false);
  75. return false;
  76. }
  77. return true;
  78. }
  79. #define __expect_eq(suffix, ...) \
  80. ({ \
  81. int result = 0; \
  82. total_tests++; \
  83. if (!__check_eq_ ## suffix(__FILE__, __LINE__, \
  84. ##__VA_ARGS__)) { \
  85. failed_tests++; \
  86. result = 1; \
  87. } \
  88. result; \
  89. })
  90. #define expect_eq_uint(...) __expect_eq(uint, ##__VA_ARGS__)
  91. #define expect_eq_bitmap(...) __expect_eq(bitmap, ##__VA_ARGS__)
  92. #define expect_eq_pbl(...) __expect_eq(pbl, ##__VA_ARGS__)
  93. #define expect_eq_u32_array(...) __expect_eq(u32_array, ##__VA_ARGS__)
  94. static void __init test_zero_fill_copy(void)
  95. {
  96. DECLARE_BITMAP(bmap1, 1024);
  97. DECLARE_BITMAP(bmap2, 1024);
  98. bitmap_zero(bmap1, 1024);
  99. bitmap_zero(bmap2, 1024);
  100. /* single-word bitmaps */
  101. expect_eq_pbl("", bmap1, 23);
  102. bitmap_fill(bmap1, 19);
  103. expect_eq_pbl("0-18", bmap1, 1024);
  104. bitmap_copy(bmap2, bmap1, 23);
  105. expect_eq_pbl("0-18", bmap2, 1024);
  106. bitmap_fill(bmap2, 23);
  107. expect_eq_pbl("0-22", bmap2, 1024);
  108. bitmap_copy(bmap2, bmap1, 23);
  109. expect_eq_pbl("0-18", bmap2, 1024);
  110. bitmap_zero(bmap1, 23);
  111. expect_eq_pbl("", bmap1, 1024);
  112. /* multi-word bitmaps */
  113. bitmap_zero(bmap1, 1024);
  114. expect_eq_pbl("", bmap1, 1024);
  115. bitmap_fill(bmap1, 109);
  116. expect_eq_pbl("0-108", bmap1, 1024);
  117. bitmap_copy(bmap2, bmap1, 1024);
  118. expect_eq_pbl("0-108", bmap2, 1024);
  119. bitmap_fill(bmap2, 1024);
  120. expect_eq_pbl("0-1023", bmap2, 1024);
  121. bitmap_copy(bmap2, bmap1, 1024);
  122. expect_eq_pbl("0-108", bmap2, 1024);
  123. /* the following tests assume a 32- or 64-bit arch (even 128b
  124. * if we care)
  125. */
  126. bitmap_fill(bmap2, 1024);
  127. bitmap_copy(bmap2, bmap1, 109); /* ... but 0-padded til word length */
  128. expect_eq_pbl("0-108,128-1023", bmap2, 1024);
  129. bitmap_fill(bmap2, 1024);
  130. bitmap_copy(bmap2, bmap1, 97); /* ... but aligned on word length */
  131. expect_eq_pbl("0-108,128-1023", bmap2, 1024);
  132. bitmap_zero(bmap2, 97); /* ... but 0-padded til word length */
  133. expect_eq_pbl("128-1023", bmap2, 1024);
  134. }
  135. #define PARSE_TIME 0x1
  136. struct test_bitmap_parselist{
  137. const int errno;
  138. const char *in;
  139. const unsigned long *expected;
  140. const int nbits;
  141. const int flags;
  142. };
  143. static const unsigned long exp[] __initconst = {
  144. BITMAP_FROM_U64(1),
  145. BITMAP_FROM_U64(2),
  146. BITMAP_FROM_U64(0x0000ffff),
  147. BITMAP_FROM_U64(0xffff0000),
  148. BITMAP_FROM_U64(0x55555555),
  149. BITMAP_FROM_U64(0xaaaaaaaa),
  150. BITMAP_FROM_U64(0x11111111),
  151. BITMAP_FROM_U64(0x22222222),
  152. BITMAP_FROM_U64(0xffffffff),
  153. BITMAP_FROM_U64(0xfffffffe),
  154. BITMAP_FROM_U64(0x3333333311111111ULL),
  155. BITMAP_FROM_U64(0xffffffff77777777ULL)
  156. };
  157. static const unsigned long exp2[] __initconst = {
  158. BITMAP_FROM_U64(0x3333333311111111ULL),
  159. BITMAP_FROM_U64(0xffffffff77777777ULL)
  160. };
  161. static const struct test_bitmap_parselist parselist_tests[] __initconst = {
  162. #define step (sizeof(u64) / sizeof(unsigned long))
  163. {0, "0", &exp[0], 8, 0},
  164. {0, "1", &exp[1 * step], 8, 0},
  165. {0, "0-15", &exp[2 * step], 32, 0},
  166. {0, "16-31", &exp[3 * step], 32, 0},
  167. {0, "0-31:1/2", &exp[4 * step], 32, 0},
  168. {0, "1-31:1/2", &exp[5 * step], 32, 0},
  169. {0, "0-31:1/4", &exp[6 * step], 32, 0},
  170. {0, "1-31:1/4", &exp[7 * step], 32, 0},
  171. {0, "0-31:4/4", &exp[8 * step], 32, 0},
  172. {0, "1-31:4/4", &exp[9 * step], 32, 0},
  173. {0, "0-31:1/4,32-63:2/4", &exp[10 * step], 64, 0},
  174. {0, "0-31:3/4,32-63:4/4", &exp[11 * step], 64, 0},
  175. {0, "0-31:1/4,32-63:2/4,64-95:3/4,96-127:4/4", exp2, 128, 0},
  176. {0, "0-2047:128/256", NULL, 2048, PARSE_TIME},
  177. {-EINVAL, "-1", NULL, 8, 0},
  178. {-EINVAL, "-0", NULL, 8, 0},
  179. {-EINVAL, "10-1", NULL, 8, 0},
  180. {-EINVAL, "0-31:", NULL, 8, 0},
  181. {-EINVAL, "0-31:0", NULL, 8, 0},
  182. {-EINVAL, "0-31:0/0", NULL, 8, 0},
  183. {-EINVAL, "0-31:1/0", NULL, 8, 0},
  184. {-EINVAL, "0-31:10/1", NULL, 8, 0},
  185. };
  186. static void __init test_bitmap_parselist(void)
  187. {
  188. int i;
  189. int err;
  190. cycles_t cycles;
  191. DECLARE_BITMAP(bmap, 2048);
  192. for (i = 0; i < ARRAY_SIZE(parselist_tests); i++) {
  193. #define ptest parselist_tests[i]
  194. cycles = get_cycles();
  195. err = bitmap_parselist(ptest.in, bmap, ptest.nbits);
  196. cycles = get_cycles() - cycles;
  197. if (err != ptest.errno) {
  198. pr_err("test %d: input is %s, errno is %d, expected %d\n",
  199. i, ptest.in, err, ptest.errno);
  200. continue;
  201. }
  202. if (!err && ptest.expected
  203. && !__bitmap_equal(bmap, ptest.expected, ptest.nbits)) {
  204. pr_err("test %d: input is %s, result is 0x%lx, expected 0x%lx\n",
  205. i, ptest.in, bmap[0], *ptest.expected);
  206. continue;
  207. }
  208. if (ptest.flags & PARSE_TIME)
  209. pr_err("test %d: input is '%s' OK, Time: %llu\n",
  210. i, ptest.in,
  211. (unsigned long long)cycles);
  212. }
  213. }
  214. static void __init test_bitmap_u32_array_conversions(void)
  215. {
  216. DECLARE_BITMAP(bmap1, 1024);
  217. DECLARE_BITMAP(bmap2, 1024);
  218. u32 exp_arr[32], arr[32];
  219. unsigned nbits;
  220. for (nbits = 0 ; nbits < 257 ; ++nbits) {
  221. const unsigned int used_u32s = DIV_ROUND_UP(nbits, 32);
  222. unsigned int i, rv;
  223. bitmap_zero(bmap1, nbits);
  224. bitmap_set(bmap1, nbits, 1024 - nbits); /* garbage */
  225. memset(arr, 0xff, sizeof(arr));
  226. rv = bitmap_to_u32array(arr, used_u32s, bmap1, nbits);
  227. expect_eq_uint(nbits, rv);
  228. memset(exp_arr, 0xff, sizeof(exp_arr));
  229. memset(exp_arr, 0, used_u32s*sizeof(*exp_arr));
  230. expect_eq_u32_array(exp_arr, 32, arr, 32);
  231. bitmap_fill(bmap2, 1024);
  232. rv = bitmap_from_u32array(bmap2, nbits, arr, used_u32s);
  233. expect_eq_uint(nbits, rv);
  234. expect_eq_bitmap(bmap1, 1024, bmap2, 1024);
  235. for (i = 0 ; i < nbits ; ++i) {
  236. /*
  237. * test conversion bitmap -> u32[]
  238. */
  239. bitmap_zero(bmap1, 1024);
  240. __set_bit(i, bmap1);
  241. bitmap_set(bmap1, nbits, 1024 - nbits); /* garbage */
  242. memset(arr, 0xff, sizeof(arr));
  243. rv = bitmap_to_u32array(arr, used_u32s, bmap1, nbits);
  244. expect_eq_uint(nbits, rv);
  245. /* 1st used u32 words contain expected bit set, the
  246. * remaining words are left unchanged (0xff)
  247. */
  248. memset(exp_arr, 0xff, sizeof(exp_arr));
  249. memset(exp_arr, 0, used_u32s*sizeof(*exp_arr));
  250. exp_arr[i/32] = (1U<<(i%32));
  251. expect_eq_u32_array(exp_arr, 32, arr, 32);
  252. /* same, with longer array to fill
  253. */
  254. memset(arr, 0xff, sizeof(arr));
  255. rv = bitmap_to_u32array(arr, 32, bmap1, nbits);
  256. expect_eq_uint(nbits, rv);
  257. /* 1st used u32 words contain expected bit set, the
  258. * remaining words are all 0s
  259. */
  260. memset(exp_arr, 0, sizeof(exp_arr));
  261. exp_arr[i/32] = (1U<<(i%32));
  262. expect_eq_u32_array(exp_arr, 32, arr, 32);
  263. /*
  264. * test conversion u32[] -> bitmap
  265. */
  266. /* the 1st nbits of bmap2 are identical to
  267. * bmap1, the remaining bits of bmap2 are left
  268. * unchanged (all 1s)
  269. */
  270. bitmap_fill(bmap2, 1024);
  271. rv = bitmap_from_u32array(bmap2, nbits,
  272. exp_arr, used_u32s);
  273. expect_eq_uint(nbits, rv);
  274. expect_eq_bitmap(bmap1, 1024, bmap2, 1024);
  275. /* same, with more bits to fill
  276. */
  277. memset(arr, 0xff, sizeof(arr)); /* garbage */
  278. memset(arr, 0, used_u32s*sizeof(u32));
  279. arr[i/32] = (1U<<(i%32));
  280. bitmap_fill(bmap2, 1024);
  281. rv = bitmap_from_u32array(bmap2, 1024, arr, used_u32s);
  282. expect_eq_uint(used_u32s*32, rv);
  283. /* the 1st nbits of bmap2 are identical to
  284. * bmap1, the remaining bits of bmap2 are cleared
  285. */
  286. bitmap_zero(bmap1, 1024);
  287. __set_bit(i, bmap1);
  288. expect_eq_bitmap(bmap1, 1024, bmap2, 1024);
  289. /*
  290. * test short conversion bitmap -> u32[] (1
  291. * word too short)
  292. */
  293. if (used_u32s > 1) {
  294. bitmap_zero(bmap1, 1024);
  295. __set_bit(i, bmap1);
  296. bitmap_set(bmap1, nbits,
  297. 1024 - nbits); /* garbage */
  298. memset(arr, 0xff, sizeof(arr));
  299. rv = bitmap_to_u32array(arr, used_u32s - 1,
  300. bmap1, nbits);
  301. expect_eq_uint((used_u32s - 1)*32, rv);
  302. /* 1st used u32 words contain expected
  303. * bit set, the remaining words are
  304. * left unchanged (0xff)
  305. */
  306. memset(exp_arr, 0xff, sizeof(exp_arr));
  307. memset(exp_arr, 0,
  308. (used_u32s-1)*sizeof(*exp_arr));
  309. if ((i/32) < (used_u32s - 1))
  310. exp_arr[i/32] = (1U<<(i%32));
  311. expect_eq_u32_array(exp_arr, 32, arr, 32);
  312. }
  313. /*
  314. * test short conversion u32[] -> bitmap (3
  315. * bits too short)
  316. */
  317. if (nbits > 3) {
  318. memset(arr, 0xff, sizeof(arr)); /* garbage */
  319. memset(arr, 0, used_u32s*sizeof(*arr));
  320. arr[i/32] = (1U<<(i%32));
  321. bitmap_zero(bmap1, 1024);
  322. rv = bitmap_from_u32array(bmap1, nbits - 3,
  323. arr, used_u32s);
  324. expect_eq_uint(nbits - 3, rv);
  325. /* we are expecting the bit < nbits -
  326. * 3 (none otherwise), and the rest of
  327. * bmap1 unchanged (0-filled)
  328. */
  329. bitmap_zero(bmap2, 1024);
  330. if (i < nbits - 3)
  331. __set_bit(i, bmap2);
  332. expect_eq_bitmap(bmap2, 1024, bmap1, 1024);
  333. /* do the same with bmap1 initially
  334. * 1-filled
  335. */
  336. bitmap_fill(bmap1, 1024);
  337. rv = bitmap_from_u32array(bmap1, nbits - 3,
  338. arr, used_u32s);
  339. expect_eq_uint(nbits - 3, rv);
  340. /* we are expecting the bit < nbits -
  341. * 3 (none otherwise), and the rest of
  342. * bmap1 unchanged (1-filled)
  343. */
  344. bitmap_zero(bmap2, 1024);
  345. if (i < nbits - 3)
  346. __set_bit(i, bmap2);
  347. bitmap_set(bmap2, nbits-3, 1024 - nbits + 3);
  348. expect_eq_bitmap(bmap2, 1024, bmap1, 1024);
  349. }
  350. }
  351. }
  352. }
  353. static void noinline __init test_mem_optimisations(void)
  354. {
  355. DECLARE_BITMAP(bmap1, 1024);
  356. DECLARE_BITMAP(bmap2, 1024);
  357. unsigned int start, nbits;
  358. for (start = 0; start < 1024; start += 8) {
  359. for (nbits = 0; nbits < 1024 - start; nbits += 8) {
  360. memset(bmap1, 0x5a, sizeof(bmap1));
  361. memset(bmap2, 0x5a, sizeof(bmap2));
  362. bitmap_set(bmap1, start, nbits);
  363. __bitmap_set(bmap2, start, nbits);
  364. if (!bitmap_equal(bmap1, bmap2, 1024)) {
  365. printk("set not equal %d %d\n", start, nbits);
  366. failed_tests++;
  367. }
  368. if (!__bitmap_equal(bmap1, bmap2, 1024)) {
  369. printk("set not __equal %d %d\n", start, nbits);
  370. failed_tests++;
  371. }
  372. bitmap_clear(bmap1, start, nbits);
  373. __bitmap_clear(bmap2, start, nbits);
  374. if (!bitmap_equal(bmap1, bmap2, 1024)) {
  375. printk("clear not equal %d %d\n", start, nbits);
  376. failed_tests++;
  377. }
  378. if (!__bitmap_equal(bmap1, bmap2, 1024)) {
  379. printk("clear not __equal %d %d\n", start,
  380. nbits);
  381. failed_tests++;
  382. }
  383. }
  384. }
  385. }
  386. static int __init test_bitmap_init(void)
  387. {
  388. test_zero_fill_copy();
  389. test_bitmap_u32_array_conversions();
  390. test_bitmap_parselist();
  391. test_mem_optimisations();
  392. if (failed_tests == 0)
  393. pr_info("all %u tests passed\n", total_tests);
  394. else
  395. pr_warn("failed %u out of %u tests\n",
  396. failed_tests, total_tests);
  397. return failed_tests ? -EINVAL : 0;
  398. }
  399. static void __exit test_bitmap_cleanup(void)
  400. {
  401. }
  402. module_init(test_bitmap_init);
  403. module_exit(test_bitmap_cleanup);
  404. MODULE_AUTHOR("david decotigny <david.decotigny@googlers.com>");
  405. MODULE_LICENSE("GPL");