| 1 | import pathlib,random,subprocess,sys |
| 2 | binary=str(pathlib.Path(sys.argv[1]).absolute()) |
| 3 | rng=random.Random(0x715E) |
| 4 | expected=[];written=[] |
| 5 | def atom(x):return '()' if x is None else str(x) |
| 6 | def signature(nodes): |
| 7 | seen={} |
| 8 | def walk(value): |
| 9 | if not isinstance(value,tuple):return '(atom '+atom(value)+')' |
| 10 | idx=value[1] |
| 11 | if idx in seen:return '(ref '+str(seen[idx])+')' |
| 12 | serial=len(seen);seen[idx]=serial |
| 13 | kind,items=nodes[idx] |
| 14 | return '('+kind+' '+str(serial)+(' '+' '.join(map(walk,items)) if items else '')+')' |
| 15 | return walk(('ref',0)) |
| 16 | for case in range(200): |
| 17 | size=rng.randrange(1,18) |
| 18 | nodes=[] |
| 19 | for i in range(size): |
| 20 | kind=rng.choice(['pair','vector']) |
| 21 | count=2 if kind=='pair' else rng.randrange(5) |
| 22 | items=[('ref',rng.randrange(size)) if rng.random()<.7 else rng.choice([None,-1,0,1,42]) for _ in range(count)] |
| 23 | nodes.append((kind,items)) |
| 24 | source=[] |
| 25 | for i,(kind,items) in enumerate(nodes): |
| 26 | source.append(f'(define n{i} '+('(cons #f #f)' if kind=='pair' else f'(make-vector {len(items)} #f)')+')') |
| 27 | def expression(x):return 'n'+str(x[1]) if isinstance(x,tuple) else "'()" if x is None else str(x) |
| 28 | for i,(kind,items) in enumerate(nodes): |
| 29 | for j,item in enumerate(items): |
| 30 | source.append(f'({["set-car!","set-cdr!"][j]} n{i} {expression(item)})' if kind=='pair' else f'(vector-set! n{i} {j} {expression(item)})') |
| 31 | source.append('(write-shared n0) (newline)') |
| 32 | r=subprocess.run([binary,'-e',' '.join(source)],capture_output=True,text=True,timeout=20) |
| 33 | assert r.returncode==0 and not r.stderr,(case,r.stderr) |
| 34 | written.append(r.stdout.strip());expected.append(signature(nodes)) |
| 35 | guile=r''' |
| 36 | (use-modules (srfi srfi-38)) |
| 37 | (define (signature root) |
| 38 | (let ((seen (make-hash-table)) (next 0)) |
| 39 | (define (walk x) |
| 40 | (if (or (pair? x) (vector? x)) |
| 41 | (let ((found (hashq-ref seen x))) |
| 42 | (if found (list 'ref found) |
| 43 | (let ((id next)) |
| 44 | (set! next (+ next 1)) (hashq-set! seen x id) |
| 45 | (if (pair? x) |
| 46 | (let* ((head (walk (car x))) (tail (walk (cdr x)))) |
| 47 | (list 'pair id head tail)) |
| 48 | (cons 'vector (cons id |
| 49 | (let loop ((i 0) (items '())) |
| 50 | (if (= i (vector-length x)) (reverse items) |
| 51 | (let ((item (walk (vector-ref x i)))) |
| 52 | (loop (+ i 1) (cons item items))))))))))) |
| 53 | (list 'atom x))) |
| 54 | (walk root))) |
| 55 | (let loop () |
| 56 | (let ((x (read-with-shared-structure))) |
| 57 | (unless (eof-object? x) |
| 58 | (write (signature x)) (newline) (loop)))) |
| 59 | ''' |
| 60 | r=subprocess.run(['guile','--no-auto-compile','-c',guile],input='\n'.join(written)+'\n',capture_output=True,text=True,timeout=30) |
| 61 | assert r.returncode==0,r.stderr |
| 62 | actual=r.stdout.splitlines() |
| 63 | assert len(actual)==len(expected),(len(actual),len(expected),r.stderr) |
| 64 | for i,(a,b) in enumerate(zip(actual,expected)): |
| 65 | assert a==b,(i,written[i],a,b) |
| 66 | print(f'{len(expected)} generated graphs preserved identity and cycles through independent Guile reader') |
| 67 | |