11 size_t expectedCapacity,
const vector<int>& expectedElements)
13 auto expectedSize = expectedElements.size();
14 REQUIRE(buf.
size() == expectedSize);
18 CHECK(buf.
full() == (expectedCapacity == expectedSize));
20 auto iter_diff = buf.
end() - buf.
begin();
21 CHECK(ptrdiff_t(buf.
size()) == iter_diff);
23 if (expectedSize != 0) {
24 CHECK(buf.
front() == expectedElements.front());
25 CHECK(buf.
back() == expectedElements.back());
28 for (
auto i :
xrange(expectedSize)) {
29 CHECK(buf[i] == expectedElements[i]);
32 auto it1 = expectedElements.begin();
37 auto rit = expectedElements.rbegin();
38 for (
auto it = buf.
rbegin(); it != buf.
rend(); ++it) {
45 buf1.
push_back(1); check_buf(buf1, 3, {1});
46 buf1.
push_back(2); check_buf(buf1, 3, {1,2});
47 buf1.
push_front(3); check_buf(buf1, 3, {3,1,2});
48 buf1.
pop_front(); check_buf(buf1, 3, {1,2});
49 buf1.
pop_back(); check_buf(buf1, 3, {1});
50 buf1.
clear(); check_buf(buf1, 3, {});
53 buf1.
push_back({4,5}); check_buf(buf1, 3, {4,5});
54 buf2.push_back({7,8,9}); check_buf(buf2, 5, {7,8,9});
56 swap(buf1, buf2); check_buf(buf1, 5, {7,8,9});
57 check_buf(buf2, 3, {4,5});
62 size_t expectedCapacity,
const vector<int>& expectedElements)
64 auto expectedSize = expectedElements.size();
65 REQUIRE(buf.
size() == expectedSize);
69 CHECK(buf.
full() == (expectedCapacity == expectedSize));
71 auto iter_diff = buf.
end() - buf.
begin();
72 CHECK(ptrdiff_t(buf.
size()) == iter_diff);
74 if (expectedSize != 0) {
75 CHECK(*buf.
front() == expectedElements.front());
76 CHECK(*buf.
back() == expectedElements.back());
79 for (
auto i :
xrange(expectedSize)) {
80 CHECK(*buf[i] == expectedElements[i]);
83 auto it1 = expectedElements.begin();
88 auto rit = expectedElements.rbegin();
89 for (
auto it = buf.
rbegin(); it != buf.
rend(); ++it) {
90 CHECK(**it == *rit++);
96 buf1.
push_back (make_unique<int>(1)); check_buf(buf1, 3, {1});
97 buf1.
push_back (make_unique<int>(2)); check_buf(buf1, 3, {1,2});
98 buf1.
push_front(make_unique<int>(3)); check_buf(buf1, 3, {3,1,2});
99 buf1.
pop_front(); check_buf(buf1, 3, {1,2});
100 buf1.
pop_back(); check_buf(buf1, 3, {1});
101 buf1.
clear(); check_buf(buf1, 3, {});
109static void check_queue(
111 const vector<int>& expectedElements)
113 check_buf(q.
getBuffer(), expectedCapacity, expectedElements);
118 q.
push_back(2); check_queue(q, 4, {1,2});
120 q.
push_back({4,5,6,7}); check_queue(q, 8, {2,4,5,6,7});
123 q.
clear(); check_queue(q, 8, {});
126static void check_queue(
127 const cb_queue<unique_ptr<int>>& q,
int expectedCapacity,
128 const vector<int>& expectedElements)
130 check_buf(q.getBuffer(), expectedCapacity, expectedElements);
134 q.
push_back(make_unique<int>(1)); check_queue(q, 4, {1});
135 q.
push_back(make_unique<int>(2)); check_queue(q, 4, {1,2});
137 q.
clear(); check_queue(q, 4, {});
TEST_CASE("circular_buffer")
This implements a queue on top of circular_buffer (not part of boost).
Circular buffer class, based on boost::circular_buffer/.
void set_capacity(size_t new_capacity)
constexpr auto xrange(T e)