2014-03-15 17:30:08 +01:00
|
|
|
/*
|
|
|
|
* Unit tests for class CircularBuffer.
|
|
|
|
*/
|
|
|
|
|
2014-12-04 07:14:43 +01:00
|
|
|
#include "check.h"
|
2014-03-15 17:30:08 +01:00
|
|
|
#include "util/CircularBuffer.hxx"
|
|
|
|
|
|
|
|
#include <cppunit/TestFixture.h>
|
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
|
|
|
|
class TestCircularBuffer : public CppUnit::TestFixture {
|
|
|
|
CPPUNIT_TEST_SUITE(TestCircularBuffer);
|
|
|
|
CPPUNIT_TEST(TestIt);
|
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
|
|
|
|
public:
|
|
|
|
void TestIt() {
|
|
|
|
static size_t N = 8;
|
|
|
|
int data[N];
|
|
|
|
CircularBuffer<int> buffer(data, N);
|
|
|
|
|
2014-03-15 23:11:35 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(size_t(N), buffer.GetCapacity());
|
|
|
|
|
2014-03-15 17:30:08 +01:00
|
|
|
/* '.' = empty; 'O' = occupied; 'X' = blocked */
|
|
|
|
|
|
|
|
/* checks on empty buffer */
|
|
|
|
/* [.......X] */
|
2017-11-10 19:24:33 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(true, buffer.empty());
|
2014-03-15 17:30:08 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(false, buffer.IsFull());
|
2014-03-15 23:11:13 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(size_t(0), buffer.GetSize());
|
2014-03-15 23:29:07 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(size_t(7), buffer.GetSpace());
|
2017-11-10 19:24:33 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(true, buffer.Read().empty());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(false, buffer.Write().empty());
|
2014-03-15 17:30:08 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(&data[0], buffer.Write().data);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(size_t(7), buffer.Write().size);
|
|
|
|
|
|
|
|
/* append one element */
|
|
|
|
/* [O......X] */
|
|
|
|
buffer.Append(1);
|
2017-11-10 19:24:33 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(false, buffer.empty());
|
2014-03-15 17:30:08 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(false, buffer.IsFull());
|
2017-11-10 19:24:33 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(false, buffer.Read().empty());
|
2014-03-15 23:11:13 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(size_t(1), buffer.GetSize());
|
2014-03-15 23:29:07 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(size_t(6), buffer.GetSpace());
|
2014-03-15 17:30:08 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(size_t(1), buffer.Read().size);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(&data[0], buffer.Read().data);
|
2017-11-10 19:24:33 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(false, buffer.Write().empty());
|
2014-03-15 17:30:08 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(&data[1], buffer.Write().data);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(size_t(6), buffer.Write().size);
|
|
|
|
|
|
|
|
/* append 6 elements, buffer is now full */
|
|
|
|
/* [OOOOOOOX] */
|
|
|
|
buffer.Append(6);
|
2017-11-10 19:24:33 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(false, buffer.empty());
|
2014-03-15 17:30:08 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(true, buffer.IsFull());
|
2017-11-10 19:24:33 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(false, buffer.Read().empty());
|
2014-03-15 23:11:13 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(size_t(7), buffer.GetSize());
|
2014-03-15 23:29:07 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(size_t(0), buffer.GetSpace());
|
2014-03-15 17:30:08 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(size_t(7), buffer.Read().size);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(&data[0], buffer.Read().data);
|
2017-11-10 19:24:33 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(true, buffer.Write().empty());
|
2014-03-15 17:30:08 +01:00
|
|
|
|
|
|
|
/* consume [0]; can append one at [7] */
|
|
|
|
/* [XOOOOOO.] */
|
|
|
|
buffer.Consume(1);
|
2017-11-10 19:24:33 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(false, buffer.empty());
|
2014-03-15 17:30:08 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(false, buffer.IsFull());
|
2017-11-10 19:24:33 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(false, buffer.Read().empty());
|
2014-03-15 23:11:13 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(size_t(6), buffer.GetSize());
|
2014-03-15 23:29:07 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(size_t(1), buffer.GetSpace());
|
2014-03-15 17:30:08 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(size_t(6), buffer.Read().size);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(&data[1], buffer.Read().data);
|
2017-11-10 19:24:33 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(false, buffer.Write().empty());
|
2014-03-15 17:30:08 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(&data[7], buffer.Write().data);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(size_t(1), buffer.Write().size);
|
|
|
|
|
|
|
|
/* append one element; [0] is still empty but cannot
|
|
|
|
be written to because head==1 */
|
|
|
|
/* [XOOOOOOO] */
|
|
|
|
buffer.Append(1);
|
2017-11-10 19:24:33 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(false, buffer.empty());
|
2014-03-15 17:30:08 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(true, buffer.IsFull());
|
2017-11-10 19:24:33 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(false, buffer.Read().empty());
|
2014-03-15 23:11:13 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(size_t(7), buffer.GetSize());
|
2014-03-15 23:29:07 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(size_t(0), buffer.GetSpace());
|
2014-03-15 17:30:08 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(size_t(7), buffer.Read().size);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(&data[1], buffer.Read().data);
|
2017-11-10 19:24:33 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(true, buffer.Write().empty());
|
2014-03-15 17:30:08 +01:00
|
|
|
|
|
|
|
/* consume [1..3]; can append [0..2] */
|
|
|
|
/* [...XOOOO] */
|
|
|
|
buffer.Consume(3);
|
2017-11-10 19:24:33 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(false, buffer.empty());
|
2014-03-15 17:30:08 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(false, buffer.IsFull());
|
2017-11-10 19:24:33 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(false, buffer.Read().empty());
|
2014-03-15 23:11:13 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(size_t(4), buffer.GetSize());
|
2014-03-15 23:29:07 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(size_t(3), buffer.GetSpace());
|
2014-03-15 17:30:08 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(size_t(4), buffer.Read().size);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(&data[4], buffer.Read().data);
|
2017-11-10 19:24:33 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(false, buffer.Write().empty());
|
2014-03-15 17:30:08 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(&data[0], buffer.Write().data);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(size_t(3), buffer.Write().size);
|
|
|
|
|
|
|
|
/* append [0..1] */
|
|
|
|
/* [OO.XOOOO] */
|
|
|
|
buffer.Append(2);
|
2017-11-10 19:24:33 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(false, buffer.empty());
|
2014-03-15 17:30:08 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(false, buffer.IsFull());
|
2017-11-10 19:24:33 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(false, buffer.Read().empty());
|
2014-03-15 23:11:13 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(size_t(6), buffer.GetSize());
|
2014-03-15 23:29:07 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(size_t(1), buffer.GetSpace());
|
2014-03-15 17:30:08 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(size_t(4), buffer.Read().size);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(&data[4], buffer.Read().data);
|
2017-11-10 19:24:33 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(false, buffer.Write().empty());
|
2014-03-15 17:30:08 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(&data[2], buffer.Write().data);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(size_t(1), buffer.Write().size);
|
|
|
|
|
|
|
|
/* append [2] */
|
|
|
|
/* [OOOXOOOO] */
|
|
|
|
buffer.Append(1);
|
2017-11-10 19:24:33 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(false, buffer.empty());
|
2014-03-15 17:30:08 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(true, buffer.IsFull());
|
2017-11-10 19:24:33 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(false, buffer.Read().empty());
|
2014-03-15 23:11:13 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(size_t(7), buffer.GetSize());
|
2014-03-15 23:29:07 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(size_t(0), buffer.GetSpace());
|
2014-03-15 17:30:08 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(size_t(4), buffer.Read().size);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(&data[4], buffer.Read().data);
|
2017-11-10 19:24:33 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(true, buffer.Write().empty());
|
2014-03-15 17:30:08 +01:00
|
|
|
|
|
|
|
/* consume [4..7] */
|
|
|
|
/* [OOO....X] */
|
|
|
|
buffer.Consume(4);
|
2017-11-10 19:24:33 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(false, buffer.empty());
|
2014-03-15 17:30:08 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(false, buffer.IsFull());
|
2017-11-10 19:24:33 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(false, buffer.Read().empty());
|
2014-03-15 23:11:13 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(size_t(3), buffer.GetSize());
|
2014-03-15 23:29:07 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(size_t(4), buffer.GetSpace());
|
2014-03-15 17:30:08 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(size_t(3), buffer.Read().size);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(&data[0], buffer.Read().data);
|
2017-11-10 19:24:33 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(false, buffer.Write().empty());
|
2014-03-15 17:30:08 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(&data[3], buffer.Write().data);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(size_t(4), buffer.Write().size);
|
|
|
|
|
|
|
|
/* consume [0..2]; after that, we can only write 5,
|
|
|
|
because the CircularBuffer class doesn't have
|
|
|
|
special code to rewind/reset an empty buffer */
|
|
|
|
/* [..X.....] */
|
|
|
|
buffer.Consume(3);
|
2017-11-10 19:24:33 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(true, buffer.empty());
|
2014-03-15 17:30:08 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(false, buffer.IsFull());
|
2014-03-15 23:11:13 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(size_t(0), buffer.GetSize());
|
2014-03-15 23:29:07 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(size_t(7), buffer.GetSpace());
|
2017-11-10 19:24:33 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(true, buffer.Read().empty());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(false, buffer.Write().empty());
|
2014-03-15 17:30:08 +01:00
|
|
|
CPPUNIT_ASSERT_EQUAL(&data[3], buffer.Write().data);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(size_t(5), buffer.Write().size);
|
|
|
|
}
|
|
|
|
};
|