BitArrayTests.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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<4, 3> bits;
  8. bits[0] = 1;
  9. bits[1] = 2;
  10. bits[2] = 3;
  11. bits[3] = 4;
  12. test.checkEqual(1, static_cast<int> (bits[0]), "set and read correct value 1");
  13. test.checkEqual(2, static_cast<int> (bits[1]), "set and read correct value 2");
  14. test.checkEqual(3, static_cast<int> (bits[2]), "set and read correct value 3");
  15. test.checkEqual(4, static_cast<int> (bits[3]), "set and read correct value 4");
  16. }
  17. static void testBigSetRead(Test& test) {
  18. BitArray<100, 13> bits;
  19. for(int i = 0; i < bits.getLength(); i++) {
  20. bits[i] = i;
  21. }
  22. for(int i = 0; i < bits.getLength(); i++) {
  23. test.checkEqual(i, static_cast<int> (bits[i]), "set and read correct value over long array");
  24. }
  25. }
  26. static void testRandomSetRead(Test& test) {
  27. const int length = 100;
  28. int data[length];
  29. BitArray<100, 13> bits;
  30. int seed = 534;
  31. for(int k = 0; k < 20; k++) {
  32. for(int i = 0; i < bits.getLength(); i++) {
  33. seed = seed * 636455 + 53453;
  34. bits[i] = seed & (0x1FFF);
  35. data[i] = seed & (0x1FFF);
  36. }
  37. }
  38. for(int i = 0; i < bits.getLength(); i++) {
  39. test.checkEqual(data[i], static_cast<int> (bits[i]), "set and read correct value with random input");
  40. }
  41. }
  42. static void testReadOnly(Test& test) {
  43. BitArray<4, 3> bits;
  44. bits[0] = 1;
  45. bits[1] = 2;
  46. bits[2] = 3;
  47. bits[3] = 4;
  48. const BitArray<4, 3> bits2 = bits;
  49. test.checkEqual(1, bits2[0], "can read from const 1");
  50. test.checkEqual(2, bits2[1], "can read from const 2");
  51. test.checkEqual(3, bits2[2], "can read from const 3");
  52. test.checkEqual(4, bits2[3], "can read from const 4");
  53. }
  54. static void testChainedSet(Test& test) {
  55. BitArray<4, 3> bits;
  56. bits[0] = bits[2] = bits[3] = 2;
  57. bits[3] = bits[1] = 7;
  58. test.checkEqual(2, static_cast<int> (bits[0]), "chained set sets correct value 1");
  59. test.checkEqual(7, static_cast<int> (bits[1]), "chained set sets correct value 2");
  60. test.checkEqual(2, static_cast<int> (bits[2]), "chained set sets correct value 3");
  61. test.checkEqual(7, static_cast<int> (bits[3]), "chained set sets correct value 4");
  62. }
  63. static void testToString1(Test& test) {
  64. BitArray<4, 3> bits;
  65. bits[0] = 1;
  66. bits[1] = 2;
  67. bits[2] = 3;
  68. bits[3] = 4;
  69. test.checkEqual(String("[1, 2, 3, 4]"), String(bits), "bit array to string 1");
  70. }
  71. static void testToString2(Test& test) {
  72. BitArray<1, 3> a;
  73. a[0] = 1;
  74. test.checkEqual(String("[1]"), String(a), "bit array to string 1");
  75. }
  76. void BitArrayTests::test() {
  77. Test test("BitArray");
  78. testSetRead(test);
  79. testBigSetRead(test);
  80. testRandomSetRead(test);
  81. testReadOnly(test);
  82. testChainedSet(test);
  83. testToString1(test);
  84. testToString2(test);
  85. test.finalize();
  86. }