gem5  v19.0.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
circular_queue.test.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 ARM Limited
3  * All rights reserved
4  *
5  * The license below extends only to copyright in the software and shall
6  * not be construed as granting a license to any other intellectual
7  * property including but not limited to intellectual property relating
8  * to a hardware implementation of the functionality of the software
9  * licensed hereunder. You may use the software subject to the license
10  * terms below provided that you ensure that this notice is replicated
11  * unmodified and in its entirety in all distributions of the software,
12  * modified or unmodified, in source code or in binary form.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions are
16  * met: redistributions of source code must retain the above copyright
17  * notice, this list of conditions and the following disclaimer;
18  * redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution;
21  * neither the name of the copyright holders nor the names of its
22  * contributors may be used to endorse or promote products derived from
23  * this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  *
37  * Authors: Giacomo Travaglini
38  */
39 
40 #include <gtest/gtest.h>
41 
42 #include "base/circular_queue.hh"
43 
46 TEST(CircularQueueTest, Empty)
47 {
48  const auto cq_size = 8;
49  CircularQueue<uint32_t> cq(cq_size);
50 
51  ASSERT_EQ(cq.capacity(), cq_size);
52  ASSERT_EQ(cq.size(), 0);
53  ASSERT_TRUE(cq.empty());
54 }
55 
58 TEST(CircularQueueTest, HeadTailEmpty)
59 {
60  const auto cq_size = 8;
61  CircularQueue<uint32_t> cq(cq_size);
62  ASSERT_EQ(cq.head(), cq.tail() + 1);
63 }
64 
71 TEST(CircularQueueTest, AddingElements)
72 {
73  const auto cq_size = 8;
74  CircularQueue<uint32_t> cq(cq_size);
75 
76  const auto first_element = 0xAAAAAAAA;
77  cq.push_back(first_element);
78  ASSERT_EQ(cq.front(), first_element);
79  ASSERT_EQ(cq.back(), first_element);
80 
81  const auto second_element = 0x55555555;
82  cq.push_back(second_element);
83  ASSERT_EQ(cq.front(), first_element);
84  ASSERT_EQ(cq.back(), second_element);
85 
86  ASSERT_EQ(cq.size(), 2);
87 }
88 
94 TEST(CircularQueueTest, RemovingElements)
95 {
96  const auto cq_size = 8;
97  CircularQueue<uint32_t> cq(cq_size);
98 
99  // Adding first element
100  const auto first_element = 0xAAAAAAAA;
101  cq.push_back(first_element);
102 
103  // Adding second element
104  const auto second_element = 0x55555555;
105  cq.push_back(second_element);
106 
107  auto initial_head = cq.head();
108  auto initial_tail = cq.tail();
109 
110  // Removing first and second element
111  cq.pop_front();
112  ASSERT_EQ(cq.head(), initial_head + 1);
113  ASSERT_EQ(cq.tail(), initial_tail);
114 
115  cq.pop_front();
116  ASSERT_EQ(cq.head(), initial_head + 2);
117  ASSERT_EQ(cq.tail(), initial_tail);
118 
119  ASSERT_EQ(cq.size(), 0);
120  ASSERT_TRUE(cq.empty());
121 }
122 
129 TEST(CircularQueueTest, Full)
130 {
131  const auto cq_size = 8;
132  CircularQueue<uint32_t> cq(cq_size);
133 
134  const auto value = 0xAAAAAAAA;
135  for (auto idx = 0; idx < cq_size; idx++) {
136  cq.push_back(value);
137  }
138 
139  ASSERT_TRUE(cq.full());
140  ASSERT_EQ(cq.head(), cq.tail() + 1);
141 }
142 
149 TEST(CircularQueueTest, BeginEnd)
150 {
151  const auto cq_size = 8;
152  CircularQueue<uint32_t> cq(cq_size);
153 
154  // Begin/End are the same (empty)
155  ASSERT_EQ(cq.begin(), cq.end());
156 
157  const auto first_value = 0xAAAAAAAA;
158  const auto second_value = 0x55555555;
159 
160  cq.push_back(first_value);
161  cq.push_back(second_value);
162 
163  // End = Begin + 2
164  ASSERT_EQ(cq.begin() + 2, cq.end());
165 }
166 
172 TEST(CircularQueueTest, BeginFrontEndBack)
173 {
174  const auto cq_size = 8;
175  CircularQueue<uint32_t> cq(cq_size);
176 
177  const auto front_value = 0xAAAAAAAA;
178  const auto back_value = 0x55555555;
179 
180  cq.push_back(front_value);
181  cq.push_back(back_value);
182 
183  ASSERT_EQ(*(cq.begin()), cq.front());
184  ASSERT_EQ(*(cq.end() - 1), cq.back());
185 }
186 
191 TEST(CircularQueueTest, IteratorsOp)
192 {
193  const auto cq_size = 8;
194  CircularQueue<uint32_t> cq(cq_size);
195 
196  const auto first_value = 0xAAAAAAAA;
197  const auto second_value = 0x55555555;
198  cq.push_back(first_value);
199  cq.push_back(second_value);
200 
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;
205 
206  // Operators test
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);
219 
220  auto temp_it = it_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);
227 }
228 
235 TEST(CircularQueueTest, FullLoop)
236 {
237  const auto cq_size = 8;
238  CircularQueue<uint32_t> cq(cq_size);
239 
240  // ending_it does a full loop and points at the same
241  // index as starting_it but with a different round
242  auto starting_it = cq.begin();
243  auto ending_it = starting_it + cq_size;
244 
245  ASSERT_EQ(starting_it._idx, ending_it._idx);
246  ASSERT_TRUE(starting_it != ending_it);
247 }
248 
255 TEST(CircularQueueTest, MultipleRound)
256 {
257  const auto cq_size = 8;
258  CircularQueue<uint32_t> cq(cq_size);
259 
260  // Filling the queue making it round multiple times
261  auto items_added = cq_size * 3;
262  for (auto idx = 0; idx < items_added; idx++) {
263  cq.push_back(0);
264  }
265 
266  auto starting_it = cq.begin();
267  auto ending_it = cq.end();
268 
269  ASSERT_EQ(starting_it._round + 1, ending_it._round);
270  ASSERT_EQ(ending_it - starting_it, cq_size);
271 }
iterator begin()
Iterators.
uint32_t head() const
uint32_t tail() const
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...
reference back()
uint32_t size() const
size_t capacity() const
void push_back(typename Base::value_type val)
Pushes an element at the end of the queue.
iterator end()
bool empty() const
Is the queue empty?
void pop_front(size_t num_elem=1)
Circularly increase the head pointer.
Circular queue.
TEST(CircularQueueTest, Empty)
Testing that once instantiated with a fixed size, the queue is still empty.
reference front()

Generated on Fri Feb 28 2020 16:26:58 for gem5 by doxygen 1.8.13