gem5  v20.1.0.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 consume chunks until the last chunk.
64  */
65 TEST(ChunkGeneratorTest, AdvanceToLastChunk)
66 {
67  ChunkGenerator chunk_generator(0, 32, 8);
68  EXPECT_EQ(0, chunk_generator.addr());
69  EXPECT_TRUE(chunk_generator.next());
70  EXPECT_EQ(8, chunk_generator.addr());
71  EXPECT_TRUE(chunk_generator.next());
72  EXPECT_EQ(16, chunk_generator.addr());
73  EXPECT_TRUE(chunk_generator.next());
74  EXPECT_EQ(24, chunk_generator.addr());
75  EXPECT_EQ(8, chunk_generator.size());
76  EXPECT_EQ(24, chunk_generator.complete());
77  EXPECT_FALSE(chunk_generator.done());
78  EXPECT_TRUE(chunk_generator.last());
79 }
80 
81 /*
82  * A test to consume chunks, inclusive of the last chunk.
83  */
84 TEST(ChunkGeneratorTest, AdvanceToTheEnd)
85 {
86  ChunkGenerator chunk_generator(0, 32, 8);
87  EXPECT_EQ(0, chunk_generator.addr());
88  EXPECT_TRUE(chunk_generator.next());
89  EXPECT_EQ(8, chunk_generator.addr());
90  EXPECT_TRUE(chunk_generator.next());
91  EXPECT_EQ(16, chunk_generator.addr());
92  EXPECT_TRUE(chunk_generator.next());
93  EXPECT_EQ(24, chunk_generator.addr());
94  /* The following returns false because we cannot advance to the next to
95  * the next chunk (it does not exist). However, we still process the last
96  * chunk. It is therefore not indicative of failure to change the
97  * state of the object.
98  */
99  EXPECT_FALSE(chunk_generator.next());
100  EXPECT_EQ(24, chunk_generator.addr());
101  EXPECT_EQ(0, chunk_generator.size());
102  EXPECT_EQ(24, chunk_generator.complete());
103  EXPECT_TRUE(chunk_generator.done());
104  EXPECT_TRUE(chunk_generator.last());
105 }
106 
107 /*
108  * A region does is not necessisarily divisable by the chunk size. This will
109  * will result in the final chunk being smaller than the rest.
110  */
111 TEST(ChunkGeneratorTest, SmallerLastChunk)
112 {
113  // There are two chunks. The last will be 6 bytes.
114  ChunkGenerator chunk_generator(0, 14, 8);
115  EXPECT_EQ(0, chunk_generator.addr());
116  EXPECT_TRUE(chunk_generator.next());
117  EXPECT_EQ(8, chunk_generator.addr());
118  EXPECT_EQ(6, chunk_generator.size());
119  EXPECT_EQ(8, chunk_generator.complete());
120  EXPECT_FALSE(chunk_generator.done());
121  EXPECT_TRUE(chunk_generator.last());
122 }
123 
124 /*
125  * When a chunk size is greater than the total size, the chunk size
126  * is effectively that of the region size. This test will verify this
127  * corner-case.
128  */
129 TEST(ChunkGeneratorTest, ChunkSizeGreaterThanTotalSize)
130 {
131  ChunkGenerator chunk_generator(0, 32, 64);
132  EXPECT_EQ(0, chunk_generator.addr());
133  EXPECT_EQ(32, chunk_generator.size());
134  EXPECT_EQ(0, chunk_generator.complete());
135  EXPECT_FALSE(chunk_generator.done());
136  EXPECT_TRUE(chunk_generator.last());
137 
138  // Process the entire region.
139  EXPECT_FALSE(chunk_generator.next());
140  EXPECT_EQ(0, chunk_generator.addr());
141  EXPECT_EQ(0, chunk_generator.size());
142  EXPECT_EQ(0, chunk_generator.complete());
143  EXPECT_TRUE(chunk_generator.done());
144  EXPECT_TRUE(chunk_generator.last());
145 }
146 
147 /*
148  * As a special case, we assume there is no chunking when the chunk size is
149  * zero. Processing a chunk (i.e., execution of "next()"). should progress to
150  * the end of the region.
151  */
152 TEST(ChunkGeneratorTest, ChunkSizeZero)
153 {
154  ChunkGenerator chunk_generator(0, 64, 0);
155  EXPECT_EQ(0, chunk_generator.addr());
156  EXPECT_EQ(64, chunk_generator.size());
157  EXPECT_EQ(0, chunk_generator.complete());
158  EXPECT_FALSE(chunk_generator.done());
159  EXPECT_TRUE(chunk_generator.last());
160 
161  //Process the entire region.
162  EXPECT_FALSE(chunk_generator.next());
163  EXPECT_EQ(0, chunk_generator.addr());
164  EXPECT_EQ(0, chunk_generator.size());
165  EXPECT_EQ(0, chunk_generator.complete());
166  EXPECT_TRUE(chunk_generator.done());
167  EXPECT_TRUE(chunk_generator.last());
168 }
169 
170 /*
171  * A test to ensure a non-zero start functions correctly.
172  */
173 TEST(ChunkGeneratorTest, StartAtNonZero)
174 {
175  ChunkGenerator chunk_generator(4, 32, 8); //End address: 36.
176  EXPECT_EQ(4, chunk_generator.addr());
177  EXPECT_EQ(4, chunk_generator.size());
178  EXPECT_EQ(0, chunk_generator.complete());
179  EXPECT_FALSE(chunk_generator.done());
180  EXPECT_FALSE(chunk_generator.last());
181 
182  /*
183  * As the starting position is 4, moving to the next bit should move to
184  * 8 (i.e., process the remainder of the first chunk in the region).
185  */
186  EXPECT_TRUE(chunk_generator.next());
187  EXPECT_EQ(8, chunk_generator.addr());
188  EXPECT_EQ(8, chunk_generator.size());
189  EXPECT_EQ(4, chunk_generator.complete());
190  EXPECT_FALSE(chunk_generator.done());
191  EXPECT_FALSE(chunk_generator.last());
192 
193  // Process the rest of the region.
194  EXPECT_TRUE(chunk_generator.next());
195  EXPECT_EQ(16, chunk_generator.addr());
196  EXPECT_EQ(8, chunk_generator.size());
197  EXPECT_EQ(12, chunk_generator.complete());
198  EXPECT_FALSE(chunk_generator.done());
199  EXPECT_FALSE(chunk_generator.last());
200 
201  EXPECT_TRUE(chunk_generator.next());
202  EXPECT_EQ(24, chunk_generator.addr());
203  EXPECT_EQ(8, chunk_generator.size());
204  EXPECT_EQ(20, chunk_generator.complete());
205  EXPECT_FALSE(chunk_generator.done());
206  EXPECT_FALSE(chunk_generator.last());
207 
208  // The last chunk is also only 4 bytes.
209  EXPECT_TRUE(chunk_generator.next());
210  EXPECT_EQ(32, chunk_generator.addr());
211  EXPECT_EQ(4, chunk_generator.size());
212  EXPECT_EQ(28, chunk_generator.complete());
213  EXPECT_FALSE(chunk_generator.done());
214  EXPECT_TRUE(chunk_generator.last());
215 
216  EXPECT_FALSE(chunk_generator.next());
217  EXPECT_EQ(32, chunk_generator.addr());
218  EXPECT_EQ(0, chunk_generator.size());
219  EXPECT_EQ(28, chunk_generator.complete());
220  EXPECT_TRUE(chunk_generator.done());
221  EXPECT_TRUE(chunk_generator.last());
222 }
ChunkGenerator::complete
unsigned complete() const
Number of bytes we have already chunked up.
Definition: chunk_generator.hh:128
EXPECT_EQ
#define EXPECT_EQ(lhs, rhs)
A macro which verifies that lhs and rhs are equal to each other.
Definition: unittest.hh:110
EXPECT_FALSE
#define EXPECT_FALSE(expr)
A macro which verifies that expr evaluates to false.
Definition: unittest.hh:106
ChunkGenerator::size
unsigned size() const
Return size in bytes of current chunk.
Definition: chunk_generator.hh:121
ChunkGenerator::addr
Addr addr() const
Return starting address of current chunk.
Definition: chunk_generator.hh:115
EXPECT_TRUE
#define EXPECT_TRUE(expr)
A macro which verifies that expr evaluates to true.
Definition: unittest.hh:103
ChunkGenerator::next
bool next()
Advance generator to next chunk.
Definition: chunk_generator.hh:155
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:137
TEST
TEST(ChunkGeneratorTest, StartingConditions)
Definition: chunk_generator.test.cc:37
chunk_generator.hh
ChunkGenerator
This class takes an arbitrary memory region (address/length pair) and generates a series of appropria...
Definition: chunk_generator.hh:55
ChunkGenerator::last
bool last() const
Is this the last chunk?
Definition: chunk_generator.hh:145

Generated on Wed Sep 30 2020 14:02:07 for gem5 by doxygen 1.8.17