Implement Unit Testing for fancymath
#8
@@ -15,6 +15,14 @@ def fact(n: int) -> int:
|
|||||||
return n * fact(n - 1)
|
return n * fact(n - 1)
|
||||||
|
|
||||||
|
|
||||||
|
def is_even(x):
|
||||||
|
return x % 2 == 0
|
||||||
|
|
||||||
|
|
||||||
|
def is_odd(x):
|
||||||
|
return x % 2 == 1
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
n = 5
|
n = 5
|
||||||
assert fact(n) == 120
|
assert fact(n) == 120
|
||||||
|
|||||||
@@ -30,6 +30,20 @@ class Vector(object):
|
|||||||
s = "Vector(x=%s, y=%s, z=%s)" % (self.x, self.y, self.z)
|
s = "Vector(x=%s, y=%s, z=%s)" % (self.x, self.y, self.z)
|
||||||
return s
|
return s
|
||||||
|
|
||||||
|
def __add__(self, other):
|
||||||
|
|
|||||||
|
x = self.x + other.x
|
||||||
|
y = self.y + other.y
|
||||||
|
z = self.z + other.z
|
||||||
|
return self.__class__(x, y, z)
|
||||||
|
|
||||||
|
def __mul__(self, other):
|
||||||
|
if not isinstance(other, Vector):
|
||||||
|
corran
commented
Do we want to handle scalar multiplication here? If so we should implement Do we want to handle scalar multiplication here? If so we should implement `__rmul__` and `__imul__`.
|
|||||||
|
return NotImplemented
|
||||||
|
x = self.y * other.z - self.z * other.y
|
||||||
|
y = self.z * other.x - self.x * other.z
|
||||||
|
z = self.x * other.y - self.y * other.x
|
||||||
|
return self.__class__(x, y, z)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
v1 = Vector(3, 4, 0)
|
v1 = Vector(3, 4, 0)
|
||||||
|
|||||||
0
test/__init__.py
Normal file
0
test/__init__.py
Normal file
25
test/test_math.py
Normal file
25
test/test_math.py
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
import unittest
|
||||||
|
|
||||||
|
from fancymath.math import is_even, is_odd
|
||||||
|
|
||||||
|
|
||||||
|
class TestMath(unittest.TestCase):
|
||||||
|
def test_is_even(self):
|
||||||
|
"Test the is_even function"
|
||||||
|
self.assertTrue(is_even(2))
|
||||||
|
self.assertTrue(is_even(-4))
|
||||||
|
self.assertTrue(is_even(0))
|
||||||
|
self.assertFalse(is_even(1))
|
||||||
|
self.assertFalse(is_even(2.5))
|
||||||
|
|
||||||
|
def test_is_odd(self):
|
||||||
|
"Test the is_odd function"
|
||||||
|
self.assertTrue(is_odd(3))
|
||||||
|
self.assertTrue(is_odd(-5))
|
||||||
|
self.assertFalse(is_odd(0))
|
||||||
|
self.assertFalse(is_odd(2))
|
||||||
|
self.assertFalse(is_odd(2.5))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
0
test/test_util.py
Normal file
0
test/test_util.py
Normal file
59
test/test_vector.py
Normal file
59
test/test_vector.py
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
import unittest
|
||||||
|
|
||||||
|
from fancymath.vector import Vector
|
||||||
|
|
||||||
|
|
||||||
|
class TestVector(unittest.TestCase):
|
||||||
|
def test_cross_product(self):
|
||||||
|
# Dictionary of (v1, v2): v3
|
||||||
|
values = {
|
||||||
|
(Vector(1, 2, 3), Vector(3, 2, 1)): Vector(-4, 8, -4),
|
||||||
|
(Vector(0, 0, 0), Vector(1, 2, 3)): Vector(0, 0, 0),
|
||||||
|
(Vector(1, 0, 0), Vector(1, 0, 0)): Vector(0, 0, 0),
|
||||||
|
}
|
||||||
|
|
||||||
|
for inputs, expected in values.items():
|
||||||
|
v1, v2 = inputs
|
||||||
|
with self.subTest(v1=v1, v2=v2):
|
||||||
|
v3 = v1 * v2
|
||||||
|
|
||||||
|
self.assertIsInstance(v3, Vector)
|
||||||
|
self.assertEqual(v3.x, expected.x)
|
||||||
|
self.assertEqual(v3.y, expected.y)
|
||||||
|
self.assertEqual(v3.z, expected.z)
|
||||||
|
|
||||||
|
def test_cross_product_failure(self):
|
||||||
|
v1 = Vector(1, 2, 3)
|
||||||
|
v2 = (3, 2, 1)
|
||||||
|
|
||||||
|
with self.assertRaises(TypeError):
|
||||||
|
v1 * v2
|
||||||
|
|
||||||
|
|
||||||
|
def test_add_vector(self):
|
||||||
|
v1 = Vector(1, 2, 3)
|
||||||
|
v2 = Vector(-3, -2, -1)
|
||||||
|
v3 = v1 + v2
|
||||||
|
self.assertIsInstance(v3, Vector)
|
||||||
|
self.assertEqual(v3.x, v1.x + v2.x)
|
||||||
|
|
||||||
|
def test_abs(self):
|
||||||
|
v1 = Vector(0, 0, 0)
|
||||||
|
v2 = Vector(1, 2, 2)
|
||||||
|
v3 = Vector(1, -2, 2)
|
||||||
|
self.assertEqual(v1.abs(), 0)
|
||||||
|
self.assertEqual(v2.abs(), 3)
|
||||||
|
self.assertEqual(v2.abs(), v3.abs())
|
||||||
|
|
||||||
|
def test_dot_failure(self):
|
||||||
|
with self.assertRaises(ValueError):
|
||||||
|
v1 = Vector(1, 2, 3)
|
||||||
|
v1.dot(3)
|
||||||
|
|
||||||
|
def test_dot_product(self):
|
||||||
|
v1 = Vector(1, 2, 3)
|
||||||
|
v2 = Vector(-3, -2, -1)
|
||||||
|
v3 = v1.dot(v2)
|
||||||
|
corran
commented
Seeing this in use, perhaps we should use Seeing this in use, perhaps we should use `@` for dot product? So we get `v1 @ v2`. Can we open an issue to discuss this?
|
|||||||
|
self.assertEqual(v3,v1.x * v2.x + v1.y * v2.y + v1.z * v2.z)
|
||||||
|
vzero = Vector(0, 0, 0)
|
||||||
|
self.assertEqual(v1.dot(vzero),0)
|
||||||
Reference in New Issue
Block a user
This should handle case where
otheris not aVector.