BitArrayTests.cpp 4.7 KB

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