blob: fc72f2f6df7269acd25c8a2f99ddde50c1032b49 [file] [log] [blame]
Tao Bao8f237572017-03-26 13:36:49 -07001/*
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
Tao Bao67983152017-11-04 00:08:08 -070020#include <limits>
Tao Bao8f237572017-03-26 13:36:49 -070021#include <vector>
22
23#include <gtest/gtest.h>
24
Tao Bao09e468f2017-09-29 14:39:33 -070025#include "otautil/rangeset.h"
Tao Bao8f237572017-03-26 13:36:49 -070026
Tao Bao67983152017-11-04 00:08:08 -070027TEST(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
Tao Bao8f237572017-03-26 13:36:49 -070038TEST(RangeSetTest, Parse_smoke) {
39 RangeSet rs = RangeSet::Parse("2,1,10");
Tao Baobf5b77d2017-03-30 16:57:29 -070040 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());
Tao Bao8f237572017-03-26 13:36:49 -070043
44 RangeSet rs2 = RangeSet::Parse("4,15,20,1,10");
Tao Baobf5b77d2017-03-30 16:57:29 -070045 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());
Tao Bao8f237572017-03-26 13:36:49 -070049
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"));
Tao Bao67983152017-11-04 00:08:08 -070052 ASSERT_FALSE(RangeSet::Parse("2,1,10 "));
Tao Bao8f237572017-03-26 13:36:49 -070053}
54
55TEST(RangeSetTest, Parse_InvalidCases) {
56 // Insufficient number of tokens.
Tao Bao67983152017-11-04 00:08:08 -070057 ASSERT_FALSE(RangeSet::Parse(""));
58 ASSERT_FALSE(RangeSet::Parse("2,1"));
Tao Bao8f237572017-03-26 13:36:49 -070059
60 // The first token (i.e. the number of following tokens) is invalid.
Tao Bao67983152017-11-04 00:08:08 -070061 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"));
Tao Bao8f237572017-03-26 13:36:49 -070065
66 // Invalid tokens.
Tao Bao67983152017-11-04 00:08:08 -070067 ASSERT_FALSE(RangeSet::Parse("2,1,10a"));
68 ASSERT_FALSE(RangeSet::Parse("2,,10"));
Tao Bao8f237572017-03-26 13:36:49 -070069
70 // Empty or negative range.
Tao Bao67983152017-11-04 00:08:08 -070071 ASSERT_FALSE(RangeSet::Parse("2,2,2"));
72 ASSERT_FALSE(RangeSet::Parse("2,2,1"));
73}
74
75TEST(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
86TEST(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
99TEST(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);
Tao Bao8f237572017-03-26 13:36:49 -0700110}
111
112TEST(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
Tao Bao160514b2017-11-04 00:08:08 -0700126TEST(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
172TEST(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
Tao Bao8f237572017-03-26 13:36:49 -0700206TEST(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
Tao Bao42be0d42018-06-05 14:03:34 -0700212 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
Tao Bao8f237572017-03-26 13:36:49 -0700213 // Out of bound.
214 ASSERT_EXIT(rs.GetBlockNumber(9), ::testing::KilledBySignal(SIGABRT), "");
215}
Tao Baobf5b77d2017-03-30 16:57:29 -0700216
217TEST(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
Tao Bao67983152017-11-04 00:08:08 -0700223 // The orders of Range's matter, e.g. "4,1,5,8,10" != "4,8,10,1,5".
Tao Baobf5b77d2017-03-30 16:57:29 -0700224 ASSERT_NE(RangeSet::Parse("4,1,5,8,10"), RangeSet::Parse("4,8,10,1,5"));
225}
226
227TEST(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}
Tianjie Xub9e7fc72017-07-26 16:41:24 -0700243
Tao Bao67983152017-11-04 00:08:08 -0700244TEST(RangeSetTest, ToString) {
245 ASSERT_EQ("", RangeSet::Parse("").ToString());
Tianjie Xub9e7fc72017-07-26 16:41:24 -0700246 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
Tao Bao67983152017-11-04 00:08:08 -0700251TEST(SortedRangeSetTest, Insert) {
Tianjie Xub9e7fc72017-07-26 16:41:24 -0700252 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
271TEST(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));
Tao Bao42be0d42018-06-05 14:03:34 -0700288
289 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
Tianjie Xub9e7fc72017-07-26 16:41:24 -0700290 // block#10 not in range.
291 ASSERT_EXIT(rs.GetOffsetInRangeSet(40970), ::testing::KilledBySignal(SIGABRT), "");
Tao Bao09e468f2017-09-29 14:39:33 -0700292}