40 #include <gtest/gtest.h> 46 TEST(CircularQueueTest, Empty)
48 const auto cq_size = 8;
52 ASSERT_EQ(cq.
size(), 0);
53 ASSERT_TRUE(cq.
empty());
58 TEST(CircularQueueTest, HeadTailEmpty)
60 const auto cq_size = 8;
71 TEST(CircularQueueTest, AddingElements)
73 const auto cq_size = 8;
76 const auto first_element = 0xAAAAAAAA;
78 ASSERT_EQ(cq.
front(), first_element);
79 ASSERT_EQ(cq.
back(), first_element);
81 const auto second_element = 0x55555555;
83 ASSERT_EQ(cq.
front(), first_element);
84 ASSERT_EQ(cq.
back(), second_element);
86 ASSERT_EQ(cq.
size(), 2);
94 TEST(CircularQueueTest, RemovingElements)
96 const auto cq_size = 8;
100 const auto first_element = 0xAAAAAAAA;
104 const auto second_element = 0x55555555;
107 auto initial_head = cq.
head();
108 auto initial_tail = cq.
tail();
112 ASSERT_EQ(cq.
head(), initial_head + 1);
113 ASSERT_EQ(cq.
tail(), initial_tail);
116 ASSERT_EQ(cq.
head(), initial_head + 2);
117 ASSERT_EQ(cq.
tail(), initial_tail);
119 ASSERT_EQ(cq.
size(), 0);
120 ASSERT_TRUE(cq.
empty());
131 const auto cq_size = 8;
134 const auto value = 0xAAAAAAAA;
135 for (
auto idx = 0; idx < cq_size; idx++) {
139 ASSERT_TRUE(cq.
full());
140 ASSERT_EQ(cq.
head(), cq.
tail() + 1);
149 TEST(CircularQueueTest, BeginEnd)
151 const auto cq_size = 8;
157 const auto first_value = 0xAAAAAAAA;
158 const auto second_value = 0x55555555;
164 ASSERT_EQ(cq.
begin() + 2, cq.
end());
172 TEST(CircularQueueTest, BeginFrontEndBack)
174 const auto cq_size = 8;
177 const auto front_value = 0xAAAAAAAA;
178 const auto back_value = 0x55555555;
184 ASSERT_EQ(*(cq.
end() - 1), cq.
back());
191 TEST(CircularQueueTest, IteratorsOp)
193 const auto cq_size = 8;
196 const auto first_value = 0xAAAAAAAA;
197 const auto second_value = 0x55555555;
201 auto negative_offset = -(cq_size + 1);
202 auto it_1 = cq.
begin();
203 auto it_2 = cq.
begin() + 1;
204 auto it_3 = cq.
begin() - negative_offset;
207 ASSERT_TRUE(it_1 != it_2);
208 ASSERT_FALSE(it_1 == it_2);
209 ASSERT_FALSE(it_1 > it_2);
210 ASSERT_FALSE(it_1 >= it_2);
211 ASSERT_TRUE(it_1 < it_2);
212 ASSERT_TRUE(it_1 <= it_2);
213 ASSERT_EQ(*it_1, first_value);
214 ASSERT_EQ(it_1 + 1, it_2);
215 ASSERT_EQ(it_1, it_2 - 1);
216 ASSERT_EQ(it_2 - it_1, 1);
217 ASSERT_EQ(it_1 - it_2, -1);
218 ASSERT_EQ(it_3._round, 1);
221 ASSERT_EQ(++temp_it, it_2);
222 ASSERT_EQ(--temp_it, it_1);
223 ASSERT_EQ(temp_it++, it_1);
224 ASSERT_EQ(temp_it, it_2);
225 ASSERT_EQ(temp_it--, it_2);
226 ASSERT_EQ(temp_it, it_1);
235 TEST(CircularQueueTest, FullLoop)
237 const auto cq_size = 8;
242 auto starting_it = cq.
begin();
243 auto ending_it = starting_it + cq_size;
245 ASSERT_EQ(starting_it._idx, ending_it._idx);
246 ASSERT_TRUE(starting_it != ending_it);
255 TEST(CircularQueueTest, MultipleRound)
257 const auto cq_size = 8;
261 auto items_added = cq_size * 3;
262 for (
auto idx = 0; idx < items_added; idx++) {
266 auto starting_it = cq.
begin();
267 auto ending_it = cq.
end();
269 ASSERT_EQ(starting_it._round + 1, ending_it._round);
270 ASSERT_EQ(ending_it - starting_it, cq_size);
iterator begin()
Iterators.
bool full() const
Is the queue full? A queue is full if the head is the 0^{th} element and the tail is the (size-1)^{th...
void push_back(typename Base::value_type val)
Pushes an element at the end of the queue.
bool empty() const
Is the queue empty?
void pop_front(size_t num_elem=1)
Circularly increase the head pointer.
TEST(CircularQueueTest, Empty)
Testing that once instantiated with a fixed size, the queue is still empty.