test_bitmap.c 12 KB

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