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.
5 #include "media/base/ranges.h"
9 #include "base/strings/string_piece.h"
10 #include "testing/gtest/include/gtest/gtest.h"
14 // Human-readable output operator, for debugging/testability.
16 std::ostream
& operator<<(std::ostream
& os
, const Ranges
<T
>& r
) {
18 for(size_t i
= 0; i
< r
.size(); ++i
)
19 os
<< "[" << r
.start(i
) << "," << r
.end(i
) << ") ";
24 // Helper method for asserting stringified form of |r| matches expectation.
26 static void ExpectRanges(const Ranges
<T
>& r
,
27 const base::StringPiece
& expected_string
) {
30 ASSERT_EQ(ss
.str(), expected_string
);
33 #define ASSERT_RANGES(ranges, expectation) \
34 ASSERT_NO_FATAL_FAILURE(ExpectRanges(ranges, expectation));
36 TEST(RangesTest
, SimpleTests
) {
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) }");
55 TEST(RangesTest
, ExtendRange
) {
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) }");
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) }");
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) }");
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) }");
81 TEST(RangesTest
, CoalesceRanges
) {
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) }");
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) }");
97 ASSERT_EQ(r
.Add(0, 1), 1u) << r
;
98 ASSERT_EQ(r
.Add(1, 2), 1u) << r
;
99 ASSERT_RANGES(r
, "{ [0,2) }");
102 TEST(RangesTest
, IntersectionWith
) {
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
;
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
), "{ }");
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) }");
123 // Test intersections with a disjoint ranges.
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
), "{ }");
132 // Test intersections with partially overlapping ranges.
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) }");
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.
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) }");