blob: 5141bb67f4e8b30774f3d4d14c430c5edf07f2a9 [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
126TEST(RangeSetTest, GetBlockNumber) {
127 RangeSet rs = RangeSet::Parse("2,1,10");
128 ASSERT_EQ(static_cast<size_t>(1), rs.GetBlockNumber(0));
129 ASSERT_EQ(static_cast<size_t>(6), rs.GetBlockNumber(5));
130 ASSERT_EQ(static_cast<size_t>(9), rs.GetBlockNumber(8));
131
132 // Out of bound.
133 ASSERT_EXIT(rs.GetBlockNumber(9), ::testing::KilledBySignal(SIGABRT), "");
134}
Tao Baobf5b77d2017-03-30 16:57:29 -0700135
136TEST(RangeSetTest, equality) {
137 ASSERT_EQ(RangeSet::Parse("2,1,6"), RangeSet::Parse("2,1,6"));
138
139 ASSERT_NE(RangeSet::Parse("2,1,6"), RangeSet::Parse("2,1,7"));
140 ASSERT_NE(RangeSet::Parse("2,1,6"), RangeSet::Parse("2,2,7"));
141
Tao Bao67983152017-11-04 00:08:08 -0700142 // 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 -0700143 ASSERT_NE(RangeSet::Parse("4,1,5,8,10"), RangeSet::Parse("4,8,10,1,5"));
144}
145
146TEST(RangeSetTest, iterators) {
147 RangeSet rs = RangeSet::Parse("4,1,5,8,10");
148 std::vector<Range> ranges;
149 for (const auto& range : rs) {
150 ranges.push_back(range);
151 }
152 ASSERT_EQ((std::vector<Range>{ Range{ 1, 5 }, Range{ 8, 10 } }), ranges);
153
154 ranges.clear();
155
156 // Reverse iterators.
157 for (auto it = rs.crbegin(); it != rs.crend(); it++) {
158 ranges.push_back(*it);
159 }
160 ASSERT_EQ((std::vector<Range>{ Range{ 8, 10 }, Range{ 1, 5 } }), ranges);
161}
Tianjie Xub9e7fc72017-07-26 16:41:24 -0700162
Tao Bao67983152017-11-04 00:08:08 -0700163TEST(RangeSetTest, ToString) {
164 ASSERT_EQ("", RangeSet::Parse("").ToString());
Tianjie Xub9e7fc72017-07-26 16:41:24 -0700165 ASSERT_EQ("2,1,6", RangeSet::Parse("2,1,6").ToString());
166 ASSERT_EQ("4,1,5,8,10", RangeSet::Parse("4,1,5,8,10").ToString());
167 ASSERT_EQ("6,1,3,4,6,15,22", RangeSet::Parse("6,1,3,4,6,15,22").ToString());
168}
169
Tao Bao67983152017-11-04 00:08:08 -0700170TEST(SortedRangeSetTest, Insert) {
Tianjie Xub9e7fc72017-07-26 16:41:24 -0700171 SortedRangeSet rs({ { 2, 3 }, { 4, 6 }, { 8, 14 } });
172 rs.Insert({ 1, 2 });
173 ASSERT_EQ(SortedRangeSet({ { 1, 3 }, { 4, 6 }, { 8, 14 } }), rs);
174 ASSERT_EQ(static_cast<size_t>(10), rs.blocks());
175 rs.Insert({ 3, 5 });
176 ASSERT_EQ(SortedRangeSet({ { 1, 6 }, { 8, 14 } }), rs);
177 ASSERT_EQ(static_cast<size_t>(11), rs.blocks());
178
179 SortedRangeSet r1({ { 20, 22 }, { 15, 18 } });
180 rs.Insert(r1);
181 ASSERT_EQ(SortedRangeSet({ { 1, 6 }, { 8, 14 }, { 15, 18 }, { 20, 22 } }), rs);
182 ASSERT_EQ(static_cast<size_t>(16), rs.blocks());
183
184 SortedRangeSet r2({ { 2, 7 }, { 15, 21 }, { 20, 25 } });
185 rs.Insert(r2);
186 ASSERT_EQ(SortedRangeSet({ { 1, 7 }, { 8, 14 }, { 15, 25 } }), rs);
187 ASSERT_EQ(static_cast<size_t>(22), rs.blocks());
188}
189
190TEST(SortedRangeSetTest, file_range) {
191 SortedRangeSet rs;
192 rs.Insert(4096, 4096);
193 ASSERT_EQ(SortedRangeSet({ { 1, 2 } }), rs);
194 // insert block 2-9
195 rs.Insert(4096 * 3 - 1, 4096 * 7);
196 ASSERT_EQ(SortedRangeSet({ { 1, 10 } }), rs);
197 // insert block 15-19
198 rs.Insert(4096 * 15 + 1, 4096 * 4);
199 ASSERT_EQ(SortedRangeSet({ { 1, 10 }, { 15, 20 } }), rs);
200
201 // rs overlaps block 2-2
202 ASSERT_TRUE(rs.Overlaps(4096 * 2 - 1, 10));
203 ASSERT_FALSE(rs.Overlaps(4096 * 10, 4096 * 5));
204
205 ASSERT_EQ(static_cast<size_t>(10), rs.GetOffsetInRangeSet(4106));
206 ASSERT_EQ(static_cast<size_t>(40970), rs.GetOffsetInRangeSet(4096 * 16 + 10));
207 // block#10 not in range.
208 ASSERT_EXIT(rs.GetOffsetInRangeSet(40970), ::testing::KilledBySignal(SIGABRT), "");
Tao Bao09e468f2017-09-29 14:39:33 -0700209}