"""Independent manual hashtable check: operations against dict and finite graph classes. Run: python3 scheme/testdata/check_hashtables.py /path/to/vile [report.json] These are manual CLI/property probes, not additional V _test.v cases. """ import itertools, json, pathlib, random, subprocess, sys from fractions import Fraction if len(sys.argv) not in (2, 3): raise SystemExit('usage: check_hashtables.py VILE_BINARY [REPORT_JSON]') binary = str(pathlib.Path(sys.argv[1]).resolve()) rng = random.Random(613) checks = 0 def run(expressions, expected, count_operations=True): global checks command = [binary, '-s'] for expression in expressions: command.extend(['-e', expression]) result = subprocess.run(command, capture_output=True, text=True, timeout=40) assert result.returncode == 0 and not result.stderr, (result.stderr, expressions) assert result.stdout.splitlines() == expected, (result.stdout.splitlines(), expected, expressions) if count_operations: checks += len(expected) modes = { 'eq': '(make-eq-hashtable)', 'eqv': '(make-eqv-hashtable)', 'collision': '(make-hashtable (lambda (x) 0) =)', 'structural': '(make-hashtable equal-hash equal?)', 'casefold': '(make-hashtable string-ci-hash string-ci=?)', } for mode, constructor in modes.items(): data = {} commands, expected = [f'(define h {constructor})'], [] def key(i): if mode == 'structural': return f'(list (vector {i}) (list {i + 1}))' if mode == 'casefold': text = f'Key{i}' return '"' + ''.join(c.upper() if rng.randrange(2) else c.lower() for c in text) + '"' return str(i) for step in range(350): operation = rng.choice(['set', 'ref', 'contains', 'delete', 'update', 'copy', 'clear']) i = rng.randrange(-18, 19) k = key(i) if operation == 'set': value = rng.randrange(-10000, 10000) commands.append(f'(hashtable-set! h {k} {value}) (hashtable-size h)') data[i] = value expected.append(str(len(data))) elif operation == 'ref': commands.append(f'(hashtable-ref h {k} 77777)') expected.append(str(data.get(i, 77777))) elif operation == 'contains': commands.append(f'(hashtable-contains? h {k})') expected.append('#t' if i in data else '#f') elif operation == 'delete': commands.append(f'(hashtable-delete! h {k}) (hashtable-size h)') data.pop(i, None) expected.append(str(len(data))) elif operation == 'update': commands.append(f'(hashtable-update! h {k} (lambda (x) (+ x 1)) 100) (hashtable-ref h {k} #f)') data[i] = data.get(i, 100) + 1 expected.append(str(data[i])) elif operation == 'copy': commands.append(f'(define frozen (hashtable-copy h)) (define changed (hashtable-copy frozen #t)) (hashtable-set! changed {k} -999) (list (hashtable-size frozen) (hashtable-ref frozen {k} 77777))') expected.append(f'({len(data)} {data.get(i, 77777)})') else: commands.append('(hashtable-clear! h 4) (hashtable-size h)') data.clear() expected.append('0') run(commands, expected) # Independent coinductive classes of finite pair graphs. Pointer layout and # number of graph nodes must not enter the structural hash result. graphs = [] for size in range(1, 4): for labels in itertools.product([0, 1], repeat=size): for tails in itertools.product(range(-1, size), repeat=size): graphs.append((labels, tails)) commands = [] for labels, tails in graphs: bindings = ' '.join(f'(n{i} (cons {label} (list)))' for i, label in enumerate(labels)) links = ' '.join(f'(set-cdr! n{i} n{tail})' for i, tail in enumerate(tails) if tail >= 0) commands.append(f'(let ({bindings}) {links} (equal-hash n0))') command = [binary, '-s'] for program in commands: command.extend(['-e', program]) result = subprocess.run(command, capture_output=True, text=True, timeout=40) assert result.returncode == 0 and not result.stderr, result.stderr hashes = list(map(int, result.stdout.splitlines())) assert len(hashes) == len(graphs) def equal(a, b): pending, seen = [(0, 0)], set() while pending: x, y = pending.pop() if (x, y) in seen: continue seen.add((x, y)) if a[0][x] != b[0][y]: return False ax, by = a[1][x], b[1][y] if ax == -1 or by == -1: if ax != by: return False else: pending.append((ax, by)) return True equivalent_pairs = 0 for i, graph in enumerate(graphs): for j in range(i, len(graphs)): if equal(graph, graphs[j]): assert hashes[i] == hashes[j], (graph, graphs[j], hashes[i], hashes[j]) equivalent_pairs += 1 # Independent exact-number hash oracle. Keep these separate from the original # operation count and the finite-graph property checks in the report. MASK = (1 <<64) -1 OFFSET =14695981039346656037 PRIME =1099511628211 WORD_LIMIT =2048 def hash_word(state, value): for shift in range(0,64,8): state = ((state ^ ((value >>shift) &255)) *PRIME) &MASK return state def hash_component(state, value): magnitude =abs(value) words =(magnitude.bit_length() +31) //32 state =hash_word(hash_word(state, int(value <0)), words) for index in range(min(words,WORD_LIMIT)): state =hash_word(state, (magnitude >> (32 *index)) &0xffffffff) if words >WORD_LIMIT: state =hash_word(state,0x424947435554) return state def exact_hash(value): state =hash_word(OFFSET,0x4558414354) state =hash_component(state,value.numerator) state =hash_component(state,value.denominator) return state &0x7fffffffffffffff numeric =[] for value in [0,1,-1,(1 <<31)-1,1 <<31,(1 <<32)-1,1 <<32,(1 <<63)-1, -(1 <<63),1 <<63,-(1 <<63)-1,(1 <<64)-1,1 <<64, -(1 <<200)+17]: numeric.append((Fraction(value),str(value))) for bits in [65,97,130,200,511,1024,4097]: value =(1 <