1 import math
2 import unittest
4 from ..linexprs import Symbol
5 from ..coordinates import *
8 class TestPoint(unittest.TestCase):
10 def setUp(self):
11 self.x = Symbol('x')
12 self.y = Symbol('y')
13 self.z = Symbol('z')
14 self.pt1 = Point({self.x: 10, self.y: 5, self.z: 1})
15 self.pt2 = Point({self.x: 15, self.y: 40, self.z: 60})
16 self.vec1 = Vector({self.x: 20, self.y: 30, self.z: 40})
19 self.assertEqual(self.pt1 + self.vec1, Point({self.x: 30, self.y: 35, self.z: 41}))
20 with self.assertRaises(TypeError):
21 self.pt1 + self.pt2
23 def test_eq(self):
24 self.assertEqual(self.pt1, self.pt1)
25 self.assertNotEqual(self.pt1, self.pt2)
26 self.assertNotEqual(self.pt1, self.vec1)
28 def test_sub(self):
29 self.assertEqual(self.pt1 - self.pt2, Vector({self.x: -5, self.y: -35, self.z: -59}))
30 self.assertEqual(self.pt1 - self.vec1, Point({self.x: -10, self.y: -25, self.z: -39}))
33 class TestVector(unittest.TestCase):
35 def setUp(self):
36 self.x = Symbol('x')
37 self.y = Symbol('y')
38 self.z = Symbol('z')
39 self.pt1 = Point({self.x: 10, self.y: 5, self.z: 1})
40 self.pt2 = Point({self.x: 15, self.y: 40, self.z: 60})
41 self.vec1 = Vector({self.x: 20, self.y: 30, self.z: 40})
42 self.vec2 = Vector({self.x: 45, self.y: 70, self.z: 80})
45 self.assertEqual(self.vec1 + self.pt1, Point({self.x: 30, self.y: 35, self.z: 41}))
46 self.assertEqual(self.vec1 + self.vec2, Vector({self.x: 65, self.y: 100, self.z: 120}))
48 def test_angle(self):
49 self.assertEqual(math.degrees(self.vec1.angle(self.vec1)), 0)
50 self.assertAlmostEqual(math.degrees(self.vec1.angle(self.vec2)), 4.15129, places=5)
51 self.assertAlmostEqual(math.degrees(self.vec2.angle(self.vec1)), 4.15129, places=5)
53 def test_cross(self):
54 self.assertEqual(self.vec1.cross(self.vec2), Vector({self.x: -400, self.y: 200, self.z: 50}))
56 def test_div(self):
57 self.assertEqual(self.vec1 / 10, Vector({self.x: 2, self.y: 3, self.z: 4}))
59 def test_dot(self):
60 self.assertEqual(self.vec1.dot(self.vec2), 6200)
62 def test_eq(self):
63 self.assertEqual(self.vec1, self.vec1)
64 self.assertNotEqual(self.vec1, self.vec2)
66 def test_mul(self):
67 self.assertEqual(75 * self.vec1, Vector({self.x: 1500, self.y: 2250, self.z: 3000}))
68 self.assertEqual(self.vec1 * 75, Vector({self.x: 1500, self.y: 2250, self.z: 3000}))
70 def test_neg(self):
71 self.assertEqual(-self.vec1, Vector({self.x: -20, self.y: -30, self.z: -40}))
73 def test_norm(self):
74 self.assertAlmostEqual(self.vec1.norm(), 53.85165, places=5)
76 def test_norm2(self):
77 self.assertEqual(self.vec1.norm2(), 2900)
79 def test_asunit(self):
80 unit = self.vec1.asunit()
81 self.assertAlmostEqual(unit[self.x], 0.37139, 5)
82 self.assertAlmostEqual(unit[self.y], 0.55709, 5)
83 self.assertAlmostEqual(unit[self.z], 0.74278, 5)
85 def test_sub(self):
86 self.assertEqual(self.vec1 - self.pt1, Point({self.x: 10, self.y: 25, self.z: 39}))
87 self.assertEqual(self.vec1 - self.vec2, Vector({self.x: -25, self.y: -40, self.z: -40}))