test_bitmap.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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[] = {1, 2, 0x0000ffff, 0xffff0000, 0x55555555,
  144. 0xaaaaaaaa, 0x11111111, 0x22222222, 0xffffffff,
  145. 0xfffffffe, 0x3333333311111111, 0xffffffff77777777};
  146. static const unsigned long exp2[] = {0x3333333311111111, 0xffffffff77777777};
  147. static const struct test_bitmap_parselist parselist_tests[] __initconst = {
  148. {0, "0", &exp[0], 8, 0},
  149. {0, "1", &exp[1], 8, 0},
  150. {0, "0-15", &exp[2], 32, 0},
  151. {0, "16-31", &exp[3], 32, 0},
  152. {0, "0-31:1/2", &exp[4], 32, 0},
  153. {0, "1-31:1/2", &exp[5], 32, 0},
  154. {0, "0-31:1/4", &exp[6], 32, 0},
  155. {0, "1-31:1/4", &exp[7], 32, 0},
  156. {0, "0-31:4/4", &exp[8], 32, 0},
  157. {0, "1-31:4/4", &exp[9], 32, 0},
  158. {0, "0-31:1/4,32-63:2/4", &exp[10], 64, 0},
  159. {0, "0-31:3/4,32-63:4/4", &exp[11], 64, 0},
  160. {0, "0-31:1/4,32-63:2/4,64-95:3/4,96-127:4/4", exp2, 128, 0},
  161. {0, "0-2047:128/256", NULL, 2048, PARSE_TIME},
  162. {-EINVAL, "-1", NULL, 8, 0},
  163. {-EINVAL, "-0", NULL, 8, 0},
  164. {-EINVAL, "10-1", NULL, 8, 0},
  165. {-EINVAL, "0-31:10/1", NULL, 8, 0},
  166. };
  167. static void __init test_bitmap_parselist(void)
  168. {
  169. int i;
  170. int err;
  171. cycles_t cycles;
  172. DECLARE_BITMAP(bmap, 2048);
  173. for (i = 0; i < ARRAY_SIZE(parselist_tests); i++) {
  174. #define ptest parselist_tests[i]
  175. cycles = get_cycles();
  176. err = bitmap_parselist(ptest.in, bmap, ptest.nbits);
  177. cycles = get_cycles() - cycles;
  178. if (err != ptest.errno) {
  179. pr_err("test %d: input is %s, errno is %d, expected %d\n",
  180. i, ptest.in, err, ptest.errno);
  181. continue;
  182. }
  183. if (!err && ptest.expected
  184. && !__bitmap_equal(bmap, ptest.expected, ptest.nbits)) {
  185. pr_err("test %d: input is %s, result is 0x%lx, expected 0x%lx\n",
  186. i, ptest.in, bmap[0], *ptest.expected);
  187. continue;
  188. }
  189. if (ptest.flags & PARSE_TIME)
  190. pr_err("test %d: input is '%s' OK, Time: %llu\n",
  191. i, ptest.in,
  192. (unsigned long long)cycles);
  193. }
  194. }
  195. static void __init test_bitmap_u32_array_conversions(void)
  196. {
  197. DECLARE_BITMAP(bmap1, 1024);
  198. DECLARE_BITMAP(bmap2, 1024);
  199. u32 exp_arr[32], arr[32];
  200. unsigned nbits;
  201. for (nbits = 0 ; nbits < 257 ; ++nbits) {
  202. const unsigned int used_u32s = DIV_ROUND_UP(nbits, 32);
  203. unsigned int i, rv;
  204. bitmap_zero(bmap1, nbits);
  205. bitmap_set(bmap1, nbits, 1024 - nbits); /* garbage */
  206. memset(arr, 0xff, sizeof(arr));
  207. rv = bitmap_to_u32array(arr, used_u32s, bmap1, nbits);
  208. expect_eq_uint(nbits, rv);
  209. memset(exp_arr, 0xff, sizeof(exp_arr));
  210. memset(exp_arr, 0, used_u32s*sizeof(*exp_arr));
  211. expect_eq_u32_array(exp_arr, 32, arr, 32);
  212. bitmap_fill(bmap2, 1024);
  213. rv = bitmap_from_u32array(bmap2, nbits, arr, used_u32s);
  214. expect_eq_uint(nbits, rv);
  215. expect_eq_bitmap(bmap1, 1024, bmap2, 1024);
  216. for (i = 0 ; i < nbits ; ++i) {
  217. /*
  218. * test conversion bitmap -> u32[]
  219. */
  220. bitmap_zero(bmap1, 1024);
  221. __set_bit(i, bmap1);
  222. bitmap_set(bmap1, nbits, 1024 - nbits); /* garbage */
  223. memset(arr, 0xff, sizeof(arr));
  224. rv = bitmap_to_u32array(arr, used_u32s, bmap1, nbits);
  225. expect_eq_uint(nbits, rv);
  226. /* 1st used u32 words contain expected bit set, the
  227. * remaining words are left unchanged (0xff)
  228. */
  229. memset(exp_arr, 0xff, sizeof(exp_arr));
  230. memset(exp_arr, 0, used_u32s*sizeof(*exp_arr));
  231. exp_arr[i/32] = (1U<<(i%32));
  232. expect_eq_u32_array(exp_arr, 32, arr, 32);
  233. /* same, with longer array to fill
  234. */
  235. memset(arr, 0xff, sizeof(arr));
  236. rv = bitmap_to_u32array(arr, 32, bmap1, nbits);
  237. expect_eq_uint(nbits, rv);
  238. /* 1st used u32 words contain expected bit set, the
  239. * remaining words are all 0s
  240. */
  241. memset(exp_arr, 0, sizeof(exp_arr));
  242. exp_arr[i/32] = (1U<<(i%32));
  243. expect_eq_u32_array(exp_arr, 32, arr, 32);
  244. /*
  245. * test conversion u32[] -> bitmap
  246. */
  247. /* the 1st nbits of bmap2 are identical to
  248. * bmap1, the remaining bits of bmap2 are left
  249. * unchanged (all 1s)
  250. */
  251. bitmap_fill(bmap2, 1024);
  252. rv = bitmap_from_u32array(bmap2, nbits,
  253. exp_arr, used_u32s);
  254. expect_eq_uint(nbits, rv);
  255. expect_eq_bitmap(bmap1, 1024, bmap2, 1024);
  256. /* same, with more bits to fill
  257. */
  258. memset(arr, 0xff, sizeof(arr)); /* garbage */
  259. memset(arr, 0, used_u32s*sizeof(u32));
  260. arr[i/32] = (1U<<(i%32));
  261. bitmap_fill(bmap2, 1024);
  262. rv = bitmap_from_u32array(bmap2, 1024, arr, used_u32s);
  263. expect_eq_uint(used_u32s*32, rv);
  264. /* the 1st nbits of bmap2 are identical to
  265. * bmap1, the remaining bits of bmap2 are cleared
  266. */
  267. bitmap_zero(bmap1, 1024);
  268. __set_bit(i, bmap1);
  269. expect_eq_bitmap(bmap1, 1024, bmap2, 1024);
  270. /*
  271. * test short conversion bitmap -> u32[] (1
  272. * word too short)
  273. */
  274. if (used_u32s > 1) {
  275. bitmap_zero(bmap1, 1024);
  276. __set_bit(i, bmap1);
  277. bitmap_set(bmap1, nbits,
  278. 1024 - nbits); /* garbage */
  279. memset(arr, 0xff, sizeof(arr));
  280. rv = bitmap_to_u32array(arr, used_u32s - 1,
  281. bmap1, nbits);
  282. expect_eq_uint((used_u32s - 1)*32, rv);
  283. /* 1st used u32 words contain expected
  284. * bit set, the remaining words are
  285. * left unchanged (0xff)
  286. */
  287. memset(exp_arr, 0xff, sizeof(exp_arr));
  288. memset(exp_arr, 0,
  289. (used_u32s-1)*sizeof(*exp_arr));
  290. if ((i/32) < (used_u32s - 1))
  291. exp_arr[i/32] = (1U<<(i%32));
  292. expect_eq_u32_array(exp_arr, 32, arr, 32);
  293. }
  294. /*
  295. * test short conversion u32[] -> bitmap (3
  296. * bits too short)
  297. */
  298. if (nbits > 3) {
  299. memset(arr, 0xff, sizeof(arr)); /* garbage */
  300. memset(arr, 0, used_u32s*sizeof(*arr));
  301. arr[i/32] = (1U<<(i%32));
  302. bitmap_zero(bmap1, 1024);
  303. rv = bitmap_from_u32array(bmap1, nbits - 3,
  304. arr, used_u32s);
  305. expect_eq_uint(nbits - 3, rv);
  306. /* we are expecting the bit < nbits -
  307. * 3 (none otherwise), and the rest of
  308. * bmap1 unchanged (0-filled)
  309. */
  310. bitmap_zero(bmap2, 1024);
  311. if (i < nbits - 3)
  312. __set_bit(i, bmap2);
  313. expect_eq_bitmap(bmap2, 1024, bmap1, 1024);
  314. /* do the same with bmap1 initially
  315. * 1-filled
  316. */
  317. bitmap_fill(bmap1, 1024);
  318. rv = bitmap_from_u32array(bmap1, nbits - 3,
  319. arr, used_u32s);
  320. expect_eq_uint(nbits - 3, rv);
  321. /* we are expecting the bit < nbits -
  322. * 3 (none otherwise), and the rest of
  323. * bmap1 unchanged (1-filled)
  324. */
  325. bitmap_zero(bmap2, 1024);
  326. if (i < nbits - 3)
  327. __set_bit(i, bmap2);
  328. bitmap_set(bmap2, nbits-3, 1024 - nbits + 3);
  329. expect_eq_bitmap(bmap2, 1024, bmap1, 1024);
  330. }
  331. }
  332. }
  333. }
  334. static void noinline __init test_mem_optimisations(void)
  335. {
  336. DECLARE_BITMAP(bmap1, 1024);
  337. DECLARE_BITMAP(bmap2, 1024);
  338. unsigned int start, nbits;
  339. for (start = 0; start < 1024; start += 8) {
  340. memset(bmap1, 0x5a, sizeof(bmap1));
  341. memset(bmap2, 0x5a, sizeof(bmap2));
  342. for (nbits = 0; nbits < 1024 - start; nbits += 8) {
  343. bitmap_set(bmap1, start, nbits);
  344. __bitmap_set(bmap2, start, nbits);
  345. if (!bitmap_equal(bmap1, bmap2, 1024))
  346. printk("set not equal %d %d\n", start, nbits);
  347. if (!__bitmap_equal(bmap1, bmap2, 1024))
  348. printk("set not __equal %d %d\n", start, nbits);
  349. bitmap_clear(bmap1, start, nbits);
  350. __bitmap_clear(bmap2, start, nbits);
  351. if (!bitmap_equal(bmap1, bmap2, 1024))
  352. printk("clear not equal %d %d\n", start, nbits);
  353. if (!__bitmap_equal(bmap1, bmap2, 1024))
  354. printk("clear not __equal %d %d\n", start,
  355. nbits);
  356. }
  357. }
  358. }
  359. static int __init test_bitmap_init(void)
  360. {
  361. test_zero_fill_copy();
  362. test_bitmap_u32_array_conversions();
  363. test_bitmap_parselist();
  364. test_mem_optimisations();
  365. if (failed_tests == 0)
  366. pr_info("all %u tests passed\n", total_tests);
  367. else
  368. pr_warn("failed %u out of %u tests\n",
  369. failed_tests, total_tests);
  370. return failed_tests ? -EINVAL : 0;
  371. }
  372. static void __exit test_bitmap_cleanup(void)
  373. {
  374. }
  375. module_init(test_bitmap_init);
  376. module_exit(test_bitmap_cleanup);
  377. MODULE_AUTHOR("david decotigny <david.decotigny@googlers.com>");
  378. MODULE_LICENSE("GPL");