BitArrayTests.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include "tests/BitArrayTests.h"
  2. #include "tests/Test.h"
  3. #include "utils/BitArray.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), "set and read correct value over long array");
  21. }
  22. }
  23. static void testRandomSetReadResize(Test& test) {
  24. const int length = 100;
  25. int data[length];
  26. BitArray bits(100, 13);
  27. int seed = 534;
  28. for(int k = 0; k < 20; k++) {
  29. for(int i = 0; i < bits.getLength(); i++) {
  30. seed = seed * 636455 + 53453;
  31. bits.set(i, seed & (0x1FFF));
  32. data[i] = seed & (0x1FFF);
  33. }
  34. }
  35. for(int i = 0; i < bits.getLength(); i++) {
  36. test.checkEqual(data[i], bits.get(i), "set and read correct value with random input");
  37. }
  38. bits.resize(bits.getBits() + 1);
  39. test.checkEqual(14, bits.getBits(), "corrects bits after resize");
  40. test.checkEqual(100, bits.getLength(), "correct length after resize");
  41. for(int i = 0; i < bits.getLength(); i++) {
  42. test.checkEqual(data[i], bits.get(i), "set and read correct value with random input after resize");
  43. }
  44. }
  45. static void testReadOnly(Test& test) {
  46. BitArray bits(4, 3);
  47. bits.set(0, 1).set(1, 2).set(2, 3).set(3, 4);
  48. const BitArray bits2 = bits;
  49. test.checkEqual(1, bits2.get(0), "can read from const 1");
  50. test.checkEqual(2, bits2.get(1), "can read from const 2");
  51. test.checkEqual(3, bits2.get(2), "can read from const 3");
  52. test.checkEqual(4, bits2.get(3), "can read from const 4");
  53. }
  54. static void testToString1(Test& test) {
  55. BitArray bits(4, 3);
  56. bits.set(0, 1).set(1, 2).set(2, 3).set(3, 4);
  57. test.checkEqual(String("[1, 2, 3, 4]"), String(bits), "bit array to string 1");
  58. }
  59. static void testToString2(Test& test) {
  60. BitArray bits(1, 3);
  61. bits.set(0, 1);
  62. test.checkEqual(String("[1]"), String(bits), "bit array to string 1");
  63. }
  64. void BitArrayTests::test() {
  65. Test test("BitArray");
  66. testSetRead(test);
  67. testBigSetRead(test);
  68. testRandomSetReadResize(test);
  69. testReadOnly(test);
  70. testToString1(test);
  71. testToString2(test);
  72. test.finalize();
  73. }