]>
gitweb.michael.orlitzky.com - dead/census-tools.git/blob - Unit/GeometryTest.py
6 class TwoVectorTest(unittest
.TestCase
):
10 Add a couple of test vectors and check that they sum
14 v2
= TwoVector(100, -19)
15 self
.assertTrue(v2
== (v1
+v2
))
17 v1
= TwoVector(23, -10)
18 v2
= TwoVector(-23, 10)
19 zero
= TwoVector(0, 0)
20 self
.assertTrue((v1
+v2
) == zero
)
23 def testDotProduct(self
):
25 The dot product of a vector v with a vector to which it's
26 perpendicular, v_perp, should always be zero.
28 The dot product of any vector with the zero vector should be
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()))
36 zero
= TwoVector(0, 0)
37 self
.assertEqual(0, v1
.dot_product(zero
))
41 class PolygonTest(unittest
.TestCase
):
45 We defined our own __eq__ method; make sure it works.
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
)
51 hexagon
= Polygon([ (1,0),
56 (0.5, -sqrt(3)/2.0) ])
58 self
.assertFalse(unit_square1
== hexagon
)
63 Drag the unit square 5 units to the right.
65 unit_square
= Polygon([ (0,0), (1,0), (1,1), (0,1) ])
67 result
= unit_square
.drag(v
)
68 expected_result
= Polygon([ (0,0), (6,0), (6,1), (0,1) ])
69 self
.assertTrue(result
== expected_result
)
73 def testDragVertices(self
):
75 A diamond shape has nice drag vertices which we can calculate
78 diamond
= Polygon([ (0,1), (-1,0), (0,-1), (1,0) ])
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
)
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
)
94 suite
= unittest
.TestSuite()
95 suite
.addTest(unittest
.makeSuite(TwoVectorTest
))
96 suite
.addTest(unittest
.makeSuite(PolygonTest
))