"""Read-only differential audit; missing native features are findings, not passes.""" from pathlib import Path from concurrent.futures import ThreadPoolExecutor import argparse,hashlib,json,re,subprocess ROOT=Path(__file__).resolve().parents[1] BASE=r'''\catcode123=1 \catcode125=2 \catcode35=6 \font\rm=cmr10 \rm \hsize=100pt \baselineskip=12pt \hbadness=10000 \vbadness=10000 ''' MATH=(ROOT/'examples/native-bootstrap/math-fonts.tex').read_text() CASES=[ ('control-numexpr','control',r'\count0=\numexpr(7+5)*3/2\relax\message{AUDIT=\the\count0;}',0), ('control-latin-ligatures','control',r'\setbox0=\hbox{office AV}\message{AUDIT=\the\wd0;}',0), ('control-hfuzz','control',r'\hfuzz=.1pt\message{AUDIT=\the\hfuzz;}',0), ('showbox-first-job','diagnostics',r'\showboxbreadth=20\showboxdepth=5\setbox0=\hbox{office AV}\showbox0',1), ('errorcontextlines','diagnostics',r'\errorcontextlines=5\message{AUDIT=\the\errorcontextlines;}',0), ('nested-error-context','diagnostics',r'\def\A#1{\B{#1}\relax}\def\B#1{\C{#1}\relax}\def\C#1{\errmessage{AUDIT_NESTED_ERROR}}\A{X}',1), ('show','diagnostics',r'\show\relax',1), ('showthe','diagnostics',r'\count0=17\showthe\count0',1), ('showlists','diagnostics',r'\setbox0=\vbox{\hbox{A}\showlists}',1), ('math-compound-showbox','diagnostics',r'\setbox0=\hbox{$x^2$}\showboxbreadth=100\showboxdepth=10\showbox0',1), ('math-glue-showbox','diagnostics',r'\setbox0=\hbox{$x+x$}\showboxbreadth=100\showboxdepth=10\showbox0',1), ('punctuation-ligature','text',r"\setbox0=\hbox{--- -- ``Hi''}\message{AUDIT=\the\wd0;}",0), ('sfcode','text',r'\sfcode`\.=3000\setbox0=\hbox{A. B}\message{AUDIT=\the\wd0;}',0), ('everypar','hooks',r'\everypar={\message{AUDIT=EVERYPAR;}}\noindent A\par',0), ('everymath','hooks',r'\everymath={\message{AUDIT=EVERYMATH;}}\setbox0=\hbox{$x$}',0), ('everydisplay','hooks',r'\everydisplay={\message{AUDIT=EVERYDISPLAY;}}\noindent A$$x$$\par',0), ('dimexpr','expressions',r'\dimen0=\dimexpr(1pt+2pt)*3/2\relax\message{AUDIT=\the\dimen0;}',0), ('glueexpr','expressions',r'\skip0=\glueexpr1pt plus2fil+3pt plus1fill\relax\message{AUDIT=\the\skip0;}',0), ('muexpr','expressions',r'\muskip0=\muexpr3mu plus1fil*2\relax\message{AUDIT=\the\muskip0;}',0), ('unhcopy','lists',r'\setbox0=\hbox{AB}\setbox1=\hbox{C\unhcopy0 D}\message{AUDIT=\the\wd1;}',0), ('lastbox','lists',r'\setbox0=\vbox{\hbox{A}\setbox1=\lastbox\message{AUDIT=\the\wd1;}}',0), ('hfill','lists',r'\setbox0=\hbox to30pt{A\hfill B}\message{AUDIT=\the\wd0;}',0), ('vrule','lists',r'\setbox0=\hbox{\vrule width1pt height2pt depth1pt}\message{AUDIT=\the\wd0;}',0), ('overfullrule','page',r'\overfullrule=5pt\setbox0=\hbox to1pt{AB}\message{AUDIT=\the\wd0;}',0), ('maxdepth','page',r'\maxdepth=4pt\message{AUDIT=\the\maxdepth;}',0), ('splitmaxdepth','page',r'\splitmaxdepth=4pt\message{AUDIT=\the\splitmaxdepth;}',0), ('topskip','page',r'\topskip=10pt\message{AUDIT=\the\topskip;}',0), ('splittopskip','page',r'\splittopskip=10pt\message{AUDIT=\the\splittopskip;}',0), ('vsize','page',r'\vsize=100pt\message{AUDIT=\the\vsize;}',0), ('font-at','font',r'\font\t=cmr10 at12pt\t\setbox0=\hbox{AV}\message{AUDIT=\the\wd0;}',0), ('font-scaled','font',r'\font\t=cmr10 scaled1200\t\setbox0=\hbox{AV}\message{AUDIT=\the\wd0;}',0), ('font-cmr7-text','font',r'\font\t=cmr7\t\setbox0=\hbox{AV}\message{AUDIT=\the\wd0;}',0), ('font-bold','font',r'\font\t=cmbx10\t\setbox0=\hbox{AV}\message{AUDIT=\the\wd0;}',0), ('fontdimen','font',r'\fontdimen2\rm=4pt\message{AUDIT=\the\fontdimen2\rm;}',0), ('futurelet','expansion',r'\def\A{\ifx\peek\relax\message{AUDIT=YES;}\fi}\futurelet\peek\A\relax',0), ('afterassignment','expansion',r'\afterassignment\relax\count0=2\message{AUDIT=\the\count0;}',0), ('aftergroup','expansion',r'\def\A{\message{AUDIT=AFTERGROUP;}}{\aftergroup\A}',0), ('outer','expansion',r'\outer\def\A{\relax}\A',0), ('radical','math',r'\setbox0=\hbox{$\radical"270370 x$}\message{AUDIT=\the\wd0;}',0), ('mathchoice','math',r'\setbox0=\hbox{$\mathchoice{a}{b}{c}{d}$}\message{AUDIT=\the\wd0;}',0), ] def run(case, binary, output_dir): name,group,body,expected=case;out=output_dir/name;out.mkdir(parents=True,exist_ok=True) source=out/'probe.tex';source.write_text(BASE+(MATH if group=='math' or group=='hooks' or name.startswith('math-')else'')+body+'\n\\end\n') native=subprocess.run([str(binary),'native','--initex','--etex',str(source),str(out/'native.pdf')],cwd=out,capture_output=True,text=True,timeout=30) args=['pdftex','-ini','-etex','-interaction=nonstopmode'] if name=='nested-error-context':args.append('-halt-on-error') reference=subprocess.run(args+['probe.tex'],cwd=out,capture_output=True,text=True,timeout=30) for label,result in [('native',native),('reference',reference)]: (out/f'{label}-stdout.txt').write_text(result.stdout);(out/f'{label}-stderr.txt').write_text(result.stderr) native_text=native.stdout+'\n'+native.stderr;reference_text=(out/'probe.log').read_text() errors=re.findall(r'(?m)^! (.*)',reference_text) assert reference.returncode==expected,(name,reference.returncode,reference.stdout[-1500:]) if expected==0:assert not errors,(name,errors) elif name in ['show','showthe']:assert not errors and re.search(r'(?m)^> ',reference_text),(name,errors) elif name!='nested-error-context':assert errors==['OK.'],(name,errors) marks=lambda t:re.findall(r'AUDIT=([^;]*);',t) controls_ok=None if group=='control': controls_ok=native.returncode==reference.returncode and marks(native_text)==marks(reference_text) assert controls_ok,(name,native_text,reference_text) first=next((line for line in native.stderr.splitlines()if line.startswith('error:')),'') box_equal=None if name=='showbox-first-job': n=(out/'native.log').read_text();b=lambda t:re.findall(r'(?ms)^> \\box\d+=.*?(?=\n! OK\.)',t) box_equal=b(n)==b(reference_text)and len(b(n))==1 return dict(id=name,group=group,source=str(source),source_sha256=hashlib.sha256(source.read_bytes()).hexdigest(),native_exit=native.returncode,reference_exit=reference.returncode,native_error=first,native_values=marks(native_text),reference_values=marks(reference_text),control_equal=controls_ok,box_body_equal=box_equal,native_has_context_frames=bool(re.search(r'(?m)^\\[ABC] ',native_text)),reference_has_context_frames=bool(re.search(r'(?m)^\\[ABC] ',reference_text))) def main(): parser=argparse.ArgumentParser(description=__doc__) parser.add_argument('--binary',type=Path,default=ROOT/'target/release/texrra') parser.add_argument('--output-dir',type=Path,required=True) args=parser.parse_args() binary=args.binary.resolve();output_dir=args.output_dir.resolve() output_dir.mkdir(parents=True,exist_ok=True) with ThreadPoolExecutor(max_workers=4)as pool: results=list(pool.map(lambda item:run(item,binary,output_dir),CASES)) findings=dict( undefined_entry_probes=sum('undefined native control sequence'in r['native_error']for r in results), unsupported_semantic_probes=sum(bool(r['native_error'])and'undefined native control sequence'not in r['native_error']and r['id']!='nested-error-context'for r in results), missing_context_probes=sum(r['reference_has_context_frames']and not r['native_has_context_frames']for r in results), first_show_body_mismatches=sum(r['box_body_equal']is False for r in results)) report=dict(audit_completed=True,production_code_changed=False,goal_implemented=False, binary=str(binary),binary_sha256=hashlib.sha256(binary.read_bytes()).hexdigest(), reference_version=subprocess.check_output(['pdftex','--version'],text=True).splitlines()[0], cases=len(results),controls=sum(r['group']=='control'for r in results),findings=findings, results=results,scope='Representative probes, not a complete primitive count or conformance score. Unsupported cases remain failures of native capability, never successful compatibility tests.') (output_dir/'audit.json').write_text(json.dumps(report,indent=2)+'\n') for r in results:print(r['id'],r['native_exit'],r['reference_exit'],r['native_error']) print('Audit completed:',len(results),'probes; implementation remains pending.') if __name__=='__main__': main()