aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/math/rect_test.html
blob: 92529bc221881c15fa3a52608fa3434a7b6a0969 (plain)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<!DOCTYPE html>
<html>
<!--
Copyright 2006 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Closure Unit Tests - goog.math.Rect</title>
<script src="../base.js"></script>
<script>
goog.require('goog.math.Box');
goog.require('goog.math.Coordinate');
goog.require('goog.math.Rect');
goog.require('goog.math.Size');
goog.require('goog.testing.jsunit');
</script>
</head>
<body>
<script>

/**
 * Produce legible assertion results. If two rects are not equal, the error
 * message will be of the form
 * "Expected <(1, 2 - 10 x 10)> (Object) but was <(3, 4 - 20 x 20)> (Object)"
 */
function assertRectsEqual(expected, actual) {
  if (!goog.math.Rect.equals(expected, actual)) {
    assertEquals(expected, actual);
  }
}

function createRect(a) {
  return a ? new goog.math.Rect(a[0], a[1], a[2] - a[0], a[3] - a[1]) : null;
}

function testRectClone() {
  var r = new goog.math.Rect(0, 0, 0, 0);
  assertRectsEqual(r, r.clone());
  r.left = -10;
  r.top = -20;
  r.width = 10;
  r.height = 20;
  assertRectsEqual(r, r.clone());
}

function testRectIntersection() {
  var tests = [[[10, 10, 20, 20], [15, 15, 25, 25], [15, 15, 20, 20]],
               [[10, 10, 20, 20], [20, 0, 30, 10], [20, 10, 20, 10]],
               [[0, 0, 1, 1], [10, 11, 12, 13], null],
               [[11, 12, 98, 99], [22, 23, 34, 35], [22, 23, 34, 35]]];
  for (var i = 0; i < tests.length; ++i) {
    var t = tests[i];
    var r0 = createRect(t[0]);
    var r1 = createRect(t[1]);

    var expected = createRect(t[2]);

    assertRectsEqual(expected, goog.math.Rect.intersection(r0, r1));
    assertRectsEqual(expected, goog.math.Rect.intersection(r1, r0));

    // Test in place methods.
    var clone = r0.clone();

    assertRectsEqual(expected, clone.intersection(r1) ? clone : null);
    assertRectsEqual(expected, r1.intersection(r0) ? r1 : null);
  }
}

function testRectIntersects() {
  var r0 = createRect([10, 10, 20, 20]);
  var r1 = createRect([15, 15, 25, 25]);
  var r2 = createRect([0, 0, 1, 1]);

  assertTrue(goog.math.Rect.intersects(r0, r1));
  assertTrue(goog.math.Rect.intersects(r1, r0));
  assertTrue(r0.intersects(r1));
  assertTrue(r1.intersects(r0));

  assertFalse(goog.math.Rect.intersects(r0, r2));
  assertFalse(goog.math.Rect.intersects(r2, r0));
  assertFalse(r0.intersects(r2));
  assertFalse(r2.intersects(r0));
}

function testRectBoundingRect() {
  var tests = [[[10, 10, 20, 20], [15, 15, 25, 25], [10, 10, 25, 25]],
               [[10, 10, 20, 20], [20, 0, 30, 10], [10, 0, 30, 20]],
               [[0, 0, 1, 1], [10, 11, 12, 13], [0, 0, 12, 13]],
               [[11, 12, 98, 99], [22, 23, 34, 35], [11, 12, 98, 99]]];
  for (var i = 0; i < tests.length; ++i) {
    var t = tests[i];
    var r0 = createRect(t[0]);
    var r1 = createRect(t[1]);
    var expected = createRect(t[2]);
    assertRectsEqual(expected, goog.math.Rect.boundingRect(r0, r1));
    assertRectsEqual(expected, goog.math.Rect.boundingRect(r1, r0));

    // Test in place methods.
    var clone = r0.clone();

    clone.boundingRect(r1);
    assertRectsEqual(expected, clone);

    r1.boundingRect(r0);
    assertRectsEqual(expected, r1);
  }
}

function testRectDifference() {
  // B is the same as A.
  assertDifference([10, 10, 20, 20], [10, 10, 20, 20], []);
  // B does not touch A.
  assertDifference([10, 10, 20, 20], [0, 0, 5, 5], [[10, 10, 20, 20]]);
  // B overlaps top half of A.
  assertDifference([10, 10, 20, 20], [5, 15, 25, 25],
      [[10, 10, 20, 15]]);
  // B overlaps bottom half of A.
  assertDifference([10, 10, 20, 20], [5, 5, 25, 15],
      [[10, 15, 20, 20]]);
  // B overlaps right half of A.
  assertDifference([10, 10, 20, 20], [15, 5, 25, 25],
      [[10, 10, 15, 20]]);
  // B overlaps left half of A.
  assertDifference([10, 10, 20, 20], [5, 5, 15, 25],
      [[15, 10, 20, 20]]);
  // B touches A at its bottom right corner
  assertDifference([10, 10, 20, 20], [20, 20, 30, 30],
      [[10, 10, 20, 20]]);
  // B touches A at its top left corner
  assertDifference([10, 10, 20, 20], [5, 5, 10, 10],
      [[10, 10, 20, 20]]);
  // B touches A along its bottom edge
  assertDifference([10, 10, 20, 20], [12, 20, 17, 25],
      [[10, 10, 20, 20]]);
  // B splits A horizontally.
  assertDifference([10, 10, 20, 20], [5, 12, 25, 18],
      [[10, 10, 20, 12], [10, 18, 20, 20]]);
  // B splits A vertically.
  assertDifference([10, 10, 20, 20], [12, 5, 18, 25],
      [[10, 10, 12, 20], [18, 10, 20, 20]]);
  // B subtracts a notch from the top of A.
  assertDifference([10, 10, 20, 20], [12, 5, 18, 15],
      [[10, 15, 20, 20], [10, 10, 12, 15], [18, 10, 20, 15]]);
  // B subtracts a notch from the bottom left of A
  assertDifference([1, 6, 3, 9], [1, 7, 2, 9],
      [[1, 6, 3, 7], [2, 7, 3, 9]]);
  // B subtracts a notch from the bottom right of A
  assertDifference([1, 6, 3, 9], [2, 7, 3, 9],
      [[1, 6, 3, 7], [1, 7, 2, 9]]);
  // B subtracts a notch from the top left of A
  assertDifference([1, 6, 3, 9], [1, 6, 2, 8],
      [[1, 8, 3, 9], [2, 6, 3, 8]]);
  // B subtracts a notch from the top left of A (no coinciding edge)
  assertDifference([1, 6, 3, 9], [0, 5, 2, 8],
      [[1, 8, 3, 9], [2, 6, 3, 8]]);
  // B subtracts a hole from the center of A.
  assertDifference([-20, -20, -10, -10], [-18, -18, -12, -12],
      [[-20, -20, -10, -18], [-20, -12, -10, -10],
          [-20, -18, -18, -12], [-12, -18, -10, -12]]);
}

function assertDifference(a, b, expected) {
  var r0 = createRect(a);
  var r1 = createRect(b);
  var diff = goog.math.Rect.difference(r0, r1);

  assertEquals('Wrong number of rectangles in difference ',
      expected.length, diff.length);

  for (var j = 0; j < expected.length; ++j) {
    var e = createRect(expected[j]);
    if (!goog.math.Rect.equals(e, diff[j])) {
      alert(j + ": " + e + " != " + diff[j]);
    }
    assertRectsEqual(e, diff[j]);
  }

  // Test in place version
  var diff = r0.difference(r1);

  assertEquals('Wrong number of rectangles in in-place difference ',
      expected.length, diff.length);

  for (var j = 0; j < expected.length; ++j) {
    var e = createRect(expected[j]);
    if (!goog.math.Rect.equals(e, diff[j])) {
      alert(j + ": " + e + " != " + diff[j]);
    }
    assertRectsEqual(e, diff[j]);
  }
}

function testRectToBox() {
  var r = new goog.math.Rect(0, 0, 0, 0);
  assertEquals(new goog.math.Box(0, 0, 0, 0).toString(),
               r.toBox().toString());

  r.top = 10;
  r.left = 10;
  r.width = 20;
  r.height = 20;
  assertEquals(new goog.math.Box(10, 30, 30, 10).toString(),
               r.toBox().toString());

  r.top = -10;
  r.left = 0;
  r.width = 10;
  r.height = 10;
  assertEquals(new goog.math.Box(-10, 10, 0, 0).toString(),
               r.toBox().toString());
}

function testBoxToRect() {
  var box = new goog.math.Box(0, 0, 0, 0);
  assertEquals(new goog.math.Rect(0, 0, 0, 0).toString(),
               goog.math.Rect.createFromBox(box).toString());

  box.top = 10;
  box.left = 15;
  box.right = 23;
  box.bottom = 27;
  assertEquals(goog.math.Rect.createFromBox(box).toString(),
               new goog.math.Rect(15, 10, 8, 17).toString());

  box.top = -10;
  box.left = 3;
  box.right = 12;
  box.bottom = 7;
  assertEquals(new goog.math.Rect(3, -10, 9, 17).toString(),
               goog.math.Rect.createFromBox(box).toString());
}

function testBoxToRectAndBack() {
  rectToBoxAndBackTest(new goog.math.Rect(8, 11, 20, 23));
  rectToBoxAndBackTest(new goog.math.Rect(9, 13, NaN, NaN));
  rectToBoxAndBackTest(new goog.math.Rect(10, 13, NaN, 21));
  rectToBoxAndBackTest(new goog.math.Rect(5, 7, 14, NaN));
}

function rectToBoxAndBackTest(rect) {
  var box = rect.toBox();
  var rect2 = goog.math.Rect.createFromBox(box);
  assertEquals(rect.toString(), rect2.toString());
}

function testRectToBoxAndBack() {
  // This doesn't work if left or top is undefined.
  boxToRectAndBackTest(new goog.math.Box(11, 13, 20, 17));
  boxToRectAndBackTest(new goog.math.Box(10, NaN, NaN, 11));
  boxToRectAndBackTest(new goog.math.Box(9, 14, NaN, 11));
  boxToRectAndBackTest(new goog.math.Box(10, NaN, 22, 15));
}

function boxToRectAndBackTest(box) {
  var rect = goog.math.Rect.createFromBox(box);
  var box2 = rect.toBox();
  assertEquals(box.toString(), box2.toString());
}

function testRectContainsRect() {
  var r = new goog.math.Rect(-10, 0, 20, 10);
  assertTrue(r.contains(r));
  assertFalse(r.contains(new goog.math.Rect(NaN, NaN, NaN, NaN)));
  var r2 = new goog.math.Rect(0, 2, 5, 5);
  assertTrue(r.contains(r2));
  assertFalse(r2.contains(r));
  r2.left = -11;
  assertFalse(r.contains(r2));
  r2.left = 0;
  r2.width = 15;
  assertFalse(r.contains(r2));
  r2.width = 5;
  r2.height = 10;
  assertFalse(r.contains(r2));
  r2.top = 0;
  assertTrue(r.contains(r2));
}

function testRectContainsCoordinate() {
  var r = new goog.math.Rect(20, 40, 60, 80);

  // Test middle.
  assertTrue(r.contains(new goog.math.Coordinate(50, 80)));

  // Test edges.
  assertTrue(r.contains(new goog.math.Coordinate(20, 40)));
  assertTrue(r.contains(new goog.math.Coordinate(50, 40)));
  assertTrue(r.contains(new goog.math.Coordinate(80, 40)));
  assertTrue(r.contains(new goog.math.Coordinate(80, 80)));
  assertTrue(r.contains(new goog.math.Coordinate(80, 120)));
  assertTrue(r.contains(new goog.math.Coordinate(50, 120)));
  assertTrue(r.contains(new goog.math.Coordinate(20, 120)));
  assertTrue(r.contains(new goog.math.Coordinate(20, 80)));

  // Test outside.
  assertFalse(r.contains(new goog.math.Coordinate(0, 0)));
  assertFalse(r.contains(new goog.math.Coordinate(50, 0)));
  assertFalse(r.contains(new goog.math.Coordinate(100, 0)));
  assertFalse(r.contains(new goog.math.Coordinate(100, 80)));
  assertFalse(r.contains(new goog.math.Coordinate(100, 160)));
  assertFalse(r.contains(new goog.math.Coordinate(50, 160)));
  assertFalse(r.contains(new goog.math.Coordinate(0, 160)));
  assertFalse(r.contains(new goog.math.Coordinate(0, 80)));
}

function testGetSize() {
  assertEquals(new goog.math.Size(60, 80).toString(),
               new goog.math.Rect(20, 40, 60, 80).getSize().toString());
}
</script>
</body>
</html>