run.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include <ccan/bitmap/bitmap.h>
  2. #include <ccan/tap/tap.h>
  3. #include <ccan/array_size/array_size.h>
  4. #include <ccan/foreach/foreach.h>
  5. int bitmap_sizes[] = {
  6. 1, 2, 3, 4, 5, 6, 7, 8,
  7. 16, 17, 24, 32, 33,
  8. 64, 65, 127, 128, 129,
  9. 1023, 1024, 1025,
  10. };
  11. #define NSIZES ARRAY_SIZE(bitmap_sizes)
  12. #define NTESTS 9
  13. static void test_sizes(int nbits, bool dynalloc)
  14. {
  15. BITMAP_DECLARE(sbitmap, nbits);
  16. uint32_t marker;
  17. bitmap *bitmap;
  18. int i, j;
  19. bool wrong;
  20. if (dynalloc) {
  21. bitmap = bitmap_alloc(nbits);
  22. ok1(bitmap != NULL);
  23. } else {
  24. bitmap = sbitmap;
  25. marker = 0xdeadbeef;
  26. }
  27. bitmap_zero(bitmap, nbits);
  28. wrong = false;
  29. for (i = 0; i < nbits; i++) {
  30. wrong = wrong || bitmap_test_bit(bitmap, i);
  31. }
  32. ok1(!wrong);
  33. bitmap_fill(bitmap, nbits);
  34. wrong = false;
  35. for (i = 0; i < nbits; i++) {
  36. wrong = wrong || !bitmap_test_bit(bitmap, i);
  37. }
  38. ok1(!wrong);
  39. wrong = false;
  40. for (i = 0; i < nbits; i++) {
  41. bitmap_zero(bitmap, nbits);
  42. bitmap_set_bit(bitmap, i);
  43. for (j = 0; j < nbits; j++) {
  44. bool val = (i == j);
  45. wrong = wrong || (bitmap_test_bit(bitmap, j) != val);
  46. }
  47. }
  48. ok1(!wrong);
  49. wrong = false;
  50. for (i = 0; i < nbits; i++) {
  51. bitmap_fill(bitmap, nbits);
  52. bitmap_clear_bit(bitmap, i);
  53. for (j = 0; j < nbits; j++) {
  54. bool val = !(i == j);
  55. wrong = wrong || (bitmap_test_bit(bitmap, j) != val);
  56. }
  57. }
  58. ok1(!wrong);
  59. bitmap_zero(bitmap, nbits);
  60. ok1(bitmap_empty(bitmap, nbits));
  61. wrong = false;
  62. for (i = 0; i < nbits; i++) {
  63. bitmap_zero(bitmap, nbits);
  64. bitmap_set_bit(bitmap, i);
  65. wrong = wrong || bitmap_empty(bitmap, nbits);
  66. }
  67. ok1(!wrong);
  68. bitmap_fill(bitmap, nbits);
  69. ok1(bitmap_full(bitmap, nbits));
  70. wrong = false;
  71. for (i = 0; i < nbits; i++) {
  72. bitmap_fill(bitmap, nbits);
  73. bitmap_clear_bit(bitmap, i);
  74. wrong = wrong || bitmap_full(bitmap, nbits);
  75. }
  76. ok1(!wrong);
  77. if (dynalloc) {
  78. free(bitmap);
  79. } else {
  80. ok1(marker == 0xdeadbeef);
  81. }
  82. }
  83. int main(void)
  84. {
  85. int i;
  86. bool dynalloc;
  87. /* This is how many tests you plan to run */
  88. plan_tests(NSIZES * NTESTS * 2);
  89. for (i = 0; i < NSIZES; i++) {
  90. foreach_int(dynalloc, false, true) {
  91. diag("Testing %d-bit bitmap (%s allocation)",
  92. bitmap_sizes[i], dynalloc ? "dynamic" : "static");
  93. test_sizes(bitmap_sizes[i], dynalloc);
  94. }
  95. }
  96. /* This exits depending on whether all tests passed */
  97. return exit_status();
  98. }