"""Enkle, åbne kontroller af python_oevelser.py; ingen eksterne pakker."""
import math
import sys

try:
    from python_oevelser import predict, batch_predict, mean_squared_error
except Exception as exc:
    raise SystemExit(f'Kunne ikke læse python_oevelser.py: {exc}') from exc


def close(actual, expected):
    return isinstance(actual, (int, float)) and not isinstance(actual, bool) and math.isclose(actual, expected, rel_tol=1e-8, abs_tol=1e-8)


def run(name, cases):
    failures=[]
    for func,args,expected in cases:
        try:
            actual=func(*args)
            valid=(len(actual)==len(expected) and all(close(a,b) for a,b in zip(actual,expected))) if isinstance(expected,list) and isinstance(actual,list) else close(actual,expected)
            if not valid:failures.append(f'input {args}: forventede {expected}, fik {actual}')
        except Exception as exc:
            failures.append(f'input {args}: {type(exc).__name__}: {exc}')
    if failures:
        print(f'PRØV IGEN · {name}: {failures[0]}'); return False
    print(f'BESTÅET · {name}'); return True


results=[
    run('predict',[(predict,(2,3,1),7),(predict,(-2,.5,-1),-2),(predict,(0,9,3),3)]),
    run('batch_predict',[(batch_predict,([0,1],2,1),[1,3]),(batch_predict,([-1,2],-2,0),[2,-4]),(batch_predict,([],4,1),[])]),
    run('mean_squared_error',[(mean_squared_error,([1,3],[0,5]),2.5),(mean_squared_error,([0,0],[0,0]),0),(mean_squared_error,([-2],[2]),16)])
]
print(f'\n{sum(results)} af {len(results)} opgaver bestået. Åbne kontroller giver feedback, men er ikke en fuldstændig test.')
sys.exit(0 if all(results) else 1)
