2
0

bitmap.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #include "test/jemalloc_test.h"
  2. #if (LG_BITMAP_MAXBITS > 12)
  3. # define MAXBITS 4500
  4. #else
  5. # define MAXBITS (1U << LG_BITMAP_MAXBITS)
  6. #endif
  7. TEST_BEGIN(test_bitmap_size)
  8. {
  9. size_t i, prev_size;
  10. prev_size = 0;
  11. for (i = 1; i <= MAXBITS; i++) {
  12. size_t size = bitmap_size(i);
  13. assert_true(size >= prev_size,
  14. "Bitmap size is smaller than expected");
  15. prev_size = size;
  16. }
  17. }
  18. TEST_END
  19. TEST_BEGIN(test_bitmap_init)
  20. {
  21. size_t i;
  22. for (i = 1; i <= MAXBITS; i++) {
  23. bitmap_info_t binfo;
  24. bitmap_info_init(&binfo, i);
  25. {
  26. size_t j;
  27. bitmap_t *bitmap = malloc(sizeof(bitmap_t) *
  28. bitmap_info_ngroups(&binfo));
  29. bitmap_init(bitmap, &binfo);
  30. for (j = 0; j < i; j++) {
  31. assert_false(bitmap_get(bitmap, &binfo, j),
  32. "Bit should be unset");
  33. }
  34. free(bitmap);
  35. }
  36. }
  37. }
  38. TEST_END
  39. TEST_BEGIN(test_bitmap_set)
  40. {
  41. size_t i;
  42. for (i = 1; i <= MAXBITS; i++) {
  43. bitmap_info_t binfo;
  44. bitmap_info_init(&binfo, i);
  45. {
  46. size_t j;
  47. bitmap_t *bitmap = malloc(sizeof(bitmap_t) *
  48. bitmap_info_ngroups(&binfo));
  49. bitmap_init(bitmap, &binfo);
  50. for (j = 0; j < i; j++)
  51. bitmap_set(bitmap, &binfo, j);
  52. assert_true(bitmap_full(bitmap, &binfo),
  53. "All bits should be set");
  54. free(bitmap);
  55. }
  56. }
  57. }
  58. TEST_END
  59. TEST_BEGIN(test_bitmap_unset)
  60. {
  61. size_t i;
  62. for (i = 1; i <= MAXBITS; i++) {
  63. bitmap_info_t binfo;
  64. bitmap_info_init(&binfo, i);
  65. {
  66. size_t j;
  67. bitmap_t *bitmap = malloc(sizeof(bitmap_t) *
  68. bitmap_info_ngroups(&binfo));
  69. bitmap_init(bitmap, &binfo);
  70. for (j = 0; j < i; j++)
  71. bitmap_set(bitmap, &binfo, j);
  72. assert_true(bitmap_full(bitmap, &binfo),
  73. "All bits should be set");
  74. for (j = 0; j < i; j++)
  75. bitmap_unset(bitmap, &binfo, j);
  76. for (j = 0; j < i; j++)
  77. bitmap_set(bitmap, &binfo, j);
  78. assert_true(bitmap_full(bitmap, &binfo),
  79. "All bits should be set");
  80. free(bitmap);
  81. }
  82. }
  83. }
  84. TEST_END
  85. TEST_BEGIN(test_bitmap_sfu)
  86. {
  87. size_t i;
  88. for (i = 1; i <= MAXBITS; i++) {
  89. bitmap_info_t binfo;
  90. bitmap_info_init(&binfo, i);
  91. {
  92. ssize_t j;
  93. bitmap_t *bitmap = malloc(sizeof(bitmap_t) *
  94. bitmap_info_ngroups(&binfo));
  95. bitmap_init(bitmap, &binfo);
  96. /* Iteratively set bits starting at the beginning. */
  97. for (j = 0; j < i; j++) {
  98. assert_zd_eq(bitmap_sfu(bitmap, &binfo), j,
  99. "First unset bit should be just after "
  100. "previous first unset bit");
  101. }
  102. assert_true(bitmap_full(bitmap, &binfo),
  103. "All bits should be set");
  104. /*
  105. * Iteratively unset bits starting at the end, and
  106. * verify that bitmap_sfu() reaches the unset bits.
  107. */
  108. for (j = i - 1; j >= 0; j--) {
  109. bitmap_unset(bitmap, &binfo, j);
  110. assert_zd_eq(bitmap_sfu(bitmap, &binfo), j,
  111. "First unset bit should the bit previously "
  112. "unset");
  113. bitmap_unset(bitmap, &binfo, j);
  114. }
  115. assert_false(bitmap_get(bitmap, &binfo, 0),
  116. "Bit should be unset");
  117. /*
  118. * Iteratively set bits starting at the beginning, and
  119. * verify that bitmap_sfu() looks past them.
  120. */
  121. for (j = 1; j < i; j++) {
  122. bitmap_set(bitmap, &binfo, j - 1);
  123. assert_zd_eq(bitmap_sfu(bitmap, &binfo), j,
  124. "First unset bit should be just after the "
  125. "bit previously set");
  126. bitmap_unset(bitmap, &binfo, j);
  127. }
  128. assert_zd_eq(bitmap_sfu(bitmap, &binfo), i - 1,
  129. "First unset bit should be the last bit");
  130. assert_true(bitmap_full(bitmap, &binfo),
  131. "All bits should be set");
  132. free(bitmap);
  133. }
  134. }
  135. }
  136. TEST_END
  137. int
  138. main(void)
  139. {
  140. return (test(
  141. test_bitmap_size,
  142. test_bitmap_init,
  143. test_bitmap_set,
  144. test_bitmap_unset,
  145. test_bitmap_sfu));
  146. }