From the 101 lk/man/pth code-gen sources: 63 tasks (C11 36, C++17 27, 11 canonical families), 38 skipped for scope or duplication. Each task has at least one independent contract-edge defect, boundary types rotate, and public docs state trap clauses as plain contract prose. Claude reviewed 63/63 USABLE against current digests. Difficulty is unmeasured. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
454 lines
19 KiB
Python
454 lines
19 KiB
Python
import copy
|
||
import json
|
||
from pathlib import Path
|
||
import random
|
||
import re
|
||
import subprocess
|
||
import sys
|
||
import tempfile
|
||
|
||
MIN = -(1 << 63)
|
||
MAX = (1 << 63) - 1
|
||
N = 1000000000
|
||
FILES = ['capture.hpp', 'timestamp_math.cpp', 'payload_codec.cpp',
|
||
'capture_book.cpp', 'capture_console.cpp']
|
||
OK, INVALID, RANGE, MISSING, CAPACITY = range(5)
|
||
NAMES = ['OK', 'INVALID', 'RANGE', 'MISSING', 'CAPACITY']
|
||
|
||
|
||
def truncate(value, divisor):
|
||
return (-1 if value < 0 else 1) * (abs(value) // divisor)
|
||
|
||
|
||
def normalize(sec, ns):
|
||
sec, ns = divmod(sec * N + ns, N)
|
||
return (OK, sec, ns) if MIN <= sec <= MAX else (RANGE, 0, 0)
|
||
|
||
|
||
def parse_num(text):
|
||
if re.fullmatch(rb'[+-]?[0-9]+', text) is None:
|
||
return INVALID, 0
|
||
stripped = text.lstrip(b'+-').lstrip(b'0') or b'0'
|
||
if len(stripped) > 19:
|
||
return RANGE, 0
|
||
value = int(stripped) * (-1 if text.startswith(b'-') else 1)
|
||
return (OK, value) if MIN <= value <= MAX else (RANGE, 0)
|
||
|
||
|
||
def decode(text):
|
||
if text == b'-':
|
||
return OK, b''
|
||
if not text or len(text) % 2 or re.fullmatch(rb'[0-9a-fA-F]+', text) is None:
|
||
return INVALID, b''
|
||
if len(text) > 512:
|
||
return CAPACITY, b''
|
||
return OK, bytes.fromhex(text.decode('ascii'))
|
||
|
||
|
||
def digest(data):
|
||
result = 2166136261
|
||
for byte in data:
|
||
result = ((result ^ byte) * 16777619) & 0xffffffff
|
||
return result
|
||
|
||
|
||
def empty_entry():
|
||
return [0, 0, 0, 0, '']
|
||
|
||
|
||
def empty_book():
|
||
return [0, [empty_entry() for _ in range(8)]]
|
||
|
||
|
||
def entry_json(entry):
|
||
_, sec, ns, sequence, payload = entry
|
||
data = bytes.fromhex(payload)
|
||
return dict(sec=sec, nsec=ns, sequence=sequence, length=len(data),
|
||
hex=payload or '-', hash=digest(data))
|
||
|
||
|
||
def capture(books, b, ch, sec, ns, data, nil=False):
|
||
if nil or not 0 <= ch < 8:
|
||
return INVALID
|
||
if len(data) > 256:
|
||
return CAPACITY
|
||
status, whole, sub = normalize(sec, ns)
|
||
if status:
|
||
return status
|
||
if books[b][0] == 64:
|
||
return CAPACITY
|
||
books[b][0] += 1
|
||
books[b][1][ch] = [1, whole, sub, books[b][0], data.hex()]
|
||
return OK
|
||
|
||
|
||
def elapsed(a, b):
|
||
if not 0 <= a[1] < N or not 0 <= b[1] < N:
|
||
return INVALID, 0
|
||
result = truncate((a[0] - b[0]) * N + a[1] - b[1], 1000)
|
||
return (OK, result) if MIN <= result <= MAX else (RANGE, 0)
|
||
|
||
|
||
def token(data):
|
||
return data.hex() or '-'
|
||
|
||
|
||
def raw_cases():
|
||
lines, wanted = [], []
|
||
|
||
def add(line, expected):
|
||
lines.append(line)
|
||
wanted.append(copy.deepcopy(expected))
|
||
|
||
times = [(0, 0), (0, -1), (-1, N), (MIN, 0), (MAX, N-1),
|
||
(MIN, -1), (MAX, N), (MAX, MIN), (MIN, MAX),
|
||
(0, MIN), (0, MAX), (-99, -2000000001)]
|
||
rng = random.Random(228127)
|
||
times += [(rng.randint(-100000000000, 100000000000), rng.randint(MIN, MAX))
|
||
for _ in range(24)]
|
||
for sec, ns in times:
|
||
for initial in [(-71, -17), (MAX, 987654321)]:
|
||
for nil in (0, 1):
|
||
status, a, b = normalize(sec, ns)
|
||
if nil:
|
||
status = INVALID
|
||
add(f'N {sec} {ns} {initial[0]} {initial[1]} {nil}',
|
||
[status, a if status == OK else initial[0], b if status == OK else initial[1]])
|
||
|
||
pairs = [((0, 1000), (0, 999)), ((0, 999), (0, 1000)),
|
||
((0, 1), (-1, 999999999)), ((-1, 999999999), (0, 1)),
|
||
((9007199254740992, 9001), (9007199254740992, 8999)),
|
||
((MAX, N-1), (MAX, N-2)), ((MIN, 0), (MIN, 999)),
|
||
((MAX, 0), (MIN, 0)), ((0, -1), (0, 0)),
|
||
((0, N), (0, 0)), ((-3, 0), (2, 0)), ((0, 0), (0, 0))]
|
||
for result in (MAX, MIN):
|
||
for offset in (-1000, -999, 0, 999, 1000):
|
||
sec, ns = divmod(result * 1000 + offset, N)
|
||
pairs.append(((sec, ns), (0, 0)))
|
||
pairs += [((rng.randint(MIN, MAX), rng.randrange(N)),
|
||
(rng.randint(MIN, MAX), rng.randrange(N))) for _ in range(20)]
|
||
for a, b in pairs:
|
||
for init in (-93, 17):
|
||
for nil in (0, 1):
|
||
status, result = elapsed(a, b)
|
||
if nil:
|
||
status = INVALID
|
||
add(f'D {a[0]} {a[1]} {b[0]} {b[1]} {init} {nil}',
|
||
[status, result if status == OK else init, *a, *b])
|
||
valid = 0 <= a[1] < N and 0 <= b[1] < N and not nil
|
||
comparison = (a > b) - (a < b)
|
||
add(f'C {a[0]} {a[1]} {b[0]} {b[1]} {init} {nil}',
|
||
[OK if valid else INVALID, comparison if valid else init, *a, *b])
|
||
|
||
text_values = [b'', b'+', b'-', b'0', b'-0', b'+001', str(MIN).encode(),
|
||
str(MAX).encode(), str(MAX+1).encode(), str(MIN-1).encode(),
|
||
b'0' * 6000 + b'42', b'-' + b'0' * 4100 + b'7',
|
||
b'9' * 30 + b'x', b'\x0b1', b'\x0c1', b' 1', b'1 ', b'1\0',
|
||
b'\xff', b'++1', b'1e3', b'0x01', '12'.encode()]
|
||
for text in text_values:
|
||
for initial in (-71, MAX):
|
||
for nil in (0, 1):
|
||
status, value = parse_num(text)
|
||
if nil:
|
||
status = INVALID
|
||
add(f'P {token(text)} {initial} {nil}',
|
||
[status, value if status == OK else initial, text.hex()])
|
||
|
||
hex_texts = [b'-', b'', b'00Ff', bytes(range(256)).hex().encode(),
|
||
b'00' * 257, b'00' * 256 + b'0g', b'0', b' 00', b'0\0', b'\xff\xff']
|
||
for text in hex_texts:
|
||
for initial in (b'', b'\0\xffseed'):
|
||
for nil in (0, 1):
|
||
status, value = decode(text)
|
||
if nil:
|
||
status = INVALID
|
||
add(f'H {token(text)} {token(initial)} {nil}',
|
||
[status, (value if status == OK else initial).hex(), text.hex()])
|
||
payloads = [b'', b'\0', b'\xff', bytes(range(256)), bytes(range(256)) + b'x']
|
||
payloads += [bytes(rng.randrange(256) for _ in range(size)) for size in (2, 17, 255)]
|
||
for data in payloads:
|
||
add(f'G {token(data)}', [digest(data), data.hex()])
|
||
for initial in (b'', b'\xffseed'):
|
||
for nil in (0, 1):
|
||
status = INVALID if nil else CAPACITY if len(data) > 256 else OK
|
||
result = (data.hex() or '-').encode() if status == OK else initial
|
||
add(f'E {token(data)} {token(initial)} {nil}', [status, result.hex(), data.hex()])
|
||
for status in (-19, 0, 1, 2, 3, 4, 5, 2147483647):
|
||
add(f'S {status}', [(NAMES[status] if 0 <= status < 5 else 'INVALID').encode().hex()])
|
||
|
||
entries = [[1, MIN, 0, 1, ''], [1, MAX, N-1, 64, bytes(range(256)).hex()],
|
||
[0, 0, 0, 1, ''], [1, 0, -1, 1, ''], [1, 0, N, 1, ''],
|
||
[1, 0, 0, 0, ''], [1, 0, 0, 65, ''], [1, 0, 0, 1, 'ff'*257]]
|
||
for entry in entries:
|
||
for initial in (b'', b'\xffold'):
|
||
for nil in (0, 1):
|
||
present, sec, ns, seq, hexed = entry
|
||
valid = present and 0 <= ns < N and 1 <= seq <= 64 and len(hexed) <= 512 and not nil
|
||
expected = entry_json(entry) if valid else initial.hex()
|
||
add(f'F {present} {sec} {ns} {seq} {hexed or "-"} {token(initial)} {nil}',
|
||
[OK if valid else INVALID, expected, entry])
|
||
|
||
books = [empty_book(), empty_book()]
|
||
|
||
def write(b, ch, sec, ns, data, nil=0):
|
||
status = capture(books, b, ch, sec, ns, data, nil)
|
||
add(f'W {b} {ch} {sec} {ns} {token(data)} {nil}', [status, data.hex(), books])
|
||
|
||
def read(b, ch, initial, book_nil=0, out_nil=0):
|
||
status = INVALID if book_nil or out_nil or ch >= 8 else MISSING if not books[b][1][ch][0] else OK
|
||
out = books[b][1][ch] if status == OK else initial
|
||
p, sec, ns, seq, hexed = initial
|
||
add(f'R {b} {ch} {p} {sec} {ns} {seq} {hexed or "-"} {book_nil} {out_nil}', [status, out, books])
|
||
|
||
def reset_or_clear(op, b, ch=0, nil=0):
|
||
status = INVALID if nil or (op == 'X' and ch >= 8) else OK
|
||
if status == OK:
|
||
if op == 'Z':
|
||
books[b] = empty_book()
|
||
else:
|
||
books[b][1][ch] = empty_entry()
|
||
add(f'{op} {b} {ch} {nil}', [status, books])
|
||
|
||
initials = [empty_entry(), [1, -887, 17, 63, '00ff']]
|
||
for initial in initials:
|
||
for bn, on in [(0, 0), (1, 0), (0, 1)]:
|
||
read(0, 0, initial, bn, on)
|
||
read(1, 8, initial, bn, on)
|
||
for ch in range(8):
|
||
write(0, ch, 9007199254740992 + ch, -1, bytes(range(256)) if ch == 7 else bytes([ch]))
|
||
write(1, 7-ch, -ch, N+1, b'')
|
||
for b in range(2):
|
||
for ch in range(8):
|
||
write(b, ch, -99-ch, ch*1001, b'\0\xff' + bytes([ch]))
|
||
read(b, ch, initials[1])
|
||
replacement = [0, 712345, 987654321, 0, 'fe00017f80']
|
||
for b, ch in ((0, 7), (1, 0)):
|
||
add(f'M {b} {ch} 0 712345 987654321 0 fe00017f80',
|
||
[OK, replacement, OK, books[b][1][ch], books])
|
||
for args in [(0, 8, 0, 0, b''), (0, 7, MAX, N, b''),
|
||
(0, 7, MAX, N, b'x'*257), (0, 7, 0, 0, b'x'*257)]:
|
||
write(*args)
|
||
write(0, 0, 1, 0, b'x', 1)
|
||
for initial in initials:
|
||
for bn, on in [(0, 0), (1, 0), (0, 1)]:
|
||
read(0, 0, initial, bn, on)
|
||
reset_or_clear('X', 0, 8)
|
||
reset_or_clear('X', 0, 0, 1)
|
||
reset_or_clear('Z', 1, nil=1)
|
||
reset_or_clear('X', 0, 0)
|
||
reset_or_clear('X', 0, 0)
|
||
books[1] = copy.deepcopy(books[0])
|
||
add('Q 0 1', [books])
|
||
write(0, 7, 71, 3, b'changed')
|
||
reset_or_clear('Z', 0)
|
||
for i in range(64):
|
||
write(0, i % 8, i, i, bytes([i]))
|
||
write(0, 0, 999, 0, b'last')
|
||
reset_or_clear('X', 0, 0)
|
||
write(0, 0, 999, 0, b'last')
|
||
write(0, 8, MAX, N, b'x'*257)
|
||
write(0, 0, MAX, N, b'x'*257)
|
||
write(0, 0, MAX, N, b'')
|
||
reset_or_clear('Z', 0)
|
||
reset_or_clear('Z', 0)
|
||
write(0, 0, MIN, 0, b'')
|
||
read(1, 7, initials[1])
|
||
return ('\n'.join(lines) + '\n').encode(), wanted, lines
|
||
|
||
|
||
def console_expected(data):
|
||
books = [empty_book(), empty_book()]
|
||
result = []
|
||
for line in data.split(b'\n'):
|
||
if len(line) > 4096:
|
||
result.append(dict(status='INVALID'))
|
||
continue
|
||
words = re.split(rb'[ \t\r]+', line.strip(b' \t\r')) if line.strip(b' \t\r') else []
|
||
if not words:
|
||
continue
|
||
status, extra = INVALID, {}
|
||
op = words[0]
|
||
if op == b'NORMALIZE' and len(words) == 3:
|
||
status, sec = parse_num(words[1])
|
||
if status == OK:
|
||
status, ns = parse_num(words[2])
|
||
if status == OK:
|
||
status, sec, ns = normalize(sec, ns)
|
||
if status == OK:
|
||
extra = dict(sec=sec, nsec=ns)
|
||
elif len(words) >= 2 and words[1] in (b'A', b'B'):
|
||
b = int(words[1] == b'B')
|
||
arity = {b'RESET': 2, b'WRITE': 6, b'READ': 3, b'CLEAR': 3, b'DELTA': 4}
|
||
if arity.get(op) == len(words):
|
||
if op == b'RESET':
|
||
books[b], status = empty_book(), OK
|
||
else:
|
||
status, ch = parse_num(words[2])
|
||
if status == OK and not 0 <= ch < 8:
|
||
status = INVALID
|
||
if status == OK and op == b'WRITE':
|
||
status, sec = parse_num(words[3])
|
||
if status == OK:
|
||
status, ns = parse_num(words[4])
|
||
if status == OK:
|
||
status, payload = decode(words[5])
|
||
if status == OK:
|
||
status = capture(books, b, ch, sec, ns, payload)
|
||
elif status == OK and op == b'CLEAR':
|
||
books[b][1][ch] = empty_entry()
|
||
elif status == OK and op == b'READ':
|
||
entry = books[b][1][ch]
|
||
status = OK if entry[0] else MISSING
|
||
if status == OK:
|
||
extra = dict(entry=entry_json(entry))
|
||
elif status == OK and op == b'DELTA':
|
||
status, earlier = parse_num(words[3])
|
||
if status == OK and not 0 <= earlier < 8:
|
||
status = INVALID
|
||
if status == OK:
|
||
a, c = books[b][1][ch], books[b][1][earlier]
|
||
status = OK if a[0] and c[0] else MISSING
|
||
if status == OK:
|
||
status, value = elapsed(a[1:3], c[1:3])
|
||
if status == OK:
|
||
extra = dict(microseconds=value)
|
||
result.append(dict(status=NAMES[status], **extra))
|
||
return result
|
||
|
||
|
||
def console_cases():
|
||
streams = [b'', b'\n\t \r\n', b'WRITE A 0 40 123000 a0\nREAD A 0\nWRITE A 0 41 456000 00ff\nREAD A 0\nWRITE B 7 -2 -1 -\nREAD B 7\n']
|
||
lines = ['READ A 0', 'READ B 7', 'DELTA A 0 9', 'WRITE A 0 0 999 -',
|
||
'WRITE A 1 0 1000 00ff', 'DELTA A 1 0', 'DELTA A 0 1',
|
||
'WRITE B 0 -1 999999999 -', 'WRITE B 1 0 1 -',
|
||
'DELTA B 1 0', 'DELTA B 0 1', 'DELTA A 1 1']
|
||
lines += [f'WRITE A {i} {9007199254740992+i} {i-4} {bytes(range(256)).hex()}' for i in range(8)]
|
||
lines += [f'READ A {i}' for i in range(8)]
|
||
lines += ['WRITE A 7 1 0 -', 'READ A 7', 'READ B 1', 'CLEAR A 7', 'READ A 7', 'CLEAR A 7']
|
||
lines += [f'NORMALIZE {s} {n}' for s, n in [(MIN, -1), (MAX, N), (MIN, MAX), (MAX, MIN), (0, MIN), (0, MAX)]]
|
||
lines += ['BOGUS A', 'READ C 0', 'READ A', 'READ A 0 extra', 'RESET A extra',
|
||
'WRITE A -1 0 0 -', 'WRITE A 8 0 0 -', 'READ A 9223372036854775808',
|
||
'WRITE A 0 + 0 -', 'WRITE A 0 0 0 zz', 'WRITE A 0 0 0 ' + '00'*257,
|
||
'WRITE A 0 9223372036854775807 1000000000 -',
|
||
'WRITE A 0 0 0 0', 'NORMALIZE 99999999999999999999999999x 0',
|
||
'READ A 0', 'NORMALIZE +'+'0'*4000+'7 -'+'0'*15+'1',
|
||
'NORMALIZE 0 0' + ' '*4083, 'READ A 0'+' '*4089,
|
||
'READ A 0'+' '*4090, 'NORMALIZE 0 0'+' '*4096,
|
||
'RESET A', 'READ A 0']
|
||
lines += [f'WRITE A {i%8} {-i} {i-17} 00' for i in range(64)]
|
||
lines += ['WRITE A 0 1 0 -', 'CLEAR A 0', 'WRITE A 0 1 0 -', 'READ A 0',
|
||
'WRITE A 0 9223372036854775807 1000000000 -', 'RESET A',
|
||
'WRITE A 0 1 0 -', 'READ A 0', 'READ B 0', 'RESET B', 'READ B 0']
|
||
streams.append(('\n'.join(lines)).encode())
|
||
streams.append(b'NORMALIZE \x0b1 0\nNORMALIZE \x0c1 0\nWRITE A 0 1 0 00\nNORMALIZE 1\x00 0\nREAD A 0\n\xff A\n')
|
||
rng = random.Random(271022)
|
||
generated = []
|
||
for i in range(120):
|
||
b = rng.choice(['A', 'B'])
|
||
ch = rng.randrange(8)
|
||
if i % 17 == 0:
|
||
generated.append(f'RESET {b}')
|
||
elif i % 5 == 0:
|
||
generated.append(f'CLEAR {b} {ch}')
|
||
elif i % 3 == 0:
|
||
generated.append(f'DELTA {b} {ch} {rng.randrange(8)}')
|
||
else:
|
||
payload = bytes(rng.randrange(256) for _ in range(rng.randrange(257)))
|
||
generated.append(f'WRITE {b} {ch} {rng.randrange(-10**16,10**16)} {rng.randrange(MIN,MAX)} {token(payload)}')
|
||
generated.append(f'READ {b} {ch}')
|
||
streams.append(('\n'.join(generated)+'\n').encode())
|
||
return streams
|
||
|
||
|
||
REQUIRED = {'status', 'sec', 'nsec', 'sequence', 'length', 'hash', 'hex', 'entry', 'microseconds'}
|
||
|
||
|
||
def pairs(items):
|
||
result = {}
|
||
for key, value in items:
|
||
if key in REQUIRED and key in result:
|
||
raise ValueError('duplicate required JSON key')
|
||
result[key] = value
|
||
return result
|
||
|
||
|
||
def json_stream(data):
|
||
text = data.decode('utf-8')
|
||
decoder = json.JSONDecoder(object_pairs_hook=pairs, parse_constant=lambda value: (_ for _ in ()).throw(ValueError(value)))
|
||
values, pos = [], 0
|
||
while pos < len(text):
|
||
start = pos
|
||
while pos < len(text) and text[pos] in ' \t\r\n':
|
||
pos += 1
|
||
if pos == len(text):
|
||
break
|
||
if values and pos == start:
|
||
raise ValueError('JSON objects require separating whitespace')
|
||
value, pos = decoder.raw_decode(text, pos)
|
||
values.append(value)
|
||
return values
|
||
|
||
|
||
def matches(got, wanted):
|
||
if type(got) is not type(wanted):
|
||
return False
|
||
if isinstance(wanted, dict):
|
||
return all(key in got and matches(got[key], value) for key, value in wanted.items())
|
||
if isinstance(wanted, list):
|
||
return len(got) == len(wanted) and all(matches(a, b) for a, b in zip(got, wanted))
|
||
return got == wanted
|
||
|
||
|
||
def child(argv, cwd, data=None, timeout=30):
|
||
result = subprocess.run(argv, cwd=cwd, input=data, capture_output=True, timeout=timeout)
|
||
if result.returncode:
|
||
print(result.stderr.decode('utf-8', 'replace')[-2000:], file=sys.stderr)
|
||
raise ValueError('candidate build or execution failed')
|
||
return result.stdout
|
||
|
||
|
||
def main():
|
||
candidate = Path(sys.argv[1])
|
||
contents = {name: (candidate / name).read_bytes() for name in FILES}
|
||
observer = (Path(__file__).parent / 'observer.cc').read_bytes()
|
||
raw_input, raw_wanted, labels = raw_cases()
|
||
streams = console_cases()
|
||
wanted_streams = [console_expected(data) for data in streams]
|
||
scratch = Path(tempfile.mkdtemp(prefix='timestamp-capture-', dir=Path.cwd()))
|
||
for name, data in contents.items():
|
||
(scratch / name).write_bytes(data)
|
||
(scratch / name).chmod(0o644)
|
||
(scratch / 'observer.cc').write_bytes(observer)
|
||
(scratch / 'observer.cc').chmod(0o644)
|
||
# All candidate reads and scratch writes precede the first child.
|
||
sources = [name for name in FILES if name.endswith('.cpp')]
|
||
child(['g++', '-std=c++17', '-O2', *sources, '-o', 'console'], scratch)
|
||
child(['g++', '-std=c++17', '-O2', '-Dmain=candidate_application_main',
|
||
*sources, 'observer.cc', '-o', 'observe'], scratch)
|
||
observed = json_stream(child([str(scratch / 'observe')], scratch, raw_input))
|
||
if len(observed) != len(raw_wanted):
|
||
raise ValueError('raw observation count mismatch')
|
||
for index, (got, wanted) in enumerate(zip(observed, raw_wanted)):
|
||
if labels[index].startswith('F ') and wanted[0] == OK:
|
||
got = list(got)
|
||
parsed = json_stream(bytes.fromhex(got[1]))
|
||
got[1] = parsed[0] if len(parsed) == 1 else None
|
||
if not matches(got, wanted):
|
||
raise ValueError('raw API mismatch at ' + labels[index][:160])
|
||
for index, (data, wanted) in enumerate(zip(streams, wanted_streams)):
|
||
output = child([str(scratch / 'console')], scratch, data)
|
||
got = json_stream(output)
|
||
if not matches(got, wanted):
|
||
raise ValueError('console semantic mismatch in stream ' + str(index))
|
||
if data == b'' and output != b'':
|
||
raise ValueError('empty stdin produced output bytes')
|
||
print('PASS')
|
||
|
||
|
||
if __name__ == '__main__':
|
||
try:
|
||
main()
|
||
except (ValueError, OSError, KeyError, IndexError, TypeError, subprocess.SubprocessError) as error:
|
||
print('FAIL: ' + str(error))
|
||
sys.exit(1)
|