Commit 539d177f authored by Fabrice Salvaire's avatar Fabrice Salvaire

Geometry Engine: vector unit test cleanup

parent eceaab3e
......@@ -32,108 +32,106 @@ class TestVector2D(unittest.TestCase):
##############################################
def test_ctor(self):
v1 = Vector2D(10, 20)
v1 = Vector2D((10, 20))
v1 = Vector2D([10, 20])
v1 = Vector2D(10, 20)
self.assertEqual(v1.x, 10)
self.assertEqual(v1.y, 20)
self.assertEqual(v1, v1.clone())
def _check_vector(self, vector, x ,y):
self.assertEqual(vector.x, x)
self.assertEqual(vector.y, y)
##############################################
def test_math_operations(self):
v1 = Vector2D(10, 20)
v2 = Vector2D(20, 30)
self.assertEqual(v1 + v2, Vector2D(30, 50))
v1 = Vector2D(10, 20)
v2 = Vector2D(20, 30)
self.assertEqual(v1 - v2, Vector2D(-10, -10))
v1 = Vector2D(10, 20)
v1 += Vector2D(20, 30)
self.assertEqual(v1, Vector2D(30, 50))
def test_ctor(self):
v1 = Vector2D(10, 20)
v1 -= Vector2D(20, 30)
self.assertEqual(v1, Vector2D(-10, -10))
x, y = 10, 20
v1 = Vector2D(10, 20)
self.assertEqual(v1 * 10, Vector2D(100, 200))
v1 = Vector2D(x, y)
self._check_vector(v1, x, y)
v1 = Vector2D((x, y))
self._check_vector(v1, x, y)
v1 = Vector2D([x, y])
self._check_vector(v1, x, y)
v1 = Vector2D(10, 20)
v1 *= 10
self.assertEqual(v1, Vector2D(100, 200))
self.assertEqual(v1, v1.clone())
#?# v1 = Vector2D(10, 20)
#?# v1 /= 10
#?# self.assertEqual(v1, Vector2D(1, 2))
for angle in (20, 60, 100, 180):
v1 = Vector2D.from_angle(angle)
self.assertAlmostEqual(v1.orientation, angle)
v1 = Vector2D.from_angle(-angle)
self.assertAlmostEqual(v1.orientation, -angle)
##############################################
def test_properties(self):
v1 = Vector2D(10, 20)
self.assertEqual(v1.magnitude_square, 10**2 + 20**2)
x, y = 10, 20
v1 = Vector2D(10, 20)
self.assertEqual(v1.magnitude, sqrt(10**2 + 20**2))
v1 = Vector2D(x, y)
magnitude_square = x**2 + y**2
self.assertEqual(v1.magnitude_square, magnitude_square)
self.assertEqual(v1.magnitude, sqrt(magnitude_square))
v1 = Vector2D(10, 0)
v1 = Vector2D(x, 0)
self.assertEqual(v1.orientation, 0)
v1 = Vector2D(-10, 0)
v1 = Vector2D(-x, 0)
self.assertEqual(v1.orientation, 180)
v1 = Vector2D(0, 10)
v1 = Vector2D(0, x)
self.assertEqual(v1.orientation, 90)
v1 = Vector2D(0, -10)
v1 = Vector2D(0, -x)
self.assertEqual(v1.orientation, -90)
v1 = Vector2D(10, 10)
v1 = Vector2D(x, x)
self.assertEqual(v1.orientation, 45)
v1 = Vector2D(10, -10)
v1 = Vector2D(x, -x)
self.assertEqual(v1.orientation, -45)
v1 = Vector2D(-10, 10)
v1 = Vector2D(-x, x)
self.assertEqual(v1.orientation, 135)
v1 = Vector2D(-10, -10)
v1 = Vector2D(-x, -x)
self.assertEqual(v1.orientation, -135)
v1 = Vector2D.from_angle(25)
self.assertAlmostEqual(v1.orientation, 25)
v1 = Vector2D.from_angle(-25)
self.assertAlmostEqual(v1.orientation, -25)
##############################################
def test_math_operations(self):
a, b, c = 10, 20, 30
v1 = Vector2D(a, b)
v2 = Vector2D(b, c)
self.assertEqual(v1 + v2, Vector2D(a+b, b+c))
v1 = Vector2D.from_angle(60)
self.assertAlmostEqual(v1.orientation, 60)
v1 = Vector2D.from_angle(-60)
self.assertAlmostEqual(v1.orientation, -60)
v1 = Vector2D(a, b)
v2 = Vector2D(b, c)
self.assertEqual(v1 - v2, Vector2D(-a, -a))
v1 = Vector2D.from_angle(100)
self.assertAlmostEqual(v1.orientation, 100)
v1 = Vector2D.from_angle(-100)
self.assertAlmostEqual(v1.orientation, -100)
v1 = Vector2D(a, b)
v1 += Vector2D(b, c)
self.assertEqual(v1, Vector2D(a+b, b+c))
v1 = Vector2D.from_angle(160)
self.assertAlmostEqual(v1.orientation, 160)
v1 = Vector2D.from_angle(-160)
self.assertAlmostEqual(v1.orientation, -160)
v1 = Vector2D(a, b)
v1 -= Vector2D(b, c)
self.assertEqual(v1, Vector2D(-a, -a))
v1 = Vector2D(a, b)
self.assertEqual(v1 * a, Vector2D(a*a, b*a))
v1 = Vector2D(a, b)
v1 *= a
self.assertEqual(v1, Vector2D(a*a, b*a))
#?# v1 = Vector2D(a, b)
#?# v1 /= a
#?# self.assertEqual(v1, Vector2D(1, 2))
##############################################
def test_two_vector_operations(self):
v1 = Vector2D(10, 10)
v2 = Vector2D(-10, 10)
x = 10
v1 = Vector2D(x, x)
v2 = Vector2D(-x, x)
self.assertTrue(v1.is_orthogonal(v2))
self.assertTrue(v1.is_orthogonal(v1.rotate(90)))
v1 = Vector2D(10, 10)
self.assertTrue(v1.is_parallel(v1 * -10))
v1 = Vector2D(x, x)
self.assertTrue(v1.is_parallel(v1 * -x))
self.assertTrue(v1.is_parallel(v1.rotate(180)))
angle1 = 10
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment