BitArrayTests.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include "../Tests.hpp"
  2. #include "core/BitArray.hpp"
  3. #include "core/Test.hpp"
  4. static void testSetRead() {
  5. Core::BitArray bits;
  6. bits.resize(4, 3);
  7. bits.set(0, 1).set(1, 2).set(2, 3).set(3, 4);
  8. TEST(1, bits.get(0));
  9. TEST(2, bits.get(1));
  10. TEST(3, bits.get(2));
  11. TEST(4, bits.get(3));
  12. Core::BitArray copy;
  13. copy = bits;
  14. TEST(1, copy.get(0));
  15. TEST(2, copy.get(1));
  16. TEST(3, copy.get(2));
  17. TEST(4, copy.get(3));
  18. Core::BitArray move = Core::move(copy);
  19. TEST(1, move.get(0));
  20. TEST(2, move.get(1));
  21. TEST(3, move.get(2));
  22. TEST(4, move.get(3));
  23. }
  24. static void testBigSetRead() {
  25. Core::BitArray bits;
  26. bits.resize(100, 13);
  27. for(size_t i = 0; i < bits.getLength(); i++) {
  28. bits.set(i, i);
  29. }
  30. for(size_t i = 0; i < bits.getLength(); i++) {
  31. TEST(i, bits.get(i));
  32. }
  33. }
  34. static void testRandomSetReadResize() {
  35. constexpr int length = 100;
  36. u64 data[length];
  37. Core::BitArray bits;
  38. bits.resize(100, 13);
  39. u64 seed = 534;
  40. for(int k = 0; k < 20; k++) {
  41. for(u64 i = 0; i < bits.getLength(); i++) {
  42. seed = seed * 636'455 + 53'453;
  43. bits.set(i, seed);
  44. data[i] = seed & (0x1FFF);
  45. }
  46. }
  47. for(size_t i = 0; i < bits.getLength(); i++) {
  48. TEST(data[i], bits.get(i));
  49. }
  50. bits.resize(bits.getLength(), bits.getBits() + 1);
  51. TEST(14, bits.getBits());
  52. TEST(100, bits.getLength());
  53. for(size_t i = 0; i < bits.getLength(); i++) {
  54. TEST(data[i], bits.get(i));
  55. }
  56. }
  57. static void testReadOnly() {
  58. Core::BitArray bits;
  59. bits.resize(4, 3);
  60. bits.set(0, 1).set(1, 2).set(2, 3).set(3, 4);
  61. const Core::BitArray copy = bits;
  62. TEST(1, copy.get(0));
  63. TEST(2, copy.get(1));
  64. TEST(3, copy.get(2));
  65. TEST(4, copy.get(3));
  66. }
  67. static void testSelect() {
  68. Core::BitArray bits;
  69. bits.resize(90, 1);
  70. bits.fill(0);
  71. bits.set(0, 1).set(5, 1).set(20, 1).set(31, 1);
  72. bits.set(32, 1).set(33, 1).set(60, 1);
  73. TEST(-1, bits.select(0));
  74. TEST(0, bits.select(1));
  75. TEST(5, bits.select(2));
  76. TEST(20, bits.select(3));
  77. TEST(31, bits.select(4));
  78. TEST(32, bits.select(5));
  79. TEST(33, bits.select(6));
  80. TEST(60, bits.select(7));
  81. TEST(-1, bits.select(8));
  82. }
  83. static void testToString1() {
  84. Core::BitArray bits;
  85. bits.resize(4, 3);
  86. bits.set(0, 1).set(1, 2).set(2, 3).set(3, 4);
  87. TEST_STRING("[1, 2, 3, 4]", bits);
  88. }
  89. static void testToString2() {
  90. Core::BitArray bits;
  91. bits.resize(1, 3);
  92. bits.set(0, 1);
  93. TEST_STRING("[1]", bits);
  94. }
  95. static void testToString3() {
  96. Core::BitArray bits;
  97. TEST_STRING("[]", bits);
  98. }
  99. static void testResizeExact() {
  100. Core::BitArray bits;
  101. TEST(0, bits.getInternalByteSize());
  102. // the size in bytes matches the internal storage type
  103. size_t elements = sizeof(u64);
  104. bits.resize(elements, 8);
  105. for(size_t i = 0; i < elements; i++) {
  106. bits.set(i, i);
  107. }
  108. for(size_t i = 0; i < elements; i++) {
  109. TEST(i, bits.get(i));
  110. }
  111. TEST(sizeof(u64), bits.getInternalByteSize());
  112. }
  113. static void testInvalidArgument() {
  114. Core::BitArray bits;
  115. bits.resize(0, 5);
  116. TEST(0, bits.getLength());
  117. TEST(0, bits.getBits());
  118. bits.resize(5, 0);
  119. TEST(0, bits.getLength());
  120. TEST(0, bits.getBits());
  121. bits.resize(0, 0);
  122. TEST(0, bits.getLength());
  123. TEST(0, bits.getBits());
  124. bits.resize(1, 65);
  125. TEST(1, bits.getLength());
  126. TEST(64, bits.getBits());
  127. bits.resize(5, 68);
  128. TEST(5, bits.getLength());
  129. TEST(64, bits.getBits());
  130. }
  131. void testBitArray() {
  132. testBigSetRead();
  133. testInvalidArgument();
  134. testRandomSetReadResize();
  135. testReadOnly();
  136. testResizeExact();
  137. testSelect();
  138. testSetRead();
  139. testToString1();
  140. testToString2();
  141. testToString3();
  142. }