File: quadtree_test.js

package info (click to toggle)
aseba-plugin-blockly 20180211%2Bgit-2
  • links: PTS
  • area: non-free
  • in suites: buster
  • size: 64,472 kB
  • sloc: xml: 7,976; python: 2,314; sh: 261; lisp: 24; makefile: 10
file content (177 lines) | stat: -rw-r--r-- 5,603 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright 2008 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

goog.provide('goog.structs.QuadTreeTest');
goog.setTestOnly('goog.structs.QuadTreeTest');

goog.require('goog.structs');
goog.require('goog.structs.QuadTree');
goog.require('goog.testing.jsunit');

function getTree() {
  var qt = new goog.structs.QuadTree(0, 0, 100, 100);
  qt.set(5, 20, 'Foo');
  qt.set(50, 32, 'Bar');
  qt.set(47, 96, 'Baz');
  qt.set(50, 50, 'Bing');
  qt.set(12, 0, 'Bong');
  return qt;
}

function testGetCount() {
  var qt = getTree();
  assertEquals('Count should be 5', 5, qt.getCount());
  qt.remove(50, 32);
  assertEquals('Count should be 4', 4, qt.getCount());
}

function testGetKeys() {
  var keys = getTree().getKeys();
  var keyString = keys.sort().join(' ');
  var expected = '(12, 0) (47, 96) (5, 20) (50, 32) (50, 50)';
  assertEquals('Sorted keys should be ' + expected, expected, keyString);
}

function testGetValues() {
  var values = getTree().getValues();
  var valueString = values.sort().join(',');
  assertEquals(
      'Sorted values should be Bar,Baz,Bing,Bong,Foo', 'Bar,Baz,Bing,Bong,Foo',
      valueString);
}

function testContains() {
  var qt = getTree();
  assertTrue('Should contain (5, 20)', qt.contains(5, 20));
  assertFalse('Should not contain (13, 13)', qt.contains(13, 13));
}

function testClear() {
  var qt = getTree();
  qt.clear();
  assertTrue('Tree should be empty', qt.isEmpty());
  assertFalse('Tree should not contain (5, 20)', qt.contains(5, 20));
}

function testConstructor() {
  var qt = new goog.structs.QuadTree(-10, -5, 6, 12);
  var root = qt.getRootNode();
  assertEquals('X of root should be -10', -10, root.x);
  assertEquals('Y of root should be -5', -5, root.y);
  assertEquals('Width of root should be 16', 16, root.w);
  assertEquals('Height of root should be 17', 17, root.h);
  assertTrue('Tree should be empty', qt.isEmpty());
}

function testClone() {
  var qt = getTree().clone();
  assertFalse('Clone should not be empty', qt.isEmpty());
  assertTrue('Should contain (47, 96)', qt.contains(47, 96));
}

function testRemove() {
  var qt = getTree();
  assertEquals('(5, 20) should be removed', 'Foo', qt.remove(5, 20));
  assertEquals('(5, 20) should be removed', 'Bar', qt.remove(50, 32));
  assertEquals('(5, 20) should be removed', 'Baz', qt.remove(47, 96));
  assertEquals('(5, 20) should be removed', 'Bing', qt.remove(50, 50));
  assertEquals('(5, 20) should be removed', 'Bong', qt.remove(12, 0));
  assertNull('(6, 6) wasn\'t there to remove', qt.remove(6, 6));
  assertTrue('Tree should be empty', qt.isEmpty());
  assertTreesChildrenAreNull(qt);
}

function testIsEmpty() {
  var qt = getTree();
  qt.clear();
  assertTrue(qt.isEmpty());
  assertEquals(
      'Root should  be empty node', goog.structs.QuadTree.NodeType.EMPTY,
      qt.getRootNode().nodeType);
  assertTreesChildrenAreNull(qt);
}

function testForEach() {
  var qt = getTree();
  var s = '';
  goog.structs.forEach(qt, function(val, key, qt2) {
    assertNotUndefined(key);
    assertEquals(qt, qt2);
    s += key.x + ',' + key.y + '=' + val + ' ';
  });
  assertEquals('50,32=Bar 50,50=Bing 47,96=Baz 5,20=Foo 12,0=Bong ', s);
}

function testBalancing() {
  var qt = new goog.structs.QuadTree(0, 0, 100, 100);
  var root = qt.getRootNode();

  // Add a point to the NW quadrant.
  qt.set(25, 25, 'first');

  assertEquals(
      'Root should be a leaf node.', goog.structs.QuadTree.NodeType.LEAF,
      root.nodeType);
  assertTreesChildrenAreNull(qt);

  assertEquals('first', root.point.value);

  // Add another point in the NW quadrant
  qt.set(25, 30, 'second');

  assertEquals(
      'Root should now be a pointer.', goog.structs.QuadTree.NodeType.POINTER,
      root.nodeType);
  assertNotNull('NE should be not be null', root.ne);
  assertNotNull('NW should be not be null', root.nw);
  assertNotNull('SE should be not be null', root.se);
  assertNotNull('SW should be not be null', root.sw);
  assertNull(root.point);

  // Delete the second point.
  qt.remove(25, 30);

  assertEquals(
      'Root should have been rebalanced and be a leaf node.',
      goog.structs.QuadTree.NodeType.LEAF, root.nodeType);
  assertTreesChildrenAreNull(qt);
  assertEquals('first', root.point.value);
}

function testTreeBounds() {
  var qt = getTree();
  assertFails(qt, qt.set, [-10, -10, 1]);
  assertFails(qt, qt.set, [-10, 10, 2]);
  assertFails(qt, qt.set, [10, -10, 3]);
  assertFails(qt, qt.set, [-10, 110, 4]);
  assertFails(qt, qt.set, [10, 130, 5]);
  assertFails(qt, qt.set, [110, -10, 6]);
  assertFails(qt, qt.set, [150, 14, 7]);
}

// Helper functions

function assertFails(context, fn, args) {
  assertThrows(
      'Exception expected from ' + fn.toString() + ' with arguments ' + args,
      function() { fn.apply(context, args); });
}

function assertTreesChildrenAreNull(qt) {
  var root = qt.getRootNode();
  assertNull('NE should be null', root.ne);
  assertNull('NW should be null', root.nw);
  assertNull('SE should be null', root.se);
  assertNull('SW should be null', root.sw);
}