BitArrayTests.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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.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 testToString1(Test& test) {
  59. BitArray bits(4, 3);
  60. bits.set(0, 1).set(1, 2).set(2, 3).set(3, 4);
  61. test.checkEqual(String("[1, 2, 3, 4]"), String(bits),
  62. "bit array to string 1");
  63. }
  64. static void testToString2(Test& test) {
  65. BitArray bits(1, 3);
  66. bits.set(0, 1);
  67. test.checkEqual(String("[1]"), String(bits), "bit array to string 1");
  68. }
  69. void BitArrayTests::test() {
  70. Test test("BitArray");
  71. testSetRead(test);
  72. testBigSetRead(test);
  73. testRandomSetReadResize(test);
  74. testReadOnly(test);
  75. testToString1(test);
  76. testToString2(test);
  77. test.finalize();
  78. }