RingBufferTests.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include "tests/RingBufferTests.h"
  2. #include "tests/Test.h"
  3. #include "utils/RingBuffer.h"
  4. static void testReadAndWrite(Test& test) {
  5. RingBuffer<int, 5> buffer;
  6. test.checkEqual(false, buffer.canRead(), "no read from empty buffer");
  7. buffer.write(4);
  8. test.checkEqual(true, buffer.canRead(), "can read from buffer with value");
  9. test.checkEqual(4, buffer.read(), "can read value");
  10. test.checkEqual(false, buffer.canRead(), "value is gone after read");
  11. }
  12. static void testOverflow(Test& test) {
  13. RingBuffer<int, 3> buffer;
  14. bool r1 = buffer.write(1);
  15. bool r2 = buffer.write(2);
  16. bool r3 = buffer.write(3);
  17. bool r4 = buffer.write(4);
  18. bool r5 = buffer.write(5);
  19. test.checkEqual(false, r1, "check buffer write return value 1");
  20. test.checkEqual(false, r2, "check buffer write return value 2");
  21. test.checkEqual(false, r3, "check buffer write return value 3");
  22. test.checkEqual(true, r4, "check buffer write return value 4");
  23. test.checkEqual(true, r5, "check buffer write return value 5");
  24. test.checkEqual(1, buffer.read(), "can read value from overflowed buffer 1");
  25. test.checkEqual(2, buffer.read(), "can read value from overflowed buffer 2");
  26. test.checkEqual(3, buffer.read(), "can read value from overflowed buffer 3");
  27. test.checkEqual(false, buffer.canRead(), "values are gone after full read");
  28. }
  29. static void testRefill(Test& test) {
  30. RingBuffer<int, 3> buffer;
  31. buffer.write(1);
  32. buffer.write(2);
  33. buffer.write(3);
  34. buffer.write(4);
  35. test.checkEqual(true, buffer.canRead(), "values are ready to be read 2");
  36. test.checkEqual(1, buffer.read(), "can read value from overflowed buffer 1");
  37. test.checkEqual(2, buffer.read(), "can read value from overflowed buffer 2");
  38. test.checkEqual(3, buffer.read(), "can read value from overflowed buffer 3");
  39. test.checkEqual(false, buffer.canRead(), "values are gone after full read 1");
  40. buffer.write(5);
  41. buffer.write(6);
  42. test.checkEqual(true, buffer.canRead(), "values are ready to be read 2");
  43. test.checkEqual(5, buffer.read(), "can read value from overflowed buffer 4");
  44. test.checkEqual(6, buffer.read(), "can read value from overflowed buffer 5");
  45. test.checkEqual(false, buffer.canRead(), "values are gone after full read 1");
  46. }
  47. void RingBufferTests::test() {
  48. Test test("RingBuffer");
  49. testReadAndWrite(test);
  50. testOverflow(test);
  51. testRefill(test);
  52. test.finalize();
  53. }