aboutsummaryrefslogtreecommitdiffstats
path: root/test/TestCircularBuffer.hxx
blob: e4285484bb6b8d9ae3b380c3e2de88ef6e74a94d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
 * Unit tests for class CircularBuffer.
 */

#include "config.h"
#include "util/CircularBuffer.hxx"

#include <cppunit/TestFixture.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>
#include <cppunit/extensions/HelperMacros.h>

#include <string.h>
#include <stdlib.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);

		/* '.' = empty; 'O' = occupied; 'X' = blocked */

		/* checks on empty buffer */
		/* [.......X] */
		CPPUNIT_ASSERT_EQUAL(true, buffer.IsEmpty());
		CPPUNIT_ASSERT_EQUAL(false, buffer.IsFull());
		CPPUNIT_ASSERT_EQUAL(true, buffer.Read().IsEmpty());
		CPPUNIT_ASSERT_EQUAL(false, buffer.Write().IsEmpty());
		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);
		CPPUNIT_ASSERT_EQUAL(false, buffer.IsEmpty());
		CPPUNIT_ASSERT_EQUAL(false, buffer.IsFull());
		CPPUNIT_ASSERT_EQUAL(false, buffer.Read().IsEmpty());
		CPPUNIT_ASSERT_EQUAL(size_t(1), buffer.Read().size);
		CPPUNIT_ASSERT_EQUAL(&data[0], buffer.Read().data);
		CPPUNIT_ASSERT_EQUAL(false, buffer.Write().IsEmpty());
		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);
		CPPUNIT_ASSERT_EQUAL(false, buffer.IsEmpty());
		CPPUNIT_ASSERT_EQUAL(true, buffer.IsFull());
		CPPUNIT_ASSERT_EQUAL(false, buffer.Read().IsEmpty());
		CPPUNIT_ASSERT_EQUAL(size_t(7), buffer.Read().size);
		CPPUNIT_ASSERT_EQUAL(&data[0], buffer.Read().data);
		CPPUNIT_ASSERT_EQUAL(true, buffer.Write().IsEmpty());

		/* consume [0]; can append one at [7] */
		/* [XOOOOOO.] */
		buffer.Consume(1);
		CPPUNIT_ASSERT_EQUAL(false, buffer.IsEmpty());
		CPPUNIT_ASSERT_EQUAL(false, buffer.IsFull());
		CPPUNIT_ASSERT_EQUAL(false, buffer.Read().IsEmpty());
		CPPUNIT_ASSERT_EQUAL(size_t(6), buffer.Read().size);
		CPPUNIT_ASSERT_EQUAL(&data[1], buffer.Read().data);
		CPPUNIT_ASSERT_EQUAL(false, buffer.Write().IsEmpty());
		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);
		CPPUNIT_ASSERT_EQUAL(false, buffer.IsEmpty());
		CPPUNIT_ASSERT_EQUAL(true, buffer.IsFull());
		CPPUNIT_ASSERT_EQUAL(false, buffer.Read().IsEmpty());
		CPPUNIT_ASSERT_EQUAL(size_t(7), buffer.Read().size);
		CPPUNIT_ASSERT_EQUAL(&data[1], buffer.Read().data);
		CPPUNIT_ASSERT_EQUAL(true, buffer.Write().IsEmpty());

		/* consume [1..3]; can append [0..2] */
		/* [...XOOOO] */
		buffer.Consume(3);
		CPPUNIT_ASSERT_EQUAL(false, buffer.IsEmpty());
		CPPUNIT_ASSERT_EQUAL(false, buffer.IsFull());
		CPPUNIT_ASSERT_EQUAL(false, buffer.Read().IsEmpty());
		CPPUNIT_ASSERT_EQUAL(size_t(4), buffer.Read().size);
		CPPUNIT_ASSERT_EQUAL(&data[4], buffer.Read().data);
		CPPUNIT_ASSERT_EQUAL(false, buffer.Write().IsEmpty());
		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);
		CPPUNIT_ASSERT_EQUAL(false, buffer.IsEmpty());
		CPPUNIT_ASSERT_EQUAL(false, buffer.IsFull());
		CPPUNIT_ASSERT_EQUAL(false, buffer.Read().IsEmpty());
		CPPUNIT_ASSERT_EQUAL(size_t(4), buffer.Read().size);
		CPPUNIT_ASSERT_EQUAL(&data[4], buffer.Read().data);
		CPPUNIT_ASSERT_EQUAL(false, buffer.Write().IsEmpty());
		CPPUNIT_ASSERT_EQUAL(&data[2], buffer.Write().data);
		CPPUNIT_ASSERT_EQUAL(size_t(1), buffer.Write().size);

		/* append [2] */
		/* [OOOXOOOO] */
		buffer.Append(1);
		CPPUNIT_ASSERT_EQUAL(false, buffer.IsEmpty());
		CPPUNIT_ASSERT_EQUAL(true, buffer.IsFull());
		CPPUNIT_ASSERT_EQUAL(false, buffer.Read().IsEmpty());
		CPPUNIT_ASSERT_EQUAL(size_t(4), buffer.Read().size);
		CPPUNIT_ASSERT_EQUAL(&data[4], buffer.Read().data);
		CPPUNIT_ASSERT_EQUAL(true, buffer.Write().IsEmpty());

		/* consume [4..7] */
		/* [OOO....X] */
		buffer.Consume(4);
		CPPUNIT_ASSERT_EQUAL(false, buffer.IsEmpty());
		CPPUNIT_ASSERT_EQUAL(false, buffer.IsFull());
		CPPUNIT_ASSERT_EQUAL(false, buffer.Read().IsEmpty());
		CPPUNIT_ASSERT_EQUAL(size_t(3), buffer.Read().size);
		CPPUNIT_ASSERT_EQUAL(&data[0], buffer.Read().data);
		CPPUNIT_ASSERT_EQUAL(false, buffer.Write().IsEmpty());
		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);
		CPPUNIT_ASSERT_EQUAL(true, buffer.IsEmpty());
		CPPUNIT_ASSERT_EQUAL(false, buffer.IsFull());
		CPPUNIT_ASSERT_EQUAL(true, buffer.Read().IsEmpty());
		CPPUNIT_ASSERT_EQUAL(false, buffer.Write().IsEmpty());
		CPPUNIT_ASSERT_EQUAL(&data[3], buffer.Write().data);
		CPPUNIT_ASSERT_EQUAL(size_t(5), buffer.Write().size);
	}
};