#!/usr/bin/env python3
"""Torn-verify for 017 - Narration Collapse. Standard library only."""
import math

FAILS = []
def check(name, condition, detail=""):
    print(("PASS" if condition else "FAIL") + "  " + name + (("  [" + detail + "]") if detail else ""))
    if not condition: FAILS.append(name)
def sigma(t): return 1.0 - t
def entropy(t): return 0.5 * math.log(2.0 * math.pi * math.e * sigma(t) ** 2)
def precision(t): return 1.0 / sigma(t) ** 2

ts = [0.0, 0.30, 0.60, 0.85, 0.95, 0.99]
want_s = [1.0, .7, .4, .15, .05, .01]
want_p = [1.0, 2.0408163265, 6.25, 44.4444444444, 400.0, 10000.0]
check("sigma follows 1-t", all(abs(sigma(t)-w)<1e-12 for t,w in zip(ts,want_s)))
check("precision follows 1/sigma^2", all(abs(precision(t)-w)<1e-7 for t,w in zip(ts,want_p)))
check("precision rises 1 to 10000 on zero new observations", precision(0)==1 and abs(precision(.99)-10000)<1e-8)
check("entropy falls strictly", all(entropy(a)>entropy(b) for a,b in zip(ts,ts[1:])))
check("displayed endpoint is finite", math.isfinite(entropy(.99)) and math.isfinite(precision(.99)))
check("literal t=1 is singular and excluded", sigma(1)==0)
for t in ts: print("t=%.2f sigma=%.5f H=%+.5f precision=%.5f"%(t,sigma(t),entropy(t),precision(t)))
print("\nclaim holds:", not FAILS); print("FAILS:", FAILS if FAILS else "none")
raise SystemExit(1 if FAILS else 0)
