12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- #include "tests/RingBufferTests.h"
- #include "tests/Test.h"
- #include "utils/RingBuffer.h"
- static void testReadAndWrite(Test& test) {
- RingBuffer<int, 5> buffer;
- test.checkEqual(false, buffer.canRead(), "no read from empty buffer");
- buffer.write(4);
- test.checkEqual(true, buffer.canRead(), "can read from buffer with value");
- test.checkEqual(4, buffer.read(), "can read value");
- test.checkEqual(false, buffer.canRead(), "value is gone after read");
- }
- static void testOverflow(Test& test) {
- RingBuffer<int, 3> buffer;
- bool r1 = buffer.write(1);
- bool r2 = buffer.write(2);
- bool r3 = buffer.write(3);
- bool r4 = buffer.write(4);
- bool r5 = buffer.write(5);
- test.checkEqual(false, r1, "check buffer write return value 1");
- test.checkEqual(false, r2, "check buffer write return value 2");
- test.checkEqual(false, r3, "check buffer write return value 3");
- test.checkEqual(true, r4, "check buffer write return value 4");
- test.checkEqual(true, r5, "check buffer write return value 5");
- test.checkEqual(1, buffer.read(), "can read value from overflowed buffer 1");
- test.checkEqual(2, buffer.read(), "can read value from overflowed buffer 2");
- test.checkEqual(3, buffer.read(), "can read value from overflowed buffer 3");
- test.checkEqual(false, buffer.canRead(), "values are gone after full read");
- }
- static void testRefill(Test& test) {
- RingBuffer<int, 3> buffer;
- buffer.write(1);
- buffer.write(2);
- buffer.write(3);
- buffer.write(4);
- test.checkEqual(true, buffer.canRead(), "values are ready to be read 2");
- test.checkEqual(1, buffer.read(), "can read value from overflowed buffer 1");
- test.checkEqual(2, buffer.read(), "can read value from overflowed buffer 2");
- test.checkEqual(3, buffer.read(), "can read value from overflowed buffer 3");
- test.checkEqual(false, buffer.canRead(), "values are gone after full read 1");
- buffer.write(5);
- buffer.write(6);
- test.checkEqual(true, buffer.canRead(), "values are ready to be read 2");
- test.checkEqual(5, buffer.read(), "can read value from overflowed buffer 4");
- test.checkEqual(6, buffer.read(), "can read value from overflowed buffer 5");
- test.checkEqual(false, buffer.canRead(), "values are gone after full read 1");
-
- }
- void RingBufferTests::test() {
- Test test("RingBuffer");
- testReadAndWrite(test);
- testOverflow(test);
- testRefill(test);
- test.finalize();
- }
|