RingBufferTests.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include "tests/RingBufferTests.h"
  2. #include "data/RingBuffer.h"
  3. #include "tests/Test.h"
  4. static void testReadAndWrite(Test& test) {
  5. RingBuffer<int, 5> buffer;
  6. test.checkFalse(buffer.canRemove(), "no read from empty buffer");
  7. buffer.add(4);
  8. test.checkTrue(buffer.canRemove(), "can read from buffer with value");
  9. test.checkEqual(4, buffer[0], "can read value");
  10. test.checkFalse(buffer.remove(), "can remove value");
  11. test.checkFalse(buffer.canRemove(), "value is gone after read");
  12. }
  13. static void testOverflow(Test& test) {
  14. RingBuffer<int, 3> buffer;
  15. bool r1 = buffer.add(1);
  16. bool r2 = buffer.add(2);
  17. bool r3 = buffer.add(3);
  18. bool r4 = buffer.add(4);
  19. bool r5 = buffer.add(5);
  20. test.checkFalse(r1, "check buffer write return value 1");
  21. test.checkFalse(r2, "check buffer write return value 2");
  22. test.checkFalse(r3, "check buffer write return value 3");
  23. test.checkTrue(r4, "check buffer write return value 4");
  24. test.checkTrue(r5, "check buffer write return value 5");
  25. test.checkEqual(1, buffer[0], "can read value from overflowed buffer 1");
  26. test.checkFalse(buffer.remove(), "can remove from overflowed buffer 1");
  27. test.checkEqual(2, buffer[0], "can read value from overflowed buffer 2");
  28. test.checkFalse(buffer.remove(), "can remove from overflowed buffer 2");
  29. test.checkEqual(3, buffer[0], "can read value from overflowed buffer 3");
  30. test.checkFalse(buffer.remove(), "can read value from overflowed buffer 3");
  31. test.checkFalse(buffer.canRemove(), "values are gone after full read");
  32. }
  33. static void testRefill(Test& test) {
  34. RingBuffer<int, 3> buffer;
  35. buffer.add(1);
  36. buffer.add(2);
  37. buffer.add(3);
  38. buffer.add(4);
  39. test.checkTrue(buffer.canRemove(), "values are ready to be read 2");
  40. test.checkEqual(1, buffer[0], "can read value from overflowed buffer 1");
  41. test.checkFalse(buffer.remove(), "can remove from overflowed buffer 1");
  42. test.checkEqual(2, buffer[0], "can read value from overflowed buffer 2");
  43. test.checkFalse(buffer.remove(), "can remove from overflowed buffer 2");
  44. test.checkEqual(3, buffer[0], "can read value from overflowed buffer 3");
  45. test.checkFalse(buffer.remove(), "can read value from overflowed buffer 3");
  46. test.checkFalse(buffer.canRemove(), "values are gone after full read 1");
  47. buffer.add(5);
  48. buffer.add(6);
  49. test.checkTrue(buffer.canRemove(), "values are ready to be read 2");
  50. test.checkEqual(5, buffer[0], "can read value from overflowed buffer 4");
  51. test.checkFalse(buffer.remove(), "can remove from overflowed buffer 4");
  52. test.checkEqual(6, buffer[0], "can read value from overflowed buffer 5");
  53. test.checkFalse(buffer.remove(), "can read value from overflowed buffer 5");
  54. test.checkFalse(buffer.canRemove(), "values are gone after full read 1");
  55. }
  56. static void testClear(Test& test) {
  57. RingBuffer<int, 3> buffer;
  58. buffer.add(1);
  59. buffer.add(2);
  60. buffer.clear();
  61. test.checkFalse(buffer.canRemove(), "no read after clear");
  62. }
  63. void RingBufferTests::test() {
  64. Test test("RingBuffer");
  65. testReadAndWrite(test);
  66. testOverflow(test);
  67. testRefill(test);
  68. testClear(test);
  69. test.finalize();
  70. }