]> gitweb.michael.orlitzky.com - dead/census-tools.git/blob - src/Tests/Unit/GeometryTest.py
Updated the Geometry tests to use the new operator overloads.
[dead/census-tools.git] / src / Tests / Unit / GeometryTest.py
1 import unittest
2
3 import Tests.Fixtures
4 from Geometry import *
5
6
7 class TwoVectorTest(unittest.TestCase):
8
9 def testAdd(self):
10 """
11 Add a couple of test vectors and check that they sum
12 correctly.
13 """
14 v1 = TwoVector(0,0)
15 v2 = TwoVector(100, -19)
16 self.assertTrue(v2 == (v1+v2))
17
18 v1 = TwoVector(23, -10)
19 v2 = TwoVector(-23, 10)
20 zero = TwoVector(0, 0)
21 self.assertTrue((v1+v2) == zero)
22
23
24 def testDotProduct(self):
25 """
26 The dot product of a vector v with a vector to which it's
27 perpendicular, v_perp, should always be zero.
28
29 The dot product of any vector with the zero vector should be
30 zero.
31 """
32 v1 = TwoVector(235, -2352)
33 v2 = TwoVector(9382335.024, -235235.1242)
34 self.assertEqual(0, v1.dot_product(v1.perpendicular()))
35 self.assertEqual(0, v2.dot_product(v2.perpendicular()))
36
37 zero = TwoVector(0, 0)
38 self.assertEqual(0, v1.dot_product(zero))
39
40
41
42 class PolygonTest(unittest.TestCase):
43
44 def testEquals(self):
45 """
46 We defined our own __eq__ method; make sure it works.
47 """
48 unit_square1 = Polygon([ (0,0), (1,0), (1,1), (0,1) ])
49 unit_square2 = Polygon([ (0,0), (1,0), (1,1), (0,1) ])
50 self.assertTrue(unit_square1 == unit_square2)
51
52 hexagon = Polygon([ (1,0),
53 (0.5, sqrt(3)/2.0),
54 (-0.5, sqrt(3)/2.0),
55 (-1, 0),
56 (-0.5, -sqrt(3)/2.0),
57 (0.5, -sqrt(3)/2.0) ])
58
59 self.assertFalse(unit_square1 == hexagon)
60
61
62 def testDrag(self):
63 """
64 Drag the unit square 5 units to the right.
65 """
66 unit_square = Polygon([ (0,0), (1,0), (1,1), (0,1) ])
67 v = TwoVector(5,0)
68 result = unit_square.drag(v)
69 expected_result = Polygon([ (0,0), (6,0), (6,1), (0,1) ])
70 self.assertTrue(result == expected_result)
71
72
73
74 def testDragVertices(self):
75 """
76 A diamond shape has nice drag vertices which we can calculate
77 by hand.
78 """
79 diamond = Polygon([ (0,1), (-1,0), (0,-1), (1,0) ])
80
81 # First, we drag it to the right along the x-axis.
82 drag_vector = TwoVector(5,0)
83 dvs = set(diamond.drag_vertices(drag_vector))
84 expected_dvs = set([(0,1), (0,-1)])
85 self.assertTrue(dvs == expected_dvs)
86
87 # Now, drag it up.
88 drag_vector = TwoVector(0,5)
89 dvs = set(diamond.drag_vertices(drag_vector))
90 expected_dvs = set([(1,0), (-1,0)])
91 self.assertTrue(dvs == expected_dvs)
92
93
94 def suite():
95 suite = unittest.TestSuite()
96 suite.addTest(unittest.makeSuite(TwoVectorTest))
97 suite.addTest(unittest.makeSuite(PolygonTest))
98 return suite