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