Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / core / html / TimeRangesTest.cpp
blobed2a9288825abafe970c730bad9e33405f3a26ea
1 /*
2 * Copyright (c) 2013, Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include "config.h"
32 #include "core/html/TimeRanges.h"
34 #include "bindings/core/v8/ExceptionStatePlaceholder.h"
35 #include <gtest/gtest.h>
36 #include <sstream>
38 namespace blink {
40 static std::string ToString(const TimeRanges& ranges)
42 std::stringstream ss;
43 ss << "{";
44 for (unsigned i = 0; i < ranges.length(); ++i)
45 ss << " [" << ranges.start(i, IGNORE_EXCEPTION) << "," << ranges.end(i, IGNORE_EXCEPTION) << ")";
46 ss << " }";
48 return ss.str();
51 #define ASSERT_RANGE(expected, range) ASSERT_EQ(expected, ToString(*range))
53 TEST(TimeRangesTest, Empty)
55 ASSERT_RANGE("{ }", TimeRanges::create());
58 TEST(TimeRangesTest, SingleRange)
60 ASSERT_RANGE("{ [1,2) }", TimeRanges::create(1, 2));
63 TEST(TimeRangesTest, CreateFromWebTimeRanges)
65 blink::WebTimeRanges webRanges(static_cast<size_t>(2));
66 webRanges[0].start = 0;
67 webRanges[0].end = 1;
68 webRanges[1].start = 2;
69 webRanges[1].end = 3;
70 ASSERT_RANGE("{ [0,1) [2,3) }", TimeRanges::create(webRanges));
73 TEST(TimeRangesTest, AddOrder)
75 TimeRanges* rangeA = TimeRanges::create();
76 TimeRanges* rangeB = TimeRanges::create();
78 rangeA->add(0, 2);
79 rangeA->add(3, 4);
80 rangeA->add(5, 100);
82 std::string expected = "{ [0,2) [3,4) [5,100) }";
83 ASSERT_RANGE(expected, rangeA);
85 // Add the values in rangeA to rangeB in reverse order.
86 for (int i = rangeA->length() - 1; i >= 0; --i)
87 rangeB->add(rangeA->start(i, IGNORE_EXCEPTION), rangeA->end(i, IGNORE_EXCEPTION));
89 ASSERT_RANGE(expected, rangeB);
92 TEST(TimeRangesTest, OverlappingAdds)
94 TimeRanges* ranges = TimeRanges::create();
96 ranges->add(0, 2);
97 ranges->add(10, 11);
98 ASSERT_RANGE("{ [0,2) [10,11) }", ranges);
100 ranges->add(0, 2);
101 ASSERT_RANGE("{ [0,2) [10,11) }", ranges);
103 ranges->add(2, 3);
104 ASSERT_RANGE("{ [0,3) [10,11) }", ranges);
106 ranges->add(2, 6);
107 ASSERT_RANGE("{ [0,6) [10,11) }", ranges);
109 ranges->add(9, 10);
110 ASSERT_RANGE("{ [0,6) [9,11) }", ranges);
112 ranges->add(8, 10);
113 ASSERT_RANGE("{ [0,6) [8,11) }", ranges);
115 ranges->add(-1, 7);
116 ASSERT_RANGE("{ [-1,7) [8,11) }", ranges);
118 ranges->add(6, 9);
119 ASSERT_RANGE("{ [-1,11) }", ranges);
122 TEST(TimeRangesTest, IntersectWith_Self)
124 TimeRanges* ranges = TimeRanges::create(0, 2);
126 ASSERT_RANGE("{ [0,2) }", ranges);
128 ranges->intersectWith(ranges);
130 ASSERT_RANGE("{ [0,2) }", ranges);
133 TEST(TimeRangesTest, IntersectWith_IdenticalRange)
135 TimeRanges* rangesA = TimeRanges::create(0, 2);
136 TimeRanges* rangesB = rangesA->copy();
138 ASSERT_RANGE("{ [0,2) }", rangesA);
139 ASSERT_RANGE("{ [0,2) }", rangesB);
141 rangesA->intersectWith(rangesB);
143 ASSERT_RANGE("{ [0,2) }", rangesA);
144 ASSERT_RANGE("{ [0,2) }", rangesB);
147 TEST(TimeRangesTest, IntersectWith_Empty)
149 TimeRanges* rangesA = TimeRanges::create(0, 2);
150 TimeRanges* rangesB = TimeRanges::create();
152 ASSERT_RANGE("{ [0,2) }", rangesA);
153 ASSERT_RANGE("{ }", rangesB);
155 rangesA->intersectWith(rangesB);
157 ASSERT_RANGE("{ }", rangesA);
158 ASSERT_RANGE("{ }", rangesB);
161 TEST(TimeRangesTest, IntersectWith_DisjointRanges1)
163 TimeRanges* rangesA = TimeRanges::create();
164 TimeRanges* rangesB = TimeRanges::create();
166 rangesA->add(0, 1);
167 rangesA->add(4, 5);
169 rangesB->add(2, 3);
170 rangesB->add(6, 7);
172 ASSERT_RANGE("{ [0,1) [4,5) }", rangesA);
173 ASSERT_RANGE("{ [2,3) [6,7) }", rangesB);
175 rangesA->intersectWith(rangesB);
177 ASSERT_RANGE("{ }", rangesA);
178 ASSERT_RANGE("{ [2,3) [6,7) }", rangesB);
181 TEST(TimeRangesTest, IntersectWith_DisjointRanges2)
183 TimeRanges* rangesA = TimeRanges::create();
184 TimeRanges* rangesB = TimeRanges::create();
186 rangesA->add(0, 1);
187 rangesA->add(4, 5);
189 rangesB->add(1, 4);
190 rangesB->add(5, 7);
192 ASSERT_RANGE("{ [0,1) [4,5) }", rangesA);
193 ASSERT_RANGE("{ [1,4) [5,7) }", rangesB);
195 rangesA->intersectWith(rangesB);
197 ASSERT_RANGE("{ }", rangesA);
198 ASSERT_RANGE("{ [1,4) [5,7) }", rangesB);
201 TEST(TimeRangesTest, IntersectWith_CompleteOverlap1)
203 TimeRanges* rangesA = TimeRanges::create();
204 TimeRanges* rangesB = TimeRanges::create();
206 rangesA->add(1, 3);
207 rangesA->add(4, 5);
208 rangesA->add(6, 9);
210 rangesB->add(0, 10);
212 ASSERT_RANGE("{ [1,3) [4,5) [6,9) }", rangesA);
213 ASSERT_RANGE("{ [0,10) }", rangesB);
215 rangesA->intersectWith(rangesB);
217 ASSERT_RANGE("{ [1,3) [4,5) [6,9) }", rangesA);
218 ASSERT_RANGE("{ [0,10) }", rangesB);
221 TEST(TimeRangesTest, IntersectWith_CompleteOverlap2)
223 TimeRanges* rangesA = TimeRanges::create();
224 TimeRanges* rangesB = TimeRanges::create();
226 rangesA->add(1, 3);
227 rangesA->add(4, 5);
228 rangesA->add(6, 9);
230 rangesB->add(1, 9);
232 ASSERT_RANGE("{ [1,3) [4,5) [6,9) }", rangesA);
233 ASSERT_RANGE("{ [1,9) }", rangesB);
235 rangesA->intersectWith(rangesB);
237 ASSERT_RANGE("{ [1,3) [4,5) [6,9) }", rangesA);
238 ASSERT_RANGE("{ [1,9) }", rangesB);
241 TEST(TimeRangesTest, IntersectWith_Gaps1)
243 TimeRanges* rangesA = TimeRanges::create();
244 TimeRanges* rangesB = TimeRanges::create();
246 rangesA->add(0, 2);
247 rangesA->add(4, 6);
249 rangesB->add(1, 5);
251 ASSERT_RANGE("{ [0,2) [4,6) }", rangesA);
252 ASSERT_RANGE("{ [1,5) }", rangesB);
254 rangesA->intersectWith(rangesB);
256 ASSERT_RANGE("{ [1,2) [4,5) }", rangesA);
257 ASSERT_RANGE("{ [1,5) }", rangesB);
260 TEST(TimeRangesTest, IntersectWith_Gaps2)
262 TimeRanges* rangesA = TimeRanges::create();
263 TimeRanges* rangesB = TimeRanges::create();
265 rangesA->add(0, 2);
266 rangesA->add(4, 6);
267 rangesA->add(8, 10);
269 rangesB->add(1, 9);
271 ASSERT_RANGE("{ [0,2) [4,6) [8,10) }", rangesA);
272 ASSERT_RANGE("{ [1,9) }", rangesB);
274 rangesA->intersectWith(rangesB);
276 ASSERT_RANGE("{ [1,2) [4,6) [8,9) }", rangesA);
277 ASSERT_RANGE("{ [1,9) }", rangesB);
280 TEST(TimeRangesTest, IntersectWith_Gaps3)
282 TimeRanges* rangesA = TimeRanges::create();
283 TimeRanges* rangesB = TimeRanges::create();
285 rangesA->add(0, 2);
286 rangesA->add(4, 7);
287 rangesA->add(8, 10);
289 rangesB->add(1, 5);
290 rangesB->add(6, 9);
292 ASSERT_RANGE("{ [0,2) [4,7) [8,10) }", rangesA);
293 ASSERT_RANGE("{ [1,5) [6,9) }", rangesB);
295 rangesA->intersectWith(rangesB);
297 ASSERT_RANGE("{ [1,2) [4,5) [6,7) [8,9) }", rangesA);
298 ASSERT_RANGE("{ [1,5) [6,9) }", rangesB);
301 TEST(TimeRangesTest, Nearest)
303 TimeRanges* ranges = TimeRanges::create();
304 ranges->add(0, 2);
305 ranges->add(5, 7);
307 ASSERT_EQ(0, ranges->nearest(0, 0));
308 ASSERT_EQ(1, ranges->nearest(1, 0));
309 ASSERT_EQ(2, ranges->nearest(2, 0));
310 ASSERT_EQ(2, ranges->nearest(3, 0));
311 ASSERT_EQ(5, ranges->nearest(4, 0));
312 ASSERT_EQ(5, ranges->nearest(5, 0));
313 ASSERT_EQ(7, ranges->nearest(8, 0));
315 ranges->add(9, 11);
316 ASSERT_EQ(7, ranges->nearest(8, 6));
317 ASSERT_EQ(7, ranges->nearest(8, 8));
318 ASSERT_EQ(9, ranges->nearest(8, 10));
321 } // namespace blink