"""A patch needs two test partitions
Deterministic, standard-library experiment. RESULTS contains the full data.
"""
tasks = {
    'absolute': (
        [(-2, 2), (-3, 3)], [(0, 0), (2, 2), (3, 3)],
        [lambda x: x, lambda x: 2, lambda x: -x,
         lambda x: 2 if x == -2 else x, abs]),
    'clamp': (
        [(-1, 0), (-5, 0)], [(0, 0), (5, 5), (10, 10), (15, 10)],
        [lambda x: min(x, 10), lambda x: 0, lambda x: max(0, x),
         lambda x: 0 if x == -1 else min(x, 10),
         lambda x: max(0, min(x, 10))]),
    'average': (
        [([], 0)], [([2, 4], 3), ([1], 1), ([-2, 4], 1)],
        [lambda x: sum(x) / len(x), lambda x: 0, lambda x: sum(x),
         lambda x: x[0] if x else 0, lambda x: sum(x) / (len(x) or 1)]),
}

def passes(fn, case):
    value, expected = case
    try:
        return fn(value) == expected
    except Exception:
        return False

resolved = 0
for task, (f2p, p2p, candidates) in tasks.items():
    for index, fn in enumerate(candidates):
        fixed = sum(passes(fn, case) for case in f2p)
        kept = sum(passes(fn, case) for case in p2p)
        ok = fixed == len(f2p) and kept == len(p2p)
        resolved += ok
        print(task, index, f'F2P={fixed}/{len(f2p)}',
              f'P2P={kept}/{len(p2p)}', 'resolved=' + str(ok))
print('resolved_candidates', resolved, 'of', 15)
assert resolved == 3


labels={'absolute':['identity','constant 2','negate','special-case -2','absolute value'],'clamp':['upper bound only','constant 0','lower bound only','special-case -1','both bounds'],'average':['divide by length','constant 0','sum only','first element','empty-aware mean']}
rows=[]
for task,(f2p,p2p,candidates) in tasks.items():
    for i,fn in enumerate(candidates):
        results=[dict(partition=part,input=value,expected=expected,passed=passes(fn,(value,expected))) for part,caseset in [('F2P',f2p),('P2P',p2p)] for value,expected in caseset]
        rows.append(dict(task=task,candidate=i,name=labels[task][i],tests=results,resolved=all(r['passed'] for r in results)))
held_out=[('absolute',-0.5,0.5),('clamp',-0.5,0),('clamp',10.5,10),('clamp',3.5,3.5),('average',[-4,-2],-3)]
for task,value,expected in held_out: assert passes(tasks[task][2][-1],(value,expected))
RESULTS=dict(candidates=rows,held_out=[dict(task=t,input=v,expected=e,passed=True) for t,v,e in held_out])
print('Held-out boundary checks:',len(held_out),'passed')
