OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library rect_test; | 5 library rect_test; |
6 | 6 |
7 import 'dart:html'; | 7 import 'dart:math'; |
8 import '../../pkg/unittest/lib/unittest.dart'; | 8 import 'package:unittest/unittest.dart'; |
9 import '../../pkg/unittest/lib/html_config.dart'; | |
10 | 9 |
11 main() { | 10 main() { |
12 useHtmlConfiguration(); | 11 Rectangle createRectangle(List<num> a) { |
13 | 12 return a != null ? new Rectangle(a[0], a[1], a[2] - a[0], a[3] - a[1]) |
14 Rect createRect(List<num> a) { | 13 : null; |
15 return a != null ? new Rect(a[0], a[1], a[2] - a[0], a[3] - a[1]) : null; | |
16 } | 14 } |
17 | 15 |
18 test('construction', () { | 16 test('construction', () { |
19 var r0 = new Rect(10, 20, 30, 40); | 17 var r0 = new Rectangle(10, 20, 30, 40); |
20 expect(r0.toString(), '(10, 20, 30, 40)'); | 18 expect(r0.toString(), 'Rectangle (10, 20) 30 x 40'); |
21 expect(r0.right, 40); | 19 expect(r0.right, 40); |
22 expect(r0.bottom, 60); | 20 expect(r0.bottom, 60); |
23 | 21 |
24 var r1 = new Rect.fromPoints(r0.topLeft, r0.bottomRight); | 22 var r1 = new Rectangle.fromPoints(r0.topLeft, r0.bottomRight); |
25 expect(r1, r0); | 23 expect(r1, r0); |
26 | 24 |
27 var r2 = new Rect.fromPoints(r0.bottomRight, r0.topLeft); | 25 var r2 = new Rectangle.fromPoints(r0.bottomRight, r0.topLeft); |
28 expect(r2, r0); | 26 expect(r2, r0); |
29 }); | 27 }); |
30 | 28 |
31 test('intersection', () { | 29 test('intersection', () { |
32 var tests = [ | 30 var tests = [ |
33 [[10, 10, 20, 20], [15, 15, 25, 25], [15, 15, 20, 20]], | 31 [[10, 10, 20, 20], [15, 15, 25, 25], [15, 15, 20, 20]], |
34 [[10, 10, 20, 20], [20, 0, 30, 10], [20, 10, 20, 10]], | 32 [[10, 10, 20, 20], [20, 0, 30, 10], [20, 10, 20, 10]], |
35 [[0, 0, 1, 1], [10, 11, 12, 13], null], | 33 [[0, 0, 1, 1], [10, 11, 12, 13], null], |
36 [[11, 12, 98, 99], [22, 23, 34, 35], [22, 23, 34, 35]]]; | 34 [[11, 12, 98, 99], [22, 23, 34, 35], [22, 23, 34, 35]]]; |
37 | 35 |
38 for (var test in tests) { | 36 for (var test in tests) { |
39 var r0 = createRect(test[0]); | 37 var r0 = createRectangle(test[0]); |
40 var r1 = createRect(test[1]); | 38 var r1 = createRectangle(test[1]); |
41 var expected = createRect(test[2]); | 39 var expected = createRectangle(test[2]); |
42 | 40 |
43 expect(r0.intersection(r1), expected); | 41 expect(r0.intersection(r1), expected); |
44 expect(r1.intersection(r0), expected); | 42 expect(r1.intersection(r0), expected); |
45 } | 43 } |
46 }); | 44 }); |
47 | 45 |
48 test('intersects', () { | 46 test('intersects', () { |
49 var r0 = new Rect(10, 10, 20, 20); | 47 var r0 = new Rectangle(10, 10, 20, 20); |
50 var r1 = new Rect(15, 15, 25, 25); | 48 var r1 = new Rectangle(15, 15, 25, 25); |
51 var r2 = new Rect(0, 0, 1, 1); | 49 var r2 = new Rectangle(0, 0, 1, 1); |
52 | 50 |
53 expect(r0.intersects(r1), isTrue); | 51 expect(r0.intersects(r1), isTrue); |
54 expect(r1.intersects(r0), isTrue); | 52 expect(r1.intersects(r0), isTrue); |
55 | 53 |
56 expect(r0.intersects(r2), isFalse); | 54 expect(r0.intersects(r2), isFalse); |
57 expect(r2.intersects(r0), isFalse); | 55 expect(r2.intersects(r0), isFalse); |
58 }); | 56 }); |
59 | 57 |
60 test('union', () { | 58 test('boundingBox', () { |
61 var tests = [ | 59 var tests = [ |
62 [[10, 10, 20, 20], [15, 15, 25, 25], [10, 10, 25, 25]], | 60 [[10, 10, 20, 20], [15, 15, 25, 25], [10, 10, 25, 25]], |
63 [[10, 10, 20, 20], [20, 0, 30, 10], [10, 0, 30, 20]], | 61 [[10, 10, 20, 20], [20, 0, 30, 10], [10, 0, 30, 20]], |
64 [[0, 0, 1, 1], [10, 11, 12, 13], [0, 0, 12, 13]], | 62 [[0, 0, 1, 1], [10, 11, 12, 13], [0, 0, 12, 13]], |
65 [[11, 12, 98, 99], [22, 23, 34, 35], [11, 12, 98, 99]]]; | 63 [[11, 12, 98, 99], [22, 23, 34, 35], [11, 12, 98, 99]]]; |
66 | 64 |
67 for (var test in tests) { | 65 for (var test in tests) { |
68 var r0 = createRect(test[0]); | 66 var r0 = createRectangle(test[0]); |
69 var r1 = createRect(test[1]); | 67 var r1 = createRectangle(test[1]); |
70 var expected = createRect(test[2]); | 68 var expected = createRectangle(test[2]); |
71 | 69 |
72 expect(r0.union(r1), expected); | 70 expect(r0.boundingBox(r1), expected); |
73 expect(r1.union(r0), expected); | 71 expect(r1.boundingBox(r0), expected); |
74 } | 72 } |
75 }); | 73 }); |
76 | 74 |
77 test('containsRect', () { | 75 test('containsRectangle', () { |
78 var r = new Rect(-10, 0, 20, 10); | 76 var r = new Rectangle(-10, 0, 20, 10); |
79 expect(r.containsRect(r), isTrue); | 77 expect(r.contains(r), isTrue); |
80 | 78 |
81 expect(r.containsRect( | 79 expect(r.contains( |
82 new Rect(double.NAN, double.NAN, double.NAN, double.NAN)), isFalse); | 80 new Rectangle(double.NAN, double.NAN, double.NAN, double.NAN)), isFalse)
; |
83 | 81 |
84 var r2 = new Rect(0, 2, 5, 5); | 82 var r2 = new Rectangle(0, 2, 5, 5); |
85 expect(r.containsRect(r2), isTrue); | 83 expect(r.contains(r2), isTrue); |
86 expect(r2.containsRect(r), isFalse); | 84 expect(r2.contains(r), isFalse); |
87 | 85 |
88 r2 = new Rect(-11, 2, 5, 5); | 86 r2 = new Rectangle(-11, 2, 5, 5); |
89 expect(r.containsRect(r2), isFalse); | 87 expect(r.contains(r2), isFalse); |
90 r2 = new Rect(0, 2, 15, 5); | 88 r2 = new Rectangle(0, 2, 15, 5); |
91 expect(r.containsRect(r2), isFalse); | 89 expect(r.contains(r2), isFalse); |
92 r2 = new Rect(0, 2, 5, 10); | 90 r2 = new Rectangle(0, 2, 5, 10); |
93 expect(r.containsRect(r2), isFalse); | 91 expect(r.contains(r2), isFalse); |
94 r2 = new Rect(0, 0, 5, 10); | 92 r2 = new Rectangle(0, 0, 5, 10); |
95 expect(r.containsRect(r2), isTrue); | 93 expect(r.contains(r2), isTrue); |
96 }); | 94 }); |
97 | 95 |
98 test('containsPoint', () { | 96 test('containsPoint', () { |
99 var r = new Rect(20, 40, 60, 80); | 97 var r = new Rectangle(20, 40, 60, 80); |
100 | 98 |
101 // Test middle. | 99 // Test middle. |
102 expect(r.containsPoint(new Point(50, 80)), isTrue); | 100 expect(r.containsPoint(new Point(50, 80)), isTrue); |
103 | 101 |
104 // Test edges. | 102 // Test edges. |
105 expect(r.containsPoint(new Point(20, 40)), isTrue); | 103 expect(r.containsPoint(new Point(20, 40)), isTrue); |
106 expect(r.containsPoint(new Point(50, 40)), isTrue); | 104 expect(r.containsPoint(new Point(50, 40)), isTrue); |
107 expect(r.containsPoint(new Point(80, 40)), isTrue); | 105 expect(r.containsPoint(new Point(80, 40)), isTrue); |
108 expect(r.containsPoint(new Point(80, 80)), isTrue); | 106 expect(r.containsPoint(new Point(80, 80)), isTrue); |
109 expect(r.containsPoint(new Point(80, 120)), isTrue); | 107 expect(r.containsPoint(new Point(80, 120)), isTrue); |
110 expect(r.containsPoint(new Point(50, 120)), isTrue); | 108 expect(r.containsPoint(new Point(50, 120)), isTrue); |
111 expect(r.containsPoint(new Point(20, 120)), isTrue); | 109 expect(r.containsPoint(new Point(20, 120)), isTrue); |
112 expect(r.containsPoint(new Point(20, 80)), isTrue); | 110 expect(r.containsPoint(new Point(20, 80)), isTrue); |
113 | 111 |
114 // Test outside. | 112 // Test outside. |
115 expect(r.containsPoint(new Point(0, 0)), isFalse); | 113 expect(r.containsPoint(new Point(0, 0)), isFalse); |
116 expect(r.containsPoint(new Point(50, 0)), isFalse); | 114 expect(r.containsPoint(new Point(50, 0)), isFalse); |
117 expect(r.containsPoint(new Point(100, 0)), isFalse); | 115 expect(r.containsPoint(new Point(100, 0)), isFalse); |
118 expect(r.containsPoint(new Point(100, 80)), isFalse); | 116 expect(r.containsPoint(new Point(100, 80)), isFalse); |
119 expect(r.containsPoint(new Point(100, 160)), isFalse); | 117 expect(r.containsPoint(new Point(100, 160)), isFalse); |
120 expect(r.containsPoint(new Point(50, 160)), isFalse); | 118 expect(r.containsPoint(new Point(50, 160)), isFalse); |
121 expect(r.containsPoint(new Point(0, 160)), isFalse); | 119 expect(r.containsPoint(new Point(0, 160)), isFalse); |
122 expect(r.containsPoint(new Point(0, 80)), isFalse); | 120 expect(r.containsPoint(new Point(0, 80)), isFalse); |
123 }); | 121 }); |
124 | 122 |
125 test('ceil', () { | |
126 var rect = new Rect(11.4, 26.6, 17.8, 9.2); | |
127 expect(rect.ceil(), new Rect(12.0, 27.0, 18.0, 10.0)); | |
128 }); | |
129 | |
130 test('floor', () { | |
131 var rect = new Rect(11.4, 26.6, 17.8, 9.2); | |
132 expect(rect.floor(), new Rect(11.0, 26.0, 17.0, 9.0)); | |
133 }); | |
134 | |
135 test('round', () { | |
136 var rect = new Rect(11.4, 26.6, 17.8, 9.2); | |
137 expect(rect.round(), new Rect(11.0, 27.0, 18.0, 9.0)); | |
138 }); | |
139 | |
140 test('toInt', () { | |
141 var rect = new Rect(11.4, 26.6, 17.8, 9.2); | |
142 var b = rect.toInt(); | |
143 expect(b, new Rect(11, 26, 17, 9)); | |
144 | |
145 expect(b.left is int, isTrue); | |
146 expect(b.top is int, isTrue); | |
147 expect(b.width is int, isTrue); | |
148 expect(b.height is int, isTrue); | |
149 }); | |
150 | |
151 test('hashCode', () { | 123 test('hashCode', () { |
152 var a = new Rect(0, 1, 2, 3); | 124 var a = new Rectangle(0, 1, 2, 3); |
153 var b = new Rect(0, 1, 2, 3); | 125 var b = new Rectangle(0, 1, 2, 3); |
154 expect(a.hashCode, b.hashCode); | 126 expect(a.hashCode, b.hashCode); |
155 | 127 |
156 var c = new Rect(1, 0, 2, 3); | 128 var c = new Rectangle(1, 0, 2, 3); |
157 expect(a.hashCode == c.hashCode, isFalse); | 129 expect(a.hashCode == c.hashCode, isFalse); |
158 }); | 130 }); |
159 } | 131 } |
OLD | NEW |