From ff3aa3f3498d061d1762c431b8b422c605329acd Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Tue, 8 Mar 2022 00:16:22 -0800 Subject: [PATCH] Add test coverage. Rationals are 100% tested, Vectors are 82% covered. I used example cases from the class, so it's not exhaustive but it should be good. The untested code in the vector class is all related to raising nonconformant errors. --- kmath/rat.py | 5 +++- tests/linea.py | 6 ++++ tests/rat.py | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 tests/rat.py diff --git a/kmath/rat.py b/kmath/rat.py index 226dd6d..d202c27 100644 --- a/kmath/rat.py +++ b/kmath/rat.py @@ -1,5 +1,7 @@ import math +import numpy + class Rational: """ @@ -39,7 +41,7 @@ class Rational: return Rational(self.n * other.d, self.d * other.n) def lcm(self, other): - return int(math.lcm(self.d, other.d)) + return int(numpy.lcm(self.d, other.d)) def normalize(self, other): lcm = self.lcm(other) @@ -107,6 +109,7 @@ def ipow(n, m): while m > 1: x = x * n + m -= 1 return x diff --git a/tests/linea.py b/tests/linea.py index 1f64dce..3f8d25f 100644 --- a/tests/linea.py +++ b/tests/linea.py @@ -1,3 +1,5 @@ +import sys + import numpy import pytest @@ -165,3 +167,7 @@ def test_cross_product(): v7 = vec.Vector(-6.007, 0.124, 5.772) area = 42.565 assert fequal(vec.area_triangle(v6, v7), area) + + +if __name__ == '__main__': + sys.exit(pytest.main(["-qq"])) diff --git a/tests/rat.py b/tests/rat.py new file mode 100644 index 0000000..d603639 --- /dev/null +++ b/tests/rat.py @@ -0,0 +1,81 @@ +import sys + +import pytest + +import kmath.rat as rat + + +def test_add(): + r1 = rat.Rational(1, 1) + r2 = rat.Int(3) + r3 = r1 + r2 + assert r3.n == 4 + assert r3.d == 1 + assert float(r3) == 4.0 + assert int(r3) == 4 + + +def test_mul(): + r1 = rat.Rational(1, 2) + r2 = rat.Rational(1, 8) + r3 = rat.Rational(1, 16) + assert r1 * r2 == r3 + + +def test_div(): + r1 = rat.Rational(1, 2) + r2 = rat.Rational(1, 8) + r3 = rat.Int(4) + assert r1 / r2 == r3 + + +def test_pow(): + r1 = rat.Rational(1, 2) + r2 = rat.Rational(3, 4) + r3 = rat.Rational(1, 4) + r4 = rat.Rational(9, 16) + r5 = rat.Rational(1, 8) + assert r1 ** 2 == r3 + assert r2 ** 2 == r4 + assert rat.ipow(r1, 3) == r5 + + +def test_scale(): + r1 = rat.Rational(1, 2) + r2 = rat.Rational(2, 4) + assert r1.scale(2) == r2 + assert r1.scale(2).reduce() == r1 + + +def test_dbz_protection(): + assert rat.Rational(0, 0) == rat.Rational(1, 0) + + +def test_equality(): + r1 = rat.Rational(1, 2) + r2 = rat.Rational(3, 4) + r3 = rat.Rational(2, 4) + + assert r1 < r2 + assert r3 < r2 + assert r1 <= r3 + assert r1 == r3 + assert r2 > r1 + assert r2 >= r3 + + +def test_sub(): + r1 = rat.Rational(1, 2) + r2 = rat.Rational(1, 8) + r3 = rat.Rational(3, 8) + assert r1 - r2 == r3 + + +def test_recurse(): + """test from the calculus course""" + r = rat.Rational(239, 169) + assert rat.recurse(5) == r + + +if __name__ == '__main__': + sys.exit(pytest.main(["-qq"]))