BitArrayTests.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #include "../../src/ErrorSimulator.hpp"
  2. #include "../Tests.hpp"
  3. #include "core/data/BitArray.hpp"
  4. static void testSetRead() {
  5. Core::BitArray bits;
  6. CORE_TEST_ERROR(bits.resize(4, 3));
  7. bits.set(0, 1).set(1, 2).set(2, 3).set(3, 4);
  8. CORE_TEST_EQUAL(1, bits.get(0));
  9. CORE_TEST_EQUAL(2, bits.get(1));
  10. CORE_TEST_EQUAL(3, bits.get(2));
  11. CORE_TEST_EQUAL(4, bits.get(3));
  12. }
  13. static void testOutOfBoundsSetRead() {
  14. Core::BitArray bits;
  15. bits.set(0, 1).set(1, 2).set(2, 3).set(3, 4);
  16. CORE_TEST_EQUAL(0, bits.get(0));
  17. CORE_TEST_EQUAL(0, bits.get(1));
  18. CORE_TEST_EQUAL(0, bits.get(2));
  19. CORE_TEST_EQUAL(0, bits.get(3));
  20. }
  21. static void testBigSetRead() {
  22. Core::BitArray bits;
  23. CORE_TEST_ERROR(bits.resize(100, 13));
  24. for(u64 i = 0; i < bits.getLength(); i++) {
  25. bits.set(i, i);
  26. }
  27. for(u64 i = 0; i < bits.getLength(); i++) {
  28. CORE_TEST_EQUAL(i, bits.get(i));
  29. }
  30. }
  31. static void testRandomSetReadResize() {
  32. const int length = 100;
  33. u64 data[length];
  34. Core::BitArray bits;
  35. CORE_TEST_ERROR(bits.resize(100, 13));
  36. u64 seed = 534;
  37. for(int k = 0; k < 20; k++) {
  38. for(u64 i = 0; i < bits.getLength(); i++) {
  39. seed = seed * 636455 + 53453;
  40. bits.set(i, seed & (0x1FFF));
  41. data[i] = seed & (0x1FFF);
  42. }
  43. }
  44. for(u64 i = 0; i < bits.getLength(); i++) {
  45. CORE_TEST_EQUAL(data[i], bits.get(i));
  46. }
  47. CORE_TEST_ERROR(bits.resize(bits.getLength(), bits.getBits() + 1));
  48. CORE_TEST_EQUAL(14, bits.getBits());
  49. CORE_TEST_EQUAL(100, bits.getLength());
  50. for(u64 i = 0; i < bits.getLength(); i++) {
  51. CORE_TEST_EQUAL(data[i], bits.get(i));
  52. }
  53. }
  54. static void testReadOnly() {
  55. Core::BitArray bits;
  56. CORE_TEST_ERROR(bits.resize(4, 3));
  57. bits.set(0, 1).set(1, 2).set(2, 3).set(3, 4);
  58. Core::BitArray copy;
  59. CORE_TEST_ERROR(copy.copyFrom(bits));
  60. const Core::BitArray bits2 = Core::move(copy);
  61. CORE_TEST_EQUAL(1, bits2.get(0));
  62. CORE_TEST_EQUAL(2, bits2.get(1));
  63. CORE_TEST_EQUAL(3, bits2.get(2));
  64. CORE_TEST_EQUAL(4, bits2.get(3));
  65. }
  66. static void testSelect() {
  67. Core::BitArray bits;
  68. CORE_TEST_ERROR(bits.resize(90, 1));
  69. bits.fill(0);
  70. bits.set(0, 1).set(5, 1).set(20, 1).set(31, 1);
  71. bits.set(32, 1).set(33, 1).set(60, 1);
  72. CORE_TEST_EQUAL(-1, bits.select(0));
  73. CORE_TEST_EQUAL(0, bits.select(1));
  74. CORE_TEST_EQUAL(5, bits.select(2));
  75. CORE_TEST_EQUAL(20, bits.select(3));
  76. CORE_TEST_EQUAL(31, bits.select(4));
  77. CORE_TEST_EQUAL(32, bits.select(5));
  78. CORE_TEST_EQUAL(33, bits.select(6));
  79. CORE_TEST_EQUAL(60, bits.select(7));
  80. CORE_TEST_EQUAL(-1, bits.select(8));
  81. }
  82. static void testToString1() {
  83. Core::BitArray bits;
  84. CORE_TEST_ERROR(bits.resize(4, 3));
  85. bits.set(0, 1).set(1, 2).set(2, 3).set(3, 4);
  86. CORE_TEST_STRING("[1, 2, 3, 4]", bits);
  87. }
  88. static void testToString2() {
  89. Core::BitArray bits;
  90. CORE_TEST_ERROR(bits.resize(1, 3));
  91. bits.set(0, 1);
  92. CORE_TEST_STRING("[1]", bits);
  93. }
  94. static void testToString3() {
  95. Core::BitArray bits;
  96. CORE_TEST_STRING("[]", bits);
  97. }
  98. static void testResizeExact() {
  99. Core::BitArray bits;
  100. CORE_TEST_EQUAL(0, bits.getInternalByteSize());
  101. // the size in bytes matches the internal storage type
  102. u64 elements = sizeof(u64);
  103. CORE_TEST_ERROR(bits.resize(elements, 8));
  104. for(u64 i = 0; i < elements; i++) {
  105. bits.set(i, i);
  106. }
  107. for(u64 i = 0; i < elements; i++) {
  108. CORE_TEST_EQUAL(i, bits.get(i));
  109. }
  110. CORE_TEST_EQUAL(CORE_SIZE(u64), bits.getInternalByteSize());
  111. }
  112. static void testMoveAssignment() {
  113. Core::BitArray bits;
  114. CORE_TEST_ERROR(bits.resize(8, 8));
  115. for(u64 i = 0; i < bits.getLength(); i++) {
  116. bits.set(i, i);
  117. }
  118. Core::BitArray m;
  119. m = Core::move(bits);
  120. CORE_TEST_EQUAL(8, m.getLength());
  121. for(u64 i = 0; i < m.getLength(); i++) {
  122. CORE_TEST_EQUAL(i, m.get(i));
  123. }
  124. }
  125. static void testInvalidArgument() {
  126. Core::BitArray bits;
  127. CORE_TEST_EQUAL(Core::Error::INVALID_ARGUMENT, bits.resize(0, 5));
  128. CORE_TEST_EQUAL(Core::Error::INVALID_ARGUMENT, bits.resize(5, 0));
  129. CORE_TEST_EQUAL(Core::Error::INVALID_ARGUMENT, bits.resize(0, 0));
  130. CORE_TEST_EQUAL(Core::Error::INVALID_ARGUMENT, bits.resize(1, 65));
  131. CORE_TEST_EQUAL(Core::Error::INVALID_ARGUMENT, bits.resize(5, 68));
  132. }
  133. static void testOutOfMemory() {
  134. #ifdef ERROR_SIMULATOR
  135. Core::BitArray bits;
  136. Core::Fail::leftAllocations = 0;
  137. CORE_TEST_EQUAL(Core::Error::OUT_OF_MEMORY, bits.resize(8, 4));
  138. Core::Fail::leftAllocations = -1;
  139. #endif
  140. }
  141. static void testOutOfMemory2() {
  142. Core::BitArray bits;
  143. CORE_TEST_EQUAL(Core::Error::OUT_OF_MEMORY,
  144. bits.resize(0x01FF'FFFF'FFFF'FFFF, 64));
  145. }
  146. void Core::testBitArray(bool outOfMemoryTest) {
  147. testSetRead();
  148. testOutOfBoundsSetRead();
  149. testBigSetRead();
  150. testRandomSetReadResize();
  151. testReadOnly();
  152. testSelect();
  153. testToString1();
  154. testToString2();
  155. testToString3();
  156. testResizeExact();
  157. testMoveAssignment();
  158. testInvalidArgument();
  159. if(outOfMemoryTest) {
  160. testOutOfMemory();
  161. }
  162. testOutOfMemory2();
  163. }