#!/usr/bin/env python3 """Check the public Deque fixture's byte vectors against Python's deque oracle.""" from collections import deque from pathlib import Path import re FIXTURE = Path(__file__).resolve().parents[1] / "tests/conformance/source/deque/model.aji" SEEDS = (3, 19, 42, 91) def vectors(operations, values, indices): queue = deque() capacity = 3 result = [list(operations), list(values), list(indices), [], [], [], []] for operation, value, index in zip(operations, values, indices): assert operation < 6 and value in range(1, 64) and index < 9 failed = 0 returned = 0 if operation in (0, 1): if len(queue) == capacity: failed = 1 elif operation == 0: queue.append(value) else: queue.appendleft(value) elif operation in (2, 3): if not queue: failed = 1 elif operation == 2: returned = queue.popleft() else: returned = queue.pop() elif operation == 4: capacity = max(capacity, index) else: if index >= len(queue): failed = 1 else: returned = queue[index] queue[index] = value result[3].append(failed) result[4].append(returned) result[5].append(len(queue)) result[6].extend(list(queue) + [0] * (8 - len(queue))) return [bytes(values) for values in result] def main(): calls = re.findall(r' trace\(heap, (.*?)\);', FIXTURE.read_text()) assert len(calls) == len(SEEDS), len(calls) for seed, call in zip(SEEDS, calls): strings = re.findall(r'ref b"((?:\\x[0-9a-f]{2})*)"', call) actual = [bytes.fromhex(value.replace("\\x", "")) for value in strings] assert len(actual) == 7 and all(len(row) == 32 for row in actual[:6]), seed assert actual == vectors(*actual[:3]), f"fixture disagrees with Python deque for seed {seed}" print(f"deque oracle vectors passed ({len(SEEDS)} seeds, {len(SEEDS) * 32} operations)") if __name__ == "__main__": main()