BitArrayTests.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. void Core::testBitArray() {
  99. testSetRead();
  100. testOutOfBoundsSetRead();
  101. testBigSetRead();
  102. testRandomSetReadResize();
  103. testReadOnly();
  104. testSelect();
  105. testToString1();
  106. testToString2();
  107. testToString3();
  108. }