gem5  v21.0.1.0
chunk_generator.test.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 The Regents of the University of California
3  * All rights reserved
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met: redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer;
9  * redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution;
12  * neither the name of the copyright holders nor the names of its
13  * contributors may be used to endorse or promote products derived from
14  * this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <gtest/gtest.h>
30 
31 #include "chunk_generator.hh"
32 
33 
34 /*
35  * A test to ensure the object is in a sane state after initialization.
36  */
37 TEST(ChunkGeneratorTest, StartingConditions)
38 {
39  ChunkGenerator chunk_generator(0, 1024, 8);
40  EXPECT_EQ(0, chunk_generator.addr());
41  EXPECT_EQ(8, chunk_generator.size());
42  EXPECT_EQ(0, chunk_generator.complete());
43  EXPECT_FALSE(chunk_generator.done());
44  EXPECT_FALSE(chunk_generator.last());
45 }
46 
47 /*
48  * A simple test to check the move to the next chunk under normal conditions.
49  */
50 TEST(ChunkGeneratorTest, AdvanceToNextChunk)
51 {
52  ChunkGenerator chunk_generator(0, 1024, 8);
53  EXPECT_EQ(0, chunk_generator.addr());
54  EXPECT_TRUE(chunk_generator.next());
55  EXPECT_EQ(8, chunk_generator.addr());
56  EXPECT_EQ(8, chunk_generator.size());
57  EXPECT_EQ(8, chunk_generator.complete());
58  EXPECT_FALSE(chunk_generator.done());
59  EXPECT_FALSE(chunk_generator.last());
60 }
61 
62 /*
63  * A test to check skipping over bytes.
64  */
65 TEST(ChunkGeneratorTest, SkipBytes)
66 {
67  ChunkGenerator chunk_generator(0, 1024, 8);
68  EXPECT_EQ(0, chunk_generator.addr());
69  EXPECT_TRUE(chunk_generator.next());
70  EXPECT_EQ(8, chunk_generator.addr());
71 
72  chunk_generator.setNext(23);
73  EXPECT_EQ(23 - 8, chunk_generator.size());
74  EXPECT_TRUE(chunk_generator.next());
75  EXPECT_EQ(23, chunk_generator.addr());
76  EXPECT_EQ(1, chunk_generator.size());
77  EXPECT_TRUE(chunk_generator.next());
78  EXPECT_EQ(24, chunk_generator.addr());
79  EXPECT_EQ(8, chunk_generator.size());
80 
81  chunk_generator.setNext(32);
82  EXPECT_EQ(32 - 24, chunk_generator.size());
83  EXPECT_TRUE(chunk_generator.next());
84  EXPECT_EQ(32, chunk_generator.addr());
85  EXPECT_EQ(8, chunk_generator.size());
86 
87  chunk_generator.setNext(64);
88  EXPECT_EQ(64 - 32, chunk_generator.size());
89  EXPECT_TRUE(chunk_generator.next());
90  EXPECT_EQ(64, chunk_generator.addr());
91  EXPECT_EQ(8, chunk_generator.size());
92 
93  chunk_generator.setNext(2048);
94  EXPECT_EQ(1024 - 64, chunk_generator.size());
95  EXPECT_TRUE(chunk_generator.last());
96  EXPECT_FALSE(chunk_generator.next());
97  EXPECT_TRUE(chunk_generator.done());
98 }
99 
100 /*
101  * A test to consume chunks until the last chunk.
102  */
103 TEST(ChunkGeneratorTest, AdvanceToLastChunk)
104 {
105  ChunkGenerator chunk_generator(0, 32, 8);
106  EXPECT_EQ(0, chunk_generator.addr());
107  EXPECT_TRUE(chunk_generator.next());
108  EXPECT_EQ(8, chunk_generator.addr());
109  EXPECT_TRUE(chunk_generator.next());
110  EXPECT_EQ(16, chunk_generator.addr());
111  EXPECT_TRUE(chunk_generator.next());
112  EXPECT_EQ(24, chunk_generator.addr());
113  EXPECT_EQ(8, chunk_generator.size());
114  EXPECT_EQ(24, chunk_generator.complete());
115  EXPECT_FALSE(chunk_generator.done());
116  EXPECT_TRUE(chunk_generator.last());
117 }
118 
119 /*
120  * A test to consume chunks, inclusive of the last chunk.
121  */
122 TEST(ChunkGeneratorTest, AdvanceToTheEnd)
123 {
124  ChunkGenerator chunk_generator(0, 32, 8);
125  EXPECT_EQ(0, chunk_generator.addr());
126  EXPECT_TRUE(chunk_generator.next());
127  EXPECT_EQ(8, chunk_generator.addr());
128  EXPECT_TRUE(chunk_generator.next());
129  EXPECT_EQ(16, chunk_generator.addr());
130  EXPECT_TRUE(chunk_generator.next());
131  EXPECT_EQ(24, chunk_generator.addr());
132  /* The following returns false because we cannot advance to the next to
133  * the next chunk (it does not exist). However, we still process the last
134  * chunk. It is therefore not indicative of failure to change the
135  * state of the object.
136  */
137  EXPECT_FALSE(chunk_generator.next());
138  EXPECT_EQ(24, chunk_generator.addr());
139  EXPECT_EQ(0, chunk_generator.size());
140  EXPECT_EQ(24, chunk_generator.complete());
141  EXPECT_TRUE(chunk_generator.done());
142  EXPECT_TRUE(chunk_generator.last());
143 }
144 
145 /*
146  * A region does is not necessisarily divisable by the chunk size. This will
147  * will result in the final chunk being smaller than the rest.
148  */
149 TEST(ChunkGeneratorTest, SmallerLastChunk)
150 {
151  // There are two chunks. The last will be 6 bytes.
152  ChunkGenerator chunk_generator(0, 14, 8);
153  EXPECT_EQ(0, chunk_generator.addr());
154  EXPECT_TRUE(chunk_generator.next());
155  EXPECT_EQ(8, chunk_generator.addr());
156  EXPECT_EQ(6, chunk_generator.size());
157  EXPECT_EQ(8, chunk_generator.complete());
158  EXPECT_FALSE(chunk_generator.done());
159  EXPECT_TRUE(chunk_generator.last());
160 }
161 
162 /*
163  * When a chunk size is greater than the total size, the chunk size
164  * is effectively that of the region size. This test will verify this
165  * corner-case.
166  */
167 TEST(ChunkGeneratorTest, ChunkSizeGreaterThanTotalSize)
168 {
169  ChunkGenerator chunk_generator(0, 32, 64);
170  EXPECT_EQ(0, chunk_generator.addr());
171  EXPECT_EQ(32, chunk_generator.size());
172  EXPECT_EQ(0, chunk_generator.complete());
173  EXPECT_FALSE(chunk_generator.done());
174  EXPECT_TRUE(chunk_generator.last());
175 
176  // Process the entire region.
177  EXPECT_FALSE(chunk_generator.next());
178  EXPECT_EQ(0, chunk_generator.addr());
179  EXPECT_EQ(0, chunk_generator.size());
180  EXPECT_EQ(0, chunk_generator.complete());
181  EXPECT_TRUE(chunk_generator.done());
182  EXPECT_TRUE(chunk_generator.last());
183 }
184 
185 /*
186  * As a special case, we assume there is no chunking when the chunk size is
187  * zero. Processing a chunk (i.e., execution of "next()"). should progress to
188  * the end of the region.
189  */
190 TEST(ChunkGeneratorTest, ChunkSizeZero)
191 {
192  ChunkGenerator chunk_generator(0, 64, 0);
193  EXPECT_EQ(0, chunk_generator.addr());
194  EXPECT_EQ(64, chunk_generator.size());
195  EXPECT_EQ(0, chunk_generator.complete());
196  EXPECT_FALSE(chunk_generator.done());
197  EXPECT_TRUE(chunk_generator.last());
198 
199  //Process the entire region.
200  EXPECT_FALSE(chunk_generator.next());
201  EXPECT_EQ(0, chunk_generator.addr());
202  EXPECT_EQ(0, chunk_generator.size());
203  EXPECT_EQ(0, chunk_generator.complete());
204  EXPECT_TRUE(chunk_generator.done());
205  EXPECT_TRUE(chunk_generator.last());
206 }
207 
208 /*
209  * A test to ensure a non-zero start functions correctly.
210  */
211 TEST(ChunkGeneratorTest, StartAtNonZero)
212 {
213  ChunkGenerator chunk_generator(4, 32, 8); //End address: 36.
214  EXPECT_EQ(4, chunk_generator.addr());
215  EXPECT_EQ(4, chunk_generator.size());
216  EXPECT_EQ(0, chunk_generator.complete());
217  EXPECT_FALSE(chunk_generator.done());
218  EXPECT_FALSE(chunk_generator.last());
219 
220  /*
221  * As the starting position is 4, moving to the next bit should move to
222  * 8 (i.e., process the remainder of the first chunk in the region).
223  */
224  EXPECT_TRUE(chunk_generator.next());
225  EXPECT_EQ(8, chunk_generator.addr());
226  EXPECT_EQ(8, chunk_generator.size());
227  EXPECT_EQ(4, chunk_generator.complete());
228  EXPECT_FALSE(chunk_generator.done());
229  EXPECT_FALSE(chunk_generator.last());
230 
231  // Process the rest of the region.
232  EXPECT_TRUE(chunk_generator.next());
233  EXPECT_EQ(16, chunk_generator.addr());
234  EXPECT_EQ(8, chunk_generator.size());
235  EXPECT_EQ(12, chunk_generator.complete());
236  EXPECT_FALSE(chunk_generator.done());
237  EXPECT_FALSE(chunk_generator.last());
238 
239  EXPECT_TRUE(chunk_generator.next());
240  EXPECT_EQ(24, chunk_generator.addr());
241  EXPECT_EQ(8, chunk_generator.size());
242  EXPECT_EQ(20, chunk_generator.complete());
243  EXPECT_FALSE(chunk_generator.done());
244  EXPECT_FALSE(chunk_generator.last());
245 
246  // The last chunk is also only 4 bytes.
247  EXPECT_TRUE(chunk_generator.next());
248  EXPECT_EQ(32, chunk_generator.addr());
249  EXPECT_EQ(4, chunk_generator.size());
250  EXPECT_EQ(28, chunk_generator.complete());
251  EXPECT_FALSE(chunk_generator.done());
252  EXPECT_TRUE(chunk_generator.last());
253 
254  EXPECT_FALSE(chunk_generator.next());
255  EXPECT_EQ(32, chunk_generator.addr());
256  EXPECT_EQ(0, chunk_generator.size());
257  EXPECT_EQ(28, chunk_generator.complete());
258  EXPECT_TRUE(chunk_generator.done());
259  EXPECT_TRUE(chunk_generator.last());
260 }
ChunkGenerator::complete
Addr complete() const
Number of bytes we have already chunked up.
Definition: chunk_generator.hh:129
ChunkGenerator::setNext
void setNext(Addr next)
Grow this chunk to cover additional bytes which are already handled.
Definition: chunk_generator.hh:155
ChunkGenerator::addr
Addr addr() const
Return starting address of current chunk.
Definition: chunk_generator.hh:116
ChunkGenerator::next
bool next()
Advance generator to next chunk.
Definition: chunk_generator.hh:182
ChunkGenerator::done
bool done() const
Are we done? That is, did the last call to next() advance past the end of the region?
Definition: chunk_generator.hh:138
TEST
TEST(ChunkGeneratorTest, StartingConditions)
Definition: chunk_generator.test.cc:37
ChunkGenerator::size
Addr size() const
Return size in bytes of current chunk.
Definition: chunk_generator.hh:122
chunk_generator.hh
ChunkGenerator
This class takes an arbitrary memory region (address/length pair) and generates a series of appropria...
Definition: chunk_generator.hh:56
ChunkGenerator::last
bool last() const
Is this the last chunk?
Definition: chunk_generator.hh:146

Generated on Tue Jun 22 2021 15:28:25 for gem5 by doxygen 1.8.17