25 #include <boost/test/unit_test.hpp> 26 #include <boost/mpl/list.hpp> 30 BOOST_AUTO_TEST_SUITE(ts_RingBuffer)
32 typedef boost::mpl::list<
char,
short,
int,
long,
float,
double>
TestTypes;
39 BOOST_CHECK_EQUAL(ringbuffer.
size(), 0u);
40 BOOST_CHECK_EQUAL(ringbuffer.
capacity(), 10u);
41 BOOST_CHECK_EQUAL(ringbuffer.
reserve(), 10u);
42 BOOST_CHECK_EQUAL(ringbuffer.
empty(),
true);
43 BOOST_CHECK_EQUAL(ringbuffer.
full(),
false);
46 bool read_exception_called =
false;
47 try { ringbuffer.
read(); }
48 catch (std::out_of_range&) { read_exception_called =
true; }
49 BOOST_CHECK_EQUAL(read_exception_called,
true);
54 BOOST_CHECK_EQUAL(ringbuffer.
size(), 1u);
55 BOOST_CHECK_EQUAL(ringbuffer.
capacity(), 10u);
56 BOOST_CHECK_EQUAL(ringbuffer.
reserve(), 9u);
57 BOOST_CHECK_EQUAL(ringbuffer.
empty(),
false);
58 BOOST_CHECK_EQUAL(ringbuffer.
full(),
false);
68 BOOST_CHECK_EQUAL(ringbuffer.
size(), 7u);
69 BOOST_CHECK_EQUAL(ringbuffer.
capacity(), 10u);
70 BOOST_CHECK_EQUAL(ringbuffer.
reserve(), 3u);
71 BOOST_CHECK_EQUAL(ringbuffer.
empty(),
false);
72 BOOST_CHECK_EQUAL(ringbuffer.
full(),
false);
79 BOOST_CHECK_EQUAL(ringbuffer.
size(), 10u);
80 BOOST_CHECK_EQUAL(ringbuffer.
capacity(), 10u);
81 BOOST_CHECK_EQUAL(ringbuffer.
reserve(), 0u);
82 BOOST_CHECK_EQUAL(ringbuffer.
empty(),
false);
83 BOOST_CHECK_EQUAL(ringbuffer.
full(),
true);
86 BOOST_CHECK_EQUAL(ringbuffer.
at(0), T(0));
87 BOOST_CHECK_EQUAL(ringbuffer.
at(1), T(1));
88 BOOST_CHECK_EQUAL(ringbuffer.
at(2), T(2));
89 BOOST_CHECK_EQUAL(ringbuffer.
at(3), T(3));
90 BOOST_CHECK_EQUAL(ringbuffer.
at(4), T(4));
91 BOOST_CHECK_EQUAL(ringbuffer.
at(5), T(5));
92 BOOST_CHECK_EQUAL(ringbuffer.
at(6), T(6));
93 BOOST_CHECK_EQUAL(ringbuffer.
at(7), T(7));
94 BOOST_CHECK_EQUAL(ringbuffer.
at(8), T(8));
95 BOOST_CHECK_EQUAL(ringbuffer.
at(9), T(9));
98 BOOST_CHECK_EQUAL(ringbuffer.
read(), T(0));
99 BOOST_CHECK_EQUAL(ringbuffer.
read(), T(1));
100 BOOST_CHECK_EQUAL(ringbuffer.
read(), T(2));
101 BOOST_CHECK_EQUAL(ringbuffer.
read(), T(3));
104 BOOST_CHECK_EQUAL(ringbuffer.
size(), 6u);
105 BOOST_CHECK_EQUAL(ringbuffer.
capacity(), 10u);
106 BOOST_CHECK_EQUAL(ringbuffer.
reserve(), 4u);
107 BOOST_CHECK_EQUAL(ringbuffer.
empty(),
false);
108 BOOST_CHECK_EQUAL(ringbuffer.
full(),
false);
111 BOOST_CHECK_EQUAL(ringbuffer.
at(0), T(4));
112 BOOST_CHECK_EQUAL(ringbuffer.
at(1), T(5));
113 BOOST_CHECK_EQUAL(ringbuffer.
at(2), T(6));
114 BOOST_CHECK_EQUAL(ringbuffer.
at(3), T(7));
115 BOOST_CHECK_EQUAL(ringbuffer.
at(4), T(8));
116 BOOST_CHECK_EQUAL(ringbuffer.
at(5), T(9));
119 ringbuffer.
write(10);
120 ringbuffer.
write(11);
121 ringbuffer.
write(12);
122 ringbuffer.
write(13);
124 BOOST_CHECK_EQUAL(ringbuffer.
size(), 10u);
125 BOOST_CHECK_EQUAL(ringbuffer.
capacity(), 10u);
126 BOOST_CHECK_EQUAL(ringbuffer.
reserve(), 0u);
127 BOOST_CHECK_EQUAL(ringbuffer.
empty(),
false);
128 BOOST_CHECK_EQUAL(ringbuffer.
full(),
true);
131 BOOST_CHECK_EQUAL(ringbuffer.
at(0), T(4));
132 BOOST_CHECK_EQUAL(ringbuffer.
at(1), T(5));
133 BOOST_CHECK_EQUAL(ringbuffer.
at(2), T(6));
134 BOOST_CHECK_EQUAL(ringbuffer.
at(3), T(7));
135 BOOST_CHECK_EQUAL(ringbuffer.
at(4), T(8));
136 BOOST_CHECK_EQUAL(ringbuffer.
at(5), T(9));
137 BOOST_CHECK_EQUAL(ringbuffer.
at(6), T(10));
138 BOOST_CHECK_EQUAL(ringbuffer.
at(7), T(11));
139 BOOST_CHECK_EQUAL(ringbuffer.
at(8), T(12));
140 BOOST_CHECK_EQUAL(ringbuffer.
at(9), T(13));
143 bool write_exception_called =
false;
144 try { ringbuffer.
write(14); }
145 catch (std::out_of_range&) { write_exception_called =
true; }
146 BOOST_CHECK_EQUAL(write_exception_called,
true);
152 BOOST_CHECK_EQUAL(ringbuffer.
size(), 6u);
153 BOOST_CHECK_EQUAL(ringbuffer.
capacity(), 10u);
154 BOOST_CHECK_EQUAL(ringbuffer.
reserve(), 4u);
155 BOOST_CHECK_EQUAL(ringbuffer.
empty(),
false);
156 BOOST_CHECK_EQUAL(ringbuffer.
full(),
false);
159 BOOST_CHECK_EQUAL(ringbuffer.
at(0), T(8));
160 BOOST_CHECK_EQUAL(ringbuffer.
at(1), T(9));
161 BOOST_CHECK_EQUAL(ringbuffer.
at(2), T(10));
162 BOOST_CHECK_EQUAL(ringbuffer.
at(3), T(11));
163 BOOST_CHECK_EQUAL(ringbuffer.
at(4), T(12));
164 BOOST_CHECK_EQUAL(ringbuffer.
at(5), T(13));
169 BOOST_CHECK_EQUAL(ringbuffer.
size(), 0u);
170 BOOST_CHECK_EQUAL(ringbuffer.
capacity(), 10u);
171 BOOST_CHECK_EQUAL(ringbuffer.
reserve(), 10u);
172 BOOST_CHECK_EQUAL(ringbuffer.
empty(),
true);
173 BOOST_CHECK_EQUAL(ringbuffer.
full(),
false);
176 bool skip_exception_called =
false;
177 try { ringbuffer.
skip(); }
178 catch (std::out_of_range&) { skip_exception_called =
true; }
179 BOOST_CHECK_EQUAL(skip_exception_called,
true);
183 BOOST_CHECK_EQUAL(ringbuffer.
arrayOne().first, ringbuffer.
arrayTwo().first);
185 BOOST_CHECK_EQUAL(ringbuffer.
arrayOne().second, 0u);
186 BOOST_CHECK_EQUAL(ringbuffer.
arrayTwo().second, 0u);
199 BOOST_CHECK_EQUAL(ringbuffer.
arrayOne().second, 3u);
200 BOOST_CHECK_EQUAL(ringbuffer.
arrayTwo().second, 0u);
213 BOOST_CHECK_EQUAL(ringbuffer.
arrayOne().second, 5u);
214 BOOST_CHECK_EQUAL(ringbuffer.
arrayTwo().second, 0u);
219 ringbuffer.
write(10);
220 ringbuffer.
write(11);
221 ringbuffer.
write(12);
222 ringbuffer.
write(13);
226 BOOST_CHECK_EQUAL(ringbuffer.
arrayOne().second, 3u);
227 BOOST_CHECK_EQUAL(ringbuffer.
arrayTwo().second, 3u);
235 BOOST_CHECK_EQUAL(ringbuffer.
at(i), *it);
241 BOOST_CHECK_EQUAL(ringbuffer.
at(i), *it);
247 BOOST_CHECK_EQUAL(ringbuffer.
at(i), *it);
253 BOOST_CHECK_EQUAL(ringbuffer.
at(i), *it);
257 BOOST_CHECK((ringbuffer.
begin() + 3) - 3 == ringbuffer.
begin());
258 BOOST_CHECK((ringbuffer.
begin() - 3) + 3 == ringbuffer.
begin());
259 BOOST_CHECK_EQUAL(ringbuffer.
end() - ringbuffer.
begin(), 6);
260 BOOST_CHECK_EQUAL(ringbuffer.
begin() - ringbuffer.
end(), -6);
266 for (
size_t i=0; i<5; ++i)
268 empty_space[i] = T(i+14);
274 BOOST_CHECK_EQUAL(ringbuffer.
size(), 8u);
275 BOOST_CHECK_EQUAL(ringbuffer.
capacity(), 10u);
276 BOOST_CHECK_EQUAL(ringbuffer.
reserve(), 2u);
277 BOOST_CHECK_EQUAL(ringbuffer.
empty(),
false);
278 BOOST_CHECK_EQUAL(ringbuffer.
full(),
false);
279 BOOST_CHECK_EQUAL(ringbuffer.
at(0), T(11));
280 BOOST_CHECK_EQUAL(ringbuffer.
at(1), T(12));
281 BOOST_CHECK_EQUAL(ringbuffer.
at(2), T(13));
282 BOOST_CHECK_EQUAL(ringbuffer.
at(3), T(14));
283 BOOST_CHECK_EQUAL(ringbuffer.
at(4), T(15));
284 BOOST_CHECK_EQUAL(ringbuffer.
at(5), T(16));
285 BOOST_CHECK_EQUAL(ringbuffer.
at(6), T(17));
286 BOOST_CHECK_EQUAL(ringbuffer.
at(7), T(18));
288 BOOST_CHECK_EQUAL(ringbuffer.
arrayOne().second, 8u);
289 BOOST_CHECK_EQUAL(ringbuffer.
arrayTwo().second, 0u);
294 ringbuffer.
write(T(19));
295 ringbuffer.
write(T(20));
296 BOOST_CHECK_EQUAL(ringbuffer.
size(), 10u);
297 BOOST_CHECK_EQUAL(ringbuffer.
capacity(), 10u);
298 BOOST_CHECK_EQUAL(ringbuffer.
reserve(), 0u);
299 BOOST_CHECK_EQUAL(ringbuffer.
empty(),
false);
300 BOOST_CHECK_EQUAL(ringbuffer.
full(),
true);
301 BOOST_CHECK_EQUAL(ringbuffer.
at(8), T(19));
302 BOOST_CHECK_EQUAL(ringbuffer.
at(9), T(20));
303 BOOST_CHECK_EQUAL(ringbuffer.
arrayOne().second, 10u);
304 BOOST_CHECK_EQUAL(ringbuffer.
arrayTwo().second, 0u);
309 BOOST_CHECK((ringbuffer.
begin() + 3) - 3 == ringbuffer.
begin());
310 BOOST_CHECK((ringbuffer.
begin() - 3) + 3 == ringbuffer.
begin());
311 BOOST_CHECK_EQUAL(ringbuffer.
end() - ringbuffer.
begin(), 10);
312 BOOST_CHECK_EQUAL(ringbuffer.
begin() - ringbuffer.
end(), -10);
333 BOOST_CHECK_EQUAL(ringbuffer_const.
size(), 10u);
334 BOOST_CHECK_EQUAL(ringbuffer_const.
capacity(), 10u);
335 BOOST_CHECK_EQUAL(ringbuffer_const.
reserve(), 0u);
336 BOOST_CHECK_EQUAL(ringbuffer_const.
empty(),
false);
337 BOOST_CHECK_EQUAL(ringbuffer_const.
full(),
true);
340 BOOST_CHECK_EQUAL(ringbuffer_const.
at(0), T(0));
341 BOOST_CHECK_EQUAL(ringbuffer_const.
at(1), T(1));
342 BOOST_CHECK_EQUAL(ringbuffer_const.
at(2), T(2));
343 BOOST_CHECK_EQUAL(ringbuffer_const.
at(3), T(3));
344 BOOST_CHECK_EQUAL(ringbuffer_const.
at(4), T(4));
345 BOOST_CHECK_EQUAL(ringbuffer_const.
at(5), T(5));
346 BOOST_CHECK_EQUAL(ringbuffer_const.
at(6), T(6));
347 BOOST_CHECK_EQUAL(ringbuffer_const.
at(7), T(7));
348 BOOST_CHECK_EQUAL(ringbuffer_const.
at(8), T(8));
349 BOOST_CHECK_EQUAL(ringbuffer_const.
at(9), T(9));
355 BOOST_CHECK_EQUAL(ringbuffer_const.
at(i), *it);
361 BOOST_CHECK_EQUAL(ringbuffer_const.
at(i), *it);
365 BOOST_CHECK((ringbuffer_const.
begin() + 3) - 3 == ringbuffer_const.
begin());
366 BOOST_CHECK((ringbuffer_const.
begin() - 3) + 3 == ringbuffer_const.
begin());
367 BOOST_CHECK_EQUAL(ringbuffer_const.
end() - ringbuffer_const.
begin(), 10);
368 BOOST_CHECK_EQUAL(ringbuffer_const.
begin() - ringbuffer_const.
end(), -10);
371 BOOST_AUTO_TEST_SUITE_END()
const T & at(size_type pos) const
Read an arbitrary element from the ring buffer without removing it.
size_type capacity() const
Returns the capacity of the ring buffer.
void fakeWrite(size_t count)
size_type size() const
Returns the current number of elements in the ring buffer.
void skip()
Removes an element from the ring buffer without returning it.
bool empty() const
Returns true if the buffer is empty.
T read()
Removes an element from the ring buffer provided there is one present.
void write(const T &val, bool overwrite=false)
Adds an element to the ring buffer provided there is room.
size_type reserve() const
Returns the remaining reserve (free space) of the ring buffer.
const_iterator end() const
array_range emptyArrayOne()
BOOST_AUTO_TEST_CASE_TEMPLATE(RingBufferCapacity, T, TestTypes)
array_range emptyArrayTwo()
A simple ring buffer implementation based on std::vector.
bool full() const
Returns true if the buffer is full.
const_iterator begin() const