BitArrayTests.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include "tests/BitArrayTests.h"
  2. #include "tests/Test.h"
  3. #include "utils/BitArray.h"
  4. static void testSetRead(Test& test) {
  5. BitArray<4, 3> bits;
  6. bits[0] = 1;
  7. bits[1] = 2;
  8. bits[2] = 3;
  9. bits[3] = 4;
  10. test.checkEqual(1, static_cast<int> (bits[0]), "set and read correct value");
  11. test.checkEqual(2, static_cast<int> (bits[1]), "set and read correct value");
  12. test.checkEqual(3, static_cast<int> (bits[2]), "set and read correct value");
  13. test.checkEqual(4, static_cast<int> (bits[3]), "set and read correct value");
  14. }
  15. static void testBigSetRead(Test& test) {
  16. BitArray<100, 13> bits;
  17. for(int i = 0; i < bits.getLength(); i++) {
  18. bits[i] = i;
  19. }
  20. for(int i = 0; i < bits.getLength(); i++) {
  21. test.checkEqual(i, static_cast<int> (bits[i]), "set and read correct value over long array");
  22. }
  23. }
  24. static void testRandomSetRead(Test& test) {
  25. const int length = 100;
  26. int data[length];
  27. BitArray<100, 13> bits;
  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[i] = seed & (0x1FFF);
  33. data[i] = seed & (0x1FFF);
  34. }
  35. }
  36. for(int i = 0; i < bits.getLength(); i++) {
  37. test.checkEqual(data[i], static_cast<int> (bits[i]), "set and read correct value with random input");
  38. }
  39. }
  40. static void testReadOnly(Test& test) {
  41. BitArray<4, 3> bits;
  42. bits[0] = 1;
  43. bits[1] = 2;
  44. bits[2] = 3;
  45. bits[3] = 4;
  46. const BitArray<4, 3> bits2 = bits;
  47. test.checkEqual(1, bits2[0], "can read from const");
  48. test.checkEqual(2, bits2[1], "can read from const");
  49. test.checkEqual(3, bits2[2], "can read from const");
  50. test.checkEqual(4, bits2[3], "can read from const");
  51. }
  52. static void testChainedSet(Test& test) {
  53. BitArray<4, 3> bits;
  54. bits[0] = bits[2] = bits[3] = 2;
  55. bits[3] = bits[1] = 7;
  56. test.checkEqual(2, static_cast<int>(bits[0]), "chained set sets correct value");
  57. test.checkEqual(7, static_cast<int>(bits[1]), "chained set sets correct value");
  58. test.checkEqual(2, static_cast<int>(bits[2]), "chained set sets correct value");
  59. test.checkEqual(7, static_cast<int>(bits[3]), "chained set sets correct value");
  60. }
  61. void BitArrayTests::test() {
  62. Test test("BitArray");
  63. testSetRead(test);
  64. testBigSetRead(test);
  65. testRandomSetRead(test);
  66. testReadOnly(test);
  67. testChainedSet(test);
  68. test.finalize();
  69. }