interplane.Get Omega
Read in your language

omega-bridge

Omega-first language interoperability: checked conversion backends and native language bindings

Brought by drakestapleton

branch main Branches Tags History

omega-bridge / tests / native / v2 / span_compare.py

57 lines · 2297 bytes Raw Blame History
1#!/usr/bin/env python3
2# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3"""Compare historical generated C, admitting only counted span zero-init insertions."""
4from pathlib import Path
5import sys
6
7OLD = b'ombr_span sp[7];'
8NEW = b'ombr_span sp[7] = {{0}};'
9
10def compare(old, new, expected):
11 if old.count(OLD) != expected or new.count(NEW) != expected:
12 raise ValueError('span declaration count differs from the explicit fixture count')
13 i = j = count = 0
14 while i < len(old) and j < len(new):
15 if old.startswith(OLD, i) and new.startswith(NEW, j):
16 i += len(OLD)
17 j += len(NEW)
18 count += 1
19 elif old[i] == new[j]:
20 i += 1
21 j += 1
22 else:
23 raise ValueError(f'non-span byte difference at historical/current offsets {i}/{j}')
24 if i != len(old) or j != len(new) or count != expected:
25 raise ValueError('length or replacement count differs')
26 return count
27
28def self_test():
29 old = b'void wrapper(void) { ' + OLD + b' unsigned ns = 0; }\n'
30 new = old.replace(OLD, NEW)
31 assert compare(old, new, 1) == 1
32 assert compare(old + old, new + new, 2) == 2
33 controls = [
34 (old, new.replace(b'ns = 0', b'ns = 1'), 1), # One other byte in the same wrapper.
35 (old, old, 1), # Missing normalization.
36 (old + old, new + new, 1), # Stray normalization.
37 (old, new.replace(b' = {{0}};', b'={{0}};'), 1),
38 (old.replace(b'[7]', b'[8]'), new.replace(b'[7]', b'[8]'), 1),
39 ]
40 for before, after, count in controls:
41 try:
42 compare(before, after, count)
43 except ValueError:
44 continue
45 raise AssertionError('historical span comparison accepted a forbidden change')
46 print('SPAN-COMPARE PASS (counts 1/2; five refusal controls, including other-byte mutation)')
47
48if __name__ == '__main__':
49 if sys.argv[1:] == ['--self-test']:
50 self_test()
51 else:
52 try:
53 count = compare(Path(sys.argv[1]).read_bytes(), Path(sys.argv[2]).read_bytes(), int(sys.argv[3]))
54 except ValueError as e:
55 print(f'SPAN-COMPARE FAIL {e}', file=sys.stderr)
56 sys.exit(1)
57 print(f'SPAN-COMPARE PASS normalized={count}')