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
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
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.
32 #include "core/html/TimeRanges.h"
34 #include "bindings/core/v8/ExceptionStatePlaceholder.h"
35 #include <gtest/gtest.h>
40 static std::string
ToString(const TimeRanges
& ranges
)
44 for (unsigned i
= 0; i
< ranges
.length(); ++i
)
45 ss
<< " [" << ranges
.start(i
, IGNORE_EXCEPTION
) << "," << ranges
.end(i
, IGNORE_EXCEPTION
) << ")";
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;
68 webRanges
[1].start
= 2;
70 ASSERT_RANGE("{ [0,1) [2,3) }", TimeRanges::create(webRanges
));
73 TEST(TimeRangesTest
, AddOrder
)
75 TimeRanges
* rangeA
= TimeRanges::create();
76 TimeRanges
* rangeB
= TimeRanges::create();
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();
98 ASSERT_RANGE("{ [0,2) [10,11) }", ranges
);
101 ASSERT_RANGE("{ [0,2) [10,11) }", ranges
);
104 ASSERT_RANGE("{ [0,3) [10,11) }", ranges
);
107 ASSERT_RANGE("{ [0,6) [10,11) }", ranges
);
110 ASSERT_RANGE("{ [0,6) [9,11) }", ranges
);
113 ASSERT_RANGE("{ [0,6) [8,11) }", ranges
);
116 ASSERT_RANGE("{ [-1,7) [8,11) }", ranges
);
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();
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();
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();
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();
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();
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();
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();
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();
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));
316 ASSERT_EQ(7, ranges
->nearest(8, 6));
317 ASSERT_EQ(7, ranges
->nearest(8, 8));
318 ASSERT_EQ(9, ranges
->nearest(8, 10));