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