BitArrayTests.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include "tests/BitArrayTests.h"
  2. #include "data/BitArray.h"
  3. #include "test/Test.h"
  4. using String = Core::ArrayString<128>;
  5. template<typename T>
  6. static String build(Core::Test& test, const T& t) {
  7. String s;
  8. test.checkFalse(s.append(t), "append works");
  9. return s;
  10. }
  11. static void testSetRead(Core::Test& test) {
  12. Core::BitArray bits;
  13. test.checkFalse(bits.resize(4, 3), "resize works 1");
  14. bits.set(0, 1).set(1, 2).set(2, 3).set(3, 4);
  15. test.checkEqual(1, bits.get(0), "set and read correct value 1");
  16. test.checkEqual(2, bits.get(1), "set and read correct value 2");
  17. test.checkEqual(3, bits.get(2), "set and read correct value 3");
  18. test.checkEqual(4, bits.get(3), "set and read correct value 4");
  19. }
  20. static void testOutOfBoundsSetRead(Core::Test& test) {
  21. Core::BitArray bits;
  22. bits.set(0, 1).set(1, 2).set(2, 3).set(3, 4);
  23. test.checkEqual(0, bits.get(0), "set and read default value 1");
  24. test.checkEqual(0, bits.get(1), "set and read default value 2");
  25. test.checkEqual(0, bits.get(2), "set and read default value 3");
  26. test.checkEqual(0, bits.get(3), "set and read default value 4");
  27. }
  28. static void testBigSetRead(Core::Test& test) {
  29. Core::BitArray bits;
  30. test.checkFalse(bits.resize(100, 13), "resize works 2");
  31. for(int i = 0; i < bits.getLength(); i++) {
  32. bits.set(i, i);
  33. }
  34. for(int i = 0; i < bits.getLength(); i++) {
  35. test.checkEqual(i, bits.get(i),
  36. "set and read correct value over long array");
  37. }
  38. }
  39. static void testRandomSetReadResize(Core::Test& test) {
  40. const int length = 100;
  41. int data[length];
  42. Core::BitArray bits;
  43. test.checkFalse(bits.resize(100, 13), "resize works 3");
  44. int seed = 534;
  45. for(int k = 0; k < 20; k++) {
  46. for(int i = 0; i < bits.getLength(); i++) {
  47. seed = seed * 636455 + 53453;
  48. bits.set(i, seed & (0x1FFF));
  49. data[i] = seed & (0x1FFF);
  50. }
  51. }
  52. for(int i = 0; i < bits.getLength(); i++) {
  53. test.checkEqual(data[i], bits.get(i),
  54. "set and read correct value with random input");
  55. }
  56. test.checkFalse(bits.resize(bits.getLength(), bits.getBits() + 1),
  57. "resize works 4");
  58. test.checkEqual(14, bits.getBits(), "corrects bits after resize");
  59. test.checkEqual(100, bits.getLength(), "correct length after resize");
  60. for(int i = 0; i < bits.getLength(); i++) {
  61. test.checkEqual(
  62. data[i], bits.get(i),
  63. "set and read correct value with random input after resize");
  64. }
  65. }
  66. static void testReadOnly(Core::Test& test) {
  67. Core::BitArray bits;
  68. test.checkFalse(bits.resize(4, 3), "resize works 5");
  69. bits.set(0, 1).set(1, 2).set(2, 3).set(3, 4);
  70. Core::BitArray copy;
  71. test.checkFalse(copy.copyFrom(bits), "copy works");
  72. const Core::BitArray bits2 = Core::move(copy);
  73. test.checkEqual(1, bits2.get(0), "can read from const 1");
  74. test.checkEqual(2, bits2.get(1), "can read from const 2");
  75. test.checkEqual(3, bits2.get(2), "can read from const 3");
  76. test.checkEqual(4, bits2.get(3), "can read from const 4");
  77. }
  78. static void testSelect(Core::Test& test) {
  79. Core::BitArray bits;
  80. test.checkFalse(bits.resize(90, 1), "resize works 6");
  81. bits.fill(0);
  82. bits.set(0, 1).set(5, 1).set(20, 1).set(31, 1);
  83. bits.set(32, 1).set(33, 1).set(60, 1);
  84. test.checkEqual(-1, bits.select(-1), "select -1 bit");
  85. test.checkEqual(-1, bits.select(0), "select 0 bit");
  86. test.checkEqual(0, bits.select(1), "select 1 bit");
  87. test.checkEqual(5, bits.select(2), "select 2 bit");
  88. test.checkEqual(20, bits.select(3), "select 3 bit");
  89. test.checkEqual(31, bits.select(4), "select 4 bit");
  90. test.checkEqual(32, bits.select(5), "select 5 bit");
  91. test.checkEqual(33, bits.select(6), "select 6 bit");
  92. test.checkEqual(60, bits.select(7), "select 7 bit");
  93. test.checkEqual(-1, bits.select(8), "select 8 bit");
  94. }
  95. static void testToString1(Core::Test& test) {
  96. Core::BitArray bits;
  97. test.checkFalse(bits.resize(4, 3), "resize works 7");
  98. bits.set(0, 1).set(1, 2).set(2, 3).set(3, 4);
  99. test.checkEqual(build(test, "[1, 2, 3, 4]"), build(test, bits),
  100. "bit array to string 1");
  101. }
  102. static void testToString2(Core::Test& test) {
  103. Core::BitArray bits;
  104. test.checkFalse(bits.resize(1, 3), "resize works 8");
  105. bits.set(0, 1);
  106. test.checkEqual(build(test, "[1]"), build(test, bits),
  107. "bit array to string 2");
  108. }
  109. static void testToString3(Core::Test& test) {
  110. Core::BitArray bits;
  111. test.checkEqual(build(test, "[]"), build(test, bits),
  112. "bit array to string 3");
  113. }
  114. void Core::BitArrayTests::test() {
  115. Core::Test test("Core::BitArray");
  116. testSetRead(test);
  117. testOutOfBoundsSetRead(test);
  118. testBigSetRead(test);
  119. testRandomSetReadResize(test);
  120. testReadOnly(test);
  121. testSelect(test);
  122. testToString1(test);
  123. testToString2(test);
  124. testToString3(test);
  125. test.finalize();
  126. }