BitArrayTests.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include "tests/BitArrayTests.h"
  2. #include "data/BitArray.h"
  3. #include "tests/Test.h"
  4. #include "utils/StringBuffer.h"
  5. typedef StringBuffer<50> String;
  6. static void testSetRead(Test& test) {
  7. BitArray bits(4, 3);
  8. bits.set(0, 1).set(1, 2).set(2, 3).set(3, 4);
  9. test.checkEqual(1, bits.get(0), "set and read correct value 1");
  10. test.checkEqual(2, bits.get(1), "set and read correct value 2");
  11. test.checkEqual(3, bits.get(2), "set and read correct value 3");
  12. test.checkEqual(4, bits.get(3), "set and read correct value 4");
  13. }
  14. static void testBigSetRead(Test& test) {
  15. BitArray bits(100, 13);
  16. for(int i = 0; i < bits.getLength(); i++) {
  17. bits.set(i, i);
  18. }
  19. for(int i = 0; i < bits.getLength(); i++) {
  20. test.checkEqual(i, bits.get(i),
  21. "set and read correct value over long array");
  22. }
  23. }
  24. static void testRandomSetReadResize(Test& test) {
  25. const int length = 100;
  26. int data[length];
  27. BitArray bits(100, 13);
  28. int seed = 534;
  29. for(int k = 0; k < 20; k++) {
  30. for(int i = 0; i < bits.getLength(); i++) {
  31. seed = seed * 636455 + 53453;
  32. bits.set(i, seed & (0x1FFF));
  33. data[i] = seed & (0x1FFF);
  34. }
  35. }
  36. for(int i = 0; i < bits.getLength(); i++) {
  37. test.checkEqual(data[i], bits.get(i),
  38. "set and read correct value with random input");
  39. }
  40. bits.resize(bits.getLength(), bits.getBits() + 1);
  41. test.checkEqual(14, bits.getBits(), "corrects bits after resize");
  42. test.checkEqual(100, bits.getLength(), "correct length after resize");
  43. for(int i = 0; i < bits.getLength(); i++) {
  44. test.checkEqual(
  45. data[i], bits.get(i),
  46. "set and read correct value with random input after resize");
  47. }
  48. }
  49. static void testReadOnly(Test& test) {
  50. BitArray bits(4, 3);
  51. bits.set(0, 1).set(1, 2).set(2, 3).set(3, 4);
  52. const BitArray bits2 = bits;
  53. test.checkEqual(1, bits2.get(0), "can read from const 1");
  54. test.checkEqual(2, bits2.get(1), "can read from const 2");
  55. test.checkEqual(3, bits2.get(2), "can read from const 3");
  56. test.checkEqual(4, bits2.get(3), "can read from const 4");
  57. }
  58. static void testSelect(Test& test) {
  59. BitArray bits(90, 1);
  60. bits.fill(0);
  61. bits.set(0, 1).set(5, 1).set(20, 1).set(31, 1);
  62. bits.set(32, 1).set(33, 1).set(60, 1);
  63. test.checkEqual(-1, bits.select(-1), "select -1 bit");
  64. test.checkEqual(-1, bits.select(0), "select 0 bit");
  65. test.checkEqual(0, bits.select(1), "select 1 bit");
  66. test.checkEqual(5, bits.select(2), "select 2 bit");
  67. test.checkEqual(20, bits.select(3), "select 3 bit");
  68. test.checkEqual(31, bits.select(4), "select 4 bit");
  69. test.checkEqual(32, bits.select(5), "select 5 bit");
  70. test.checkEqual(33, bits.select(6), "select 6 bit");
  71. test.checkEqual(60, bits.select(7), "select 7 bit");
  72. test.checkEqual(-1, bits.select(8), "select 8 bit");
  73. }
  74. static void testToString1(Test& test) {
  75. BitArray bits(4, 3);
  76. bits.set(0, 1).set(1, 2).set(2, 3).set(3, 4);
  77. test.checkEqual(String("[1, 2, 3, 4]"), String(bits),
  78. "bit array to string 1");
  79. }
  80. static void testToString2(Test& test) {
  81. BitArray bits(1, 3);
  82. bits.set(0, 1);
  83. test.checkEqual(String("[1]"), String(bits), "bit array to string 1");
  84. }
  85. void BitArrayTests::test() {
  86. Test test("BitArray");
  87. testSetRead(test);
  88. testBigSetRead(test);
  89. testRandomSetReadResize(test);
  90. testReadOnly(test);
  91. testSelect(test);
  92. testToString1(test);
  93. testToString2(test);
  94. test.finalize();
  95. }