fb.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  1. #include "test/jemalloc_test.h"
  2. #include "jemalloc/internal/fb.h"
  3. #include "test/nbits.h"
  4. static void
  5. do_test_init(size_t nbits) {
  6. size_t sz = FB_NGROUPS(nbits) * sizeof(fb_group_t);
  7. fb_group_t *fb = malloc(sz);
  8. /* Junk fb's contents. */
  9. memset(fb, 99, sz);
  10. fb_init(fb, nbits);
  11. for (size_t i = 0; i < nbits; i++) {
  12. expect_false(fb_get(fb, nbits, i),
  13. "bitmap should start empty");
  14. }
  15. free(fb);
  16. }
  17. TEST_BEGIN(test_fb_init) {
  18. #define NB(nbits) \
  19. do_test_init(nbits);
  20. NBITS_TAB
  21. #undef NB
  22. }
  23. TEST_END
  24. static void
  25. do_test_get_set_unset(size_t nbits) {
  26. size_t sz = FB_NGROUPS(nbits) * sizeof(fb_group_t);
  27. fb_group_t *fb = malloc(sz);
  28. fb_init(fb, nbits);
  29. /* Set the bits divisible by 3. */
  30. for (size_t i = 0; i < nbits; i++) {
  31. if (i % 3 == 0) {
  32. fb_set(fb, nbits, i);
  33. }
  34. }
  35. /* Check them. */
  36. for (size_t i = 0; i < nbits; i++) {
  37. expect_b_eq(i % 3 == 0, fb_get(fb, nbits, i),
  38. "Unexpected bit at position %zu", i);
  39. }
  40. /* Unset those divisible by 5. */
  41. for (size_t i = 0; i < nbits; i++) {
  42. if (i % 5 == 0) {
  43. fb_unset(fb, nbits, i);
  44. }
  45. }
  46. /* Check them. */
  47. for (size_t i = 0; i < nbits; i++) {
  48. expect_b_eq(i % 3 == 0 && i % 5 != 0, fb_get(fb, nbits, i),
  49. "Unexpected bit at position %zu", i);
  50. }
  51. free(fb);
  52. }
  53. TEST_BEGIN(test_get_set_unset) {
  54. #define NB(nbits) \
  55. do_test_get_set_unset(nbits);
  56. NBITS_TAB
  57. #undef NB
  58. }
  59. TEST_END
  60. static ssize_t
  61. find_3_5_compute(ssize_t i, size_t nbits, bool bit, bool forward) {
  62. for(; i < (ssize_t)nbits && i >= 0; i += (forward ? 1 : -1)) {
  63. bool expected_bit = i % 3 == 0 || i % 5 == 0;
  64. if (expected_bit == bit) {
  65. return i;
  66. }
  67. }
  68. return forward ? (ssize_t)nbits : (ssize_t)-1;
  69. }
  70. static void
  71. do_test_search_simple(size_t nbits) {
  72. size_t sz = FB_NGROUPS(nbits) * sizeof(fb_group_t);
  73. fb_group_t *fb = malloc(sz);
  74. fb_init(fb, nbits);
  75. /* We pick multiples of 3 or 5. */
  76. for (size_t i = 0; i < nbits; i++) {
  77. if (i % 3 == 0) {
  78. fb_set(fb, nbits, i);
  79. }
  80. /* This tests double-setting a little, too. */
  81. if (i % 5 == 0) {
  82. fb_set(fb, nbits, i);
  83. }
  84. }
  85. for (size_t i = 0; i < nbits; i++) {
  86. size_t ffs_compute = find_3_5_compute(i, nbits, true, true);
  87. size_t ffs_search = fb_ffs(fb, nbits, i);
  88. expect_zu_eq(ffs_compute, ffs_search, "ffs mismatch at %zu", i);
  89. ssize_t fls_compute = find_3_5_compute(i, nbits, true, false);
  90. size_t fls_search = fb_fls(fb, nbits, i);
  91. expect_zu_eq(fls_compute, fls_search, "fls mismatch at %zu", i);
  92. size_t ffu_compute = find_3_5_compute(i, nbits, false, true);
  93. size_t ffu_search = fb_ffu(fb, nbits, i);
  94. expect_zu_eq(ffu_compute, ffu_search, "ffu mismatch at %zu", i);
  95. size_t flu_compute = find_3_5_compute(i, nbits, false, false);
  96. size_t flu_search = fb_flu(fb, nbits, i);
  97. expect_zu_eq(flu_compute, flu_search, "flu mismatch at %zu", i);
  98. }
  99. free(fb);
  100. }
  101. TEST_BEGIN(test_search_simple) {
  102. #define NB(nbits) \
  103. do_test_search_simple(nbits);
  104. NBITS_TAB
  105. #undef NB
  106. }
  107. TEST_END
  108. static void
  109. expect_exhaustive_results(fb_group_t *mostly_full, fb_group_t *mostly_empty,
  110. size_t nbits, size_t special_bit, size_t position) {
  111. if (position < special_bit) {
  112. expect_zu_eq(special_bit, fb_ffs(mostly_empty, nbits, position),
  113. "mismatch at %zu, %zu", position, special_bit);
  114. expect_zd_eq(-1, fb_fls(mostly_empty, nbits, position),
  115. "mismatch at %zu, %zu", position, special_bit);
  116. expect_zu_eq(position, fb_ffu(mostly_empty, nbits, position),
  117. "mismatch at %zu, %zu", position, special_bit);
  118. expect_zd_eq(position, fb_flu(mostly_empty, nbits, position),
  119. "mismatch at %zu, %zu", position, special_bit);
  120. expect_zu_eq(position, fb_ffs(mostly_full, nbits, position),
  121. "mismatch at %zu, %zu", position, special_bit);
  122. expect_zd_eq(position, fb_fls(mostly_full, nbits, position),
  123. "mismatch at %zu, %zu", position, special_bit);
  124. expect_zu_eq(special_bit, fb_ffu(mostly_full, nbits, position),
  125. "mismatch at %zu, %zu", position, special_bit);
  126. expect_zd_eq(-1, fb_flu(mostly_full, nbits, position),
  127. "mismatch at %zu, %zu", position, special_bit);
  128. } else if (position == special_bit) {
  129. expect_zu_eq(special_bit, fb_ffs(mostly_empty, nbits, position),
  130. "mismatch at %zu, %zu", position, special_bit);
  131. expect_zd_eq(special_bit, fb_fls(mostly_empty, nbits, position),
  132. "mismatch at %zu, %zu", position, special_bit);
  133. expect_zu_eq(position + 1, fb_ffu(mostly_empty, nbits, position),
  134. "mismatch at %zu, %zu", position, special_bit);
  135. expect_zd_eq(position - 1, fb_flu(mostly_empty, nbits,
  136. position), "mismatch at %zu, %zu", position, special_bit);
  137. expect_zu_eq(position + 1, fb_ffs(mostly_full, nbits, position),
  138. "mismatch at %zu, %zu", position, special_bit);
  139. expect_zd_eq(position - 1, fb_fls(mostly_full, nbits,
  140. position), "mismatch at %zu, %zu", position, special_bit);
  141. expect_zu_eq(position, fb_ffu(mostly_full, nbits, position),
  142. "mismatch at %zu, %zu", position, special_bit);
  143. expect_zd_eq(position, fb_flu(mostly_full, nbits, position),
  144. "mismatch at %zu, %zu", position, special_bit);
  145. } else {
  146. /* position > special_bit. */
  147. expect_zu_eq(nbits, fb_ffs(mostly_empty, nbits, position),
  148. "mismatch at %zu, %zu", position, special_bit);
  149. expect_zd_eq(special_bit, fb_fls(mostly_empty, nbits,
  150. position), "mismatch at %zu, %zu", position, special_bit);
  151. expect_zu_eq(position, fb_ffu(mostly_empty, nbits, position),
  152. "mismatch at %zu, %zu", position, special_bit);
  153. expect_zd_eq(position, fb_flu(mostly_empty, nbits, position),
  154. "mismatch at %zu, %zu", position, special_bit);
  155. expect_zu_eq(position, fb_ffs(mostly_full, nbits, position),
  156. "mismatch at %zu, %zu", position, special_bit);
  157. expect_zd_eq(position, fb_fls(mostly_full, nbits, position),
  158. "mismatch at %zu, %zu", position, special_bit);
  159. expect_zu_eq(nbits, fb_ffu(mostly_full, nbits, position),
  160. "mismatch at %zu, %zu", position, special_bit);
  161. expect_zd_eq(special_bit, fb_flu(mostly_full, nbits, position),
  162. "mismatch at %zu, %zu", position, special_bit);
  163. }
  164. }
  165. static void
  166. do_test_search_exhaustive(size_t nbits) {
  167. /* This test is quadratic; let's not get too big. */
  168. if (nbits > 1000) {
  169. return;
  170. }
  171. size_t sz = FB_NGROUPS(nbits) * sizeof(fb_group_t);
  172. fb_group_t *empty = malloc(sz);
  173. fb_init(empty, nbits);
  174. fb_group_t *full = malloc(sz);
  175. fb_init(full, nbits);
  176. fb_set_range(full, nbits, 0, nbits);
  177. for (size_t i = 0; i < nbits; i++) {
  178. fb_set(empty, nbits, i);
  179. fb_unset(full, nbits, i);
  180. for (size_t j = 0; j < nbits; j++) {
  181. expect_exhaustive_results(full, empty, nbits, i, j);
  182. }
  183. fb_unset(empty, nbits, i);
  184. fb_set(full, nbits, i);
  185. }
  186. free(empty);
  187. free(full);
  188. }
  189. TEST_BEGIN(test_search_exhaustive) {
  190. #define NB(nbits) \
  191. do_test_search_exhaustive(nbits);
  192. NBITS_TAB
  193. #undef NB
  194. }
  195. TEST_END
  196. TEST_BEGIN(test_range_simple) {
  197. /*
  198. * Just pick a constant big enough to have nontrivial middle sizes, and
  199. * big enough that usages of things like weirdnum (below) near the
  200. * beginning fit comfortably into the beginning of the bitmap.
  201. */
  202. size_t nbits = 64 * 10;
  203. size_t ngroups = FB_NGROUPS(nbits);
  204. fb_group_t *fb = malloc(sizeof(fb_group_t) * ngroups);
  205. fb_init(fb, nbits);
  206. for (size_t i = 0; i < nbits; i++) {
  207. if (i % 2 == 0) {
  208. fb_set_range(fb, nbits, i, 1);
  209. }
  210. }
  211. for (size_t i = 0; i < nbits; i++) {
  212. expect_b_eq(i % 2 == 0, fb_get(fb, nbits, i),
  213. "mismatch at position %zu", i);
  214. }
  215. fb_set_range(fb, nbits, 0, nbits / 2);
  216. fb_unset_range(fb, nbits, nbits / 2, nbits / 2);
  217. for (size_t i = 0; i < nbits; i++) {
  218. expect_b_eq(i < nbits / 2, fb_get(fb, nbits, i),
  219. "mismatch at position %zu", i);
  220. }
  221. static const size_t weirdnum = 7;
  222. fb_set_range(fb, nbits, 0, nbits);
  223. fb_unset_range(fb, nbits, weirdnum, FB_GROUP_BITS + weirdnum);
  224. for (size_t i = 0; i < nbits; i++) {
  225. expect_b_eq(7 <= i && i <= 2 * weirdnum + FB_GROUP_BITS - 1,
  226. !fb_get(fb, nbits, i), "mismatch at position %zu", i);
  227. }
  228. free(fb);
  229. }
  230. TEST_END
  231. static void
  232. do_test_empty_full_exhaustive(size_t nbits) {
  233. size_t sz = FB_NGROUPS(nbits) * sizeof(fb_group_t);
  234. fb_group_t *empty = malloc(sz);
  235. fb_init(empty, nbits);
  236. fb_group_t *full = malloc(sz);
  237. fb_init(full, nbits);
  238. fb_set_range(full, nbits, 0, nbits);
  239. expect_true(fb_full(full, nbits), "");
  240. expect_false(fb_empty(full, nbits), "");
  241. expect_false(fb_full(empty, nbits), "");
  242. expect_true(fb_empty(empty, nbits), "");
  243. for (size_t i = 0; i < nbits; i++) {
  244. fb_set(empty, nbits, i);
  245. fb_unset(full, nbits, i);
  246. expect_false(fb_empty(empty, nbits), "error at bit %zu", i);
  247. if (nbits != 1) {
  248. expect_false(fb_full(empty, nbits),
  249. "error at bit %zu", i);
  250. expect_false(fb_empty(full, nbits),
  251. "error at bit %zu", i);
  252. } else {
  253. expect_true(fb_full(empty, nbits),
  254. "error at bit %zu", i);
  255. expect_true(fb_empty(full, nbits),
  256. "error at bit %zu", i);
  257. }
  258. expect_false(fb_full(full, nbits), "error at bit %zu", i);
  259. fb_unset(empty, nbits, i);
  260. fb_set(full, nbits, i);
  261. }
  262. free(empty);
  263. free(full);
  264. }
  265. TEST_BEGIN(test_empty_full) {
  266. #define NB(nbits) \
  267. do_test_empty_full_exhaustive(nbits);
  268. NBITS_TAB
  269. #undef NB
  270. }
  271. TEST_END
  272. /*
  273. * This tests both iter_range and the longest range functionality, which is
  274. * built closely on top of it.
  275. */
  276. TEST_BEGIN(test_iter_range_simple) {
  277. size_t set_limit = 30;
  278. size_t nbits = 100;
  279. fb_group_t fb[FB_NGROUPS(100)];
  280. fb_init(fb, nbits);
  281. /*
  282. * Failing to initialize these can lead to build failures with -Wall;
  283. * the compiler can't prove that they're set.
  284. */
  285. size_t begin = (size_t)-1;
  286. size_t len = (size_t)-1;
  287. bool result;
  288. /* A set of checks with only the first set_limit bits *set*. */
  289. fb_set_range(fb, nbits, 0, set_limit);
  290. expect_zu_eq(set_limit, fb_srange_longest(fb, nbits),
  291. "Incorrect longest set range");
  292. expect_zu_eq(nbits - set_limit, fb_urange_longest(fb, nbits),
  293. "Incorrect longest unset range");
  294. for (size_t i = 0; i < set_limit; i++) {
  295. result = fb_srange_iter(fb, nbits, i, &begin, &len);
  296. expect_true(result, "Should have found a range at %zu", i);
  297. expect_zu_eq(i, begin, "Incorrect begin at %zu", i);
  298. expect_zu_eq(set_limit - i, len, "Incorrect len at %zu", i);
  299. result = fb_urange_iter(fb, nbits, i, &begin, &len);
  300. expect_true(result, "Should have found a range at %zu", i);
  301. expect_zu_eq(set_limit, begin, "Incorrect begin at %zu", i);
  302. expect_zu_eq(nbits - set_limit, len, "Incorrect len at %zu", i);
  303. result = fb_srange_riter(fb, nbits, i, &begin, &len);
  304. expect_true(result, "Should have found a range at %zu", i);
  305. expect_zu_eq(0, begin, "Incorrect begin at %zu", i);
  306. expect_zu_eq(i + 1, len, "Incorrect len at %zu", i);
  307. result = fb_urange_riter(fb, nbits, i, &begin, &len);
  308. expect_false(result, "Should not have found a range at %zu", i);
  309. }
  310. for (size_t i = set_limit; i < nbits; i++) {
  311. result = fb_srange_iter(fb, nbits, i, &begin, &len);
  312. expect_false(result, "Should not have found a range at %zu", i);
  313. result = fb_urange_iter(fb, nbits, i, &begin, &len);
  314. expect_true(result, "Should have found a range at %zu", i);
  315. expect_zu_eq(i, begin, "Incorrect begin at %zu", i);
  316. expect_zu_eq(nbits - i, len, "Incorrect len at %zu", i);
  317. result = fb_srange_riter(fb, nbits, i, &begin, &len);
  318. expect_true(result, "Should have found a range at %zu", i);
  319. expect_zu_eq(0, begin, "Incorrect begin at %zu", i);
  320. expect_zu_eq(set_limit, len, "Incorrect len at %zu", i);
  321. result = fb_urange_riter(fb, nbits, i, &begin, &len);
  322. expect_true(result, "Should have found a range at %zu", i);
  323. expect_zu_eq(set_limit, begin, "Incorrect begin at %zu", i);
  324. expect_zu_eq(i - set_limit + 1, len, "Incorrect len at %zu", i);
  325. }
  326. /* A set of checks with only the first set_limit bits *unset*. */
  327. fb_unset_range(fb, nbits, 0, set_limit);
  328. fb_set_range(fb, nbits, set_limit, nbits - set_limit);
  329. expect_zu_eq(nbits - set_limit, fb_srange_longest(fb, nbits),
  330. "Incorrect longest set range");
  331. expect_zu_eq(set_limit, fb_urange_longest(fb, nbits),
  332. "Incorrect longest unset range");
  333. for (size_t i = 0; i < set_limit; i++) {
  334. result = fb_srange_iter(fb, nbits, i, &begin, &len);
  335. expect_true(result, "Should have found a range at %zu", i);
  336. expect_zu_eq(set_limit, begin, "Incorrect begin at %zu", i);
  337. expect_zu_eq(nbits - set_limit, len, "Incorrect len at %zu", i);
  338. result = fb_urange_iter(fb, nbits, i, &begin, &len);
  339. expect_true(result, "Should have found a range at %zu", i);
  340. expect_zu_eq(i, begin, "Incorrect begin at %zu", i);
  341. expect_zu_eq(set_limit - i, len, "Incorrect len at %zu", i);
  342. result = fb_srange_riter(fb, nbits, i, &begin, &len);
  343. expect_false(result, "Should not have found a range at %zu", i);
  344. result = fb_urange_riter(fb, nbits, i, &begin, &len);
  345. expect_true(result, "Should not have found a range at %zu", i);
  346. expect_zu_eq(0, begin, "Incorrect begin at %zu", i);
  347. expect_zu_eq(i + 1, len, "Incorrect len at %zu", i);
  348. }
  349. for (size_t i = set_limit; i < nbits; i++) {
  350. result = fb_srange_iter(fb, nbits, i, &begin, &len);
  351. expect_true(result, "Should have found a range at %zu", i);
  352. expect_zu_eq(i, begin, "Incorrect begin at %zu", i);
  353. expect_zu_eq(nbits - i, len, "Incorrect len at %zu", i);
  354. result = fb_urange_iter(fb, nbits, i, &begin, &len);
  355. expect_false(result, "Should not have found a range at %zu", i);
  356. result = fb_srange_riter(fb, nbits, i, &begin, &len);
  357. expect_true(result, "Should have found a range at %zu", i);
  358. expect_zu_eq(set_limit, begin, "Incorrect begin at %zu", i);
  359. expect_zu_eq(i - set_limit + 1, len, "Incorrect len at %zu", i);
  360. result = fb_urange_riter(fb, nbits, i, &begin, &len);
  361. expect_true(result, "Should have found a range at %zu", i);
  362. expect_zu_eq(0, begin, "Incorrect begin at %zu", i);
  363. expect_zu_eq(set_limit, len, "Incorrect len at %zu", i);
  364. }
  365. }
  366. TEST_END
  367. /*
  368. * Doing this bit-by-bit is too slow for a real implementation, but for testing
  369. * code, it's easy to get right. In the exhaustive tests, we'll compare the
  370. * (fast but tricky) real implementation against the (slow but simple) testing
  371. * one.
  372. */
  373. static bool
  374. fb_iter_simple(fb_group_t *fb, size_t nbits, size_t start, size_t *r_begin,
  375. size_t *r_len, bool val, bool forward) {
  376. ssize_t stride = (forward ? (ssize_t)1 : (ssize_t)-1);
  377. ssize_t range_begin = (ssize_t)start;
  378. for (; range_begin != (ssize_t)nbits && range_begin != -1;
  379. range_begin += stride) {
  380. if (fb_get(fb, nbits, range_begin) == val) {
  381. ssize_t range_end = range_begin;
  382. for (; range_end != (ssize_t)nbits && range_end != -1;
  383. range_end += stride) {
  384. if (fb_get(fb, nbits, range_end) != val) {
  385. break;
  386. }
  387. }
  388. if (forward) {
  389. *r_begin = range_begin;
  390. *r_len = range_end - range_begin;
  391. } else {
  392. *r_begin = range_end + 1;
  393. *r_len = range_begin - range_end;
  394. }
  395. return true;
  396. }
  397. }
  398. return false;
  399. }
  400. /* Similar, but for finding longest ranges. */
  401. static size_t
  402. fb_range_longest_simple(fb_group_t *fb, size_t nbits, bool val) {
  403. size_t longest_so_far = 0;
  404. for (size_t begin = 0; begin < nbits; begin++) {
  405. if (fb_get(fb, nbits, begin) != val) {
  406. continue;
  407. }
  408. size_t end = begin + 1;
  409. for (; end < nbits; end++) {
  410. if (fb_get(fb, nbits, end) != val) {
  411. break;
  412. }
  413. }
  414. if (end - begin > longest_so_far) {
  415. longest_so_far = end - begin;
  416. }
  417. }
  418. return longest_so_far;
  419. }
  420. static void
  421. expect_iter_results_at(fb_group_t *fb, size_t nbits, size_t pos,
  422. bool val, bool forward) {
  423. bool iter_res;
  424. size_t iter_begin JEMALLOC_CC_SILENCE_INIT(0);
  425. size_t iter_len JEMALLOC_CC_SILENCE_INIT(0);
  426. if (val) {
  427. if (forward) {
  428. iter_res = fb_srange_iter(fb, nbits, pos,
  429. &iter_begin, &iter_len);
  430. } else {
  431. iter_res = fb_srange_riter(fb, nbits, pos,
  432. &iter_begin, &iter_len);
  433. }
  434. } else {
  435. if (forward) {
  436. iter_res = fb_urange_iter(fb, nbits, pos,
  437. &iter_begin, &iter_len);
  438. } else {
  439. iter_res = fb_urange_riter(fb, nbits, pos,
  440. &iter_begin, &iter_len);
  441. }
  442. }
  443. bool simple_iter_res;
  444. /*
  445. * These are dead stores, but the compiler can't always figure that out
  446. * statically, and warns on the uninitialized variable.
  447. */
  448. size_t simple_iter_begin = 0;
  449. size_t simple_iter_len = 0;
  450. simple_iter_res = fb_iter_simple(fb, nbits, pos, &simple_iter_begin,
  451. &simple_iter_len, val, forward);
  452. expect_b_eq(iter_res, simple_iter_res, "Result mismatch at %zu", pos);
  453. if (iter_res && simple_iter_res) {
  454. assert_zu_eq(iter_begin, simple_iter_begin,
  455. "Begin mismatch at %zu", pos);
  456. expect_zu_eq(iter_len, simple_iter_len,
  457. "Length mismatch at %zu", pos);
  458. }
  459. }
  460. static void
  461. expect_iter_results(fb_group_t *fb, size_t nbits) {
  462. for (size_t i = 0; i < nbits; i++) {
  463. expect_iter_results_at(fb, nbits, i, false, false);
  464. expect_iter_results_at(fb, nbits, i, false, true);
  465. expect_iter_results_at(fb, nbits, i, true, false);
  466. expect_iter_results_at(fb, nbits, i, true, true);
  467. }
  468. expect_zu_eq(fb_range_longest_simple(fb, nbits, true),
  469. fb_srange_longest(fb, nbits), "Longest range mismatch");
  470. expect_zu_eq(fb_range_longest_simple(fb, nbits, false),
  471. fb_urange_longest(fb, nbits), "Longest range mismatch");
  472. }
  473. static void
  474. set_pattern_3(fb_group_t *fb, size_t nbits, bool zero_val) {
  475. for (size_t i = 0; i < nbits; i++) {
  476. if ((i % 6 < 3 && zero_val) || (i % 6 >= 3 && !zero_val)) {
  477. fb_set(fb, nbits, i);
  478. } else {
  479. fb_unset(fb, nbits, i);
  480. }
  481. }
  482. }
  483. static void
  484. do_test_iter_range_exhaustive(size_t nbits) {
  485. /* This test is also pretty slow. */
  486. if (nbits > 1000) {
  487. return;
  488. }
  489. size_t sz = FB_NGROUPS(nbits) * sizeof(fb_group_t);
  490. fb_group_t *fb = malloc(sz);
  491. fb_init(fb, nbits);
  492. set_pattern_3(fb, nbits, /* zero_val */ true);
  493. expect_iter_results(fb, nbits);
  494. set_pattern_3(fb, nbits, /* zero_val */ false);
  495. expect_iter_results(fb, nbits);
  496. fb_set_range(fb, nbits, 0, nbits);
  497. fb_unset_range(fb, nbits, 0, nbits / 2 == 0 ? 1 : nbits / 2);
  498. expect_iter_results(fb, nbits);
  499. fb_unset_range(fb, nbits, 0, nbits);
  500. fb_set_range(fb, nbits, 0, nbits / 2 == 0 ? 1: nbits / 2);
  501. expect_iter_results(fb, nbits);
  502. free(fb);
  503. }
  504. /*
  505. * Like test_iter_range_simple, this tests both iteration and longest-range
  506. * computation.
  507. */
  508. TEST_BEGIN(test_iter_range_exhaustive) {
  509. #define NB(nbits) \
  510. do_test_iter_range_exhaustive(nbits);
  511. NBITS_TAB
  512. #undef NB
  513. }
  514. TEST_END
  515. /*
  516. * If all set bits in the bitmap are contiguous, in [set_start, set_end),
  517. * returns the number of set bits in [scount_start, scount_end).
  518. */
  519. static size_t
  520. scount_contiguous(size_t set_start, size_t set_end, size_t scount_start,
  521. size_t scount_end) {
  522. /* No overlap. */
  523. if (set_end <= scount_start || scount_end <= set_start) {
  524. return 0;
  525. }
  526. /* set range contains scount range */
  527. if (set_start <= scount_start && set_end >= scount_end) {
  528. return scount_end - scount_start;
  529. }
  530. /* scount range contains set range. */
  531. if (scount_start <= set_start && scount_end >= set_end) {
  532. return set_end - set_start;
  533. }
  534. /* Partial overlap, with set range starting first. */
  535. if (set_start < scount_start && set_end < scount_end) {
  536. return set_end - scount_start;
  537. }
  538. /* Partial overlap, with scount range starting first. */
  539. if (scount_start < set_start && scount_end < set_end) {
  540. return scount_end - set_start;
  541. }
  542. /*
  543. * Trigger an assert failure; the above list should have been
  544. * exhaustive.
  545. */
  546. unreachable();
  547. }
  548. static size_t
  549. ucount_contiguous(size_t set_start, size_t set_end, size_t ucount_start,
  550. size_t ucount_end) {
  551. /* No overlap. */
  552. if (set_end <= ucount_start || ucount_end <= set_start) {
  553. return ucount_end - ucount_start;
  554. }
  555. /* set range contains ucount range */
  556. if (set_start <= ucount_start && set_end >= ucount_end) {
  557. return 0;
  558. }
  559. /* ucount range contains set range. */
  560. if (ucount_start <= set_start && ucount_end >= set_end) {
  561. return (ucount_end - ucount_start) - (set_end - set_start);
  562. }
  563. /* Partial overlap, with set range starting first. */
  564. if (set_start < ucount_start && set_end < ucount_end) {
  565. return ucount_end - set_end;
  566. }
  567. /* Partial overlap, with ucount range starting first. */
  568. if (ucount_start < set_start && ucount_end < set_end) {
  569. return set_start - ucount_start;
  570. }
  571. /*
  572. * Trigger an assert failure; the above list should have been
  573. * exhaustive.
  574. */
  575. unreachable();
  576. }
  577. static void
  578. expect_count_match_contiguous(fb_group_t *fb, size_t nbits, size_t set_start,
  579. size_t set_end) {
  580. for (size_t i = 0; i < nbits; i++) {
  581. for (size_t j = i + 1; j <= nbits; j++) {
  582. size_t cnt = j - i;
  583. size_t scount_expected = scount_contiguous(set_start,
  584. set_end, i, j);
  585. size_t scount_computed = fb_scount(fb, nbits, i, cnt);
  586. expect_zu_eq(scount_expected, scount_computed,
  587. "fb_scount error with nbits=%zu, start=%zu, "
  588. "cnt=%zu, with bits set in [%zu, %zu)",
  589. nbits, i, cnt, set_start, set_end);
  590. size_t ucount_expected = ucount_contiguous(set_start,
  591. set_end, i, j);
  592. size_t ucount_computed = fb_ucount(fb, nbits, i, cnt);
  593. assert_zu_eq(ucount_expected, ucount_computed,
  594. "fb_ucount error with nbits=%zu, start=%zu, "
  595. "cnt=%zu, with bits set in [%zu, %zu)",
  596. nbits, i, cnt, set_start, set_end);
  597. }
  598. }
  599. }
  600. static void
  601. do_test_count_contiguous(size_t nbits) {
  602. size_t sz = FB_NGROUPS(nbits) * sizeof(fb_group_t);
  603. fb_group_t *fb = malloc(sz);
  604. fb_init(fb, nbits);
  605. expect_count_match_contiguous(fb, nbits, 0, 0);
  606. for (size_t i = 0; i < nbits; i++) {
  607. fb_set(fb, nbits, i);
  608. expect_count_match_contiguous(fb, nbits, 0, i + 1);
  609. }
  610. for (size_t i = 0; i < nbits; i++) {
  611. fb_unset(fb, nbits, i);
  612. expect_count_match_contiguous(fb, nbits, i + 1, nbits);
  613. }
  614. free(fb);
  615. }
  616. TEST_BEGIN(test_count_contiguous_simple) {
  617. enum {nbits = 300};
  618. fb_group_t fb[FB_NGROUPS(nbits)];
  619. fb_init(fb, nbits);
  620. /* Just an arbitrary number. */
  621. size_t start = 23;
  622. fb_set_range(fb, nbits, start, 30 - start);
  623. expect_count_match_contiguous(fb, nbits, start, 30);
  624. fb_set_range(fb, nbits, start, 40 - start);
  625. expect_count_match_contiguous(fb, nbits, start, 40);
  626. fb_set_range(fb, nbits, start, 70 - start);
  627. expect_count_match_contiguous(fb, nbits, start, 70);
  628. fb_set_range(fb, nbits, start, 120 - start);
  629. expect_count_match_contiguous(fb, nbits, start, 120);
  630. fb_set_range(fb, nbits, start, 150 - start);
  631. expect_count_match_contiguous(fb, nbits, start, 150);
  632. fb_set_range(fb, nbits, start, 200 - start);
  633. expect_count_match_contiguous(fb, nbits, start, 200);
  634. fb_set_range(fb, nbits, start, 290 - start);
  635. expect_count_match_contiguous(fb, nbits, start, 290);
  636. }
  637. TEST_END
  638. TEST_BEGIN(test_count_contiguous) {
  639. #define NB(nbits) \
  640. /* This test is *particularly* slow in debug builds. */ \
  641. if ((!config_debug && nbits < 300) || nbits < 150) { \
  642. do_test_count_contiguous(nbits); \
  643. }
  644. NBITS_TAB
  645. #undef NB
  646. }
  647. TEST_END
  648. static void
  649. expect_count_match_alternating(fb_group_t *fb_even, fb_group_t *fb_odd,
  650. size_t nbits) {
  651. for (size_t i = 0; i < nbits; i++) {
  652. for (size_t j = i + 1; j <= nbits; j++) {
  653. size_t cnt = j - i;
  654. size_t odd_scount = cnt / 2
  655. + (size_t)(cnt % 2 == 1 && i % 2 == 1);
  656. size_t odd_scount_computed = fb_scount(fb_odd, nbits,
  657. i, j - i);
  658. assert_zu_eq(odd_scount, odd_scount_computed,
  659. "fb_scount error with nbits=%zu, start=%zu, "
  660. "cnt=%zu, with alternating bits set.",
  661. nbits, i, j - i);
  662. size_t odd_ucount = cnt / 2
  663. + (size_t)(cnt % 2 == 1 && i % 2 == 0);
  664. size_t odd_ucount_computed = fb_ucount(fb_odd, nbits,
  665. i, j - i);
  666. assert_zu_eq(odd_ucount, odd_ucount_computed,
  667. "fb_ucount error with nbits=%zu, start=%zu, "
  668. "cnt=%zu, with alternating bits set.",
  669. nbits, i, j - i);
  670. size_t even_scount = cnt / 2
  671. + (size_t)(cnt % 2 == 1 && i % 2 == 0);
  672. size_t even_scount_computed = fb_scount(fb_even, nbits,
  673. i, j - i);
  674. assert_zu_eq(even_scount, even_scount_computed,
  675. "fb_scount error with nbits=%zu, start=%zu, "
  676. "cnt=%zu, with alternating bits set.",
  677. nbits, i, j - i);
  678. size_t even_ucount = cnt / 2
  679. + (size_t)(cnt % 2 == 1 && i % 2 == 1);
  680. size_t even_ucount_computed = fb_ucount(fb_even, nbits,
  681. i, j - i);
  682. assert_zu_eq(even_ucount, even_ucount_computed,
  683. "fb_ucount error with nbits=%zu, start=%zu, "
  684. "cnt=%zu, with alternating bits set.",
  685. nbits, i, j - i);
  686. }
  687. }
  688. }
  689. static void
  690. do_test_count_alternating(size_t nbits) {
  691. if (nbits > 1000) {
  692. return;
  693. }
  694. size_t sz = FB_NGROUPS(nbits) * sizeof(fb_group_t);
  695. fb_group_t *fb_even = malloc(sz);
  696. fb_group_t *fb_odd = malloc(sz);
  697. fb_init(fb_even, nbits);
  698. fb_init(fb_odd, nbits);
  699. for (size_t i = 0; i < nbits; i++) {
  700. if (i % 2 == 0) {
  701. fb_set(fb_even, nbits, i);
  702. } else {
  703. fb_set(fb_odd, nbits, i);
  704. }
  705. }
  706. expect_count_match_alternating(fb_even, fb_odd, nbits);
  707. free(fb_even);
  708. free(fb_odd);
  709. }
  710. TEST_BEGIN(test_count_alternating) {
  711. #define NB(nbits) \
  712. do_test_count_alternating(nbits);
  713. NBITS_TAB
  714. #undef NB
  715. }
  716. TEST_END
  717. static void
  718. do_test_bit_op(size_t nbits, bool (*op)(bool a, bool b),
  719. void (*fb_op)(fb_group_t *dst, fb_group_t *src1, fb_group_t *src2, size_t nbits)) {
  720. size_t sz = FB_NGROUPS(nbits) * sizeof(fb_group_t);
  721. fb_group_t *fb1 = malloc(sz);
  722. fb_group_t *fb2 = malloc(sz);
  723. fb_group_t *fb_result = malloc(sz);
  724. fb_init(fb1, nbits);
  725. fb_init(fb2, nbits);
  726. fb_init(fb_result, nbits);
  727. /* Just two random numbers. */
  728. const uint64_t prng_init1 = (uint64_t)0X4E9A9DE6A35691CDULL;
  729. const uint64_t prng_init2 = (uint64_t)0X7856E396B063C36EULL;
  730. uint64_t prng1 = prng_init1;
  731. uint64_t prng2 = prng_init2;
  732. for (size_t i = 0; i < nbits; i++) {
  733. bool bit1 = ((prng1 & (1ULL << (i % 64))) != 0);
  734. bool bit2 = ((prng2 & (1ULL << (i % 64))) != 0);
  735. if (bit1) {
  736. fb_set(fb1, nbits, i);
  737. }
  738. if (bit2) {
  739. fb_set(fb2, nbits, i);
  740. }
  741. if (i % 64 == 0) {
  742. prng1 = prng_state_next_u64(prng1);
  743. prng2 = prng_state_next_u64(prng2);
  744. }
  745. }
  746. fb_op(fb_result, fb1, fb2, nbits);
  747. /* Reset the prngs to replay them. */
  748. prng1 = prng_init1;
  749. prng2 = prng_init2;
  750. for (size_t i = 0; i < nbits; i++) {
  751. bool bit1 = ((prng1 & (1ULL << (i % 64))) != 0);
  752. bool bit2 = ((prng2 & (1ULL << (i % 64))) != 0);
  753. /* Original bitmaps shouldn't change. */
  754. expect_b_eq(bit1, fb_get(fb1, nbits, i), "difference at bit %zu", i);
  755. expect_b_eq(bit2, fb_get(fb2, nbits, i), "difference at bit %zu", i);
  756. /* New one should be bitwise and. */
  757. expect_b_eq(op(bit1, bit2), fb_get(fb_result, nbits, i),
  758. "difference at bit %zu", i);
  759. /* Update the same way we did last time. */
  760. if (i % 64 == 0) {
  761. prng1 = prng_state_next_u64(prng1);
  762. prng2 = prng_state_next_u64(prng2);
  763. }
  764. }
  765. free(fb1);
  766. free(fb2);
  767. free(fb_result);
  768. }
  769. static bool
  770. binary_and(bool a, bool b) {
  771. return a & b;
  772. }
  773. static void
  774. do_test_bit_and(size_t nbits) {
  775. do_test_bit_op(nbits, &binary_and, &fb_bit_and);
  776. }
  777. TEST_BEGIN(test_bit_and) {
  778. #define NB(nbits) \
  779. do_test_bit_and(nbits);
  780. NBITS_TAB
  781. #undef NB
  782. }
  783. TEST_END
  784. static bool
  785. binary_or(bool a, bool b) {
  786. return a | b;
  787. }
  788. static void
  789. do_test_bit_or(size_t nbits) {
  790. do_test_bit_op(nbits, &binary_or, &fb_bit_or);
  791. }
  792. TEST_BEGIN(test_bit_or) {
  793. #define NB(nbits) \
  794. do_test_bit_or(nbits);
  795. NBITS_TAB
  796. #undef NB
  797. }
  798. TEST_END
  799. static bool
  800. binary_not(bool a, bool b) {
  801. (void)b;
  802. return !a;
  803. }
  804. static void
  805. fb_bit_not_shim(fb_group_t *dst, fb_group_t *src1, fb_group_t *src2,
  806. size_t nbits) {
  807. (void)src2;
  808. fb_bit_not(dst, src1, nbits);
  809. }
  810. static void
  811. do_test_bit_not(size_t nbits) {
  812. do_test_bit_op(nbits, &binary_not, &fb_bit_not_shim);
  813. }
  814. TEST_BEGIN(test_bit_not) {
  815. #define NB(nbits) \
  816. do_test_bit_not(nbits);
  817. NBITS_TAB
  818. #undef NB
  819. }
  820. TEST_END
  821. int
  822. main(void) {
  823. return test_no_reentrancy(
  824. test_fb_init,
  825. test_get_set_unset,
  826. test_search_simple,
  827. test_search_exhaustive,
  828. test_range_simple,
  829. test_empty_full,
  830. test_iter_range_simple,
  831. test_iter_range_exhaustive,
  832. test_count_contiguous_simple,
  833. test_count_contiguous,
  834. test_count_alternating,
  835. test_bit_and,
  836. test_bit_or,
  837. test_bit_not);
  838. }