1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <signal.h>
18 #include <sys/types.h>
19
20 #include <limits>
21 #include <vector>
22
23 #include <gtest/gtest.h>
24
25 #include "otautil/rangeset.h"
26
TEST(RangeSetTest,ctor)27 TEST(RangeSetTest, ctor) {
28 RangeSet rs(std::vector<Range>{ Range{ 8, 10 }, Range{ 1, 5 } });
29 ASSERT_TRUE(rs);
30
31 RangeSet rs2(std::vector<Range>{});
32 ASSERT_FALSE(rs2);
33
34 RangeSet rs3(std::vector<Range>{ Range{ 8, 10 }, Range{ 5, 1 } });
35 ASSERT_FALSE(rs3);
36 }
37
TEST(RangeSetTest,Parse_smoke)38 TEST(RangeSetTest, Parse_smoke) {
39 RangeSet rs = RangeSet::Parse("2,1,10");
40 ASSERT_EQ(static_cast<size_t>(1), rs.size());
41 ASSERT_EQ((Range{ 1, 10 }), rs[0]);
42 ASSERT_EQ(static_cast<size_t>(9), rs.blocks());
43
44 RangeSet rs2 = RangeSet::Parse("4,15,20,1,10");
45 ASSERT_EQ(static_cast<size_t>(2), rs2.size());
46 ASSERT_EQ((Range{ 15, 20 }), rs2[0]);
47 ASSERT_EQ((Range{ 1, 10 }), rs2[1]);
48 ASSERT_EQ(static_cast<size_t>(14), rs2.blocks());
49
50 // Leading zeros are fine. But android::base::ParseUint() doesn't like trailing zeros like "10 ".
51 ASSERT_EQ(rs, RangeSet::Parse(" 2, 1, 10"));
52 ASSERT_FALSE(RangeSet::Parse("2,1,10 "));
53 }
54
TEST(RangeSetTest,Parse_InvalidCases)55 TEST(RangeSetTest, Parse_InvalidCases) {
56 // Insufficient number of tokens.
57 ASSERT_FALSE(RangeSet::Parse(""));
58 ASSERT_FALSE(RangeSet::Parse("2,1"));
59
60 // The first token (i.e. the number of following tokens) is invalid.
61 ASSERT_FALSE(RangeSet::Parse("a,1,1"));
62 ASSERT_FALSE(RangeSet::Parse("3,1,1"));
63 ASSERT_FALSE(RangeSet::Parse("-3,1,1"));
64 ASSERT_FALSE(RangeSet::Parse("2,1,2,3"));
65
66 // Invalid tokens.
67 ASSERT_FALSE(RangeSet::Parse("2,1,10a"));
68 ASSERT_FALSE(RangeSet::Parse("2,,10"));
69
70 // Empty or negative range.
71 ASSERT_FALSE(RangeSet::Parse("2,2,2"));
72 ASSERT_FALSE(RangeSet::Parse("2,2,1"));
73 }
74
TEST(RangeSetTest,Clear)75 TEST(RangeSetTest, Clear) {
76 RangeSet rs = RangeSet::Parse("2,1,6");
77 ASSERT_TRUE(rs);
78 rs.Clear();
79 ASSERT_FALSE(rs);
80
81 // No-op to clear an empty RangeSet.
82 rs.Clear();
83 ASSERT_FALSE(rs);
84 }
85
TEST(RangeSetTest,PushBack)86 TEST(RangeSetTest, PushBack) {
87 RangeSet rs;
88 ASSERT_FALSE(rs);
89
90 ASSERT_TRUE(rs.PushBack({ 3, 5 }));
91 ASSERT_EQ(RangeSet::Parse("2,3,5"), rs);
92
93 ASSERT_TRUE(rs.PushBack({ 5, 15 }));
94 ASSERT_EQ(RangeSet::Parse("4,3,5,5,15"), rs);
95 ASSERT_EQ(static_cast<size_t>(2), rs.size());
96 ASSERT_EQ(static_cast<size_t>(12), rs.blocks());
97 }
98
TEST(RangeSetTest,PushBack_InvalidInput)99 TEST(RangeSetTest, PushBack_InvalidInput) {
100 RangeSet rs;
101 ASSERT_FALSE(rs);
102 ASSERT_FALSE(rs.PushBack({ 5, 3 }));
103 ASSERT_FALSE(rs);
104 ASSERT_FALSE(rs.PushBack({ 15, 15 }));
105 ASSERT_FALSE(rs);
106
107 ASSERT_TRUE(rs.PushBack({ 5, 15 }));
108 ASSERT_FALSE(rs.PushBack({ 5, std::numeric_limits<size_t>::max() - 2 }));
109 ASSERT_EQ(RangeSet::Parse("2,5,15"), rs);
110 }
111
TEST(RangeSetTest,Overlaps)112 TEST(RangeSetTest, Overlaps) {
113 RangeSet r1 = RangeSet::Parse("2,1,6");
114 RangeSet r2 = RangeSet::Parse("2,5,10");
115 ASSERT_TRUE(r1.Overlaps(r2));
116 ASSERT_TRUE(r2.Overlaps(r1));
117
118 r2 = RangeSet::Parse("2,6,10");
119 ASSERT_FALSE(r1.Overlaps(r2));
120 ASSERT_FALSE(r2.Overlaps(r1));
121
122 ASSERT_FALSE(RangeSet::Parse("2,3,5").Overlaps(RangeSet::Parse("2,5,7")));
123 ASSERT_FALSE(RangeSet::Parse("2,5,7").Overlaps(RangeSet::Parse("2,3,5")));
124 }
125
TEST(RangeSetTest,Split)126 TEST(RangeSetTest, Split) {
127 RangeSet rs1 = RangeSet::Parse("2,1,2");
128 ASSERT_TRUE(rs1);
129 ASSERT_EQ((std::vector<RangeSet>{ RangeSet::Parse("2,1,2") }), rs1.Split(1));
130
131 RangeSet rs2 = RangeSet::Parse("2,5,10");
132 ASSERT_TRUE(rs2);
133 ASSERT_EQ((std::vector<RangeSet>{ RangeSet::Parse("2,5,8"), RangeSet::Parse("2,8,10") }),
134 rs2.Split(2));
135
136 RangeSet rs3 = RangeSet::Parse("4,0,1,5,10");
137 ASSERT_TRUE(rs3);
138 ASSERT_EQ((std::vector<RangeSet>{ RangeSet::Parse("4,0,1,5,7"), RangeSet::Parse("2,7,10") }),
139 rs3.Split(2));
140
141 RangeSet rs4 = RangeSet::Parse("6,1,3,3,4,4,5");
142 ASSERT_TRUE(rs4);
143 ASSERT_EQ((std::vector<RangeSet>{ RangeSet::Parse("2,1,3"), RangeSet::Parse("2,3,4"),
144 RangeSet::Parse("2,4,5") }),
145 rs4.Split(3));
146
147 RangeSet rs5 = RangeSet::Parse("2,0,10");
148 ASSERT_TRUE(rs5);
149 ASSERT_EQ((std::vector<RangeSet>{ RangeSet::Parse("2,0,3"), RangeSet::Parse("2,3,6"),
150 RangeSet::Parse("2,6,8"), RangeSet::Parse("2,8,10") }),
151 rs5.Split(4));
152
153 RangeSet rs6 = RangeSet::Parse(
154 "20,0,268,269,271,286,447,8350,32770,33022,98306,98558,163842,164094,196609,204800,229378,"
155 "229630,294914,295166,457564");
156 ASSERT_TRUE(rs6);
157 size_t rs6_blocks = rs6.blocks();
158 auto splits = rs6.Split(4);
159 ASSERT_EQ(
160 (std::vector<RangeSet>{
161 RangeSet::Parse("12,0,268,269,271,286,447,8350,32770,33022,98306,98558,118472"),
162 RangeSet::Parse("8,118472,163842,164094,196609,204800,229378,229630,237216"),
163 RangeSet::Parse("4,237216,294914,295166,347516"), RangeSet::Parse("2,347516,457564") }),
164 splits);
165 size_t sum = 0;
166 for (const auto& element : splits) {
167 sum += element.blocks();
168 }
169 ASSERT_EQ(rs6_blocks, sum);
170 }
171
TEST(RangeSetTest,Split_EdgeCases)172 TEST(RangeSetTest, Split_EdgeCases) {
173 // Empty RangeSet.
174 RangeSet rs1;
175 ASSERT_FALSE(rs1);
176 ASSERT_EQ((std::vector<RangeSet>{}), rs1.Split(2));
177 ASSERT_FALSE(rs1);
178
179 // Zero group.
180 RangeSet rs2 = RangeSet::Parse("2,1,5");
181 ASSERT_TRUE(rs2);
182 ASSERT_EQ((std::vector<RangeSet>{}), rs2.Split(0));
183
184 // The number of blocks equals to the number of groups.
185 RangeSet rs3 = RangeSet::Parse("2,1,5");
186 ASSERT_TRUE(rs3);
187 ASSERT_EQ((std::vector<RangeSet>{ RangeSet::Parse("2,1,2"), RangeSet::Parse("2,2,3"),
188 RangeSet::Parse("2,3,4"), RangeSet::Parse("2,4,5") }),
189 rs3.Split(4));
190
191 // Less blocks than the number of groups.
192 RangeSet rs4 = RangeSet::Parse("2,1,5");
193 ASSERT_TRUE(rs4);
194 ASSERT_EQ((std::vector<RangeSet>{ RangeSet::Parse("2,1,2"), RangeSet::Parse("2,2,3"),
195 RangeSet::Parse("2,3,4"), RangeSet::Parse("2,4,5") }),
196 rs4.Split(8));
197
198 // Less blocks than the number of groups.
199 RangeSet rs5 = RangeSet::Parse("2,0,3");
200 ASSERT_TRUE(rs5);
201 ASSERT_EQ((std::vector<RangeSet>{ RangeSet::Parse("2,0,1"), RangeSet::Parse("2,1,2"),
202 RangeSet::Parse("2,2,3") }),
203 rs5.Split(4));
204 }
205
TEST(RangeSetTest,GetBlockNumber)206 TEST(RangeSetTest, GetBlockNumber) {
207 RangeSet rs = RangeSet::Parse("2,1,10");
208 ASSERT_EQ(static_cast<size_t>(1), rs.GetBlockNumber(0));
209 ASSERT_EQ(static_cast<size_t>(6), rs.GetBlockNumber(5));
210 ASSERT_EQ(static_cast<size_t>(9), rs.GetBlockNumber(8));
211
212 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
213 // Out of bound.
214 ASSERT_EXIT(rs.GetBlockNumber(9), ::testing::KilledBySignal(SIGABRT), "");
215 }
216
TEST(RangeSetTest,equality)217 TEST(RangeSetTest, equality) {
218 ASSERT_EQ(RangeSet::Parse("2,1,6"), RangeSet::Parse("2,1,6"));
219
220 ASSERT_NE(RangeSet::Parse("2,1,6"), RangeSet::Parse("2,1,7"));
221 ASSERT_NE(RangeSet::Parse("2,1,6"), RangeSet::Parse("2,2,7"));
222
223 // The orders of Range's matter, e.g. "4,1,5,8,10" != "4,8,10,1,5".
224 ASSERT_NE(RangeSet::Parse("4,1,5,8,10"), RangeSet::Parse("4,8,10,1,5"));
225 }
226
TEST(RangeSetTest,iterators)227 TEST(RangeSetTest, iterators) {
228 RangeSet rs = RangeSet::Parse("4,1,5,8,10");
229 std::vector<Range> ranges;
230 for (const auto& range : rs) {
231 ranges.push_back(range);
232 }
233 ASSERT_EQ((std::vector<Range>{ Range{ 1, 5 }, Range{ 8, 10 } }), ranges);
234
235 ranges.clear();
236
237 // Reverse iterators.
238 for (auto it = rs.crbegin(); it != rs.crend(); it++) {
239 ranges.push_back(*it);
240 }
241 ASSERT_EQ((std::vector<Range>{ Range{ 8, 10 }, Range{ 1, 5 } }), ranges);
242 }
243
TEST(RangeSetTest,ToString)244 TEST(RangeSetTest, ToString) {
245 ASSERT_EQ("", RangeSet::Parse("").ToString());
246 ASSERT_EQ("2,1,6", RangeSet::Parse("2,1,6").ToString());
247 ASSERT_EQ("4,1,5,8,10", RangeSet::Parse("4,1,5,8,10").ToString());
248 ASSERT_EQ("6,1,3,4,6,15,22", RangeSet::Parse("6,1,3,4,6,15,22").ToString());
249 }
250
TEST(SortedRangeSetTest,Insert)251 TEST(SortedRangeSetTest, Insert) {
252 SortedRangeSet rs({ { 2, 3 }, { 4, 6 }, { 8, 14 } });
253 rs.Insert({ 1, 2 });
254 ASSERT_EQ(SortedRangeSet({ { 1, 3 }, { 4, 6 }, { 8, 14 } }), rs);
255 ASSERT_EQ(static_cast<size_t>(10), rs.blocks());
256 rs.Insert({ 3, 5 });
257 ASSERT_EQ(SortedRangeSet({ { 1, 6 }, { 8, 14 } }), rs);
258 ASSERT_EQ(static_cast<size_t>(11), rs.blocks());
259
260 SortedRangeSet r1({ { 20, 22 }, { 15, 18 } });
261 rs.Insert(r1);
262 ASSERT_EQ(SortedRangeSet({ { 1, 6 }, { 8, 14 }, { 15, 18 }, { 20, 22 } }), rs);
263 ASSERT_EQ(static_cast<size_t>(16), rs.blocks());
264
265 SortedRangeSet r2({ { 2, 7 }, { 15, 21 }, { 20, 25 } });
266 rs.Insert(r2);
267 ASSERT_EQ(SortedRangeSet({ { 1, 7 }, { 8, 14 }, { 15, 25 } }), rs);
268 ASSERT_EQ(static_cast<size_t>(22), rs.blocks());
269 }
270
TEST(SortedRangeSetTest,file_range)271 TEST(SortedRangeSetTest, file_range) {
272 SortedRangeSet rs;
273 rs.Insert(4096, 4096);
274 ASSERT_EQ(SortedRangeSet({ { 1, 2 } }), rs);
275 // insert block 2-9
276 rs.Insert(4096 * 3 - 1, 4096 * 7);
277 ASSERT_EQ(SortedRangeSet({ { 1, 10 } }), rs);
278 // insert block 15-19
279 rs.Insert(4096 * 15 + 1, 4096 * 4);
280 ASSERT_EQ(SortedRangeSet({ { 1, 10 }, { 15, 20 } }), rs);
281
282 // rs overlaps block 2-2
283 ASSERT_TRUE(rs.Overlaps(4096 * 2 - 1, 10));
284 ASSERT_FALSE(rs.Overlaps(4096 * 10, 4096 * 5));
285
286 ASSERT_EQ(static_cast<size_t>(10), rs.GetOffsetInRangeSet(4106));
287 ASSERT_EQ(static_cast<size_t>(40970), rs.GetOffsetInRangeSet(4096 * 16 + 10));
288
289 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
290 // block#10 not in range.
291 ASSERT_EXIT(rs.GetOffsetInRangeSet(40970), ::testing::KilledBySignal(SIGABRT), "");
292 }
293