2 * Copyright (c) 2019 The Regents of the University of California
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.
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.
29 #include <gtest/gtest.h>
31 #include "chunk_generator.hh"
35 * A test to ensure the object is in a sane state after initialization.
37 TEST(ChunkGeneratorTest
, StartingConditions
)
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());
48 * A simple test to check the move to the next chunk under normal conditions.
50 TEST(ChunkGeneratorTest
, AdvanceToNextChunk
)
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());
63 * A test to check skipping over bytes.
65 TEST(ChunkGeneratorTest
, SkipBytes
)
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());
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());
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());
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());
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());
101 * A test to consume chunks until the last chunk.
103 TEST(ChunkGeneratorTest
, AdvanceToLastChunk
)
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());
120 * A test to consume chunks, inclusive of the last chunk.
122 TEST(ChunkGeneratorTest
, AdvanceToTheEnd
)
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.
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());
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.
149 TEST(ChunkGeneratorTest
, SmallerLastChunk
)
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());
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
167 TEST(ChunkGeneratorTest
, ChunkSizeGreaterThanTotalSize
)
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());
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());
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.
190 TEST(ChunkGeneratorTest
, ChunkSizeZero
)
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());
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());
209 * A test to ensure a non-zero start functions correctly.
211 TEST(ChunkGeneratorTest
, StartAtNonZero
)
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());
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).
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());
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());
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());
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());
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());