1 // Copyright (c) 2013 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.
7 base.require('base.interval_tree');
9 base.unittest.testSuite('base.interval_tree', function() {
10 function buildSimpleTree() {
11 var tree = new base.IntervalTree(
12 function(s) { return s.start; },
13 function(e) { return e.end; });
15 tree.insert({start: 2}, {end: 6});
16 tree.insert({start: 1}, {end: 3});
17 tree.insert({start: 5}, {end: 7});
18 tree.insert({start: 1}, {end: 5});
19 tree.insert({start: 3}, {end: 5});
20 tree.insert({start: 3}, {end: 5});
21 tree.insert({start: 3}, {end: 6});
22 tree.insert({start: 1}, {end: 1});
23 tree.insert({start: 4}, {end: 8});
24 tree.insert({start: 0}, {end: 2});
26 tree.updateHighValues();
31 test('findIntersection', function() {
32 var tree = buildSimpleTree();
33 var intersect = tree.findIntersection(2, 4);
35 // Intersect will hold an array of begin/end objects. As a simple way
36 // to compare, we just grab the start and end values of those objects
37 // and compare to what we expect to see.
38 var values = intersect.map(function(v) { return [v[0].start, v[1].end]; });
39 values.sort(function(a, b) {
40 return (a[0] === b[0] ? a[1] - b[1] : a[0] - b[0]);
43 var expected = [[1, 3], [1, 5], [2, 6], [3, 5], [3, 5], [3, 6]];
44 assertEquals(6, values.length);
45 assertEquals(JSON.stringify(expected), JSON.stringify(values));
48 test('findIntersection_noMatching', function() {
49 var tree = buildSimpleTree();
50 var intersect = tree.findIntersection(9, 10);
51 assertArrayEquals([], intersect);
54 test('findIntersection_emptyTree', function() {
55 var tree = new base.IntervalTree();
56 tree.updateHighValues();
58 var intersect = tree.findIntersection(2, 4);
59 assertArrayEquals([], intersect);
62 test('findIntersection_emptyInterval', function() {
63 var tree = new base.IntervalTree();
64 tree.updateHighValues();
66 assertThrows(function() {
67 tree.findIntersection();
69 assertThrows(function() {
70 tree.findIntersection(1);
72 assertThrows(function() {
73 tree.findIntersection('a', 'b');
77 test('insert', function() {
78 var tree = new base.IntervalTree(
79 function(s) { return s.start; },
80 function(e) { return e.end; });
82 assertEquals(0, tree.size);
84 tree.insert({start: 1}, {end: 4});
85 tree.insert({start: 3}, {end: 5});
86 tree.updateHighValues();
95 assertEquals(2, tree.size);
96 assertEquals(JSON.stringify(outTree), JSON.stringify(tree.dump_()));
99 test('insert_withoutEnd', function() {
100 var tree = new base.IntervalTree(
101 function(s) { return s.start; },
102 function(e) { return e.end; });
104 assertEquals(0, tree.size);
106 tree.insert({start: 3, end: 5});
107 tree.insert({start: 1, end: 4});
108 tree.updateHighValues();
117 assertEquals(2, tree.size);
118 assertEquals(JSON.stringify(outTree), JSON.stringify(tree.dump_()));
121 test('insert_balancesTree', function() {
122 var tree = new base.IntervalTree(
123 function(s) { return s.start; },
124 function(e) { return e.end; });
126 assertEquals(0, tree.size);
128 for (var i = 0; i < 10; ++i)
129 tree.insert({start: i, end: 5});
130 tree.updateHighValues();
163 assertEquals(JSON.stringify(outTree, null, ' '),
164 JSON.stringify(tree.dump_(), null, ' '));
167 test('insert_withDuplicateIntervals', function() {
168 var tree = new base.IntervalTree(
169 function(s) { return s.start; },
170 function(e) { return e.end; });
172 assertEquals(0, tree.size);
174 tree.insert({start: 1}, {end: 4});
175 tree.insert({start: 3}, {end: 5});
176 tree.insert({start: 3}, {end: 5});
177 tree.insert({start: 3}, {end: 6});
178 tree.updateHighValues();
184 'node': [[3, 5], [3, 5], [3, 6]]
187 assertEquals(4, tree.size);
188 assertEquals(JSON.stringify(outTree), JSON.stringify(tree.dump_()));
191 test('insert_updatesHighValues', function() {
192 var tree = buildSimpleTree();
195 [undefined, undefined],
198 [undefined, undefined],
200 [undefined, undefined]
204 function walk(node) {
205 if (node === undefined)
209 result.push([node.maxHighLeft, node.maxHighRight]);
210 walk(node.rightNode);
214 assertEquals(JSON.stringify(expected), JSON.stringify(result));