BitArrayTests.cpp 3.3 KB

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