blob: 3c6d77ef537400f6bfd7b5e94c174556e071db68 [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
20#include <vector>
21
22#include <gtest/gtest.h>
23
24#include "updater/rangeset.h"
25
26TEST(RangeSetTest, Parse_smoke) {
27 RangeSet rs = RangeSet::Parse("2,1,10");
Tao Baobf5b77d2017-03-30 16:57:29 -070028 ASSERT_EQ(static_cast<size_t>(1), rs.size());
29 ASSERT_EQ((Range{ 1, 10 }), rs[0]);
30 ASSERT_EQ(static_cast<size_t>(9), rs.blocks());
Tao Bao8f237572017-03-26 13:36:49 -070031
32 RangeSet rs2 = RangeSet::Parse("4,15,20,1,10");
Tao Baobf5b77d2017-03-30 16:57:29 -070033 ASSERT_EQ(static_cast<size_t>(2), rs2.size());
34 ASSERT_EQ((Range{ 15, 20 }), rs2[0]);
35 ASSERT_EQ((Range{ 1, 10 }), rs2[1]);
36 ASSERT_EQ(static_cast<size_t>(14), rs2.blocks());
Tao Bao8f237572017-03-26 13:36:49 -070037
38 // Leading zeros are fine. But android::base::ParseUint() doesn't like trailing zeros like "10 ".
39 ASSERT_EQ(rs, RangeSet::Parse(" 2, 1, 10"));
40 ASSERT_EXIT(RangeSet::Parse("2,1,10 "), ::testing::KilledBySignal(SIGABRT), "");
41}
42
43TEST(RangeSetTest, Parse_InvalidCases) {
44 // Insufficient number of tokens.
45 ASSERT_EXIT(RangeSet::Parse(""), ::testing::KilledBySignal(SIGABRT), "");
46 ASSERT_EXIT(RangeSet::Parse("2,1"), ::testing::KilledBySignal(SIGABRT), "");
47
48 // The first token (i.e. the number of following tokens) is invalid.
49 ASSERT_EXIT(RangeSet::Parse("a,1,1"), ::testing::KilledBySignal(SIGABRT), "");
50 ASSERT_EXIT(RangeSet::Parse("3,1,1"), ::testing::KilledBySignal(SIGABRT), "");
51 ASSERT_EXIT(RangeSet::Parse("-3,1,1"), ::testing::KilledBySignal(SIGABRT), "");
52 ASSERT_EXIT(RangeSet::Parse("2,1,2,3"), ::testing::KilledBySignal(SIGABRT), "");
53
54 // Invalid tokens.
55 ASSERT_EXIT(RangeSet::Parse("2,1,10a"), ::testing::KilledBySignal(SIGABRT), "");
56 ASSERT_EXIT(RangeSet::Parse("2,,10"), ::testing::KilledBySignal(SIGABRT), "");
57
58 // Empty or negative range.
59 ASSERT_EXIT(RangeSet::Parse("2,2,2"), ::testing::KilledBySignal(SIGABRT), "");
60 ASSERT_EXIT(RangeSet::Parse("2,2,1"), ::testing::KilledBySignal(SIGABRT), "");
61}
62
63TEST(RangeSetTest, Overlaps) {
64 RangeSet r1 = RangeSet::Parse("2,1,6");
65 RangeSet r2 = RangeSet::Parse("2,5,10");
66 ASSERT_TRUE(r1.Overlaps(r2));
67 ASSERT_TRUE(r2.Overlaps(r1));
68
69 r2 = RangeSet::Parse("2,6,10");
70 ASSERT_FALSE(r1.Overlaps(r2));
71 ASSERT_FALSE(r2.Overlaps(r1));
72
73 ASSERT_FALSE(RangeSet::Parse("2,3,5").Overlaps(RangeSet::Parse("2,5,7")));
74 ASSERT_FALSE(RangeSet::Parse("2,5,7").Overlaps(RangeSet::Parse("2,3,5")));
75}
76
77TEST(RangeSetTest, GetBlockNumber) {
78 RangeSet rs = RangeSet::Parse("2,1,10");
79 ASSERT_EQ(static_cast<size_t>(1), rs.GetBlockNumber(0));
80 ASSERT_EQ(static_cast<size_t>(6), rs.GetBlockNumber(5));
81 ASSERT_EQ(static_cast<size_t>(9), rs.GetBlockNumber(8));
82
83 // Out of bound.
84 ASSERT_EXIT(rs.GetBlockNumber(9), ::testing::KilledBySignal(SIGABRT), "");
85}
Tao Baobf5b77d2017-03-30 16:57:29 -070086
87TEST(RangeSetTest, equality) {
88 ASSERT_EQ(RangeSet::Parse("2,1,6"), RangeSet::Parse("2,1,6"));
89
90 ASSERT_NE(RangeSet::Parse("2,1,6"), RangeSet::Parse("2,1,7"));
91 ASSERT_NE(RangeSet::Parse("2,1,6"), RangeSet::Parse("2,2,7"));
92
93 // The orders of Range's matter. "4,1,5,8,10" != "4,8,10,1,5".
94 ASSERT_NE(RangeSet::Parse("4,1,5,8,10"), RangeSet::Parse("4,8,10,1,5"));
95}
96
97TEST(RangeSetTest, iterators) {
98 RangeSet rs = RangeSet::Parse("4,1,5,8,10");
99 std::vector<Range> ranges;
100 for (const auto& range : rs) {
101 ranges.push_back(range);
102 }
103 ASSERT_EQ((std::vector<Range>{ Range{ 1, 5 }, Range{ 8, 10 } }), ranges);
104
105 ranges.clear();
106
107 // Reverse iterators.
108 for (auto it = rs.crbegin(); it != rs.crend(); it++) {
109 ranges.push_back(*it);
110 }
111 ASSERT_EQ((std::vector<Range>{ Range{ 8, 10 }, Range{ 1, 5 } }), ranges);
112}