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