102 lines
3.0 KiB
Python
102 lines
3.0 KiB
Python
from unittest import TestCase
|
|
from geometry.vector import Vector
|
|
from geometry.point import Point
|
|
|
|
class TestVector(TestCase):
|
|
def setUp(self):
|
|
self.zero = Vector()
|
|
self.unit_x = Vector((1, 0))
|
|
self.unit_y = Vector((0, 1))
|
|
self.origin = Point()
|
|
|
|
def test_init_with_none(self):
|
|
self.assertEqual(self.zero.x, 0)
|
|
|
|
def test_init_with_pair(self):
|
|
v = Vector((4, 2))
|
|
self.assertEqual(v.x, 4)
|
|
|
|
def test_init_with_point(self):
|
|
self.assertEqual(self.unit_x, Vector(Point(self.unit_x)))
|
|
|
|
def test_string_representation(self):
|
|
self.assertEqual(str(self.unit_x), "〈1, 0〉")
|
|
|
|
def test_equality(self):
|
|
self.assertEqual(self.unit_x, self.unit_x)
|
|
self.assertNotEqual(self.unit_x, self.unit_y)
|
|
|
|
def test_negation(self):
|
|
self.assertEqual(self.zero, -self.zero)
|
|
self.assertEqual(-self.unit_x, Vector((-1, 0)))
|
|
|
|
def test_addition_with_vector(self):
|
|
self.assertEqual(self.unit_x + self.unit_y, Vector((1, 1)))
|
|
|
|
def test_addition_with_point(self):
|
|
self.assertEqual(self.unit_x + Point((3, 4)), Point((4, 4)))
|
|
|
|
def test_addition_with_invalid_type(self):
|
|
with self.assertRaises(TypeError):
|
|
self.unit_x + 2
|
|
|
|
def test_subtraction_with_vector(self):
|
|
self.assertEqual(Vector((2, 0)) - self.unit_x, self.unit_x)
|
|
|
|
def test_subtraction_with_invalid_type(self):
|
|
with self.assertRaises(TypeError):
|
|
self.unit_x - 4
|
|
|
|
def test_multiplication_with_numeric(self):
|
|
self.assertEqual(self.unit_x + self.unit_x, self.unit_x * 2)
|
|
|
|
def test_multiplication_with_invalid_type(self):
|
|
with self.assertRaises(TypeError):
|
|
self.unit_x * self.unit_x
|
|
|
|
def test_division(self):
|
|
self.assertEqual(self.unit_x, Vector((2, 0)) / 2)
|
|
|
|
def test_division_with_zero(self):
|
|
with self.assertRaises(ZeroDivisionError):
|
|
self.unit_x / 0
|
|
|
|
def test_division_with_invalid_type(self):
|
|
with self.assertRaises(TypeError):
|
|
self.unit_x / self.unit_x
|
|
|
|
def test_truthiness(self):
|
|
self.assertTrue(self.unit_x)
|
|
self.assertFalse(self.zero)
|
|
|
|
def test_magnitude(self):
|
|
self.assertEqual(self.zero.mag(), 0)
|
|
self.assertEqual(self.unit_x.mag(), 1)
|
|
self.assertEqual(Vector((3, 4)).mag(), 5)
|
|
|
|
def test_normalize(self):
|
|
self.assertEqual((self.unit_x * 2).normalize(), self.unit_x)
|
|
|
|
def test_normalize_with_zero_vector(self):
|
|
with self.assertRaises(ValueError):
|
|
self.zero.normalize()
|
|
|
|
def test_is_collinear(self):
|
|
self.assertTrue(self.unit_x.is_collinear(self.unit_x * -4))
|
|
self.assertFalse(self.unit_x.is_collinear(self.unit_y))
|
|
|
|
def test_is_collinear_with_zero_vector(self):
|
|
self.assertFalse(self.unit_x.is_collinear(self.zero))
|
|
self.assertFalse(self.zero.is_collinear(self.zero))
|
|
|
|
def test_division_with_invalid_type(self):
|
|
with self.assertRaises(TypeError):
|
|
self.unit_x.is_collinear(4)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|