New module coordinates.py
[linpy.git] / pypol / tests / test_coordinates.py
1 import math
2 import unittest
3
4 from ..linexprs import Symbol
5 from ..coordinates import *
6
7
8 class TestPoint(unittest.TestCase):
9
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})
17
18 def test_add(self):
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
22
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)
27
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}))
31
32
33 class TestVector(unittest.TestCase):
34
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})
43
44 def test_add(self):
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}))
47
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)
52
53 def test_cross(self):
54 self.assertEqual(self.vec1.cross(self.vec2), Vector({self.x: -400, self.y: 200, self.z: 50}))
55
56 def test_div(self):
57 self.assertEqual(self.vec1 / 10, Vector({self.x: 2, self.y: 3, self.z: 4}))
58
59 def test_dot(self):
60 self.assertEqual(self.vec1.dot(self.vec2), 6200)
61
62 def test_eq(self):
63 self.assertEqual(self.vec1, self.vec1)
64 self.assertNotEqual(self.vec1, self.vec2)
65
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}))
69
70 def test_neg(self):
71 self.assertEqual(-self.vec1, Vector({self.x: -20, self.y: -30, self.z: -40}))
72
73 def test_norm(self):
74 self.assertAlmostEqual(self.vec1.norm(), 53.85165, places=5)
75
76 def test_norm2(self):
77 self.assertEqual(self.vec1.norm2(), 2900)
78
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)
84
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}))