surfaces/surf/test/vertex.py
2012-05-15 17:25:11 -06:00

87 lines
2.6 KiB
Python

import unittest
from surf.geometry import Vertex
class TestVertex(unittest.TestCase):
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))
def test_float_int_division(self):
v1 = self.v1 / 2
v2 = self.v1 / 2.0
self.assertEqual(v1, v2)
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))
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'
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)
if __name__ == '__main__':
unittest.main(verbosity=3)