BitArrayTests.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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(size_t i = 0; i < bits.getLength(); i++) {
  25. bits.set(i, i);
  26. }
  27. for(size_t 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(size_t 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(size_t 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. const Core::BitArray copy = bits;
  59. CORE_TEST_EQUAL(1, copy.get(0));
  60. CORE_TEST_EQUAL(2, copy.get(1));
  61. CORE_TEST_EQUAL(3, copy.get(2));
  62. CORE_TEST_EQUAL(4, copy.get(3));
  63. }
  64. static void testSelect() {
  65. Core::BitArray bits;
  66. CORE_TEST_ERROR(bits.resize(90, 1));
  67. bits.fill(0);
  68. bits.set(0, 1).set(5, 1).set(20, 1).set(31, 1);
  69. bits.set(32, 1).set(33, 1).set(60, 1);
  70. CORE_TEST_EQUAL(-1, bits.select(0));
  71. CORE_TEST_EQUAL(0, bits.select(1));
  72. CORE_TEST_EQUAL(5, bits.select(2));
  73. CORE_TEST_EQUAL(20, bits.select(3));
  74. CORE_TEST_EQUAL(31, bits.select(4));
  75. CORE_TEST_EQUAL(32, bits.select(5));
  76. CORE_TEST_EQUAL(33, bits.select(6));
  77. CORE_TEST_EQUAL(60, bits.select(7));
  78. CORE_TEST_EQUAL(-1, bits.select(8));
  79. }
  80. static void testToString1() {
  81. Core::BitArray bits;
  82. CORE_TEST_ERROR(bits.resize(4, 3));
  83. bits.set(0, 1).set(1, 2).set(2, 3).set(3, 4);
  84. CORE_TEST_STRING("[1, 2, 3, 4]", bits);
  85. }
  86. static void testToString2() {
  87. Core::BitArray bits;
  88. CORE_TEST_ERROR(bits.resize(1, 3));
  89. bits.set(0, 1);
  90. CORE_TEST_STRING("[1]", bits);
  91. }
  92. static void testToString3() {
  93. Core::BitArray bits;
  94. CORE_TEST_STRING("[]", bits);
  95. }
  96. static void testResizeExact() {
  97. Core::BitArray bits;
  98. CORE_TEST_EQUAL(0, bits.getInternalByteSize());
  99. // the size in bytes matches the internal storage type
  100. size_t elements = sizeof(u64);
  101. CORE_TEST_ERROR(bits.resize(elements, 8));
  102. for(size_t i = 0; i < elements; i++) {
  103. bits.set(i, i);
  104. }
  105. for(size_t i = 0; i < elements; i++) {
  106. CORE_TEST_EQUAL(i, bits.get(i));
  107. }
  108. CORE_TEST_EQUAL(sizeof(u64), bits.getInternalByteSize());
  109. }
  110. static void testMoveAssignment() {
  111. Core::BitArray bits;
  112. CORE_TEST_ERROR(bits.resize(8, 8));
  113. for(size_t i = 0; i < bits.getLength(); i++) {
  114. bits.set(i, i);
  115. }
  116. Core::BitArray m;
  117. m = Core::move(bits);
  118. CORE_TEST_EQUAL(8, m.getLength());
  119. for(size_t i = 0; i < m.getLength(); i++) {
  120. CORE_TEST_EQUAL(i, m.get(i));
  121. }
  122. }
  123. static void testInvalidArgument() {
  124. Core::BitArray bits;
  125. CORE_TEST_EQUAL(Core::ErrorCode::INVALID_ARGUMENT, bits.resize(0, 5));
  126. CORE_TEST_EQUAL(Core::ErrorCode::INVALID_ARGUMENT, bits.resize(5, 0));
  127. CORE_TEST_EQUAL(Core::ErrorCode::INVALID_ARGUMENT, bits.resize(0, 0));
  128. CORE_TEST_EQUAL(Core::ErrorCode::INVALID_ARGUMENT, bits.resize(1, 65));
  129. CORE_TEST_EQUAL(Core::ErrorCode::INVALID_ARGUMENT, bits.resize(5, 68));
  130. }
  131. void Core::testBitArray() {
  132. testSetRead();
  133. testOutOfBoundsSetRead();
  134. testBigSetRead();
  135. testRandomSetReadResize();
  136. testReadOnly();
  137. testSelect();
  138. testToString1();
  139. testToString2();
  140. testToString3();
  141. testResizeExact();
  142. testMoveAssignment();
  143. testInvalidArgument();
  144. }