82 lines
1.4 KiB
Python
82 lines
1.4 KiB
Python
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"]))
|