test_bitmap.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. static void __init test_bitmap_u32_array_conversions(void)
  136. {
  137. DECLARE_BITMAP(bmap1, 1024);
  138. DECLARE_BITMAP(bmap2, 1024);
  139. u32 exp_arr[32], arr[32];
  140. unsigned nbits;
  141. for (nbits = 0 ; nbits < 257 ; ++nbits) {
  142. const unsigned int used_u32s = DIV_ROUND_UP(nbits, 32);
  143. unsigned int i, rv;
  144. bitmap_zero(bmap1, nbits);
  145. bitmap_set(bmap1, nbits, 1024 - nbits); /* garbage */
  146. memset(arr, 0xff, sizeof(arr));
  147. rv = bitmap_to_u32array(arr, used_u32s, bmap1, nbits);
  148. expect_eq_uint(nbits, rv);
  149. memset(exp_arr, 0xff, sizeof(exp_arr));
  150. memset(exp_arr, 0, used_u32s*sizeof(*exp_arr));
  151. expect_eq_u32_array(exp_arr, 32, arr, 32);
  152. bitmap_fill(bmap2, 1024);
  153. rv = bitmap_from_u32array(bmap2, nbits, arr, used_u32s);
  154. expect_eq_uint(nbits, rv);
  155. expect_eq_bitmap(bmap1, 1024, bmap2, 1024);
  156. for (i = 0 ; i < nbits ; ++i) {
  157. /*
  158. * test conversion bitmap -> u32[]
  159. */
  160. bitmap_zero(bmap1, 1024);
  161. __set_bit(i, bmap1);
  162. bitmap_set(bmap1, nbits, 1024 - nbits); /* garbage */
  163. memset(arr, 0xff, sizeof(arr));
  164. rv = bitmap_to_u32array(arr, used_u32s, bmap1, nbits);
  165. expect_eq_uint(nbits, rv);
  166. /* 1st used u32 words contain expected bit set, the
  167. * remaining words are left unchanged (0xff)
  168. */
  169. memset(exp_arr, 0xff, sizeof(exp_arr));
  170. memset(exp_arr, 0, used_u32s*sizeof(*exp_arr));
  171. exp_arr[i/32] = (1U<<(i%32));
  172. expect_eq_u32_array(exp_arr, 32, arr, 32);
  173. /* same, with longer array to fill
  174. */
  175. memset(arr, 0xff, sizeof(arr));
  176. rv = bitmap_to_u32array(arr, 32, bmap1, nbits);
  177. expect_eq_uint(nbits, rv);
  178. /* 1st used u32 words contain expected bit set, the
  179. * remaining words are all 0s
  180. */
  181. memset(exp_arr, 0, sizeof(exp_arr));
  182. exp_arr[i/32] = (1U<<(i%32));
  183. expect_eq_u32_array(exp_arr, 32, arr, 32);
  184. /*
  185. * test conversion u32[] -> bitmap
  186. */
  187. /* the 1st nbits of bmap2 are identical to
  188. * bmap1, the remaining bits of bmap2 are left
  189. * unchanged (all 1s)
  190. */
  191. bitmap_fill(bmap2, 1024);
  192. rv = bitmap_from_u32array(bmap2, nbits,
  193. exp_arr, used_u32s);
  194. expect_eq_uint(nbits, rv);
  195. expect_eq_bitmap(bmap1, 1024, bmap2, 1024);
  196. /* same, with more bits to fill
  197. */
  198. memset(arr, 0xff, sizeof(arr)); /* garbage */
  199. memset(arr, 0, used_u32s*sizeof(u32));
  200. arr[i/32] = (1U<<(i%32));
  201. bitmap_fill(bmap2, 1024);
  202. rv = bitmap_from_u32array(bmap2, 1024, arr, used_u32s);
  203. expect_eq_uint(used_u32s*32, rv);
  204. /* the 1st nbits of bmap2 are identical to
  205. * bmap1, the remaining bits of bmap2 are cleared
  206. */
  207. bitmap_zero(bmap1, 1024);
  208. __set_bit(i, bmap1);
  209. expect_eq_bitmap(bmap1, 1024, bmap2, 1024);
  210. /*
  211. * test short conversion bitmap -> u32[] (1
  212. * word too short)
  213. */
  214. if (used_u32s > 1) {
  215. bitmap_zero(bmap1, 1024);
  216. __set_bit(i, bmap1);
  217. bitmap_set(bmap1, nbits,
  218. 1024 - nbits); /* garbage */
  219. memset(arr, 0xff, sizeof(arr));
  220. rv = bitmap_to_u32array(arr, used_u32s - 1,
  221. bmap1, nbits);
  222. expect_eq_uint((used_u32s - 1)*32, rv);
  223. /* 1st used u32 words contain expected
  224. * bit set, the remaining words are
  225. * left unchanged (0xff)
  226. */
  227. memset(exp_arr, 0xff, sizeof(exp_arr));
  228. memset(exp_arr, 0,
  229. (used_u32s-1)*sizeof(*exp_arr));
  230. if ((i/32) < (used_u32s - 1))
  231. exp_arr[i/32] = (1U<<(i%32));
  232. expect_eq_u32_array(exp_arr, 32, arr, 32);
  233. }
  234. /*
  235. * test short conversion u32[] -> bitmap (3
  236. * bits too short)
  237. */
  238. if (nbits > 3) {
  239. memset(arr, 0xff, sizeof(arr)); /* garbage */
  240. memset(arr, 0, used_u32s*sizeof(*arr));
  241. arr[i/32] = (1U<<(i%32));
  242. bitmap_zero(bmap1, 1024);
  243. rv = bitmap_from_u32array(bmap1, nbits - 3,
  244. arr, used_u32s);
  245. expect_eq_uint(nbits - 3, rv);
  246. /* we are expecting the bit < nbits -
  247. * 3 (none otherwise), and the rest of
  248. * bmap1 unchanged (0-filled)
  249. */
  250. bitmap_zero(bmap2, 1024);
  251. if (i < nbits - 3)
  252. __set_bit(i, bmap2);
  253. expect_eq_bitmap(bmap2, 1024, bmap1, 1024);
  254. /* do the same with bmap1 initially
  255. * 1-filled
  256. */
  257. bitmap_fill(bmap1, 1024);
  258. rv = bitmap_from_u32array(bmap1, nbits - 3,
  259. arr, used_u32s);
  260. expect_eq_uint(nbits - 3, rv);
  261. /* we are expecting the bit < nbits -
  262. * 3 (none otherwise), and the rest of
  263. * bmap1 unchanged (1-filled)
  264. */
  265. bitmap_zero(bmap2, 1024);
  266. if (i < nbits - 3)
  267. __set_bit(i, bmap2);
  268. bitmap_set(bmap2, nbits-3, 1024 - nbits + 3);
  269. expect_eq_bitmap(bmap2, 1024, bmap1, 1024);
  270. }
  271. }
  272. }
  273. }
  274. static int __init test_bitmap_init(void)
  275. {
  276. test_zero_fill_copy();
  277. test_bitmap_u32_array_conversions();
  278. if (failed_tests == 0)
  279. pr_info("all %u tests passed\n", total_tests);
  280. else
  281. pr_warn("failed %u out of %u tests\n",
  282. failed_tests, total_tests);
  283. return failed_tests ? -EINVAL : 0;
  284. }
  285. static void __exit test_bitmap_cleanup(void)
  286. {
  287. }
  288. module_init(test_bitmap_init);
  289. module_exit(test_bitmap_cleanup);
  290. MODULE_AUTHOR("david decotigny <david.decotigny@googlers.com>");
  291. MODULE_LICENSE("GPL");