]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/floatcanvas/Utilities/BBoxTest.py
Forward port recent changes on the 2.8 branch to HEAD
[wxWidgets.git] / wxPython / wx / lib / floatcanvas / Utilities / BBoxTest.py
1
2 """
3 Test code for the BBox Object
4
5 """
6
7 import unittest
8
9 from BBox import *
10
11 class testCreator(unittest.TestCase):
12 def testCreates(self):
13 B = BBox(((0,0),(5,5)))
14 self.failUnless(isinstance(B, BBox))
15
16 def testType(self):
17 B = N.array(((0,0),(5,5)))
18 self.failIf(isinstance(B, BBox))
19
20 def testDataType(self):
21 B = BBox(((0,0),(5,5)))
22 self.failUnless(B.dtype == N.float)
23
24 def testShape(self):
25 B = BBox((0,0,5,5))
26 self.failUnless(B.shape == (2,2))
27
28 def testShape2(self):
29 self.failUnlessRaises(ValueError, BBox, (0,0,5) )
30
31 def testShape3(self):
32 self.failUnlessRaises(ValueError, BBox, (0,0,5,6,7) )
33
34 def testArrayConstruction(self):
35 A = N.array(((4,5),(10,12)), N.float_)
36 B = BBox(A)
37 self.failUnless(isinstance(B, BBox))
38
39 def testMinMax(self):
40 self.failUnlessRaises(ValueError, BBox, (0,0,-1,6) )
41
42 def testMinMax2(self):
43 self.failUnlessRaises(ValueError, BBox, (0,0,1,-6) )
44
45 def testMinMax(self):
46 # OK to have a zero-sized BB
47 B = BBox(((0,0),(0,5)))
48 self.failUnless(isinstance(B, BBox))
49
50 def testMinMax2(self):
51 # OK to have a zero-sized BB
52 B = BBox(((10.0,-34),(10.0,-34.0)))
53 self.failUnless(isinstance(B, BBox))
54
55 def testMinMax3(self):
56 # OK to have a tiny BB
57 B = BBox(((0,0),(1e-20,5)))
58 self.failUnless(isinstance(B, BBox))
59
60 def testMinMax4(self):
61 # Should catch tiny difference
62 self.failUnlessRaises(ValueError, BBox, ((0,0), (-1e-20,5)) )
63
64 class testAsBBox(unittest.TestCase):
65
66 def testPassThrough(self):
67 B = BBox(((0,0),(5,5)))
68 C = asBBox(B)
69 self.failUnless(B is C)
70
71 def testPassThrough2(self):
72 B = (((0,0),(5,5)))
73 C = asBBox(B)
74 self.failIf(B is C)
75
76 def testPassArray(self):
77 # Different data type
78 A = N.array( (((0,0),(5,5))) )
79 C = asBBox(A)
80 self.failIf(A is C)
81
82 def testPassArray2(self):
83 # same data type -- should be a view
84 A = N.array( (((0,0),(5,5))), N.float_ )
85 C = asBBox(A)
86 A[0,0] = -10
87 self.failUnless(C[0,0] == A[0,0])
88
89 class testIntersect(unittest.TestCase):
90
91 def testSame(self):
92 B = BBox(((-23.5, 456),(56, 532.0)))
93 C = BBox(((-23.5, 456),(56, 532.0)))
94 self.failUnless(B.Overlaps(C) )
95
96 def testUpperLeft(self):
97 B = BBox( ( (5, 10),(15, 25) ) )
98 C = BBox( ( (0, 12),(10, 32.0) ) )
99 self.failUnless(B.Overlaps(C) )
100
101 def testUpperRight(self):
102 B = BBox( ( (5, 10),(15, 25) ) )
103 C = BBox( ( (12, 12),(25, 32.0) ) )
104 self.failUnless(B.Overlaps(C) )
105
106 def testLowerRight(self):
107 B = BBox( ( (5, 10),(15, 25) ) )
108 C = BBox( ( (12, 5),(25, 15) ) )
109 self.failUnless(B.Overlaps(C) )
110
111 def testLowerLeft(self):
112 B = BBox( ( (5, 10),(15, 25) ) )
113 C = BBox( ( (-10, 5),(8.5, 15) ) )
114 self.failUnless(B.Overlaps(C) )
115
116 def testBelow(self):
117 B = BBox( ( (5, 10),(15, 25) ) )
118 C = BBox( ( (-10, 5),(8.5, 9.2) ) )
119 self.failIf(B.Overlaps(C) )
120
121 def testAbove(self):
122 B = BBox( ( (5, 10),(15, 25) ) )
123 C = BBox( ( (-10, 25.001),(8.5, 32) ) )
124 self.failIf(B.Overlaps(C) )
125
126 def testLeft(self):
127 B = BBox( ( (5, 10),(15, 25) ) )
128 C = BBox( ( (4, 8),(4.95, 32) ) )
129 self.failIf(B.Overlaps(C) )
130
131 def testRight(self):
132 B = BBox( ( (5, 10),(15, 25) ) )
133 C = BBox( ( (17.1, 8),(17.95, 32) ) )
134 self.failIf(B.Overlaps(C) )
135
136 def testInside(self):
137 B = BBox( ( (-15, -25),(-5, -10) ) )
138 C = BBox( ( (-12, -22), (-6, -8) ) )
139 self.failUnless(B.Overlaps(C) )
140
141 def testOutside(self):
142 B = BBox( ( (-15, -25),(-5, -10) ) )
143 C = BBox( ( (-17, -26), (3, 0) ) )
144 self.failUnless(B.Overlaps(C) )
145
146 def testTouch(self):
147 B = BBox( ( (5, 10),(15, 25) ) )
148 C = BBox( ( (15, 8),(17.95, 32) ) )
149 self.failUnless(B.Overlaps(C) )
150
151 def testCorner(self):
152 B = BBox( ( (5, 10),(15, 25) ) )
153 C = BBox( ( (15, 25),(17.95, 32) ) )
154 self.failUnless(B.Overlaps(C) )
155
156 def testZeroSize(self):
157 B = BBox( ( (5, 10),(15, 25) ) )
158 C = BBox( ( (15, 25),(15, 25) ) )
159 self.failUnless(B.Overlaps(C) )
160
161 def testZeroSize2(self):
162 B = BBox( ( (5, 10),(5, 10) ) )
163 C = BBox( ( (15, 25),(15, 25) ) )
164 self.failIf(B.Overlaps(C) )
165
166 def testZeroSize3(self):
167 B = BBox( ( (5, 10),(5, 10) ) )
168 C = BBox( ( (0, 8),(10, 12) ) )
169 self.failUnless(B.Overlaps(C) )
170
171 def testZeroSize4(self):
172 B = BBox( ( (5, 1),(10, 25) ) )
173 C = BBox( ( (8, 8),(8, 8) ) )
174 self.failUnless(B.Overlaps(C) )
175
176
177
178 class testEquality(unittest.TestCase):
179 def testSame(self):
180 B = BBox( ( (1.0, 2.0), (5.0, 10.0) ) )
181 C = BBox( ( (1.0, 2.0), (5.0, 10.0) ) )
182 self.failUnless(B == C)
183
184 def testIdentical(self):
185 B = BBox( ( (1.0, 2.0), (5.0, 10.0) ) )
186 self.failUnless(B == B)
187
188 def testNotSame(self):
189 B = BBox( ( (1.0, 2.0), (5.0, 10.0) ) )
190 C = BBox( ( (1.0, 2.0), (5.0, 10.1) ) )
191 self.failIf(B == C)
192
193 def testWithArray(self):
194 B = BBox( ( (1.0, 2.0), (5.0, 10.0) ) )
195 C = N.array( ( (1.0, 2.0), (5.0, 10.0) ) )
196 self.failUnless(B == C)
197
198 def testWithArray2(self):
199 B = BBox( ( (1.0, 2.0), (5.0, 10.0) ) )
200 C = N.array( ( (1.0, 2.0), (5.0, 10.0) ) )
201 self.failUnless(C == B)
202
203 def testWithArray2(self):
204 B = BBox( ( (1.0, 2.0), (5.0, 10.0) ) )
205 C = N.array( ( (1.01, 2.0), (5.0, 10.0) ) )
206 self.failIf(C == B)
207
208 class testInside(unittest.TestCase):
209 def testSame(self):
210 B = BBox( ( (1.0, 2.0), (5.0, 10.0) ) )
211 C = BBox( ( (1.0, 2.0), (5.0, 10.0) ) )
212 self.failUnless(B.Inside(C))
213
214 def testPoint(self):
215 B = BBox( ( (1.0, 2.0), (5.0, 10.0) ) )
216 C = BBox( ( (3.0, 4.0), (3.0, 4.0) ) )
217 self.failUnless(B.Inside(C))
218
219 def testPointOutside(self):
220 B = BBox( ( (1.0, 2.0), (5.0, 10.0) ) )
221 C = BBox( ( (-3.0, 4.0), (0.10, 4.0) ) )
222 self.failIf(B.Inside(C))
223
224 def testUpperLeft(self):
225 B = BBox( ( (5, 10),(15, 25) ) )
226 C = BBox( ( (0, 12),(10, 32.0) ) )
227 self.failIf(B.Inside(C) )
228
229 def testUpperRight(self):
230 B = BBox( ( (5, 10),(15, 25) ) )
231 C = BBox( ( (12, 12),(25, 32.0) ) )
232 self.failIf(B.Inside(C) )
233
234 def testLowerRight(self):
235 B = BBox( ( (5, 10),(15, 25) ) )
236 C = BBox( ( (12, 5),(25, 15) ) )
237 self.failIf(B.Inside(C) )
238
239 def testLowerLeft(self):
240 B = BBox( ( (5, 10),(15, 25) ) )
241 C = BBox( ( (-10, 5),(8.5, 15) ) )
242 self.failIf(B.Inside(C) )
243
244 def testBelow(self):
245 B = BBox( ( (5, 10),(15, 25) ) )
246 C = BBox( ( (-10, 5),(8.5, 9.2) ) )
247 self.failIf(B.Inside(C) )
248
249 def testAbove(self):
250 B = BBox( ( (5, 10),(15, 25) ) )
251 C = BBox( ( (-10, 25.001),(8.5, 32) ) )
252 self.failIf(B.Inside(C) )
253
254 def testLeft(self):
255 B = BBox( ( (5, 10),(15, 25) ) )
256 C = BBox( ( (4, 8),(4.95, 32) ) )
257 self.failIf(B.Inside(C) )
258
259 def testRight(self):
260 B = BBox( ( (5, 10),(15, 25) ) )
261 C = BBox( ( (17.1, 8),(17.95, 32) ) )
262 self.failIf(B.Inside(C) )
263
264 class testFromPoints(unittest.TestCase):
265
266 def testCreate(self):
267 Pts = N.array( ((5,2),
268 (3,4),
269 (1,6),
270 ), N.float_ )
271 B = fromPoints(Pts)
272 #B = BBox( ( (1.0, 2.0), (5.0, 10.0) ) )
273 self.failUnless(B[0,0] == 1.0 and
274 B[0,1] == 2.0 and
275 B[1,0] == 5.0 and
276 B[1,1] == 6.0
277 )
278 def testCreateInts(self):
279 Pts = N.array( ((5,2),
280 (3,4),
281 (1,6),
282 ) )
283 B = fromPoints(Pts)
284 self.failUnless(B[0,0] == 1.0 and
285 B[0,1] == 2.0 and
286 B[1,0] == 5.0 and
287 B[1,1] == 6.0
288 )
289
290 def testSinglePoint(self):
291 Pts = N.array( (5,2), N.float_ )
292 B = fromPoints(Pts)
293 self.failUnless(B[0,0] == 5.0 and
294 B[0,1] == 2.0 and
295 B[1,0] == 5.0 and
296 B[1,1] == 2.0
297 )
298
299 def testListTuples(self):
300 Pts = [ (3, 6.5),
301 (13, 43.2),
302 (-4.32, -4),
303 (65, -23),
304 (-0.0001, 23.432),
305 ]
306 B = fromPoints(Pts)
307 self.failUnless(B[0,0] == -4.32 and
308 B[0,1] == -23.0 and
309 B[1,0] == 65.0 and
310 B[1,1] == 43.2
311 )
312 class testMerge(unittest.TestCase):
313 A = BBox( ((-23.5, 456), (56, 532.0)) )
314 B = BBox( ((-20.3, 460), (54, 465 )) )# B should be completely inside A
315 C = BBox( ((-23.5, 456), (58, 540.0)) )# up and to the right or A
316 D = BBox( ((-26.5, 12), (56, 532.0)) )
317
318 def testInside(self):
319 C = self.A.copy()
320 C.Merge(self.B)
321 self.failUnless(C == self.A)
322
323 def testFullOutside(self):
324 C = self.B.copy()
325 C.Merge(self.A)
326 self.failUnless(C == self.A)
327
328 def testUpRight(self):
329 A = self.A.copy()
330 A.Merge(self.C)
331 self.failUnless(A[0] == self.A[0] and A[1] == self.C[1])
332
333 def testDownLeft(self):
334 A = self.A.copy()
335 A.Merge(self.D)
336 self.failUnless(A[0] == self.D[0] and A[1] == self.A[1])
337
338 class testBBarray(unittest.TestCase):
339 BBarray = N.array( ( ((-23.5, 456), (56, 532.0)),
340 ((-20.3, 460), (54, 465 )),
341 ((-23.5, 456), (58, 540.0)),
342 ((-26.5, 12), (56, 532.0)),
343 ),
344 dtype=N.float)
345 print BBarray
346 BB = asBBox( ((-26.5, 12.), ( 58. , 540.)) )
347
348 def testJoin(self):
349 BB = fromBBArray(self.BBarray)
350 self.failUnless(BB == self.BB, "Wrong BB was created. It was:\n%s \nit should have been:\n%s"%(BB, self.BB))
351
352
353 if __name__ == "__main__":
354 unittest.main()