aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/math/box_test.html
blob: 3d7281a6a3e3edd6a22f642d5ea73beb8973b16a (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
<!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.Box</title>
<script src="../base.js"></script>
<script>
goog.require('goog.math.Box');
goog.require('goog.math.Coordinate');
goog.require('goog.testing.jsunit');
</script>
</head>
<body>
<script>

function testBoxClone() {
  var b = new goog.math.Box(0, 0, 0, 0);
  assertTrue(goog.math.Box.equals(b, b.clone()));

  b.left = 0;
  b.top = 1;
  b.right = 2;
  b.bottom = 3;
  assertTrue(goog.math.Box.equals(b, b.clone()));
}

function testBoxRelativePositionX() {
  var box = new goog.math.Box(50, 100, 100, 50);

  assertEquals(0,
      goog.math.Box.relativePositionX(box, new goog.math.Coordinate(75, 0)));
  assertEquals(0,
      goog.math.Box.relativePositionX(box, new goog.math.Coordinate(75, 75)));
  assertEquals(0,
      goog.math.Box.relativePositionX(box, new goog.math.Coordinate(75, 105)));
  assertEquals(-5,
      goog.math.Box.relativePositionX(box, new goog.math.Coordinate(45, 75)));
  assertEquals(5,
      goog.math.Box.relativePositionX(box, new goog.math.Coordinate(105, 75)));
}

function testBoxRelativePositionY() {
  var box = new goog.math.Box(50, 100, 100, 50);

  assertEquals(0,
      goog.math.Box.relativePositionY(box, new goog.math.Coordinate(0, 75)));
  assertEquals(0,
      goog.math.Box.relativePositionY(box, new goog.math.Coordinate(75, 75)));
  assertEquals(0,
      goog.math.Box.relativePositionY(box, new goog.math.Coordinate(105, 75)));
  assertEquals(-5,
      goog.math.Box.relativePositionY(box, new goog.math.Coordinate(75, 45)));
  assertEquals(5,
      goog.math.Box.relativePositionY(box, new goog.math.Coordinate(75, 105)));
}

function testBoxDistance() {
  var box = new goog.math.Box(50, 100, 100, 50);

  assertEquals(0,
               goog.math.Box.distance(box, new goog.math.Coordinate(75, 75)));
  assertEquals(25,
               goog.math.Box.distance(box, new goog.math.Coordinate(75, 25)));
  assertEquals(10,
               goog.math.Box.distance(box, new goog.math.Coordinate(40, 80)));
  assertEquals(5,
               goog.math.Box.distance(box, new goog.math.Coordinate(46, 47)));
  assertEquals(10,
               goog.math.Box.distance(box, new goog.math.Coordinate(106, 108)));
}

function testBoxContains() {
  var box = new goog.math.Box(50, 100, 100, 50);

  assertTrue(goog.math.Box.contains(box, new goog.math.Coordinate(75, 75)));
  assertTrue(goog.math.Box.contains(box, new goog.math.Coordinate(50, 100)));
  assertTrue(goog.math.Box.contains(box, new goog.math.Coordinate(100, 99)));
  assertFalse(goog.math.Box.contains(box, new goog.math.Coordinate(100, 101)));
  assertFalse(goog.math.Box.contains(box, new goog.math.Coordinate(49, 50)));
  assertFalse(goog.math.Box.contains(box, new goog.math.Coordinate(25, 25)));
}

function testBoxContainsBox() {
  var box = new goog.math.Box(50, 100, 100, 50);

  function assertContains(boxB) {
    assertTrue(box + ' expected to contain ' + boxB,
        goog.math.Box.contains(box, boxB));
  }

  function assertNotContains(boxB) {
    assertFalse(box + ' expected to not contain ' + boxB,
        goog.math.Box.contains(box, boxB));
  }

  assertContains(new goog.math.Box(60, 90, 90, 60));
  assertNotContains(new goog.math.Box(1, 3, 4, 2));
  assertNotContains(new goog.math.Box(30, 90, 60, 60));
  assertNotContains(new goog.math.Box(60, 110, 60, 60));
  assertNotContains(new goog.math.Box(60, 90, 110, 60));
  assertNotContains(new goog.math.Box(60, 90, 60, 40));
}

function testBoxesIntersect() {
  var box = new goog.math.Box(50, 100, 100, 50);

  function assertIntersects(boxB) {
    assertTrue(box + ' expected to intersect ' + boxB,
        goog.math.Box.intersects(box, boxB));
  }
  function assertNotIntersects(boxB) {
    assertFalse(box + ' expected to not intersect ' + boxB,
        goog.math.Box.intersects(box, boxB));
  }

  assertIntersects(box);
  assertIntersects(new goog.math.Box(20, 80, 80, 20));
  assertIntersects(new goog.math.Box(50, 80, 100, 20));
  assertIntersects(new goog.math.Box(80, 80, 120, 20));
  assertIntersects(new goog.math.Box(20, 100, 80, 50));
  assertIntersects(new goog.math.Box(80, 100, 120, 50));
  assertIntersects(new goog.math.Box(20, 120, 80, 80));
  assertIntersects(new goog.math.Box(50, 120, 100, 80));
  assertIntersects(new goog.math.Box(80, 120, 120, 80));
  assertIntersects(new goog.math.Box(20, 120, 120, 20));
  assertIntersects(new goog.math.Box(70, 80, 80, 70));
  assertNotIntersects(new goog.math.Box(10, 30, 30, 10));
  assertNotIntersects(new goog.math.Box(10, 70, 30, 30));
  assertNotIntersects(new goog.math.Box(10, 100, 30, 50));
  assertNotIntersects(new goog.math.Box(10, 120, 30, 80));
  assertNotIntersects(new goog.math.Box(10, 140, 30, 120));
  assertNotIntersects(new goog.math.Box(30, 30, 70, 10));
  assertNotIntersects(new goog.math.Box(30, 140, 70, 120));
  assertNotIntersects(new goog.math.Box(50, 30, 100, 10));
  assertNotIntersects(new goog.math.Box(50, 140, 100, 120));
  assertNotIntersects(new goog.math.Box(80, 30, 120, 10));
  assertNotIntersects(new goog.math.Box(80, 140, 120, 120));
  assertNotIntersects(new goog.math.Box(120, 30, 140, 10));
  assertNotIntersects(new goog.math.Box(120, 70, 140, 30));
  assertNotIntersects(new goog.math.Box(120, 100, 140, 50));
  assertNotIntersects(new goog.math.Box(120, 120, 140, 80));
  assertNotIntersects(new goog.math.Box(120, 140, 140, 120));
}

function testBoxesIntersectWithPadding() {
  var box = new goog.math.Box(50, 100, 100, 50);

  function assertIntersects(boxB, padding) {
    assertTrue(box + ' expected to intersect ' + boxB + ' with padding ' +
        padding, goog.math.Box.intersectsWithPadding(box, boxB, padding));
  }
  function assertNotIntersects(boxB, padding) {
    assertFalse(box + ' expected to not intersect ' + boxB + ' with padding ' +
        padding, goog.math.Box.intersectsWithPadding(box, boxB, padding));
  }

  assertIntersects(box, 10);
  assertIntersects(new goog.math.Box(20, 80, 80, 20), 10);
  assertIntersects(new goog.math.Box(50, 80, 100, 20), 10);
  assertIntersects(new goog.math.Box(80, 80, 120, 20), 10);
  assertIntersects(new goog.math.Box(20, 100, 80, 50), 10);
  assertIntersects(new goog.math.Box(80, 100, 120, 50), 10);
  assertIntersects(new goog.math.Box(20, 120, 80, 80), 10);
  assertIntersects(new goog.math.Box(50, 120, 100, 80), 10);
  assertIntersects(new goog.math.Box(80, 120, 120, 80), 10);
  assertIntersects(new goog.math.Box(20, 120, 120, 20), 10);
  assertIntersects(new goog.math.Box(70, 80, 80, 70), 10);
  assertIntersects(new goog.math.Box(10, 30, 30, 10), 20);
  assertIntersects(new goog.math.Box(10, 70, 30, 30), 20);
  assertIntersects(new goog.math.Box(10, 100, 30, 50), 20);
  assertIntersects(new goog.math.Box(10, 120, 30, 80), 20);
  assertIntersects(new goog.math.Box(10, 140, 30, 120), 20);
  assertIntersects(new goog.math.Box(30, 30, 70, 10), 20);
  assertIntersects(new goog.math.Box(30, 140, 70, 120), 20);
  assertIntersects(new goog.math.Box(50, 30, 100, 10), 20);
  assertIntersects(new goog.math.Box(50, 140, 100, 120), 20);
  assertIntersects(new goog.math.Box(80, 30, 120, 10), 20);
  assertIntersects(new goog.math.Box(80, 140, 120, 120), 20);
  assertIntersects(new goog.math.Box(120, 30, 140, 10), 20);
  assertIntersects(new goog.math.Box(120, 70, 140, 30), 20);
  assertIntersects(new goog.math.Box(120, 100, 140, 50), 20);
  assertIntersects(new goog.math.Box(120, 120, 140, 80), 20);
  assertIntersects(new goog.math.Box(120, 140, 140, 120), 20);
  assertNotIntersects(new goog.math.Box(10, 30, 30, 10), 10);
  assertNotIntersects(new goog.math.Box(10, 70, 30, 30), 10);
  assertNotIntersects(new goog.math.Box(10, 100, 30, 50), 10);
  assertNotIntersects(new goog.math.Box(10, 120, 30, 80), 10);
  assertNotIntersects(new goog.math.Box(10, 140, 30, 120), 10);
  assertNotIntersects(new goog.math.Box(30, 30, 70, 10), 10);
  assertNotIntersects(new goog.math.Box(30, 140, 70, 120), 10);
  assertNotIntersects(new goog.math.Box(50, 30, 100, 10), 10);
  assertNotIntersects(new goog.math.Box(50, 140, 100, 120), 10);
  assertNotIntersects(new goog.math.Box(80, 30, 120, 10), 10);
  assertNotIntersects(new goog.math.Box(80, 140, 120, 120), 10);
  assertNotIntersects(new goog.math.Box(120, 30, 140, 10), 10);
  assertNotIntersects(new goog.math.Box(120, 70, 140, 30), 10);
  assertNotIntersects(new goog.math.Box(120, 100, 140, 50), 10);
  assertNotIntersects(new goog.math.Box(120, 120, 140, 80), 10);
  assertNotIntersects(new goog.math.Box(120, 140, 140, 120), 10);
}

function testExpandToInclude() {
  var box = new goog.math.Box(10, 50, 50, 10);
  box.expandToInclude(new goog.math.Box(60, 70, 70, 60));
  assertEquals(10, box.left);
  assertEquals(10, box.top);
  assertEquals(70, box.right);
  assertEquals(70, box.bottom);
  box.expandToInclude(new goog.math.Box(30, 40, 40, 30));
  assertEquals(10, box.left);
  assertEquals(10, box.top);
  assertEquals(70, box.right);
  assertEquals(70, box.bottom);
  box.expandToInclude(new goog.math.Box(0, 100, 100, 0));
  assertEquals(0, box.left);
  assertEquals(0, box.top);
  assertEquals(100, box.right);
  assertEquals(100, box.bottom);
}

function testBoundingBox() {
  assertObjectEquals(
      new goog.math.Box(1, 10, 11, 0),
      goog.math.Box.boundingBox(
          new goog.math.Coordinate(5, 5),
          new goog.math.Coordinate(5, 11),
          new goog.math.Coordinate(0, 5),
          new goog.math.Coordinate(5, 1),
          new goog.math.Coordinate(10, 5)));
}

</script>
</body>
</html>