Tao Bao | 8f23757 | 2017-03-26 13:36:49 -0700 | [diff] [blame] | 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 <vector> |
| 21 | |
| 22 | #include <gtest/gtest.h> |
| 23 | |
| 24 | #include "updater/rangeset.h" |
| 25 | |
| 26 | TEST(RangeSetTest, Parse_smoke) { |
| 27 | RangeSet rs = RangeSet::Parse("2,1,10"); |
| 28 | ASSERT_EQ(static_cast<size_t>(1), rs.count); |
| 29 | ASSERT_EQ((std::vector<size_t>{ 1, 10 }), rs.pos); |
| 30 | ASSERT_EQ(static_cast<size_t>(9), rs.size); |
| 31 | |
| 32 | RangeSet rs2 = RangeSet::Parse("4,15,20,1,10"); |
| 33 | ASSERT_EQ(static_cast<size_t>(2), rs2.count); |
| 34 | ASSERT_EQ((std::vector<size_t>{ 15, 20, 1, 10 }), rs2.pos); |
| 35 | ASSERT_EQ(static_cast<size_t>(14), rs2.size); |
| 36 | |
| 37 | // Leading zeros are fine. But android::base::ParseUint() doesn't like trailing zeros like "10 ". |
| 38 | ASSERT_EQ(rs, RangeSet::Parse(" 2, 1, 10")); |
| 39 | ASSERT_EXIT(RangeSet::Parse("2,1,10 "), ::testing::KilledBySignal(SIGABRT), ""); |
| 40 | } |
| 41 | |
| 42 | TEST(RangeSetTest, Parse_InvalidCases) { |
| 43 | // Insufficient number of tokens. |
| 44 | ASSERT_EXIT(RangeSet::Parse(""), ::testing::KilledBySignal(SIGABRT), ""); |
| 45 | ASSERT_EXIT(RangeSet::Parse("2,1"), ::testing::KilledBySignal(SIGABRT), ""); |
| 46 | |
| 47 | // The first token (i.e. the number of following tokens) is invalid. |
| 48 | ASSERT_EXIT(RangeSet::Parse("a,1,1"), ::testing::KilledBySignal(SIGABRT), ""); |
| 49 | ASSERT_EXIT(RangeSet::Parse("3,1,1"), ::testing::KilledBySignal(SIGABRT), ""); |
| 50 | ASSERT_EXIT(RangeSet::Parse("-3,1,1"), ::testing::KilledBySignal(SIGABRT), ""); |
| 51 | ASSERT_EXIT(RangeSet::Parse("2,1,2,3"), ::testing::KilledBySignal(SIGABRT), ""); |
| 52 | |
| 53 | // Invalid tokens. |
| 54 | ASSERT_EXIT(RangeSet::Parse("2,1,10a"), ::testing::KilledBySignal(SIGABRT), ""); |
| 55 | ASSERT_EXIT(RangeSet::Parse("2,,10"), ::testing::KilledBySignal(SIGABRT), ""); |
| 56 | |
| 57 | // Empty or negative range. |
| 58 | ASSERT_EXIT(RangeSet::Parse("2,2,2"), ::testing::KilledBySignal(SIGABRT), ""); |
| 59 | ASSERT_EXIT(RangeSet::Parse("2,2,1"), ::testing::KilledBySignal(SIGABRT), ""); |
| 60 | } |
| 61 | |
| 62 | TEST(RangeSetTest, Overlaps) { |
| 63 | RangeSet r1 = RangeSet::Parse("2,1,6"); |
| 64 | RangeSet r2 = RangeSet::Parse("2,5,10"); |
| 65 | ASSERT_TRUE(r1.Overlaps(r2)); |
| 66 | ASSERT_TRUE(r2.Overlaps(r1)); |
| 67 | |
| 68 | r2 = RangeSet::Parse("2,6,10"); |
| 69 | ASSERT_FALSE(r1.Overlaps(r2)); |
| 70 | ASSERT_FALSE(r2.Overlaps(r1)); |
| 71 | |
| 72 | ASSERT_FALSE(RangeSet::Parse("2,3,5").Overlaps(RangeSet::Parse("2,5,7"))); |
| 73 | ASSERT_FALSE(RangeSet::Parse("2,5,7").Overlaps(RangeSet::Parse("2,3,5"))); |
| 74 | } |
| 75 | |
| 76 | TEST(RangeSetTest, GetBlockNumber) { |
| 77 | RangeSet rs = RangeSet::Parse("2,1,10"); |
| 78 | ASSERT_EQ(static_cast<size_t>(1), rs.GetBlockNumber(0)); |
| 79 | ASSERT_EQ(static_cast<size_t>(6), rs.GetBlockNumber(5)); |
| 80 | ASSERT_EQ(static_cast<size_t>(9), rs.GetBlockNumber(8)); |
| 81 | |
| 82 | // Out of bound. |
| 83 | ASSERT_EXIT(rs.GetBlockNumber(9), ::testing::KilledBySignal(SIGABRT), ""); |
| 84 | } |