from pathlib import Path
import re, random, math

HERE=Path(__file__).resolve().parent
PAPER=HERE/"CSM_RH_Paper_74_Ordinary_Prime_Pair_Error_Exponent_Wall_and_Campaign46_Closure_v0.1_2026-09-09.md"

def check_exponent_conversion():
    random.seed(74)
    for _ in range(10000):
        theta=random.uniform(0.5001,0.999)
        s=2*(1-theta)
        assert abs((3-s)-(1+2*theta))<1e-14
        assert abs((1-s/2)-theta)<1e-14
    return True

def check_seed_saturation():
    for k in [0.1,0.3,0.6,0.9,1.0]:
        theta=1-k/2
        lower_exp=1+2*theta
        assert abs(lower_exp-(3-k))<1e-14
    return True

def check_endpoint():
    s=1.0
    theta=1-s/2
    assert theta==0.5
    # GRH-calibration upper exponent 5/2 corresponds s=1/2.
    s_grh=3-2.5
    assert s_grh==0.5
    return True

def check_pair_residual_scale():
    # F-RH-022 requires xi>kappa; no algebraic contradiction here.
    for k in [0.1,0.4,0.8]:
        xi=k+0.01
        assert xi>k
    return True

def check_source():
    s=PAPER.read_text(encoding="utf-8")
    for pat in [r"(?<!\\)\\\(",r"(?<!\\)\\\)",r"(?<!\\)\\\[",r"(?<!\\)\\\]"]:
        assert re.search(pat,s) is None
    assert s.count("$$")%2==0
    tmp=re.sub(r"\$\$.*?\$\$","",s,flags=re.S)
    assert len(re.findall(r"(?<!\\)\$",tmp))%2==0
    return True

if __name__=="__main__":
    print("exponent_conversion",check_exponent_conversion())
    print("seed_saturation",check_seed_saturation())
    print("endpoint",check_endpoint())
    print("pair_residual_scale",check_pair_residual_scale())
    print("source_delimiters",check_source())
    print("PASS")
