gem5  v22.1.0.0
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 
38 #include <gtest/gtest.h>
39 
40 #include "base/circular_queue.hh"
41 
42 using namespace gem5;
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) % cq_size);
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) % cq_size);
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 it_1 = cq.begin();
202  auto it_2 = cq.begin() + 1;
203 
204  // Operators test
205  ASSERT_TRUE(it_1 != it_2);
206  ASSERT_FALSE(it_1 == it_2);
207  ASSERT_FALSE(it_1 > it_2);
208  ASSERT_FALSE(it_1 >= it_2);
209  ASSERT_TRUE(it_1 < it_2);
210  ASSERT_TRUE(it_1 <= it_2);
211  ASSERT_EQ(*it_1, first_value);
212  ASSERT_EQ(it_1 + 1, it_2);
213  ASSERT_EQ(it_1, it_2 - 1);
214  ASSERT_EQ(it_2 - it_1, 1);
215  ASSERT_EQ(it_1 - it_2, -1);
216 
217  auto temp_it = it_1;
218  ASSERT_EQ(++temp_it, it_2);
219  ASSERT_EQ(--temp_it, it_1);
220  ASSERT_EQ(temp_it++, it_1);
221  ASSERT_EQ(temp_it, it_2);
222  ASSERT_EQ(temp_it--, it_2);
223  ASSERT_EQ(temp_it, it_1);
224 }
225 
232 TEST(CircularQueueTest, FullLoop)
233 {
234  const auto cq_size = 8;
235  CircularQueue<uint32_t> cq(cq_size);
236 
237  // ending_it does a full loop and points at the same
238  // index as starting_it but with a different round
239  auto starting_it = cq.begin();
240  auto ending_it = starting_it + cq_size;
241 
242  ASSERT_EQ(ending_it - starting_it, cq_size);
243  ASSERT_TRUE(starting_it != ending_it);
244 }
245 
252 TEST(CircularQueueTest, MultipleRound)
253 {
254  const auto cq_size = 8;
255  CircularQueue<uint32_t> cq(cq_size);
256 
257  // Filling the queue making it round multiple times
258  auto items_added = cq_size * 3;
259  for (auto idx = 0; idx < items_added; idx++) {
260  cq.push_back(0);
261  }
262 
263  auto starting_it = cq.begin();
264  auto ending_it = cq.end();
265 
266  ASSERT_EQ(ending_it - starting_it, cq_size);
267 }
TEST(CircularQueueTest, Empty)
Testing that once instantiated with a fixed size, the queue is still empty.
Circular queue.
size_t size() const
void push_back(typename std::vector< T >::value_type val)
Pushes an element at the end of the queue.
size_t tail() const
void pop_front(size_t num_elem=1)
Circularly increase the head pointer.
size_t head() const
bool empty() const
Is the queue empty?
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...
iterator begin()
Iterators.
size_t capacity() const
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....

Generated on Wed Dec 21 2022 10:22:28 for gem5 by doxygen 1.9.1