test_bitmap.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Test cases for bitmap API.
  4. */
  5. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  6. #include <linux/bitmap.h>
  7. #include <linux/init.h>
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/printk.h>
  11. #include <linux/slab.h>
  12. #include <linux/string.h>
  13. #include <linux/uaccess.h>
  14. #include "../tools/testing/selftests/kselftest_module.h"
  15. KSTM_MODULE_GLOBALS();
  16. static char pbl_buffer[PAGE_SIZE] __initdata;
  17. static const unsigned long exp1[] __initconst = {
  18. BITMAP_FROM_U64(1),
  19. BITMAP_FROM_U64(2),
  20. BITMAP_FROM_U64(0x0000ffff),
  21. BITMAP_FROM_U64(0xffff0000),
  22. BITMAP_FROM_U64(0x55555555),
  23. BITMAP_FROM_U64(0xaaaaaaaa),
  24. BITMAP_FROM_U64(0x11111111),
  25. BITMAP_FROM_U64(0x22222222),
  26. BITMAP_FROM_U64(0xffffffff),
  27. BITMAP_FROM_U64(0xfffffffe),
  28. BITMAP_FROM_U64(0x3333333311111111ULL),
  29. BITMAP_FROM_U64(0xffffffff77777777ULL),
  30. BITMAP_FROM_U64(0),
  31. };
  32. static const unsigned long exp2[] __initconst = {
  33. BITMAP_FROM_U64(0x3333333311111111ULL),
  34. BITMAP_FROM_U64(0xffffffff77777777ULL),
  35. };
  36. /* Fibonacci sequence */
  37. static const unsigned long exp2_to_exp3_mask[] __initconst = {
  38. BITMAP_FROM_U64(0x008000020020212eULL),
  39. };
  40. /* exp3_0_1 = (exp2[0] & ~exp2_to_exp3_mask) | (exp2[1] & exp2_to_exp3_mask) */
  41. static const unsigned long exp3_0_1[] __initconst = {
  42. BITMAP_FROM_U64(0x33b3333311313137ULL),
  43. };
  44. /* exp3_1_0 = (exp2[1] & ~exp2_to_exp3_mask) | (exp2[0] & exp2_to_exp3_mask) */
  45. static const unsigned long exp3_1_0[] __initconst = {
  46. BITMAP_FROM_U64(0xff7fffff77575751ULL),
  47. };
  48. static bool __init
  49. __check_eq_uint(const char *srcfile, unsigned int line,
  50. const unsigned int exp_uint, unsigned int x)
  51. {
  52. if (exp_uint != x) {
  53. pr_err("[%s:%u] expected %u, got %u\n",
  54. srcfile, line, exp_uint, x);
  55. return false;
  56. }
  57. return true;
  58. }
  59. static bool __init
  60. __check_eq_bitmap(const char *srcfile, unsigned int line,
  61. const unsigned long *exp_bmap, const unsigned long *bmap,
  62. unsigned int nbits)
  63. {
  64. if (!bitmap_equal(exp_bmap, bmap, nbits)) {
  65. pr_warn("[%s:%u] bitmaps contents differ: expected \"%*pbl\", got \"%*pbl\"\n",
  66. srcfile, line,
  67. nbits, exp_bmap, nbits, bmap);
  68. return false;
  69. }
  70. return true;
  71. }
  72. static bool __init
  73. __check_eq_pbl(const char *srcfile, unsigned int line,
  74. const char *expected_pbl,
  75. const unsigned long *bitmap, unsigned int nbits)
  76. {
  77. snprintf(pbl_buffer, sizeof(pbl_buffer), "%*pbl", nbits, bitmap);
  78. if (strcmp(expected_pbl, pbl_buffer)) {
  79. pr_warn("[%s:%u] expected \"%s\", got \"%s\"\n",
  80. srcfile, line,
  81. expected_pbl, pbl_buffer);
  82. return false;
  83. }
  84. return true;
  85. }
  86. static bool __init
  87. __check_eq_u32_array(const char *srcfile, unsigned int line,
  88. const u32 *exp_arr, unsigned int exp_len,
  89. const u32 *arr, unsigned int len) __used;
  90. static bool __init
  91. __check_eq_u32_array(const char *srcfile, unsigned int line,
  92. const u32 *exp_arr, unsigned int exp_len,
  93. const u32 *arr, unsigned int len)
  94. {
  95. if (exp_len != len) {
  96. pr_warn("[%s:%u] array length differ: expected %u, got %u\n",
  97. srcfile, line,
  98. exp_len, len);
  99. return false;
  100. }
  101. if (memcmp(exp_arr, arr, len*sizeof(*arr))) {
  102. pr_warn("[%s:%u] array contents differ\n", srcfile, line);
  103. print_hex_dump(KERN_WARNING, " exp: ", DUMP_PREFIX_OFFSET,
  104. 32, 4, exp_arr, exp_len*sizeof(*exp_arr), false);
  105. print_hex_dump(KERN_WARNING, " got: ", DUMP_PREFIX_OFFSET,
  106. 32, 4, arr, len*sizeof(*arr), false);
  107. return false;
  108. }
  109. return true;
  110. }
  111. static bool __init __check_eq_clump8(const char *srcfile, unsigned int line,
  112. const unsigned int offset,
  113. const unsigned int size,
  114. const unsigned char *const clump_exp,
  115. const unsigned long *const clump)
  116. {
  117. unsigned long exp;
  118. if (offset >= size) {
  119. pr_warn("[%s:%u] bit offset for clump out-of-bounds: expected less than %u, got %u\n",
  120. srcfile, line, size, offset);
  121. return false;
  122. }
  123. exp = clump_exp[offset / 8];
  124. if (!exp) {
  125. pr_warn("[%s:%u] bit offset for zero clump: expected nonzero clump, got bit offset %u with clump value 0",
  126. srcfile, line, offset);
  127. return false;
  128. }
  129. if (*clump != exp) {
  130. pr_warn("[%s:%u] expected clump value of 0x%lX, got clump value of 0x%lX",
  131. srcfile, line, exp, *clump);
  132. return false;
  133. }
  134. return true;
  135. }
  136. #define __expect_eq(suffix, ...) \
  137. ({ \
  138. int result = 0; \
  139. total_tests++; \
  140. if (!__check_eq_ ## suffix(__FILE__, __LINE__, \
  141. ##__VA_ARGS__)) { \
  142. failed_tests++; \
  143. result = 1; \
  144. } \
  145. result; \
  146. })
  147. #define expect_eq_uint(...) __expect_eq(uint, ##__VA_ARGS__)
  148. #define expect_eq_bitmap(...) __expect_eq(bitmap, ##__VA_ARGS__)
  149. #define expect_eq_pbl(...) __expect_eq(pbl, ##__VA_ARGS__)
  150. #define expect_eq_u32_array(...) __expect_eq(u32_array, ##__VA_ARGS__)
  151. #define expect_eq_clump8(...) __expect_eq(clump8, ##__VA_ARGS__)
  152. static void __init test_zero_clear(void)
  153. {
  154. DECLARE_BITMAP(bmap, 1024);
  155. /* Known way to set all bits */
  156. memset(bmap, 0xff, 128);
  157. expect_eq_pbl("0-22", bmap, 23);
  158. expect_eq_pbl("0-1023", bmap, 1024);
  159. /* single-word bitmaps */
  160. bitmap_clear(bmap, 0, 9);
  161. expect_eq_pbl("9-1023", bmap, 1024);
  162. bitmap_zero(bmap, 35);
  163. expect_eq_pbl("64-1023", bmap, 1024);
  164. /* cross boundaries operations */
  165. bitmap_clear(bmap, 79, 19);
  166. expect_eq_pbl("64-78,98-1023", bmap, 1024);
  167. bitmap_zero(bmap, 115);
  168. expect_eq_pbl("128-1023", bmap, 1024);
  169. /* Zeroing entire area */
  170. bitmap_zero(bmap, 1024);
  171. expect_eq_pbl("", bmap, 1024);
  172. }
  173. static void __init test_fill_set(void)
  174. {
  175. DECLARE_BITMAP(bmap, 1024);
  176. /* Known way to clear all bits */
  177. memset(bmap, 0x00, 128);
  178. expect_eq_pbl("", bmap, 23);
  179. expect_eq_pbl("", bmap, 1024);
  180. /* single-word bitmaps */
  181. bitmap_set(bmap, 0, 9);
  182. expect_eq_pbl("0-8", bmap, 1024);
  183. bitmap_fill(bmap, 35);
  184. expect_eq_pbl("0-63", bmap, 1024);
  185. /* cross boundaries operations */
  186. bitmap_set(bmap, 79, 19);
  187. expect_eq_pbl("0-63,79-97", bmap, 1024);
  188. bitmap_fill(bmap, 115);
  189. expect_eq_pbl("0-127", bmap, 1024);
  190. /* Zeroing entire area */
  191. bitmap_fill(bmap, 1024);
  192. expect_eq_pbl("0-1023", bmap, 1024);
  193. }
  194. static void __init test_copy(void)
  195. {
  196. DECLARE_BITMAP(bmap1, 1024);
  197. DECLARE_BITMAP(bmap2, 1024);
  198. bitmap_zero(bmap1, 1024);
  199. bitmap_zero(bmap2, 1024);
  200. /* single-word bitmaps */
  201. bitmap_set(bmap1, 0, 19);
  202. bitmap_copy(bmap2, bmap1, 23);
  203. expect_eq_pbl("0-18", bmap2, 1024);
  204. bitmap_set(bmap2, 0, 23);
  205. bitmap_copy(bmap2, bmap1, 23);
  206. expect_eq_pbl("0-18", bmap2, 1024);
  207. /* multi-word bitmaps */
  208. bitmap_set(bmap1, 0, 109);
  209. bitmap_copy(bmap2, bmap1, 1024);
  210. expect_eq_pbl("0-108", bmap2, 1024);
  211. bitmap_fill(bmap2, 1024);
  212. bitmap_copy(bmap2, bmap1, 1024);
  213. expect_eq_pbl("0-108", bmap2, 1024);
  214. /* the following tests assume a 32- or 64-bit arch (even 128b
  215. * if we care)
  216. */
  217. bitmap_fill(bmap2, 1024);
  218. bitmap_copy(bmap2, bmap1, 109); /* ... but 0-padded til word length */
  219. expect_eq_pbl("0-108,128-1023", bmap2, 1024);
  220. bitmap_fill(bmap2, 1024);
  221. bitmap_copy(bmap2, bmap1, 97); /* ... but aligned on word length */
  222. expect_eq_pbl("0-108,128-1023", bmap2, 1024);
  223. }
  224. #define EXP2_IN_BITS (sizeof(exp2) * 8)
  225. static void __init test_replace(void)
  226. {
  227. unsigned int nbits = 64;
  228. unsigned int nlongs = DIV_ROUND_UP(nbits, BITS_PER_LONG);
  229. DECLARE_BITMAP(bmap, 1024);
  230. BUILD_BUG_ON(EXP2_IN_BITS < nbits * 2);
  231. bitmap_zero(bmap, 1024);
  232. bitmap_replace(bmap, &exp2[0 * nlongs], &exp2[1 * nlongs], exp2_to_exp3_mask, nbits);
  233. expect_eq_bitmap(bmap, exp3_0_1, nbits);
  234. bitmap_zero(bmap, 1024);
  235. bitmap_replace(bmap, &exp2[1 * nlongs], &exp2[0 * nlongs], exp2_to_exp3_mask, nbits);
  236. expect_eq_bitmap(bmap, exp3_1_0, nbits);
  237. bitmap_fill(bmap, 1024);
  238. bitmap_replace(bmap, &exp2[0 * nlongs], &exp2[1 * nlongs], exp2_to_exp3_mask, nbits);
  239. expect_eq_bitmap(bmap, exp3_0_1, nbits);
  240. bitmap_fill(bmap, 1024);
  241. bitmap_replace(bmap, &exp2[1 * nlongs], &exp2[0 * nlongs], exp2_to_exp3_mask, nbits);
  242. expect_eq_bitmap(bmap, exp3_1_0, nbits);
  243. }
  244. #define PARSE_TIME 0x1
  245. #define NO_LEN 0x2
  246. struct test_bitmap_parselist{
  247. const int errno;
  248. const char *in;
  249. const unsigned long *expected;
  250. const int nbits;
  251. const int flags;
  252. };
  253. static const struct test_bitmap_parselist parselist_tests[] __initconst = {
  254. #define step (sizeof(u64) / sizeof(unsigned long))
  255. {0, "0", &exp1[0], 8, 0},
  256. {0, "1", &exp1[1 * step], 8, 0},
  257. {0, "0-15", &exp1[2 * step], 32, 0},
  258. {0, "16-31", &exp1[3 * step], 32, 0},
  259. {0, "0-31:1/2", &exp1[4 * step], 32, 0},
  260. {0, "1-31:1/2", &exp1[5 * step], 32, 0},
  261. {0, "0-31:1/4", &exp1[6 * step], 32, 0},
  262. {0, "1-31:1/4", &exp1[7 * step], 32, 0},
  263. {0, "0-31:4/4", &exp1[8 * step], 32, 0},
  264. {0, "1-31:4/4", &exp1[9 * step], 32, 0},
  265. {0, "0-31:1/4,32-63:2/4", &exp1[10 * step], 64, 0},
  266. {0, "0-31:3/4,32-63:4/4", &exp1[11 * step], 64, 0},
  267. {0, " ,, 0-31:3/4 ,, 32-63:4/4 ,, ", &exp1[11 * step], 64, 0},
  268. {0, "0-31:1/4,32-63:2/4,64-95:3/4,96-127:4/4", exp2, 128, 0},
  269. {0, "0-2047:128/256", NULL, 2048, PARSE_TIME},
  270. {0, "", &exp1[12 * step], 8, 0},
  271. {0, "\n", &exp1[12 * step], 8, 0},
  272. {0, ",, ,, , , ,", &exp1[12 * step], 8, 0},
  273. {0, " , ,, , , ", &exp1[12 * step], 8, 0},
  274. {0, " , ,, , , \n", &exp1[12 * step], 8, 0},
  275. {-EINVAL, "-1", NULL, 8, 0},
  276. {-EINVAL, "-0", NULL, 8, 0},
  277. {-EINVAL, "10-1", NULL, 8, 0},
  278. {-EINVAL, "0-31:", NULL, 8, 0},
  279. {-EINVAL, "0-31:0", NULL, 8, 0},
  280. {-EINVAL, "0-31:0/", NULL, 8, 0},
  281. {-EINVAL, "0-31:0/0", NULL, 8, 0},
  282. {-EINVAL, "0-31:1/0", NULL, 8, 0},
  283. {-EINVAL, "0-31:10/1", NULL, 8, 0},
  284. {-EOVERFLOW, "0-98765432123456789:10/1", NULL, 8, 0},
  285. {-EINVAL, "a-31", NULL, 8, 0},
  286. {-EINVAL, "0-a1", NULL, 8, 0},
  287. {-EINVAL, "a-31:10/1", NULL, 8, 0},
  288. {-EINVAL, "0-31:a/1", NULL, 8, 0},
  289. {-EINVAL, "0-\n", NULL, 8, 0},
  290. };
  291. static void __init test_bitmap_parselist(void)
  292. {
  293. int i;
  294. int err;
  295. ktime_t time;
  296. DECLARE_BITMAP(bmap, 2048);
  297. for (i = 0; i < ARRAY_SIZE(parselist_tests); i++) {
  298. #define ptest parselist_tests[i]
  299. time = ktime_get();
  300. err = bitmap_parselist(ptest.in, bmap, ptest.nbits);
  301. time = ktime_get() - time;
  302. if (err != ptest.errno) {
  303. pr_err("parselist: %d: input is %s, errno is %d, expected %d\n",
  304. i, ptest.in, err, ptest.errno);
  305. continue;
  306. }
  307. if (!err && ptest.expected
  308. && !__bitmap_equal(bmap, ptest.expected, ptest.nbits)) {
  309. pr_err("parselist: %d: input is %s, result is 0x%lx, expected 0x%lx\n",
  310. i, ptest.in, bmap[0],
  311. *ptest.expected);
  312. continue;
  313. }
  314. if (ptest.flags & PARSE_TIME)
  315. pr_err("parselist: %d: input is '%s' OK, Time: %llu\n",
  316. i, ptest.in, time);
  317. #undef ptest
  318. }
  319. }
  320. static const unsigned long parse_test[] __initconst = {
  321. BITMAP_FROM_U64(0),
  322. BITMAP_FROM_U64(1),
  323. BITMAP_FROM_U64(0xdeadbeef),
  324. BITMAP_FROM_U64(0x100000000ULL),
  325. };
  326. static const unsigned long parse_test2[] __initconst = {
  327. BITMAP_FROM_U64(0x100000000ULL), BITMAP_FROM_U64(0xdeadbeef),
  328. BITMAP_FROM_U64(0x100000000ULL), BITMAP_FROM_U64(0xbaadf00ddeadbeef),
  329. BITMAP_FROM_U64(0x100000000ULL), BITMAP_FROM_U64(0x0badf00ddeadbeef),
  330. };
  331. static const struct test_bitmap_parselist parse_tests[] __initconst = {
  332. {0, "", &parse_test[0 * step], 32, 0},
  333. {0, " ", &parse_test[0 * step], 32, 0},
  334. {0, "0", &parse_test[0 * step], 32, 0},
  335. {0, "0\n", &parse_test[0 * step], 32, 0},
  336. {0, "1", &parse_test[1 * step], 32, 0},
  337. {0, "deadbeef", &parse_test[2 * step], 32, 0},
  338. {0, "1,0", &parse_test[3 * step], 33, 0},
  339. {0, "deadbeef,\n,0,1", &parse_test[2 * step], 96, 0},
  340. {0, "deadbeef,1,0", &parse_test2[0 * 2 * step], 96, 0},
  341. {0, "baadf00d,deadbeef,1,0", &parse_test2[1 * 2 * step], 128, 0},
  342. {0, "badf00d,deadbeef,1,0", &parse_test2[2 * 2 * step], 124, 0},
  343. {0, "badf00d,deadbeef,1,0", &parse_test2[2 * 2 * step], 124, NO_LEN},
  344. {0, " badf00d,deadbeef,1,0 ", &parse_test2[2 * 2 * step], 124, 0},
  345. {0, " , badf00d,deadbeef,1,0 , ", &parse_test2[2 * 2 * step], 124, 0},
  346. {0, " , badf00d, ,, ,,deadbeef,1,0 , ", &parse_test2[2 * 2 * step], 124, 0},
  347. {-EINVAL, "goodfood,deadbeef,1,0", NULL, 128, 0},
  348. {-EOVERFLOW, "3,0", NULL, 33, 0},
  349. {-EOVERFLOW, "123badf00d,deadbeef,1,0", NULL, 128, 0},
  350. {-EOVERFLOW, "badf00d,deadbeef,1,0", NULL, 90, 0},
  351. {-EOVERFLOW, "fbadf00d,deadbeef,1,0", NULL, 95, 0},
  352. {-EOVERFLOW, "badf00d,deadbeef,1,0", NULL, 100, 0},
  353. #undef step
  354. };
  355. static void __init test_bitmap_parse(void)
  356. {
  357. int i;
  358. int err;
  359. ktime_t time;
  360. DECLARE_BITMAP(bmap, 2048);
  361. for (i = 0; i < ARRAY_SIZE(parse_tests); i++) {
  362. struct test_bitmap_parselist test = parse_tests[i];
  363. size_t len = test.flags & NO_LEN ? UINT_MAX : strlen(test.in);
  364. time = ktime_get();
  365. err = bitmap_parse(test.in, len, bmap, test.nbits);
  366. time = ktime_get() - time;
  367. if (err != test.errno) {
  368. pr_err("parse: %d: input is %s, errno is %d, expected %d\n",
  369. i, test.in, err, test.errno);
  370. continue;
  371. }
  372. if (!err && test.expected
  373. && !__bitmap_equal(bmap, test.expected, test.nbits)) {
  374. pr_err("parse: %d: input is %s, result is 0x%lx, expected 0x%lx\n",
  375. i, test.in, bmap[0],
  376. *test.expected);
  377. continue;
  378. }
  379. if (test.flags & PARSE_TIME)
  380. pr_err("parse: %d: input is '%s' OK, Time: %llu\n",
  381. i, test.in, time);
  382. }
  383. }
  384. #define EXP1_IN_BITS (sizeof(exp1) * 8)
  385. static void __init test_bitmap_arr32(void)
  386. {
  387. unsigned int nbits, next_bit;
  388. u32 arr[EXP1_IN_BITS / 32];
  389. DECLARE_BITMAP(bmap2, EXP1_IN_BITS);
  390. memset(arr, 0xa5, sizeof(arr));
  391. for (nbits = 0; nbits < EXP1_IN_BITS; ++nbits) {
  392. bitmap_to_arr32(arr, exp1, nbits);
  393. bitmap_from_arr32(bmap2, arr, nbits);
  394. expect_eq_bitmap(bmap2, exp1, nbits);
  395. next_bit = find_next_bit(bmap2,
  396. round_up(nbits, BITS_PER_LONG), nbits);
  397. if (next_bit < round_up(nbits, BITS_PER_LONG))
  398. pr_err("bitmap_copy_arr32(nbits == %d:"
  399. " tail is not safely cleared: %d\n",
  400. nbits, next_bit);
  401. if (nbits < EXP1_IN_BITS - 32)
  402. expect_eq_uint(arr[DIV_ROUND_UP(nbits, 32)],
  403. 0xa5a5a5a5);
  404. }
  405. }
  406. static void noinline __init test_mem_optimisations(void)
  407. {
  408. DECLARE_BITMAP(bmap1, 1024);
  409. DECLARE_BITMAP(bmap2, 1024);
  410. unsigned int start, nbits;
  411. for (start = 0; start < 1024; start += 8) {
  412. for (nbits = 0; nbits < 1024 - start; nbits += 8) {
  413. memset(bmap1, 0x5a, sizeof(bmap1));
  414. memset(bmap2, 0x5a, sizeof(bmap2));
  415. bitmap_set(bmap1, start, nbits);
  416. __bitmap_set(bmap2, start, nbits);
  417. if (!bitmap_equal(bmap1, bmap2, 1024)) {
  418. printk("set not equal %d %d\n", start, nbits);
  419. failed_tests++;
  420. }
  421. if (!__bitmap_equal(bmap1, bmap2, 1024)) {
  422. printk("set not __equal %d %d\n", start, nbits);
  423. failed_tests++;
  424. }
  425. bitmap_clear(bmap1, start, nbits);
  426. __bitmap_clear(bmap2, start, nbits);
  427. if (!bitmap_equal(bmap1, bmap2, 1024)) {
  428. printk("clear not equal %d %d\n", start, nbits);
  429. failed_tests++;
  430. }
  431. if (!__bitmap_equal(bmap1, bmap2, 1024)) {
  432. printk("clear not __equal %d %d\n", start,
  433. nbits);
  434. failed_tests++;
  435. }
  436. }
  437. }
  438. }
  439. static const unsigned char clump_exp[] __initconst = {
  440. 0x01, /* 1 bit set */
  441. 0x02, /* non-edge 1 bit set */
  442. 0x00, /* zero bits set */
  443. 0x38, /* 3 bits set across 4-bit boundary */
  444. 0x38, /* Repeated clump */
  445. 0x0F, /* 4 bits set */
  446. 0xFF, /* all bits set */
  447. 0x05, /* non-adjacent 2 bits set */
  448. };
  449. static void __init test_for_each_set_clump8(void)
  450. {
  451. #define CLUMP_EXP_NUMBITS 64
  452. DECLARE_BITMAP(bits, CLUMP_EXP_NUMBITS);
  453. unsigned int start;
  454. unsigned long clump;
  455. /* set bitmap to test case */
  456. bitmap_zero(bits, CLUMP_EXP_NUMBITS);
  457. bitmap_set(bits, 0, 1); /* 0x01 */
  458. bitmap_set(bits, 9, 1); /* 0x02 */
  459. bitmap_set(bits, 27, 3); /* 0x28 */
  460. bitmap_set(bits, 35, 3); /* 0x28 */
  461. bitmap_set(bits, 40, 4); /* 0x0F */
  462. bitmap_set(bits, 48, 8); /* 0xFF */
  463. bitmap_set(bits, 56, 1); /* 0x05 - part 1 */
  464. bitmap_set(bits, 58, 1); /* 0x05 - part 2 */
  465. for_each_set_clump8(start, clump, bits, CLUMP_EXP_NUMBITS)
  466. expect_eq_clump8(start, CLUMP_EXP_NUMBITS, clump_exp, &clump);
  467. }
  468. struct test_bitmap_cut {
  469. unsigned int first;
  470. unsigned int cut;
  471. unsigned int nbits;
  472. unsigned long in[4];
  473. unsigned long expected[4];
  474. };
  475. static struct test_bitmap_cut test_cut[] = {
  476. { 0, 0, 8, { 0x0000000aUL, }, { 0x0000000aUL, }, },
  477. { 0, 0, 32, { 0xdadadeadUL, }, { 0xdadadeadUL, }, },
  478. { 0, 3, 8, { 0x000000aaUL, }, { 0x00000015UL, }, },
  479. { 3, 3, 8, { 0x000000aaUL, }, { 0x00000012UL, }, },
  480. { 0, 1, 32, { 0xa5a5a5a5UL, }, { 0x52d2d2d2UL, }, },
  481. { 0, 8, 32, { 0xdeadc0deUL, }, { 0x00deadc0UL, }, },
  482. { 1, 1, 32, { 0x5a5a5a5aUL, }, { 0x2d2d2d2cUL, }, },
  483. { 0, 15, 32, { 0xa5a5a5a5UL, }, { 0x00014b4bUL, }, },
  484. { 0, 16, 32, { 0xa5a5a5a5UL, }, { 0x0000a5a5UL, }, },
  485. { 15, 15, 32, { 0xa5a5a5a5UL, }, { 0x000125a5UL, }, },
  486. { 15, 16, 32, { 0xa5a5a5a5UL, }, { 0x0000a5a5UL, }, },
  487. { 16, 15, 32, { 0xa5a5a5a5UL, }, { 0x0001a5a5UL, }, },
  488. { BITS_PER_LONG, BITS_PER_LONG, BITS_PER_LONG,
  489. { 0xa5a5a5a5UL, 0xa5a5a5a5UL, },
  490. { 0xa5a5a5a5UL, 0xa5a5a5a5UL, },
  491. },
  492. { 1, BITS_PER_LONG - 1, BITS_PER_LONG,
  493. { 0xa5a5a5a5UL, 0xa5a5a5a5UL, },
  494. { 0x00000001UL, 0x00000001UL, },
  495. },
  496. { 0, BITS_PER_LONG * 2, BITS_PER_LONG * 2 + 1,
  497. { 0xa5a5a5a5UL, 0x00000001UL, 0x00000001UL, 0x00000001UL },
  498. { 0x00000001UL, },
  499. },
  500. { 16, BITS_PER_LONG * 2 + 1, BITS_PER_LONG * 2 + 1 + 16,
  501. { 0x0000ffffUL, 0x5a5a5a5aUL, 0x5a5a5a5aUL, 0x5a5a5a5aUL },
  502. { 0x2d2dffffUL, },
  503. },
  504. };
  505. static void __init test_bitmap_cut(void)
  506. {
  507. unsigned long b[5], *in = &b[1], *out = &b[0]; /* Partial overlap */
  508. int i;
  509. for (i = 0; i < ARRAY_SIZE(test_cut); i++) {
  510. struct test_bitmap_cut *t = &test_cut[i];
  511. memcpy(in, t->in, sizeof(t->in));
  512. bitmap_cut(out, in, t->first, t->cut, t->nbits);
  513. expect_eq_bitmap(t->expected, out, t->nbits);
  514. }
  515. }
  516. static void __init selftest(void)
  517. {
  518. test_zero_clear();
  519. test_fill_set();
  520. test_copy();
  521. test_replace();
  522. test_bitmap_arr32();
  523. test_bitmap_parse();
  524. test_bitmap_parselist();
  525. test_mem_optimisations();
  526. test_for_each_set_clump8();
  527. test_bitmap_cut();
  528. }
  529. KSTM_MODULE_LOADERS(test_bitmap);
  530. MODULE_AUTHOR("david decotigny <david.decotigny@googlers.com>");
  531. MODULE_LICENSE("GPL");