BitArrayTests.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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");
  13. test.checkEqual(2, static_cast<int> (bits[1]), "set and read correct value");
  14. test.checkEqual(3, static_cast<int> (bits[2]), "set and read correct value");
  15. test.checkEqual(4, static_cast<int> (bits[3]), "set and read correct value");
  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");
  50. test.checkEqual(2, bits2[1], "can read from const");
  51. test.checkEqual(3, bits2[2], "can read from const");
  52. test.checkEqual(4, bits2[3], "can read from const");
  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");
  59. test.checkEqual(7, static_cast<int> (bits[1]), "chained set sets correct value");
  60. test.checkEqual(2, static_cast<int> (bits[2]), "chained set sets correct value");
  61. test.checkEqual(7, static_cast<int> (bits[3]), "chained set sets correct value");
  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. String s;
  70. s.append(bits);
  71. test.checkEqual(String("[1, 2, 3, 4]"), s, "bit array to string 1");
  72. }
  73. static void testToString2(Test& test) {
  74. BitArray<1, 3> a;
  75. a[0] = 1;
  76. String s;
  77. s.append(a);
  78. test.checkEqual(String("[1]"), s, "bit array to string 1");
  79. }
  80. void BitArrayTests::test() {
  81. Test test("BitArray");
  82. testSetRead(test);
  83. testBigSetRead(test);
  84. testRandomSetRead(test);
  85. testReadOnly(test);
  86. testChainedSet(test);
  87. testToString1(test);
  88. testToString2(test);
  89. test.finalize();
  90. }