RingBufferTests.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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(),
  25. "can read value from overflowed buffer 1");
  26. test.checkEqual(2, buffer.read(),
  27. "can read value from overflowed buffer 2");
  28. test.checkEqual(3, buffer.read(),
  29. "can read value from overflowed buffer 3");
  30. test.checkEqual(false, buffer.canRead(), "values are gone after full read");
  31. }
  32. static void testRefill(Test& test) {
  33. RingBuffer<int, 3> buffer;
  34. buffer.write(1);
  35. buffer.write(2);
  36. buffer.write(3);
  37. buffer.write(4);
  38. test.checkEqual(true, buffer.canRead(), "values are ready to be read 2");
  39. test.checkEqual(1, buffer.read(),
  40. "can read value from overflowed buffer 1");
  41. test.checkEqual(2, buffer.read(),
  42. "can read value from overflowed buffer 2");
  43. test.checkEqual(3, buffer.read(),
  44. "can read value from overflowed buffer 3");
  45. test.checkEqual(false, buffer.canRead(),
  46. "values are gone after full read 1");
  47. buffer.write(5);
  48. buffer.write(6);
  49. test.checkEqual(true, buffer.canRead(), "values are ready to be read 2");
  50. test.checkEqual(5, buffer.read(),
  51. "can read value from overflowed buffer 4");
  52. test.checkEqual(6, buffer.read(),
  53. "can read value from overflowed buffer 5");
  54. test.checkEqual(false, buffer.canRead(),
  55. "values are gone after full read 1");
  56. }
  57. static void testClear(Test& test) {
  58. RingBuffer<int, 3> buffer;
  59. buffer.write(1);
  60. buffer.write(2);
  61. buffer.clear();
  62. test.checkEqual(false, buffer.canRead(), "no read after clear");
  63. }
  64. void RingBufferTests::test() {
  65. Test test("RingBuffer");
  66. testReadAndWrite(test);
  67. testOverflow(test);
  68. testRefill(test);
  69. testClear(test);
  70. test.finalize();
  71. }