2012-03-19 21:33:23 -07:00
|
|
|
import unittest
|
|
|
|
|
2012-03-22 09:09:14 -07:00
|
|
|
from surf.geometry import Vertex
|
2012-03-19 21:33:23 -07:00
|
|
|
|
|
|
|
|
2012-05-07 22:17:46 -07:00
|
|
|
class TestVertex(unittest.TestCase):
|
2012-03-19 21:33:23 -07:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.origin = Vertex(0, 0, 0)
|
|
|
|
self.v1 = Vertex(-1, -1, -1)
|
|
|
|
self.v2 = Vertex(1, 1, 1)
|
|
|
|
|
|
|
|
self.v3 = Vertex(5, 4, 3)
|
|
|
|
self.v4 = Vertex(10, -2, 13)
|
|
|
|
self.v5 = Vertex(-4, 15.3, 100)
|
|
|
|
|
|
|
|
def test_addition(self):
|
|
|
|
added_v = self.v1 + self.v2
|
|
|
|
self.assertEqual(added_v, self.origin)
|
|
|
|
self.assertEqual(self.v3 + self.v4, Vertex(15, 2, 16))
|
|
|
|
|
|
|
|
def test_summation(self):
|
|
|
|
v = sum((self.v1, self.v2, self.v3, self.v4), Vertex())
|
|
|
|
self.assertEqual(v, Vertex(15, 2, 16))
|
|
|
|
v = sum((self.v1, self.v2, self.v3, self.v4, self.v5), Vertex())
|
|
|
|
self.assertEqual(v, Vertex(11, 17.3, 116))
|
|
|
|
|
2012-05-15 16:25:11 -07:00
|
|
|
def test_float_int_division(self):
|
2012-03-19 21:33:23 -07:00
|
|
|
v1 = self.v1 / 2
|
|
|
|
v2 = self.v1 / 2.0
|
|
|
|
self.assertEqual(v1, v2)
|
|
|
|
|
2012-05-15 16:25:11 -07:00
|
|
|
def test_division(self):
|
|
|
|
v1 = Vertex(36, 12, 48)
|
|
|
|
v2 = v1 / 12
|
|
|
|
self.assertEqual(v2, Vertex(3, 1, 4))
|
|
|
|
v1 = Vertex(36.0, 12.0, 48.0)
|
|
|
|
v2 = v1 / 12.0
|
|
|
|
self.assertEqual(v2, Vertex(3.0, 1.0, 4.0))
|
|
|
|
v1 = Vertex(36, 12, 48)
|
|
|
|
v2 = v1 / 12.0
|
|
|
|
self.assertEqual(v2, Vertex(3, 1, 4))
|
|
|
|
v1 = Vertex(36., 12., 48.)
|
|
|
|
v2 = v1 / 12
|
|
|
|
self.assertEqual(v2, Vertex(3.0, 1.0, 4.0))
|
|
|
|
|
2012-03-22 09:09:14 -07:00
|
|
|
def test_inappropriate_division(self):
|
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
self.v1 / self.v2
|
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
1.0 / self.v1
|
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
self.v1 / 'asdf'
|
|
|
|
|
2012-03-20 06:16:15 -07:00
|
|
|
def test_multiply(self):
|
|
|
|
self.assertEqual(type(self.v1 * 2), Vertex)
|
|
|
|
self.assertEqual(type(self.v1 * self.v1), Vertex)
|
|
|
|
self.assertEqual(self.v1 * 2, Vertex(-2, -2, -2))
|
|
|
|
self.assertEqual(self.v1 * self.v1, Vertex(0, 0, 0))
|
|
|
|
self.assertEqual(self.v1 * self.v2, Vertex(0, 0, 0))
|
|
|
|
self.assertEqual(self.v3 * self.v4, Vertex(58, -35, -50))
|
|
|
|
|
|
|
|
def test_cross(self):
|
|
|
|
i = Vertex(1, 0, 0)
|
|
|
|
j = Vertex(0, 1, 0)
|
|
|
|
k = Vertex(0, 0, 1)
|
|
|
|
|
|
|
|
self.assertEqual(i * j, k)
|
|
|
|
self.assertEqual(j * k, i)
|
|
|
|
self.assertEqual(k * i, j)
|
|
|
|
|
|
|
|
self.assertEqual(j * i, -k)
|
|
|
|
self.assertEqual(k * j, -i)
|
|
|
|
self.assertEqual(i * k, -j)
|
|
|
|
|
|
|
|
self.assertEqual(i * i, self.origin)
|
|
|
|
self.assertEqual(j * j, self.origin)
|
|
|
|
self.assertEqual(k * k, self.origin)
|
|
|
|
|
|
|
|
def test_negative(self):
|
|
|
|
self.assertEqual(-self.v1, self.v2)
|
|
|
|
self.assertEqual(-self.v2, self.v1)
|
|
|
|
|
2012-03-19 21:33:23 -07:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main(verbosity=3)
|