BitArrayTests.cpp 4.4 KB

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