1 // Copyright (c) 2012 The Chromium 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 5 #include "media/base/ranges.h" 6 7 #include <sstream> 8 9 #include "base/strings/string_piece.h" 10 #include "testing/gtest/include/gtest/gtest.h" 11 12 namespace media { 13 14 // Human-readable output operator, for debugging/testability. 15 template<class T> 16 std::ostream& operator<<(std::ostream& os, const Ranges<T>& r) { 17 os << "{ "; 18 for(size_t i = 0; i < r.size(); ++i) 19 os << "[" << r.start(i) << "," << r.end(i) << ") "; 20 os << "}"; 21 return os; 22 } 23 24 // Helper method for asserting stringified form of |r| matches expectation. 25 template<class T> 26 static void ExpectRanges(const Ranges<T>& r, 27 const base::StringPiece& expected_string) { 28 std::stringstream ss; 29 ss << r; 30 ASSERT_EQ(ss.str(), expected_string); 31 } 32 33 #define ASSERT_RANGES(ranges, expectation) \ 34 ASSERT_NO_FATAL_FAILURE(ExpectRanges(ranges, expectation)); 35 36 TEST(RangesTest, SimpleTests) { 37 Ranges<int> r; 38 ASSERT_EQ(r.size(), 0u) << r; 39 ASSERT_EQ(r.Add(0, 1), 1u) << r; 40 ASSERT_EQ(r.size(), 1u) << r; 41 ASSERT_RANGES(r, "{ [0,1) }"); 42 ASSERT_EQ(r.Add(2, 3), 2u) << r; 43 ASSERT_RANGES(r, "{ [0,1) [2,3) }"); 44 ASSERT_EQ(r.Add(1, 2), 1u) << r; 45 ASSERT_RANGES(r, "{ [0,3) }"); 46 ASSERT_EQ(r.Add(1, 4), 1u) << r; 47 ASSERT_RANGES(r, "{ [0,4) }"); 48 ASSERT_EQ(r.Add(7, 9), 2u) << r; 49 ASSERT_EQ(r.Add(5, 6), 3u) << r; 50 ASSERT_RANGES(r, "{ [0,4) [5,6) [7,9) }"); 51 ASSERT_EQ(r.Add(6, 7), 2u) << r; 52 ASSERT_RANGES(r, "{ [0,4) [5,9) }"); 53 } 54 55 TEST(RangesTest, ExtendRange) { 56 Ranges<double> r; 57 ASSERT_EQ(r.Add(0, 1), 1u) << r; 58 ASSERT_EQ(r.Add(0.5, 1.5), 1u) << r; 59 ASSERT_RANGES(r, "{ [0,1.5) }"); 60 61 r.clear(); 62 ASSERT_EQ(r.Add(0, 1), 1u) << r; 63 ASSERT_EQ(r.Add(-0.5, 0.5), 1u) << r; 64 ASSERT_RANGES(r, "{ [-0.5,1) }"); 65 66 r.clear(); 67 ASSERT_EQ(r.Add(0, 1), 1u) << r; 68 ASSERT_EQ(r.Add(2, 3), 2u) << r; 69 ASSERT_EQ(r.Add(4, 5), 3u) << r; 70 ASSERT_EQ(r.Add(0.5, 1.5), 3u) << r; 71 ASSERT_RANGES(r, "{ [0,1.5) [2,3) [4,5) }"); 72 73 r.clear(); 74 ASSERT_EQ(r.Add(0, 1), 1u) << r; 75 ASSERT_EQ(r.Add(2, 3), 2u) << r; 76 ASSERT_EQ(r.Add(4, 5), 3u) << r; 77 ASSERT_EQ(r.Add(1.5, 2.5), 3u) << r; 78 ASSERT_RANGES(r, "{ [0,1) [1.5,3) [4,5) }"); 79 } 80 81 TEST(RangesTest, CoalesceRanges) { 82 Ranges<double> r; 83 ASSERT_EQ(r.Add(0, 1), 1u) << r; 84 ASSERT_EQ(r.Add(2, 3), 2u) << r; 85 ASSERT_EQ(r.Add(4, 5), 3u) << r; 86 ASSERT_EQ(r.Add(0.5, 2.5), 2u) << r; 87 ASSERT_RANGES(r, "{ [0,3) [4,5) }"); 88 89 r.clear(); 90 ASSERT_EQ(r.Add(0, 1), 1u) << r; 91 ASSERT_EQ(r.Add(2, 3), 2u) << r; 92 ASSERT_EQ(r.Add(4, 5), 3u) << r; 93 ASSERT_EQ(r.Add(0.5, 4.5), 1u) << r; 94 ASSERT_RANGES(r, "{ [0,5) }"); 95 96 r.clear(); 97 ASSERT_EQ(r.Add(0, 1), 1u) << r; 98 ASSERT_EQ(r.Add(1, 2), 1u) << r; 99 ASSERT_RANGES(r, "{ [0,2) }"); 100 } 101 102 TEST(RangesTest, IntersectionWith) { 103 Ranges<int> a; 104 Ranges<int> b; 105 106 ASSERT_EQ(a.Add(0, 1), 1u) << a; 107 ASSERT_EQ(a.Add(4, 7), 2u) << a; 108 ASSERT_EQ(a.Add(10, 12), 3u) << a; 109 110 // Test intersections with an empty range. 111 ASSERT_RANGES(a, "{ [0,1) [4,7) [10,12) }"); 112 ASSERT_RANGES(b, "{ }"); 113 ASSERT_RANGES(a.IntersectionWith(b), "{ }"); 114 ASSERT_RANGES(b.IntersectionWith(a), "{ }"); 115 116 // Test intersections with a completely overlaping range. 117 ASSERT_EQ(b.Add(-1, 13), 1u) << b; 118 ASSERT_RANGES(a, "{ [0,1) [4,7) [10,12) }"); 119 ASSERT_RANGES(b, "{ [-1,13) }"); 120 ASSERT_RANGES(a.IntersectionWith(b), "{ [0,1) [4,7) [10,12) }"); 121 ASSERT_RANGES(b.IntersectionWith(a), "{ [0,1) [4,7) [10,12) }"); 122 123 // Test intersections with a disjoint ranges. 124 b.clear(); 125 ASSERT_EQ(b.Add(1, 4), 1u) << b; 126 ASSERT_EQ(b.Add(8, 9), 2u) << b; 127 ASSERT_RANGES(a, "{ [0,1) [4,7) [10,12) }"); 128 ASSERT_RANGES(b, "{ [1,4) [8,9) }"); 129 ASSERT_RANGES(a.IntersectionWith(b), "{ }"); 130 ASSERT_RANGES(b.IntersectionWith(a), "{ }"); 131 132 // Test intersections with partially overlapping ranges. 133 b.clear(); 134 ASSERT_EQ(b.Add(0, 3), 1u) << b; 135 ASSERT_EQ(b.Add(5, 11), 2u) << b; 136 ASSERT_RANGES(a, "{ [0,1) [4,7) [10,12) }"); 137 ASSERT_RANGES(b, "{ [0,3) [5,11) }"); 138 ASSERT_RANGES(a.IntersectionWith(b), "{ [0,1) [5,7) [10,11) }"); 139 ASSERT_RANGES(b.IntersectionWith(a), "{ [0,1) [5,7) [10,11) }"); 140 141 // Test intersection with a range that starts at the beginning of the 142 // first range and ends at the end of the last range. 143 b.clear(); 144 ASSERT_EQ(b.Add(0, 12), 1u) << b; 145 ASSERT_RANGES(a, "{ [0,1) [4,7) [10,12) }"); 146 ASSERT_RANGES(b, "{ [0,12) }"); 147 ASSERT_RANGES(a.IntersectionWith(b), "{ [0,1) [4,7) [10,12) }"); 148 ASSERT_RANGES(b.IntersectionWith(a), "{ [0,1) [4,7) [10,12) }"); 149 } 150 151 } // namespace media 152