Package pytilities :: Package test :: Package geometry :: Module rectangle
[hide private]
[frames] | no frames]

Source Code for Module pytilities.test.geometry.rectangle

  1  # Copyright (C) 2010 Tim Diels <limyreth@users.sourceforge.net> 
  2  #  
  3  # This file is part of pytilities. 
  4  #  
  5  # pytilities is free software: you can redistribute it and/or modify 
  6  # it under the terms of the GNU General Public License as published by 
  7  # the Free Software Foundation, either version 3 of the License, or 
  8  # (at your option) any later version. 
  9  #  
 10  # pytilities is distributed in the hope that it will be useful, 
 11  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 13  # GNU General Public License for more details. 
 14  #  
 15  # You should have received a copy of the GNU General Public License 
 16  # along with pytilities.  If not, see <http://www.gnu.org/licenses/>. 
 17  # 
 18   
 19  __docformat__ = 'reStructuredText' 
 20   
 21  from unittest import TestCase 
 22  import unittest 
 23   
 24  from pytilities.geometry import Vector as V, Rectangle as R, VerboseRectangle 
 25   
26 -class RectangleCtorTestCase(TestCase):
27 - def test_ctor_points(self):
28 r = R(V(2, 3), V(3, 4)) 29 self.assertEqual(r.bounds, (2, 3, 3, 4)) 30 31 r = R(V(-2, -3), V(3, 4)) 32 self.assertEqual(r.bounds, (-2, -3, 3, 4))
33
34 - def test_ctor_nums(self):
35 r = R(2, 3, 4, 4) 36 self.assertEqual(r.bounds, (2, 3, 4, 4)) 37 38 r = R(-5, -5, 2, 2) 39 self.assertEqual(r.bounds, (-5, -5, 2, 2))
40 41
42 -class RectangleTestCase(TestCase):
43 # called for every test method in here
44 - def setUp(self):
45 #TODO rect is alway normalized, check if we can break it 46 self.r = R(-3, 2, 6, 7)
47
48 - def test_nums(self):
49 # R(-3, 2, 6, 7) 50 self.assertEqual(self.r.left, -3) 51 self.assertEqual(self.r.top, 2) 52 self.assertEqual(self.r.right, 6) 53 self.assertEqual(self.r.bottom, 7) 54 55 self.r.left = 2 56 self.r.top = 1 57 self.r.right = 3 58 self.r.bottom = 4 59 60 self.assertEqual(self.r.left, 2) 61 self.assertEqual(self.r.top, 1) 62 self.assertEqual(self.r.right, 3) 63 self.assertEqual(self.r.bottom, 4)
64
65 - def test_points(self):
66 # R(-3, 2, 6, 7) 67 self.assertEqual(self.r.top_left.xy, (-3, 2)) 68 self.assertEqual(self.r.top_right.xy, (6, 2)) 69 self.assertEqual(self.r.bottom_left.xy, (-3, 7)) 70 self.assertEqual(self.r.bottom_right.xy, (6, 7))
71
72 - def test_boundness_1(self):
73 # R(-3, 2, 6, 7) 74 self.r.top_left.x = 3 75 self.r.top_left.y = -2 76 self.r.bottom_right.x = -6 77 self.r.bottom_right.y = -7 78 79 self.assertEqual(self.r.top_left.xy, (3, -2)) 80 self.assertEqual(self.r.top_right.xy, (-6, -2)) 81 self.assertEqual(self.r.bottom_left.xy, (3, -7)) 82 self.assertEqual(self.r.bottom_right.xy, (-6, -7))
83
84 - def test_boundness_2(self):
85 # R(-3, 2, 6, 7) 86 self.r.top_right.x = -6 87 self.r.top_right.y = -2 88 self.r.bottom_left.x = 3 89 self.r.bottom_left.y = -7 90 91 self.assertEqual(self.r.top_left.xy, (3, -2)) 92 self.assertEqual(self.r.top_right.xy, (-6, -2)) 93 self.assertEqual(self.r.bottom_left.xy, (3, -7)) 94 self.assertEqual(self.r.bottom_right.xy, (-6, -7))
95
96 - def test_diagonal_points1(self):
97 # R(-3, 2, 6, 7) 98 self.r.top_left = V(1, 2) 99 self.r.bottom_right = V(4, 3) 100 101 self.assertEqual(self.r.top_left.xy, (1, 2)) 102 self.assertEqual(self.r.top_right.xy, (4, 2)) 103 self.assertEqual(self.r.bottom_left.xy, (1, 3)) 104 self.assertEqual(self.r.bottom_right.xy, (4, 3))
105
106 - def test_diagonal_points2(self):
107 # R(-3, 2, 6, 7) 108 self.r.top_right = V(4, 2) 109 self.r.bottom_left = V(1, 3) 110 111 self.assertEqual(self.r.top_left.xy, (1, 2)) 112 self.assertEqual(self.r.top_right.xy, (4, 2)) 113 self.assertEqual(self.r.bottom_left.xy, (1, 3)) 114 self.assertEqual(self.r.bottom_right.xy, (4, 3))
115
116 - def test_center(self):
117 # R(-3, 2, 6, 7) 118 self.assertEqual(self.r.center.xy, (1, 4)) 119 120 size = self.r.size.copy() 121 pos = self.r.top_left.copy() 122 123 self.r.center = self.r.center # should have no effect on r 124 self.assertEqual(pos, self.r.top_left) 125 self.assertEqual(size, self.r.size) 126 127 self.r.center = V(0, 0) 128 129 self.assertEqual(self.r.bounds, (-4, -2, 5, 3)) 130 self.assertEqual(size, self.r.size) # size musn't change
131
132 - def test_size(self):
133 # R(-3, 2, 6, 7) 134 self.assertEqual(self.r.size.xy, (9, 5)) 135 136 top_left = self.r.top_left.copy() 137 self.r.size = V(2, 2) 138 139 self.assertEqual(self.r.size.xy, (2, 2)) 140 self.assertEqual(self.r.bottom_right.xy, (-1, 4)) 141 self.assertEqual(top_left, self.r.top_left)
142
143 - def test_boundness_size(self):
144 # R(-3, 2, 6, 7) 145 top_left = self.r.top_left.copy() 146 147 self.r.size.x = 2 148 self.r.size.y = 2 149 150 self.assertEqual(self.r.size.xy, (2, 2)) 151 self.assertEqual(self.r.bottom_right.xy, (-1, 4)) 152 self.assertEqual(top_left, self.r.top_left)
153
154 - def test_bounds(self):
155 # R(-3, 2, 6, 7) 156 self.assertEqual(self.r.bounds, (-3, 2, 6, 7)) 157 158 self.r.bounds = (-5, -5, 2, 2) 159 self.assertEqual(self.r.bounds, (-5, -5, 2, 2)) 160 161 self.r.bounds = (V(-2, -3), V(3, 4)) 162 self.assertEqual(self.r.bounds, (-2, -3, 3, 4))
163
164 - def test_contains(self):
165 # R(-3, 2, 6, 7) 166 self.assertTrue(self.r.contains(V(0,3))) 167 self.assertTrue(self.r.contains(V(-3,2))) 168 self.assertFalse(self.r.contains(V(-4,0))) 169 self.assertFalse(self.r.contains(V(0,8)))
170
171 - def test_overlaps(self):
172 # R(-3, 2, 6, 7) 173 self.assertTrue(self.r.overlaps(R(0, 0, 8, 8))) 174 175 # should be false, even if just touching 176 self.assertFalse(self.r.overlaps(R(-4, 1, -3, 2)))
177
178 - def test_inflate(self):
179 # R(-3, 2, 6, 7) 180 bounds = self.r.bounds 181 182 self.r.inflate(2) 183 self.assertEqual(self.r.bounds, (-5, 0, 8, 9)) 184 185 self.r.inflate(-2) 186 self.assertEqual(self.r.bounds, bounds) 187 188 self.r.inflate(V(2, 1)) 189 self.assertEqual(self.r.bounds, (-5, 1, 8, 8))
190
191 - def test_move_to(self):
192 # R(-3, 2, 6, 7) 193 self.r.move_to(V(5, 8)) 194 self.assertEqual(self.r.bounds, (5, 8, 14, 13))
195
196 - def test_move_by(self):
197 # R(-3, 2, 6, 7) 198 self.r.move_by(V(4, -1)) 199 self.assertEqual(self.r.bounds, (1, 1, 10, 6))
200
201 - def test_independence(self):
202 # R(-3, 2, 6, 7) 203 # modifying one rect shouldn't modify another 204 r2 = R(1, 1, 50, 50) 205 self.r.move_to(V(4, -1)) 206 self.assertEqual(r2.bounds, (1, 1, 50, 50))
207 208
209 -class VerboseRectangleTestCase(RectangleTestCase):
210 # called for every test method in here
211 - def setUp(self):
212 RectangleTestCase.setUp(self) 213 self.r = VerboseRectangle(self.r)
214 215 # TODO test verboseness, and that of vector too (in vector.py, perhaps 216 # consider splitting that file as well btw) 217