BitArrayTests.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #include "../Tests.h"
  2. #include "core/BitArray.h"
  3. static void testSetRead() {
  4. BitArray bits = BIT_ARRAY;
  5. resizeBitArray(&bits, 4, 3);
  6. setBitsBitArray(&bits, 0, 1);
  7. setBitsBitArray(&bits, 1, 2);
  8. setBitsBitArray(&bits, 2, 3);
  9. setBitsBitArray(&bits, 3, 4);
  10. CORE_TEST_U64(1, getBitsBitArray(&bits, 0));
  11. CORE_TEST_U64(2, getBitsBitArray(&bits, 1));
  12. CORE_TEST_U64(3, getBitsBitArray(&bits, 2));
  13. CORE_TEST_U64(4, getBitsBitArray(&bits, 3));
  14. destroyBitArray(&bits);
  15. }
  16. static void testBigSetRead() {
  17. BitArray bits = BIT_ARRAY;
  18. resizeBitArray(&bits, 100, 13);
  19. for(size_t i = 0; i < bits.length; i++) {
  20. setBitsBitArray(&bits, i, i);
  21. }
  22. for(size_t i = 0; i < bits.length; i++) {
  23. CORE_TEST_U64(i, getBitsBitArray(&bits, i));
  24. }
  25. destroyBitArray(&bits);
  26. }
  27. static void testRandomSetReadResize() {
  28. u64 data[100];
  29. BitArray bits = BIT_ARRAY;
  30. resizeBitArray(&bits, 100, 13);
  31. u64 seed = 534;
  32. for(int k = 0; k < 20; k++) {
  33. for(u64 i = 0; i < bits.length; i++) {
  34. seed = seed * 636455 + 53453;
  35. setBitsBitArray(&bits, i, seed);
  36. data[i] = seed & 0x1FFF;
  37. }
  38. }
  39. for(size_t i = 0; i < bits.length; i++) {
  40. CORE_TEST_U64(data[i], getBitsBitArray(&bits, i));
  41. }
  42. resizeBitArray(&bits, bits.length, bits.bits + 1u);
  43. CORE_TEST_U64(14, bits.bits);
  44. CORE_TEST_U64(100, bits.length);
  45. for(size_t i = 0; i < bits.length; i++) {
  46. CORE_TEST_U64(data[i], getBitsBitArray(&bits, i));
  47. }
  48. destroyBitArray(&bits);
  49. }
  50. static void testCopy() {
  51. BitArray bits = BIT_ARRAY;
  52. resizeBitArray(&bits, 4, 3);
  53. setBitsBitArray(&bits, 0, 1);
  54. setBitsBitArray(&bits, 1, 2);
  55. setBitsBitArray(&bits, 2, 3);
  56. setBitsBitArray(&bits, 3, 4);
  57. BitArray copy = BIT_ARRAY;
  58. copyBitArray(&copy, &bits);
  59. copyBitArray(&copy, &copy);
  60. CORE_TEST_U64(1, getBitsBitArray(&copy, 0));
  61. CORE_TEST_U64(2, getBitsBitArray(&copy, 1));
  62. CORE_TEST_U64(3, getBitsBitArray(&copy, 2));
  63. CORE_TEST_U64(4, getBitsBitArray(&copy, 3));
  64. destroyBitArray(&copy);
  65. destroyBitArray(&bits);
  66. }
  67. static void testSelect() {
  68. BitArray bits = BIT_ARRAY;
  69. resizeBitArray(&bits, 90, 1);
  70. fillBitArray(&bits, 0);
  71. setBitsBitArray(&bits, 0, 1);
  72. setBitsBitArray(&bits, 5, 1);
  73. setBitsBitArray(&bits, 20, 1);
  74. setBitsBitArray(&bits, 31, 1);
  75. setBitsBitArray(&bits, 32, 1);
  76. setBitsBitArray(&bits, 33, 1);
  77. setBitsBitArray(&bits, 60, 1);
  78. CORE_TEST_I64(-1, selectBitsBitArray(&bits, 0));
  79. CORE_TEST_I64(0, selectBitsBitArray(&bits, 1));
  80. CORE_TEST_I64(5, selectBitsBitArray(&bits, 2));
  81. CORE_TEST_I64(20, selectBitsBitArray(&bits, 3));
  82. CORE_TEST_I64(31, selectBitsBitArray(&bits, 4));
  83. CORE_TEST_I64(32, selectBitsBitArray(&bits, 5));
  84. CORE_TEST_I64(33, selectBitsBitArray(&bits, 6));
  85. CORE_TEST_I64(60, selectBitsBitArray(&bits, 7));
  86. CORE_TEST_I64(-1, selectBitsBitArray(&bits, 8));
  87. destroyBitArray(&bits);
  88. }
  89. static void testToString1() {
  90. BitArray bits = BIT_ARRAY;
  91. resizeBitArray(&bits, 4, 3);
  92. setBitsBitArray(&bits, 0, 1);
  93. setBitsBitArray(&bits, 1, 2);
  94. setBitsBitArray(&bits, 2, 3);
  95. setBitsBitArray(&bits, 3, 4);
  96. char buffer[128];
  97. size_t n = toStringBitArray(&bits, buffer, sizeof(buffer));
  98. CORE_TEST_SIZE(12, n);
  99. CORE_TEST_STRING("[1, 2, 3, 4]", buffer);
  100. destroyBitArray(&bits);
  101. }
  102. static void testToString2() {
  103. BitArray bits = BIT_ARRAY;
  104. resizeBitArray(&bits, 1, 3);
  105. setBitsBitArray(&bits, 0, 1);
  106. char buffer[128];
  107. size_t n = toStringBitArray(&bits, buffer, sizeof(buffer));
  108. CORE_TEST_SIZE(3, n);
  109. CORE_TEST_STRING("[1]", buffer);
  110. destroyBitArray(&bits);
  111. }
  112. static void testToString3() {
  113. BitArray bits = BIT_ARRAY;
  114. char buffer[128];
  115. size_t n = toStringBitArray(&bits, buffer, sizeof(buffer));
  116. CORE_TEST_SIZE(2, n);
  117. CORE_TEST_STRING("[]", buffer);
  118. destroyBitArray(&bits);
  119. }
  120. static void testResizeExact() {
  121. BitArray bits = BIT_ARRAY;
  122. CORE_TEST_U64(0, getBytesBitArray(&bits));
  123. // the size in bytes matches the internal storage type
  124. size_t elements = sizeof(u64);
  125. resizeBitArray(&bits, elements, 8);
  126. for(size_t i = 0; i < elements; i++) {
  127. setBitsBitArray(&bits, i, i);
  128. }
  129. for(size_t i = 0; i < elements; i++) {
  130. CORE_TEST_U64(i, getBitsBitArray(&bits, i));
  131. }
  132. CORE_TEST_U64(sizeof(u64), getBytesBitArray(&bits));
  133. destroyBitArray(&bits);
  134. }
  135. static void testMove() {
  136. BitArray bits = BIT_ARRAY;
  137. resizeBitArray(&bits, 8, 8);
  138. for(size_t i = 0; i < bits.length; i++) {
  139. setBitsBitArray(&bits, i, i);
  140. }
  141. BitArray m = BIT_ARRAY;
  142. moveBitArray(&m, &bits);
  143. moveBitArray(&m, &m);
  144. CORE_TEST_U64(8, m.length);
  145. for(size_t i = 0; i < m.length; i++) {
  146. CORE_TEST_U64(i, getBitsBitArray(&m, i));
  147. }
  148. destroyBitArray(&m);
  149. destroyBitArray(&bits);
  150. }
  151. static void testInvalidArgument() {
  152. BitArray bits = BIT_ARRAY;
  153. resizeBitArray(&bits, 0, 5);
  154. CORE_TEST_SIZE(0, bits.length);
  155. CORE_TEST_SIZE(0, bits.bits);
  156. resizeBitArray(&bits, 5, 0);
  157. CORE_TEST_SIZE(0, bits.length);
  158. CORE_TEST_SIZE(0, bits.bits);
  159. resizeBitArray(&bits, 0, 0);
  160. CORE_TEST_SIZE(0, bits.length);
  161. CORE_TEST_SIZE(0, bits.bits);
  162. resizeBitArray(&bits, 1, 65);
  163. CORE_TEST_SIZE(1, bits.length);
  164. CORE_TEST_SIZE(64, bits.bits);
  165. resizeBitArray(&bits, 5, 68);
  166. CORE_TEST_SIZE(5, bits.length);
  167. CORE_TEST_SIZE(64, bits.bits);
  168. destroyBitArray(&bits);
  169. }
  170. void coreTestBitArray() {
  171. testSetRead();
  172. testBigSetRead();
  173. testRandomSetReadResize();
  174. testCopy();
  175. testSelect();
  176. testToString1();
  177. testToString2();
  178. testToString3();
  179. testResizeExact();
  180. testMove();
  181. testInvalidArgument();
  182. }