BitArrayTests.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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(const T& t) {
  7. String s;
  8. CORE_TEST_FALSE(s.append(t));
  9. return s;
  10. }
  11. static void testSetRead() {
  12. Core::BitArray bits;
  13. CORE_TEST_FALSE(bits.resize(4, 3));
  14. bits.set(0, 1).set(1, 2).set(2, 3).set(3, 4);
  15. CORE_TEST_EQUAL(1, bits.get(0));
  16. CORE_TEST_EQUAL(2, bits.get(1));
  17. CORE_TEST_EQUAL(3, bits.get(2));
  18. CORE_TEST_EQUAL(4, bits.get(3));
  19. }
  20. static void testOutOfBoundsSetRead() {
  21. Core::BitArray bits;
  22. bits.set(0, 1).set(1, 2).set(2, 3).set(3, 4);
  23. CORE_TEST_EQUAL(0, bits.get(0));
  24. CORE_TEST_EQUAL(0, bits.get(1));
  25. CORE_TEST_EQUAL(0, bits.get(2));
  26. CORE_TEST_EQUAL(0, bits.get(3));
  27. }
  28. static void testBigSetRead() {
  29. Core::BitArray bits;
  30. CORE_TEST_FALSE(bits.resize(100, 13));
  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. CORE_TEST_EQUAL(i, bits.get(i));
  36. }
  37. }
  38. static void testRandomSetReadResize() {
  39. const int length = 100;
  40. int data[length];
  41. Core::BitArray bits;
  42. CORE_TEST_FALSE(bits.resize(100, 13));
  43. int seed = 534;
  44. for(int k = 0; k < 20; k++) {
  45. for(int i = 0; i < bits.getLength(); i++) {
  46. seed = seed * 636455 + 53453;
  47. bits.set(i, seed & (0x1FFF));
  48. data[i] = seed & (0x1FFF);
  49. }
  50. }
  51. for(int i = 0; i < bits.getLength(); i++) {
  52. CORE_TEST_EQUAL(data[i], bits.get(i));
  53. }
  54. CORE_TEST_FALSE(bits.resize(bits.getLength(), bits.getBits() + 1));
  55. CORE_TEST_EQUAL(14, bits.getBits());
  56. CORE_TEST_EQUAL(100, bits.getLength());
  57. for(int i = 0; i < bits.getLength(); i++) {
  58. CORE_TEST_EQUAL(data[i], bits.get(i));
  59. }
  60. }
  61. static void testReadOnly() {
  62. Core::BitArray bits;
  63. CORE_TEST_FALSE(bits.resize(4, 3));
  64. bits.set(0, 1).set(1, 2).set(2, 3).set(3, 4);
  65. Core::BitArray copy;
  66. CORE_TEST_FALSE(copy.copyFrom(bits));
  67. const Core::BitArray bits2 = Core::move(copy);
  68. CORE_TEST_EQUAL(1, bits2.get(0));
  69. CORE_TEST_EQUAL(2, bits2.get(1));
  70. CORE_TEST_EQUAL(3, bits2.get(2));
  71. CORE_TEST_EQUAL(4, bits2.get(3));
  72. }
  73. static void testSelect() {
  74. Core::BitArray bits;
  75. CORE_TEST_FALSE(bits.resize(90, 1));
  76. bits.fill(0);
  77. bits.set(0, 1).set(5, 1).set(20, 1).set(31, 1);
  78. bits.set(32, 1).set(33, 1).set(60, 1);
  79. CORE_TEST_EQUAL(-1, bits.select(-1));
  80. CORE_TEST_EQUAL(-1, bits.select(0));
  81. CORE_TEST_EQUAL(0, bits.select(1));
  82. CORE_TEST_EQUAL(5, bits.select(2));
  83. CORE_TEST_EQUAL(20, bits.select(3));
  84. CORE_TEST_EQUAL(31, bits.select(4));
  85. CORE_TEST_EQUAL(32, bits.select(5));
  86. CORE_TEST_EQUAL(33, bits.select(6));
  87. CORE_TEST_EQUAL(60, bits.select(7));
  88. CORE_TEST_EQUAL(-1, bits.select(8));
  89. }
  90. static void testToString1() {
  91. Core::BitArray bits;
  92. CORE_TEST_FALSE(bits.resize(4, 3));
  93. bits.set(0, 1).set(1, 2).set(2, 3).set(3, 4);
  94. CORE_TEST_EQUAL(build("[1, 2, 3, 4]"), build(bits));
  95. }
  96. static void testToString2() {
  97. Core::BitArray bits;
  98. CORE_TEST_FALSE(bits.resize(1, 3));
  99. bits.set(0, 1);
  100. CORE_TEST_EQUAL(build("[1]"), build(bits));
  101. }
  102. static void testToString3() {
  103. Core::BitArray bits;
  104. CORE_TEST_EQUAL(build("[]"), build(bits));
  105. }
  106. void Core::BitArrayTests::test() {
  107. testSetRead();
  108. testOutOfBoundsSetRead();
  109. testBigSetRead();
  110. testRandomSetReadResize();
  111. testReadOnly();
  112. testSelect();
  113. testToString1();
  114. testToString2();
  115. testToString3();
  116. }