misc: Changed gem5 version info for gem5 20.2 dev
[gem5.git] / src / base / chunk_generator.test.cc
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 }