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

Source Code for Module pytilities.geometry.test.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 ..vector import Vector as V 
 25  from ..rectangle import Rectangle as R 
 26  from ..verboserectangle import VerboseRectangle 
 27   
28 -class RectangleCtorTestCase(TestCase):
29 - def test_ctor_points(self):
30 r = R(V(2, 3), V(3, 4)) 31 self.assertEqual(r.bounds, (2, 3, 3, 4)) 32 33 r = R(V(-2, -3), V(3, 4)) 34 self.assertEqual(r.bounds, (-2, -3, 3, 4))
35
36 - def test_ctor_nums(self):
37 r = R(2, 3, 4, 4) 38 self.assertEqual(r.bounds, (2, 3, 4, 4)) 39 40 r = R(-5, -5, 2, 2) 41 self.assertEqual(r.bounds, (-5, -5, 2, 2))
42 43
44 -class RectangleTestCase(TestCase):
45 # called for every test method in here
46 - def setUp(self):
47 #TODO rect is alway normalized, check if we can break it 48 self.r = R(-3, 2, 6, 7)
49
50 - def test_nums(self):
51 # R(-3, 2, 6, 7) 52 self.assertEqual(self.r.left, -3) 53 self.assertEqual(self.r.top, 2) 54 self.assertEqual(self.r.right, 6) 55 self.assertEqual(self.r.bottom, 7) 56 57 self.r.left = 2 58 self.r.top = 1 59 self.r.right = 3 60 self.r.bottom = 4 61 62 self.assertEqual(self.r.left, 2) 63 self.assertEqual(self.r.top, 1) 64 self.assertEqual(self.r.right, 3) 65 self.assertEqual(self.r.bottom, 4)
66
67 - def test_points(self):
68 # R(-3, 2, 6, 7) 69 self.assertEqual(self.r.top_left.xy, (-3, 2)) 70 self.assertEqual(self.r.top_right.xy, (6, 2)) 71 self.assertEqual(self.r.bottom_left.xy, (-3, 7)) 72 self.assertEqual(self.r.bottom_right.xy, (6, 7))
73
74 - def test_boundness_1(self):
75 # R(-3, 2, 6, 7) 76 self.r.top_left.x = 3 77 self.r.top_left.y = -2 78 self.r.bottom_right.x = -6 79 self.r.bottom_right.y = -7 80 81 self.assertEqual(self.r.top_left.xy, (3, -2)) 82 self.assertEqual(self.r.top_right.xy, (-6, -2)) 83 self.assertEqual(self.r.bottom_left.xy, (3, -7)) 84 self.assertEqual(self.r.bottom_right.xy, (-6, -7))
85
86 - def test_boundness_2(self):
87 # R(-3, 2, 6, 7) 88 self.r.top_right.x = -6 89 self.r.top_right.y = -2 90 self.r.bottom_left.x = 3 91 self.r.bottom_left.y = -7 92 93 self.assertEqual(self.r.top_left.xy, (3, -2)) 94 self.assertEqual(self.r.top_right.xy, (-6, -2)) 95 self.assertEqual(self.r.bottom_left.xy, (3, -7)) 96 self.assertEqual(self.r.bottom_right.xy, (-6, -7))
97
98 - def test_diagonal_points1(self):
99 # R(-3, 2, 6, 7) 100 self.r.top_left = V(1, 2) 101 self.r.bottom_right = V(4, 3) 102 103 self.assertEqual(self.r.top_left.xy, (1, 2)) 104 self.assertEqual(self.r.top_right.xy, (4, 2)) 105 self.assertEqual(self.r.bottom_left.xy, (1, 3)) 106 self.assertEqual(self.r.bottom_right.xy, (4, 3))
107
108 - def test_diagonal_points2(self):
109 # R(-3, 2, 6, 7) 110 self.r.top_right = V(4, 2) 111 self.r.bottom_left = V(1, 3) 112 113 self.assertEqual(self.r.top_left.xy, (1, 2)) 114 self.assertEqual(self.r.top_right.xy, (4, 2)) 115 self.assertEqual(self.r.bottom_left.xy, (1, 3)) 116 self.assertEqual(self.r.bottom_right.xy, (4, 3))
117
118 - def test_center(self):
119 # R(-3, 2, 6, 7) 120 self.assertEqual(self.r.center.xy, (1, 4)) 121 122 size = self.r.size.copy() 123 pos = self.r.top_left.copy() 124 125 self.r.center = self.r.center # should have no effect on r 126 self.assertEqual(pos, self.r.top_left) 127 self.assertEqual(size, self.r.size) 128 129 self.r.center = V(0, 0) 130 131 self.assertEqual(self.r.bounds, (-4, -2, 5, 3)) 132 self.assertEqual(size, self.r.size) # size musn't change
133
134 - def test_size(self):
135 # R(-3, 2, 6, 7) 136 self.assertEqual(self.r.size.xy, (9, 5)) 137 138 top_left = self.r.top_left.copy() 139 self.r.size = V(2, 2) 140 141 self.assertEqual(self.r.size.xy, (2, 2)) 142 self.assertEqual(self.r.bottom_right.xy, (-1, 4)) 143 self.assertEqual(top_left, self.r.top_left)
144
145 - def test_boundness_size(self):
146 # R(-3, 2, 6, 7) 147 top_left = self.r.top_left.copy() 148 149 self.r.size.x = 2 150 self.r.size.y = 2 151 152 self.assertEqual(self.r.size.xy, (2, 2)) 153 self.assertEqual(self.r.bottom_right.xy, (-1, 4)) 154 self.assertEqual(top_left, self.r.top_left)
155
156 - def test_bounds(self):
157 # R(-3, 2, 6, 7) 158 self.assertEqual(self.r.bounds, (-3, 2, 6, 7)) 159 160 self.r.bounds = (-5, -5, 2, 2) 161 self.assertEqual(self.r.bounds, (-5, -5, 2, 2)) 162 163 self.r.bounds = (V(-2, -3), V(3, 4)) 164 self.assertEqual(self.r.bounds, (-2, -3, 3, 4))
165
166 - def test_contains(self):
167 # R(-3, 2, 6, 7) 168 self.assertTrue(self.r.contains(V(0,3))) 169 self.assertTrue(self.r.contains(V(-3,2))) 170 self.assertFalse(self.r.contains(V(-4,0))) 171 self.assertFalse(self.r.contains(V(0,8)))
172
173 - def test_overlaps(self):
174 # R(-3, 2, 6, 7) 175 self.assertTrue(self.r.overlaps(R(0, 0, 8, 8))) 176 177 # should be false, even if just touching 178 self.assertFalse(self.r.overlaps(R(-4, 1, -3, 2)))
179
180 - def test_inflate(self):
181 # R(-3, 2, 6, 7) 182 bounds = self.r.bounds 183 184 self.r.inflate(2) 185 self.assertEqual(self.r.bounds, (-5, 0, 8, 9)) 186 187 self.r.inflate(-2) 188 self.assertEqual(self.r.bounds, bounds) 189 190 self.r.inflate(V(2, 1)) 191 self.assertEqual(self.r.bounds, (-5, 1, 8, 8))
192
193 - def test_move_to(self):
194 # R(-3, 2, 6, 7) 195 self.r.move_to(V(5, 8)) 196 self.assertEqual(self.r.bounds, (5, 8, 14, 13))
197
198 - def test_move_by(self):
199 # R(-3, 2, 6, 7) 200 self.r.move_by(V(4, -1)) 201 self.assertEqual(self.r.bounds, (1, 1, 10, 6))
202
203 - def test_independence(self):
204 # R(-3, 2, 6, 7) 205 # modifying one rect shouldn't modify another 206 r2 = R(1, 1, 50, 50) 207 self.r.move_to(V(4, -1)) 208 self.assertEqual(r2.bounds, (1, 1, 50, 50))
209 210
211 -class VerboseRectangleTestCase(RectangleTestCase):
212 # called for every test method in here
213 - def setUp(self):
214 RectangleTestCase.setUp(self) 215 self.r = VerboseRectangle(self.r)
216 217 # TODO test verboseness, and that of vector too (in vector.py, perhaps 218 # consider splitting that file as well btw) 219 220
221 -def run():
222 unittest.main(__name__)
223