BitArrayTests.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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(int i = 0; i < bits.getLength(); i++) {
  25. bits.set(i, i);
  26. }
  27. for(int 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. int data[length];
  34. Core::BitArray bits;
  35. CORE_TEST_ERROR(bits.resize(100, 13));
  36. int seed = 534;
  37. for(int k = 0; k < 20; k++) {
  38. for(int 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(int 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(int 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(-1));
  73. CORE_TEST_EQUAL(-1, bits.select(0));
  74. CORE_TEST_EQUAL(0, bits.select(1));
  75. CORE_TEST_EQUAL(5, bits.select(2));
  76. CORE_TEST_EQUAL(20, bits.select(3));
  77. CORE_TEST_EQUAL(31, bits.select(4));
  78. CORE_TEST_EQUAL(32, bits.select(5));
  79. CORE_TEST_EQUAL(33, bits.select(6));
  80. CORE_TEST_EQUAL(60, bits.select(7));
  81. CORE_TEST_EQUAL(-1, bits.select(8));
  82. }
  83. static void testToString1() {
  84. Core::BitArray bits;
  85. CORE_TEST_ERROR(bits.resize(4, 3));
  86. bits.set(0, 1).set(1, 2).set(2, 3).set(3, 4);
  87. CORE_TEST_STRING("[1, 2, 3, 4]", bits);
  88. }
  89. static void testToString2() {
  90. Core::BitArray bits;
  91. CORE_TEST_ERROR(bits.resize(1, 3));
  92. bits.set(0, 1);
  93. CORE_TEST_STRING("[1]", bits);
  94. }
  95. static void testToString3() {
  96. Core::BitArray bits;
  97. CORE_TEST_STRING("[]", bits);
  98. }
  99. static void testResizeExact() {
  100. Core::BitArray bits;
  101. CORE_TEST_EQUAL(0, bits.getInternalByteSize());
  102. // the size in bytes matches the internal storage type
  103. int elements = CORE_SIZE(int);
  104. CORE_TEST_ERROR(bits.resize(elements, 8));
  105. for(int i = 0; i < elements; i++) {
  106. bits.set(i, i);
  107. }
  108. for(int i = 0; i < elements; i++) {
  109. CORE_TEST_EQUAL(i, bits.get(i));
  110. }
  111. CORE_TEST_EQUAL(CORE_SIZE(int), bits.getInternalByteSize());
  112. }
  113. static void testMoveAssignment() {
  114. Core::BitArray bits;
  115. CORE_TEST_ERROR(bits.resize(8, 8));
  116. for(int i = 0; i < bits.getLength(); i++) {
  117. bits.set(i, i);
  118. }
  119. Core::BitArray m;
  120. m = Core::move(bits);
  121. CORE_TEST_EQUAL(8, m.getLength());
  122. for(int i = 0; i < m.getLength(); i++) {
  123. CORE_TEST_EQUAL(i, m.get(i));
  124. }
  125. }
  126. static void testNegativeArgument() {
  127. Core::BitArray bits;
  128. CORE_TEST_EQUAL(Core::Error::NEGATIVE_ARGUMENT, bits.resize(-1, 5));
  129. CORE_TEST_EQUAL(Core::Error::NEGATIVE_ARGUMENT, bits.resize(5, -1));
  130. CORE_TEST_EQUAL(Core::Error::NEGATIVE_ARGUMENT, bits.resize(-1, -1));
  131. }
  132. static void testOutOfMemory() {
  133. #ifdef ERROR_SIMULATOR
  134. Core::BitArray bits;
  135. Core::Fail::leftAllocations = 0;
  136. CORE_TEST_EQUAL(Core::Error::OUT_OF_MEMORY, bits.resize(8, 4));
  137. Core::Fail::leftAllocations = -1;
  138. #endif
  139. }
  140. void Core::testBitArray(bool outOfMemoryTest) {
  141. testSetRead();
  142. testOutOfBoundsSetRead();
  143. testBigSetRead();
  144. testRandomSetReadResize();
  145. testReadOnly();
  146. testSelect();
  147. testToString1();
  148. testToString2();
  149. testToString3();
  150. testResizeExact();
  151. testMoveAssignment();
  152. testNegativeArgument();
  153. if(outOfMemoryTest) {
  154. testOutOfMemory();
  155. }
  156. }