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

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