Alex Deymo | 437b7af | 2015-10-14 20:13:58 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Alex Deymo | dcd423b | 2017-09-13 20:54:24 +0200 | [diff] [blame] | 5 | #include "bsdiff/extents.h" |
Alex Deymo | 437b7af | 2015-10-14 20:13:58 -0700 | [diff] [blame] | 6 | |
| 7 | #include <gtest/gtest.h> |
| 8 | #include <vector> |
| 9 | |
| 10 | namespace bsdiff { |
| 11 | |
| 12 | // ex_t comparator used for testing. |
| 13 | bool operator==(const struct ex_t& lhs, const struct ex_t& rhs) { |
| 14 | return lhs.off == rhs.off && lhs.len == rhs.len; |
| 15 | } |
| 16 | |
| 17 | // PrintTo is used by gtest framework whenever it needs to print our type. |
| 18 | void PrintTo(const struct ex_t& extent, ::std::ostream* os) { |
| 19 | *os << extent.off << ":" << extent.len; |
| 20 | } |
| 21 | |
| 22 | class ExtentsTest : public testing::Test { |
| 23 | protected: |
| 24 | std::vector<ex_t> extents_; |
| 25 | }; |
| 26 | |
| 27 | TEST_F(ExtentsTest, CornerCasesHandledTest) { |
| 28 | EXPECT_TRUE(ParseExtentStr("", &extents_)); |
| 29 | EXPECT_TRUE(extents_.empty()); |
| 30 | } |
| 31 | |
| 32 | TEST_F(ExtentsTest, SimpleCasesTest) { |
| 33 | EXPECT_TRUE(ParseExtentStr("10:20,30:40", &extents_)); |
| 34 | std::vector<ex_t> expected_values = {{10, 20}, {30, 40}}; |
| 35 | EXPECT_EQ(expected_values, extents_); |
| 36 | } |
| 37 | |
| 38 | TEST_F(ExtentsTest, MalformedExtentsTest) { |
| 39 | std::vector<const char*> test_cases = { |
| 40 | ":", ",", "1,2", "1:", "1,", ":2", ",2", "1,2:3", "10:-1", "-2:10"}; |
| 41 | for (const char* test_case : test_cases) { |
| 42 | std::vector<ex_t> extents; |
| 43 | EXPECT_FALSE(ParseExtentStr(test_case, &extents)) << "while testing case \"" |
| 44 | << test_case << "\""; |
| 45 | EXPECT_EQ(std::vector<ex_t>(), extents); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | TEST_F(ExtentsTest, NegativeValuesTest) { |
| 50 | // |-1| is used as a special case to read zeros for that extent. |
| 51 | EXPECT_TRUE(ParseExtentStr("10:20,-1:40,50:60", &extents_)); |
| 52 | std::vector<ex_t> expected_values = {{10, 20}, {-1, 40}, {50, 60}}; |
| 53 | EXPECT_EQ(expected_values, extents_); |
| 54 | } |
| 55 | |
| 56 | } // namespace bsdiff |