Debian Codegraph @

closure-library/closure/goog/math/box_test.js

raw
1 // Copyright 2006 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 goog.provide('goog.math.BoxTest');
16 goog.setTestOnly('goog.math.BoxTest');
17
18 goog.require('goog.math.Box');
19 goog.require('goog.math.Coordinate');
20 goog.require('goog.testing.jsunit');
21
22 function testBoxEquals() {
23 var a = new goog.math.Box(1, 2, 3, 4);
24 var b = new goog.math.Box(1, 2, 3, 4);
25 assertTrue(goog.math.Box.equals(a, a));
26 assertTrue(goog.math.Box.equals(a, b));
27 assertTrue(goog.math.Box.equals(b, a));
28
29 assertFalse('Box should not equal null.', goog.math.Box.equals(a, null));
30 assertFalse('Box should not equal null.', goog.math.Box.equals(null, a));
31
32 assertFalse(goog.math.Box.equals(a, new goog.math.Box(4, 2, 3, 4)));
33 assertFalse(goog.math.Box.equals(a, new goog.math.Box(1, 4, 3, 4)));
34 assertFalse(goog.math.Box.equals(a, new goog.math.Box(1, 2, 4, 4)));
35 assertFalse(goog.math.Box.equals(a, new goog.math.Box(1, 2, 3, 1)));
36
37 assertTrue('Null boxes should be equal.', goog.math.Box.equals(null, null));
38 }
39
40 function testBoxClone() {
41 var b = new goog.math.Box(0, 0, 0, 0);
42 assertTrue(goog.math.Box.equals(b, b.clone()));
43
44 b.left = 0;
45 b.top = 1;
46 b.right = 2;
47 b.bottom = 3;
48 assertTrue(goog.math.Box.equals(b, b.clone()));
49 }
50
51 function testBoxRelativePositionX() {
52 var box = new goog.math.Box(50, 100, 100, 50);
53
54 assertEquals(
55 0, goog.math.Box.relativePositionX(box, new goog.math.Coordinate(75, 0)));
56 assertEquals(
57 0,
58 goog.math.Box.relativePositionX(box, new goog.math.Coordinate(75, 75)));
59 assertEquals(
60 0,
61 goog.math.Box.relativePositionX(box, new goog.math.Coordinate(75, 105)));
62 assertEquals(
63 -5,
64 goog.math.Box.relativePositionX(box, new goog.math.Coordinate(45, 75)));
65 assertEquals(
66 5,
67 goog.math.Box.relativePositionX(box, new goog.math.Coordinate(105, 75)));
68 }
69
70 function testBoxRelativePositionY() {
71 var box = new goog.math.Box(50, 100, 100, 50);
72
73 assertEquals(
74 0, goog.math.Box.relativePositionY(box, new goog.math.Coordinate(0, 75)));
75 assertEquals(
76 0,
77 goog.math.Box.relativePositionY(box, new goog.math.Coordinate(75, 75)));
78 assertEquals(
79 0,
80 goog.math.Box.relativePositionY(box, new goog.math.Coordinate(105, 75)));
81 assertEquals(
82 -5,
83 goog.math.Box.relativePositionY(box, new goog.math.Coordinate(75, 45)));
84 assertEquals(
85 5,
86 goog.math.Box.relativePositionY(box, new goog.math.Coordinate(75, 105)));
87 }
88
89 function testBoxDistance() {
90 var box = new goog.math.Box(50, 100, 100, 50);
91
92 assertEquals(
93 0, goog.math.Box.distance(box, new goog.math.Coordinate(75, 75)));
94 assertEquals(
95 25, goog.math.Box.distance(box, new goog.math.Coordinate(75, 25)));
96 assertEquals(
97 10, goog.math.Box.distance(box, new goog.math.Coordinate(40, 80)));
98 assertEquals(
99 5, goog.math.Box.distance(box, new goog.math.Coordinate(46, 47)));
100 assertEquals(
101 10, goog.math.Box.distance(box, new goog.math.Coordinate(106, 108)));
102 }
103
104 function testBoxContains() {
105 var box = new goog.math.Box(50, 100, 100, 50);
106
107 assertTrue(goog.math.Box.contains(box, new goog.math.Coordinate(75, 75)));
108 assertTrue(goog.math.Box.contains(box, new goog.math.Coordinate(50, 100)));
109 assertTrue(goog.math.Box.contains(box, new goog.math.Coordinate(100, 99)));
110 assertFalse(goog.math.Box.contains(box, new goog.math.Coordinate(100, 101)));
111 assertFalse(goog.math.Box.contains(box, new goog.math.Coordinate(49, 50)));
112 assertFalse(goog.math.Box.contains(box, new goog.math.Coordinate(25, 25)));
113 }
114
115 function testBoxContainsBox() {
116 var box = new goog.math.Box(50, 100, 100, 50);
117
118 function assertContains(boxB) {
119 assertTrue(
120 box + ' expected to contain ' + boxB,
121 goog.math.Box.contains(box, boxB));
122 }
123
124 function assertNotContains(boxB) {
125 assertFalse(
126 box + ' expected to not contain ' + boxB,
127 goog.math.Box.contains(box, boxB));
128 }
129
130 assertContains(new goog.math.Box(60, 90, 90, 60));
131 assertNotContains(new goog.math.Box(1, 3, 4, 2));
132 assertNotContains(new goog.math.Box(30, 90, 60, 60));
133 assertNotContains(new goog.math.Box(60, 110, 60, 60));
134 assertNotContains(new goog.math.Box(60, 90, 110, 60));
135 assertNotContains(new goog.math.Box(60, 90, 60, 40));
136 }
137
138 function testBoxesIntersect() {
139 var box = new goog.math.Box(50, 100, 100, 50);
140
141 function assertIntersects(boxB) {
142 assertTrue(
143 box + ' expected to intersect ' + boxB,
144 goog.math.Box.intersects(box, boxB));
145 }
146 function assertNotIntersects(boxB) {
147 assertFalse(
148 box + ' expected to not intersect ' + boxB,
149 goog.math.Box.intersects(box, boxB));
150 }
151
152 assertIntersects(box);
153 assertIntersects(new goog.math.Box(20, 80, 80, 20));
154 assertIntersects(new goog.math.Box(50, 80, 100, 20));
155 assertIntersects(new goog.math.Box(80, 80, 120, 20));
156 assertIntersects(new goog.math.Box(20, 100, 80, 50));
157 assertIntersects(new goog.math.Box(80, 100, 120, 50));
158 assertIntersects(new goog.math.Box(20, 120, 80, 80));
159 assertIntersects(new goog.math.Box(50, 120, 100, 80));
160 assertIntersects(new goog.math.Box(80, 120, 120, 80));
161 assertIntersects(new goog.math.Box(20, 120, 120, 20));
162 assertIntersects(new goog.math.Box(70, 80, 80, 70));
163 assertNotIntersects(new goog.math.Box(10, 30, 30, 10));
164 assertNotIntersects(new goog.math.Box(10, 70, 30, 30));
165 assertNotIntersects(new goog.math.Box(10, 100, 30, 50));
166 assertNotIntersects(new goog.math.Box(10, 120, 30, 80));
167 assertNotIntersects(new goog.math.Box(10, 140, 30, 120));
168 assertNotIntersects(new goog.math.Box(30, 30, 70, 10));
169 assertNotIntersects(new goog.math.Box(30, 140, 70, 120));
170 assertNotIntersects(new goog.math.Box(50, 30, 100, 10));
171 assertNotIntersects(new goog.math.Box(50, 140, 100, 120));
172 assertNotIntersects(new goog.math.Box(80, 30, 120, 10));
173 assertNotIntersects(new goog.math.Box(80, 140, 120, 120));
174 assertNotIntersects(new goog.math.Box(120, 30, 140, 10));
175 assertNotIntersects(new goog.math.Box(120, 70, 140, 30));
176 assertNotIntersects(new goog.math.Box(120, 100, 140, 50));
177 assertNotIntersects(new goog.math.Box(120, 120, 140, 80));
178 assertNotIntersects(new goog.math.Box(120, 140, 140, 120));
179 }
180
181 function testBoxesIntersectWithPadding() {
182 var box = new goog.math.Box(50, 100, 100, 50);
183
184 function assertIntersects(boxB, padding) {
185 assertTrue(
186 box + ' expected to intersect ' + boxB + ' with padding ' + padding,
187 goog.math.Box.intersectsWithPadding(box, boxB, padding));
188 }
189 function assertNotIntersects(boxB, padding) {
190 assertFalse(
191 box + ' expected to not intersect ' + boxB + ' with padding ' + padding,
192 goog.math.Box.intersectsWithPadding(box, boxB, padding));
193 }
194
195 assertIntersects(box, 10);
196 assertIntersects(new goog.math.Box(20, 80, 80, 20), 10);
197 assertIntersects(new goog.math.Box(50, 80, 100, 20), 10);
198 assertIntersects(new goog.math.Box(80, 80, 120, 20), 10);
199 assertIntersects(new goog.math.Box(20, 100, 80, 50), 10);
200 assertIntersects(new goog.math.Box(80, 100, 120, 50), 10);
201 assertIntersects(new goog.math.Box(20, 120, 80, 80), 10);
202 assertIntersects(new goog.math.Box(50, 120, 100, 80), 10);
203 assertIntersects(new goog.math.Box(80, 120, 120, 80), 10);
204 assertIntersects(new goog.math.Box(20, 120, 120, 20), 10);
205 assertIntersects(new goog.math.Box(70, 80, 80, 70), 10);
206 assertIntersects(new goog.math.Box(10, 30, 30, 10), 20);
207 assertIntersects(new goog.math.Box(10, 70, 30, 30), 20);
208 assertIntersects(new goog.math.Box(10, 100, 30, 50), 20);
209 assertIntersects(new goog.math.Box(10, 120, 30, 80), 20);
210 assertIntersects(new goog.math.Box(10, 140, 30, 120), 20);
211 assertIntersects(new goog.math.Box(30, 30, 70, 10), 20);
212 assertIntersects(new goog.math.Box(30, 140, 70, 120), 20);
213 assertIntersects(new goog.math.Box(50, 30, 100, 10), 20);
214 assertIntersects(new goog.math.Box(50, 140, 100, 120), 20);
215 assertIntersects(new goog.math.Box(80, 30, 120, 10), 20);
216 assertIntersects(new goog.math.Box(80, 140, 120, 120), 20);
217 assertIntersects(new goog.math.Box(120, 30, 140, 10), 20);
218 assertIntersects(new goog.math.Box(120, 70, 140, 30), 20);
219 assertIntersects(new goog.math.Box(120, 100, 140, 50), 20);
220 assertIntersects(new goog.math.Box(120, 120, 140, 80), 20);
221 assertIntersects(new goog.math.Box(120, 140, 140, 120), 20);
222 assertNotIntersects(new goog.math.Box(10, 30, 30, 10), 10);
223 assertNotIntersects(new goog.math.Box(10, 70, 30, 30), 10);
224 assertNotIntersects(new goog.math.Box(10, 100, 30, 50), 10);
225 assertNotIntersects(new goog.math.Box(10, 120, 30, 80), 10);
226 assertNotIntersects(new goog.math.Box(10, 140, 30, 120), 10);
227 assertNotIntersects(new goog.math.Box(30, 30, 70, 10), 10);
228 assertNotIntersects(new goog.math.Box(30, 140, 70, 120), 10);
229 assertNotIntersects(new goog.math.Box(50, 30, 100, 10), 10);
230 assertNotIntersects(new goog.math.Box(50, 140, 100, 120), 10);
231 assertNotIntersects(new goog.math.Box(80, 30, 120, 10), 10);
232 assertNotIntersects(new goog.math.Box(80, 140, 120, 120), 10);
233 assertNotIntersects(new goog.math.Box(120, 30, 140, 10), 10);
234 assertNotIntersects(new goog.math.Box(120, 70, 140, 30), 10);
235 assertNotIntersects(new goog.math.Box(120, 100, 140, 50), 10);
236 assertNotIntersects(new goog.math.Box(120, 120, 140, 80), 10);
237 assertNotIntersects(new goog.math.Box(120, 140, 140, 120), 10);
238 }
239
240 function testExpandToInclude() {
241 var box = new goog.math.Box(10, 50, 50, 10);
242 box.expandToInclude(new goog.math.Box(60, 70, 70, 60));
243 assertEquals(10, box.left);
244 assertEquals(10, box.top);
245 assertEquals(70, box.right);
246 assertEquals(70, box.bottom);
247 box.expandToInclude(new goog.math.Box(30, 40, 40, 30));
248 assertEquals(10, box.left);
249 assertEquals(10, box.top);
250 assertEquals(70, box.right);
251 assertEquals(70, box.bottom);
252 box.expandToInclude(new goog.math.Box(0, 100, 100, 0));
253 assertEquals(0, box.left);
254 assertEquals(0, box.top);
255 assertEquals(100, box.right);
256 assertEquals(100, box.bottom);
257 }
258
259 function testExpandToIncludeCoordinate() {
260 var box = new goog.math.Box(10, 50, 50, 10);
261 box.expandToIncludeCoordinate(new goog.math.Coordinate(0, 0));
262 assertEquals(0, box.left);
263 assertEquals(0, box.top);
264 assertEquals(50, box.right);
265 assertEquals(50, box.bottom);
266 box.expandToIncludeCoordinate(new goog.math.Coordinate(100, 0));
267 assertEquals(0, box.left);
268 assertEquals(0, box.top);
269 assertEquals(100, box.right);
270 assertEquals(50, box.bottom);
271 box.expandToIncludeCoordinate(new goog.math.Coordinate(0, 100));
272 assertEquals(0, box.left);
273 assertEquals(0, box.top);
274 assertEquals(100, box.right);
275 assertEquals(100, box.bottom);
276 }
277
278 function testGetWidth() {
279 var box = new goog.math.Box(10, 50, 30, 25);
280 assertEquals(25, box.getWidth());
281 }
282
283 function testGetHeight() {
284 var box = new goog.math.Box(10, 50, 30, 25);
285 assertEquals(20, box.getHeight());
286 }
287
288 function testBoundingBox() {
289 assertObjectEquals(
290 new goog.math.Box(1, 10, 11, 0),
291 goog.math.Box.boundingBox(
292 new goog.math.Coordinate(5, 5), new goog.math.Coordinate(5, 11),
293 new goog.math.Coordinate(0, 5), new goog.math.Coordinate(5, 1),
294 new goog.math.Coordinate(10, 5)));
295 }
296
297 function testBoxCeil() {
298 var box = new goog.math.Box(11.4, 26.6, 17.8, 9.2);
299 assertEquals(
300 'The function should return the target instance', box, box.ceil());
301 assertObjectEquals(new goog.math.Box(12, 27, 18, 10), box);
302 }
303
304 function testBoxFloor() {
305 var box = new goog.math.Box(11.4, 26.6, 17.8, 9.2);
306 assertEquals(
307 'The function should return the target instance', box, box.floor());
308 assertObjectEquals(new goog.math.Box(11, 26, 17, 9), box);
309 }
310
311 function testBoxRound() {
312 var box = new goog.math.Box(11.4, 26.6, 17.8, 9.2);
313 assertEquals(
314 'The function should return the target instance', box, box.round());
315 assertObjectEquals(new goog.math.Box(11, 27, 18, 9), box);
316 }
317
318 function testBoxTranslateCoordinate() {
319 var box = new goog.math.Box(10, 30, 20, 5);
320 var c = new goog.math.Coordinate(10, 5);
321 assertEquals(
322 'The function should return the target instance', box, box.translate(c));
323 assertObjectEquals(new goog.math.Box(15, 40, 25, 15), box);
324 }
325
326 function testBoxTranslateXY() {
327 var box = new goog.math.Box(10, 30, 20, 5);
328 assertEquals(
329 'The function should return the target instance', box,
330 box.translate(5, 2));
331 assertObjectEquals(new goog.math.Box(12, 35, 22, 10), box);
332 }
333
334 function testBoxTranslateX() {
335 var box = new goog.math.Box(10, 30, 20, 5);
336 assertEquals(
337 'The function should return the target instance', box, box.translate(3));
338 assertObjectEquals(new goog.math.Box(10, 33, 20, 8), box);
339 }
340
341 function testBoxScaleXY() {
342 var box = new goog.math.Box(10, 20, 30, 5);
343 assertEquals(
344 'The function should return the target instance', box, box.scale(2, 3));
345 assertObjectEquals(new goog.math.Box(30, 40, 90, 10), box);
346 }
347
348 function testBoxScaleFactor() {
349 var box = new goog.math.Box(10, 20, 30, 5);
350 assertEquals(
351 'The function should return the target instance', box, box.scale(2));
352 assertObjectEquals(new goog.math.Box(20, 40, 60, 10), box);
353 }