"""Local evaluation for Programming Problem 1: Diffuse Tokens"""

import importlib.util
from collections import Counter
from pathlib import Path

# Each test: (input_sequence, expected_output)
SAMPLE_TESTS = [
    ([5, 1, 4], [1, 4, 5]),
    ([3, 7], [3, 7]),
    ([10], [10]),
    ([9, 3, 7, 1, 5, 2], [1, 2, 3, 5, 7, 9]),
    ([1, 1, 1, 1], [1, 1, 1, 1]),
    ([999, 1, 500, 250, 750], [1, 250, 500, 750, 999]),
    ([42, 17, 88, 3, 56, 91, 24, 65, 12, 79], [12, 24, 42, 56, 88, 3, 17, 65, 79, 91]),
]


def check(seq, out, expected):
    n = len(seq)
    if len(out) != n or Counter(out) != Counter(seq):
        return False, "Invalid permutation"
    if out != expected:
        return False, "Wrong answer"
    return True, "OK"


if __name__ == "__main__":
    spec = importlib.util.spec_from_file_location(
        "solution", Path(__file__).parent / "solution.py"
    )
    mod = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(mod)

    passed = 0
    for i, (seq, expected) in enumerate(SAMPLE_TESTS, 1):
        try:
            out = mod.solve(len(seq), seq[:])
            ok, msg = check(seq, out, expected)
        except Exception as e:
            ok, msg = False, str(e)
        print(f"Test {i}: {'PASS' if ok else 'FAIL - ' + msg}")
        passed += ok
    print(f"\n{passed}/{len(SAMPLE_TESTS)} passed")
