"""Tests for exact arithmetic module."""
import sys
sys.path.insert(0, 'src')
from fractions import Fraction
from core.exact import (
frac, exact_sum, exact_product, lcm, gcd_many, lcm_many,
mod_exp, mod_inv, crt, comb, perm,
exact_eval, exact_solve, prime_factors, all_divisors, euler_phi,
format_answer
)
from core.verify import (
is_integer, is_positive, is_in_range, check_mod, check_divisible,
check_parity, verify_equation, verify_answer, sanity_check
)
def test_fractions():
assert frac(1, 3) + frac(1, 4) == Fraction(7, 12)
assert exact_sum([frac(1, 2), frac(1, 3), frac(1, 6)]) == Fraction(1)
assert exact_product([frac(2, 3), frac(3, 4), frac(4, 5)]) == Fraction(2, 5)
def test_modular():
assert mod_exp(2, 10, 1000) == 24
assert mod_inv(3, 7) == 5
assert (3 * 5) % 7 == 1
assert mod_inv(17, 43) == 38
assert (17 * 38) % 43 == 1
def test_crt():
# x ≡ 2 (mod 3), x ≡ 3 (mod 5), x ≡ 2 (mod 7)
x = crt([2, 3, 2], [3, 5, 7])
assert x % 3 == 2
assert x % 5 == 3
assert x % 7 == 2
assert x == 23
def test_combinatorics():
assert comb(10, 3) == 120
assert comb(5, 0) == 1
assert comb(5, 5) == 1
assert perm(10, 3) == 720
assert perm(5, 5) == 120
def test_exact_eval():
assert exact_eval("1/3 + 1/4") == Fraction(7, 12)
assert exact_eval("sqrt(2)**2") == 2
assert exact_eval("2**10") == 1024
def test_exact_solve():
sols = exact_solve("x**2 - 4")
assert -2 in sols and 2 in sols
def test_prime_factors():
assert prime_factors(360) == {2: 3, 3: 2, 5: 1}
assert prime_factors(17) == {17: 1}
def test_verification():
assert is_integer(42) == True
assert is_integer(42.5) == False
assert is_positive(5) == True
assert is_positive(-3) == False
assert is_in_range(50, 0, 100) == True
assert is_in_range(150, 0, 100) == False
def test_check_mod():
assert check_mod(17, 5) == 2
assert check_mod(17, 5, 2) == True
assert check_mod(17, 5, 3) == False
def test_verify_answer():
checks = {'integer': True, 'positive': True, 'range': (0, 100)}
passed, _ = verify_answer(42, checks)
assert passed
checks = {'integer': True, 'mod': {'modulus': 7, 'expected': 2}}
passed, _ = verify_answer(16, checks)
assert passed # 16 % 7 = 2
def test_sanity_check():
passed, _ = sanity_check(120, 'counting')
assert passed
passed, _ = sanity_check(-5, 'counting')
assert not passed # Counting must be non-negative
def test_format_answer():
assert format_answer(42, 'int') == '42'
assert format_answer(Fraction(3, 4), 'frac') == '3/4'
assert format_answer(Fraction(4, 1), 'int') == '4'
if __name__ == '__main__':
test_fractions()
test_modular()
test_crt()
test_combinatorics()
test_exact_eval()
test_exact_solve()
test_prime_factors()
test_verification()
test_check_mod()
test_verify_answer()
test_sanity_check()
test_format_answer()
print("All tests passed!")