12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- #include "tests/RingBufferTests.h"
- #include "data/RingBuffer.h"
- #include "tests/Test.h"
- static void testReadAndWrite(Test& test) {
- RingBuffer<int, 5> buffer;
- test.checkFalse(buffer.canRemove(), "no read from empty buffer");
- buffer.add(4);
- test.checkTrue(buffer.canRemove(), "can read from buffer with value");
- test.checkEqual(4, buffer[0], "can read value");
- test.checkFalse(buffer.remove(), "can remove value");
- test.checkFalse(buffer.canRemove(), "value is gone after read");
- }
- static void testOverflow(Test& test) {
- RingBuffer<int, 3> buffer;
- bool r1 = buffer.add(1);
- bool r2 = buffer.add(2);
- bool r3 = buffer.add(3);
- bool r4 = buffer.add(4);
- bool r5 = buffer.add(5);
- test.checkFalse(r1, "check buffer write return value 1");
- test.checkFalse(r2, "check buffer write return value 2");
- test.checkFalse(r3, "check buffer write return value 3");
- test.checkTrue(r4, "check buffer write return value 4");
- test.checkTrue(r5, "check buffer write return value 5");
- test.checkEqual(1, buffer[0], "can read value from overflowed buffer 1");
- test.checkFalse(buffer.remove(), "can remove from overflowed buffer 1");
- test.checkEqual(2, buffer[0], "can read value from overflowed buffer 2");
- test.checkFalse(buffer.remove(), "can remove from overflowed buffer 2");
- test.checkEqual(3, buffer[0], "can read value from overflowed buffer 3");
- test.checkFalse(buffer.remove(), "can read value from overflowed buffer 3");
- test.checkFalse(buffer.canRemove(), "values are gone after full read");
- }
- static void testRefill(Test& test) {
- RingBuffer<int, 3> buffer;
- buffer.add(1);
- buffer.add(2);
- buffer.add(3);
- buffer.add(4);
- test.checkTrue(buffer.canRemove(), "values are ready to be read 2");
- test.checkEqual(1, buffer[0], "can read value from overflowed buffer 1");
- test.checkFalse(buffer.remove(), "can remove from overflowed buffer 1");
- test.checkEqual(2, buffer[0], "can read value from overflowed buffer 2");
- test.checkFalse(buffer.remove(), "can remove from overflowed buffer 2");
- test.checkEqual(3, buffer[0], "can read value from overflowed buffer 3");
- test.checkFalse(buffer.remove(), "can read value from overflowed buffer 3");
- test.checkFalse(buffer.canRemove(), "values are gone after full read 1");
- buffer.add(5);
- buffer.add(6);
- test.checkTrue(buffer.canRemove(), "values are ready to be read 2");
- test.checkEqual(5, buffer[0], "can read value from overflowed buffer 4");
- test.checkFalse(buffer.remove(), "can remove from overflowed buffer 4");
- test.checkEqual(6, buffer[0], "can read value from overflowed buffer 5");
- test.checkFalse(buffer.remove(), "can read value from overflowed buffer 5");
- test.checkFalse(buffer.canRemove(), "values are gone after full read 1");
- }
- static void testClear(Test& test) {
- RingBuffer<int, 3> buffer;
- buffer.add(1);
- buffer.add(2);
- buffer.clear();
- test.checkFalse(buffer.canRemove(), "no read after clear");
- }
- void RingBufferTests::test() {
- Test test("RingBuffer");
- testReadAndWrite(test);
- testOverflow(test);
- testRefill(test);
- testClear(test);
- test.finalize();
- }
|