from pathlib import Path import re,random root=Path(__file__).resolve().parents[1] s=(root/'matrix.c').read_text();block=s.split('static const UBYTE glyphs[][7]={',1)[1].split('};',1)[0];glyphs=[list(map(int,x.split(','))) for x in re.findall(r'\{([\d,]+)\}',block)] W,H,C,R=480,320,60,27 rng=random.Random(458);cached=bytearray(W*H);previous={};updates=0 heads=[-rng.randrange(520) for _ in range(C)];tails=[rng.randrange(10,26) for _ in range(C)];cells=[[rng.randrange(48) for _ in range(R)] for _ in range(C)] def draw(buf,c,r,g,level): y=r*12 if level>=29: for gy,row in enumerate(glyphs[g]): yy=y+gy+2 if yy>=H:continue for gx in range(5): if row&(16>>gx): k=yy*W+c*8+gx+1;buf[k-1]=buf[k+1]=3 for gy,row in enumerate(glyphs[g]): yy=y+gy+2 if yy>=H:continue for gx in range(5): if row&(16>>gx):buf[yy*W+c*8+gx+1]=level for step in range(240): reference=bytearray(W*H) for c in range(C): heads[c]+=rng.randrange(1,5) if heads[c]>H+tails[c]*12:heads[c]=-tails[c]*12 for _ in range(45 if step%4==0 else 0):cells[rng.randrange(C)][rng.randrange(R)]=rng.randrange(48) for c in range(C): for r in range(R): dist=heads[c]-r*12 level=max(1,29-dist*28//(tails[c]*12)) if dist<4:level=8+dist*6 elif dist<14:level=31 elif dist<26:level=30 if dist<0 or dist>tails[c]*12:level=0 g=cells[c][r] if level:draw(reference,c,r,g,level) old=previous.get((c,r)) if old and old[0]==level and (not level or old[1]==g):continue previous[c,r]=(level,g);updates+=1 for y in range(r*12,min(H,r*12+12)):cached[y*W+c*8:y*W+c*8+8]=bytes(8) if level:draw(cached,c,r,g,level) assert reference==cached,step report=f'PASS: 240 frames identical to full redraw; changed cell fraction {updates/(240*C*R):.1%}.\n' (root/'CACHE-VALIDATION.txt').write_text(report);print(report)