Initial commit
This commit is contained in:
48
tests/test_point.py
Normal file
48
tests/test_point.py
Normal file
@@ -0,0 +1,48 @@
|
||||
from unittest import TestCase
|
||||
from geometry.point import Point
|
||||
from geometry.vector import Vector
|
||||
|
||||
class TestPoint(TestCase):
|
||||
def setUp(self):
|
||||
self.origin = Point((0, 0))
|
||||
self.a = Point((4, 0))
|
||||
self.b = Point((0, 3))
|
||||
self.c = Point((5, 2))
|
||||
self.v = Vector((1, 2))
|
||||
|
||||
def test_init_with_none(self):
|
||||
origin = Point()
|
||||
self.assertEqual(origin.x, 0)
|
||||
|
||||
def test_init_with_pair(self):
|
||||
p = Point((4, 2))
|
||||
self.assertEqual(p.x, 4)
|
||||
|
||||
def test_init_with_vector(self):
|
||||
self.assertEqual(self.a, Point(Vector(self.a)))
|
||||
|
||||
def test_string_representation(self):
|
||||
self.assertEqual(str(self.a), "(4, 0)")
|
||||
|
||||
def test_equality(self):
|
||||
self.assertEqual(self.a, self.a)
|
||||
self.assertNotEqual(self.a, self.b)
|
||||
|
||||
def test_addition_with_vector(self):
|
||||
self.assertEqual(self.a + self.v, self.c)
|
||||
|
||||
def test_addition_with_invalid_type(self):
|
||||
with self.assertRaises(TypeError):
|
||||
self.a + self.a
|
||||
|
||||
def test_subtraction_with_point(self):
|
||||
self.assertEqual(self.c - self.a, self.v)
|
||||
|
||||
def test_subtraction_with_vector(self):
|
||||
self.assertEqual(self.c - self.v, self.a)
|
||||
|
||||
def test_subtraction_with_invalid_type(self):
|
||||
with self.assertRaises(TypeError):
|
||||
self.a - 4
|
||||
|
||||
|
101
tests/test_vector.py
Normal file
101
tests/test_vector.py
Normal file
@@ -0,0 +1,101 @@
|
||||
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)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user