import unittest from Geometry import * class TwoVectorTest(unittest.TestCase): def testAdd(self): """ Add a couple of test vectors and check that they sum correctly. """ v1 = TwoVector(0,0) v2 = TwoVector(100, -19) self.assertTrue(v2 == (v1+v2)) v1 = TwoVector(23, -10) v2 = TwoVector(-23, 10) zero = TwoVector(0, 0) self.assertTrue((v1+v2) == zero) def testDotProduct(self): """ The dot product of a vector v with a vector to which it's perpendicular, v_perp, should always be zero. The dot product of any vector with the zero vector should be zero. """ v1 = TwoVector(235, -2352) v2 = TwoVector(9382335.024, -235235.1242) self.assertEqual(0, v1.dot_product(v1.perpendicular())) self.assertEqual(0, v2.dot_product(v2.perpendicular())) zero = TwoVector(0, 0) self.assertEqual(0, v1.dot_product(zero)) class PolygonTest(unittest.TestCase): def testEquals(self): """ We defined our own __eq__ method; make sure it works. """ unit_square1 = Polygon([ (0,0), (1,0), (1,1), (0,1) ]) unit_square2 = Polygon([ (0,0), (1,0), (1,1), (0,1) ]) self.assertTrue(unit_square1 == unit_square2) hexagon = Polygon([ (1,0), (0.5, sqrt(3)/2.0), (-0.5, sqrt(3)/2.0), (-1, 0), (-0.5, -sqrt(3)/2.0), (0.5, -sqrt(3)/2.0) ]) self.assertFalse(unit_square1 == hexagon) def testDrag(self): """ Drag the unit square 5 units to the right. """ unit_square = Polygon([ (0,0), (1,0), (1,1), (0,1) ]) v = TwoVector(5,0) result = unit_square.drag(v) expected_result = Polygon([ (0,0), (6,0), (6,1), (0,1) ]) self.assertTrue(result == expected_result) def testDragVertices(self): """ A diamond shape has nice drag vertices which we can calculate by hand. """ diamond = Polygon([ (0,1), (-1,0), (0,-1), (1,0) ]) # First, we drag it to the right along the x-axis. drag_vector = TwoVector(5,0) dvs = set(diamond.drag_vertices(drag_vector)) expected_dvs = set([(0,1), (0,-1)]) self.assertTrue(dvs == expected_dvs) # Now, drag it up. drag_vector = TwoVector(0,5) dvs = set(diamond.drag_vertices(drag_vector)) expected_dvs = set([(1,0), (-1,0)]) self.assertTrue(dvs == expected_dvs) def suite(): suite = unittest.TestSuite() suite.addTest(unittest.makeSuite(TwoVectorTest)) suite.addTest(unittest.makeSuite(PolygonTest)) return suite