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