OLD | NEW |
| (Empty) |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 import 'dart:math'; | |
6 import 'package:expect/expect.dart'; | |
7 | |
8 main() { | |
9 testConstruction(); | |
10 testIntersection(); | |
11 testIntersects(); | |
12 testBoundingBox(); | |
13 testContainsRectangle(); | |
14 testContainsPoint(); | |
15 testHashCode(); | |
16 testEdgeCases(); | |
17 testEquality(); | |
18 testNegativeLengths(); | |
19 testNaNLeft(); | |
20 testNaNTop(); | |
21 testNaNWidth(); | |
22 testNaNHeight(); | |
23 } | |
24 | |
25 Rectangle createRectangle(List<num> a) { | |
26 return a != null ? new Rectangle(a[0], a[1], a[2] - a[0], a[3] - a[1]) : null; | |
27 } | |
28 | |
29 testConstruction() { | |
30 var r0 = new Rectangle(10, 20, 30, 40); | |
31 Expect.equals('Rectangle (10, 20) 30 x 40', r0.toString()); | |
32 Expect.equals(40, r0.right); | |
33 Expect.equals(60, r0.bottom); | |
34 | |
35 var r1 = new Rectangle.fromPoints(r0.topLeft, r0.bottomRight); | |
36 Expect.equals(r0, r1); | |
37 | |
38 var r2 = new Rectangle.fromPoints(r0.bottomRight, r0.topLeft); | |
39 Expect.equals(r0, r2); | |
40 } | |
41 | |
42 testIntersection() { | |
43 var tests = [ | |
44 [ | |
45 [10, 10, 20, 20], | |
46 [15, 15, 25, 25], | |
47 [15, 15, 20, 20] | |
48 ], | |
49 [ | |
50 [10, 10, 20, 20], | |
51 [20, 0, 30, 10], | |
52 [20, 10, 20, 10] | |
53 ], | |
54 [ | |
55 [0, 0, 1, 1], | |
56 [10, 11, 12, 13], | |
57 null | |
58 ], | |
59 [ | |
60 [11, 12, 98, 99], | |
61 [22, 23, 34, 35], | |
62 [22, 23, 34, 35] | |
63 ] | |
64 ]; | |
65 | |
66 for (var test in tests) { | |
67 var r0 = createRectangle(test[0]); | |
68 var r1 = createRectangle(test[1]); | |
69 var expected = createRectangle(test[2]); | |
70 | |
71 Expect.equals(expected, r0.intersection(r1)); | |
72 Expect.equals(expected, r1.intersection(r0)); | |
73 } | |
74 } | |
75 | |
76 testIntersects() { | |
77 var r0 = new Rectangle(10, 10, 20, 20); | |
78 var r1 = new Rectangle(15, 15, 25, 25); | |
79 var r2 = new Rectangle(0, 0, 1, 1); | |
80 | |
81 Expect.isTrue(r0.intersects(r1)); | |
82 Expect.isTrue(r1.intersects(r0)); | |
83 | |
84 Expect.isFalse(r0.intersects(r2)); | |
85 Expect.isFalse(r2.intersects(r0)); | |
86 } | |
87 | |
88 testBoundingBox() { | |
89 var tests = [ | |
90 [ | |
91 [10, 10, 20, 20], | |
92 [15, 15, 25, 25], | |
93 [10, 10, 25, 25] | |
94 ], | |
95 [ | |
96 [10, 10, 20, 20], | |
97 [20, 0, 30, 10], | |
98 [10, 0, 30, 20] | |
99 ], | |
100 [ | |
101 [0, 0, 1, 1], | |
102 [10, 11, 12, 13], | |
103 [0, 0, 12, 13] | |
104 ], | |
105 [ | |
106 [11, 12, 98, 99], | |
107 [22, 23, 34, 35], | |
108 [11, 12, 98, 99] | |
109 ] | |
110 ]; | |
111 | |
112 for (var test in tests) { | |
113 var r0 = createRectangle(test[0]); | |
114 var r1 = createRectangle(test[1]); | |
115 var expected = createRectangle(test[2]); | |
116 | |
117 Expect.equals(expected, r0.boundingBox(r1)); | |
118 Expect.equals(expected, r1.boundingBox(r0)); | |
119 } | |
120 } | |
121 | |
122 testContainsRectangle() { | |
123 var r = new Rectangle(-10, 0, 20, 10); | |
124 Expect.isTrue(r.containsRectangle(r)); | |
125 | |
126 Expect.isFalse(r.containsRectangle( | |
127 new Rectangle(double.NAN, double.NAN, double.NAN, double.NAN))); | |
128 | |
129 var r2 = new Rectangle(0, 2, 5, 5); | |
130 Expect.isTrue(r.containsRectangle(r2)); | |
131 Expect.isFalse(r2.containsRectangle(r)); | |
132 | |
133 r2 = new Rectangle(-11, 2, 5, 5); | |
134 Expect.isFalse(r.containsRectangle(r2)); | |
135 r2 = new Rectangle(0, 2, 15, 5); | |
136 Expect.isFalse(r.containsRectangle(r2)); | |
137 r2 = new Rectangle(0, 2, 5, 10); | |
138 Expect.isFalse(r.containsRectangle(r2)); | |
139 r2 = new Rectangle(0, 0, 5, 10); | |
140 Expect.isTrue(r.containsRectangle(r2)); | |
141 } | |
142 | |
143 testContainsPoint() { | |
144 var r = new Rectangle(20, 40, 60, 80); | |
145 | |
146 // Test middle. | |
147 Expect.isTrue(r.containsPoint(new Point(50, 80))); | |
148 | |
149 // Test edges. | |
150 Expect.isTrue(r.containsPoint(new Point(20, 40))); | |
151 Expect.isTrue(r.containsPoint(new Point(50, 40))); | |
152 Expect.isTrue(r.containsPoint(new Point(80, 40))); | |
153 Expect.isTrue(r.containsPoint(new Point(80, 80))); | |
154 Expect.isTrue(r.containsPoint(new Point(80, 120))); | |
155 Expect.isTrue(r.containsPoint(new Point(50, 120))); | |
156 Expect.isTrue(r.containsPoint(new Point(20, 120))); | |
157 Expect.isTrue(r.containsPoint(new Point(20, 80))); | |
158 | |
159 // Test outside. | |
160 Expect.isFalse(r.containsPoint(new Point(0, 0))); | |
161 Expect.isFalse(r.containsPoint(new Point(50, 0))); | |
162 Expect.isFalse(r.containsPoint(new Point(100, 0))); | |
163 Expect.isFalse(r.containsPoint(new Point(100, 80))); | |
164 Expect.isFalse(r.containsPoint(new Point(100, 160))); | |
165 Expect.isFalse(r.containsPoint(new Point(50, 160))); | |
166 Expect.isFalse(r.containsPoint(new Point(0, 160))); | |
167 Expect.isFalse(r.containsPoint(new Point(0, 80))); | |
168 } | |
169 | |
170 testHashCode() { | |
171 var a = new Rectangle(0, 1, 2, 3); | |
172 var b = new Rectangle(0, 1, 2, 3); | |
173 Expect.equals(b.hashCode, a.hashCode); | |
174 | |
175 var c = new Rectangle(1, 0, 2, 3); | |
176 Expect.isFalse(a.hashCode == c.hashCode); | |
177 } | |
178 | |
179 testEdgeCases() { | |
180 edgeTest(double a, double l) { | |
181 var r = new Rectangle(a, a, l, l); | |
182 Expect.equals(r, r.boundingBox(r)); | |
183 Expect.equals(r, r.intersection(r)); | |
184 } | |
185 | |
186 var bignum1 = 0x20000000000000 + 0.0; | |
187 var bignum2 = 0x20000000000002 + 0.0; | |
188 var bignum3 = 0x20000000000004 + 0.0; | |
189 edgeTest(1.0, bignum1); | |
190 edgeTest(1.0, bignum2); | |
191 edgeTest(1.0, bignum3); | |
192 edgeTest(bignum1, 1.0); | |
193 edgeTest(bignum2, 1.0); | |
194 edgeTest(bignum3, 1.0); | |
195 } | |
196 | |
197 testEquality() { | |
198 var bignum = 0x80000000000008 + 0.0; | |
199 var r1 = new Rectangle(bignum, bignum, 1.0, 1.0); | |
200 var r2 = new Rectangle(bignum, bignum, 2.0, 2.0); | |
201 Expect.equals(r2, r1); | |
202 Expect.equals(r2.hashCode, r1.hashCode); | |
203 Expect.equals(r2.right, r1.right); | |
204 Expect.equals(r2.bottom, r1.bottom); | |
205 Expect.equals(1.0, r1.width); | |
206 Expect.equals(2.0, r2.width); | |
207 } | |
208 | |
209 testNegativeLengths() { | |
210 // Constructor allows negative lengths, but clamps them to zero. | |
211 Expect.equals(new Rectangle(4, 4, 0, 0), new Rectangle(4, 4, -2, -2)); | |
212 Expect.equals(new Rectangle(4, 4, 0, 0), new MutableRectangle(4, 4, -2, -2)); | |
213 | |
214 // Setters clamp negative lengths to zero. | |
215 var mutable = new MutableRectangle(0, 0, 1, 1); | |
216 mutable.width = -1; | |
217 mutable.height = -1; | |
218 Expect.equals(new Rectangle(0, 0, 0, 0), mutable); | |
219 | |
220 // Test that doubles are clamped to double zero. | |
221 var rectangle = new Rectangle(1.5, 1.5, -2.5, -2.5); | |
222 Expect.isTrue(identical(rectangle.width, 0.0)); | |
223 Expect.isTrue(identical(rectangle.height, 0.0)); | |
224 } | |
225 | |
226 testNaNLeft() { | |
227 var rectangles = [ | |
228 const Rectangle(double.NAN, 1, 2, 3), | |
229 new MutableRectangle(double.NAN, 1, 2, 3), | |
230 new Rectangle.fromPoints(new Point(double.NAN, 1), new Point(2, 4)), | |
231 new MutableRectangle.fromPoints(new Point(double.NAN, 1), new Point(2, 4)), | |
232 ]; | |
233 for (var r in rectangles) { | |
234 Expect.isFalse(r.containsPoint(new Point(0, 1))); | |
235 Expect.isFalse(r.containsRectangle(new Rectangle(0, 1, 2, 3))); | |
236 Expect.isFalse(r.intersects(new Rectangle(0, 1, 2, 3))); | |
237 Expect.isTrue(r.left.isNaN); | |
238 Expect.isTrue(r.right.isNaN); | |
239 } | |
240 } | |
241 | |
242 testNaNTop() { | |
243 var rectangles = [ | |
244 const Rectangle(0, double.NAN, 2, 3), | |
245 new MutableRectangle(0, double.NAN, 2, 3), | |
246 new Rectangle.fromPoints(new Point(0, double.NAN), new Point(2, 4)), | |
247 new MutableRectangle.fromPoints(new Point(0, double.NAN), new Point(2, 4)), | |
248 ]; | |
249 for (var r in rectangles) { | |
250 Expect.isFalse(r.containsPoint(new Point(0, 1))); | |
251 Expect.isFalse(r.containsRectangle(new Rectangle(0, 1, 2, 3))); | |
252 Expect.isFalse(r.intersects(new Rectangle(0, 1, 2, 3))); | |
253 Expect.isTrue(r.top.isNaN); | |
254 Expect.isTrue(r.bottom.isNaN); | |
255 } | |
256 } | |
257 | |
258 testNaNWidth() { | |
259 var rectangles = [ | |
260 const Rectangle(0, 1, double.NAN, 3), | |
261 new MutableRectangle(0, 1, double.NAN, 3), | |
262 new Rectangle.fromPoints(new Point(0, 1), new Point(double.NAN, 4)), | |
263 new MutableRectangle.fromPoints(new Point(0, 1), new Point(double.NAN, 4)), | |
264 ]; | |
265 for (var r in rectangles) { | |
266 Expect.isFalse(r.containsPoint(new Point(0, 1))); | |
267 Expect.isFalse(r.containsRectangle(new Rectangle(0, 1, 2, 3))); | |
268 Expect.isFalse(r.intersects(new Rectangle(0, 1, 2, 3))); | |
269 Expect.isTrue(r.right.isNaN); | |
270 Expect.isTrue(r.width.isNaN); | |
271 } | |
272 } | |
273 | |
274 testNaNHeight() { | |
275 var rectangles = [ | |
276 const Rectangle(0, 1, 2, double.NAN), | |
277 new MutableRectangle(0, 1, 2, double.NAN), | |
278 new Rectangle.fromPoints(new Point(0, 1), new Point(2, double.NAN)), | |
279 new MutableRectangle.fromPoints(new Point(0, 1), new Point(2, double.NAN)), | |
280 ]; | |
281 for (var r in rectangles) { | |
282 Expect.isFalse(r.containsPoint(new Point(0, 1))); | |
283 Expect.isFalse(r.containsRectangle(new Rectangle(0, 1, 2, 3))); | |
284 Expect.isFalse(r.intersects(new Rectangle(0, 1, 2, 3))); | |
285 Expect.isTrue(r.bottom.isNaN); | |
286 Expect.isTrue(r.height.isNaN); | |
287 } | |
288 } | |
OLD | NEW |