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>
303 lines
20 KiB
Python
303 lines
20 KiB
Python
HARNESS = '#include "activation.hpp"\n#include <array>\n#include <iostream>\n#include <limits>\n#include <sstream>\n#include <string>\n#include <vector>\nnamespace {\nusing activation::Command;\nusing activation::Op;\nbool integer(const std::string& text, int& value)\n{\n if (text.empty()) {\n return false;\n }\n std::size_t cursor = 0;\n if (text[0] == \'-\' || text[0] == \'+\') {\n cursor = 1;\n }\n if (cursor == text.size()) {\n return false;\n }\n for (std::size_t index = cursor; index < text.size(); ++index) {\n if (text[index] < \'0\' || text[index] > \'9\') {\n return false;\n }\n }\n try {\n const long long parsed = std::stoll(text);\n if (parsed < std::numeric_limits<int>::min() ||\n parsed > std::numeric_limits<int>::max()) {\n return false;\n }\n value = static_cast<int>(parsed);\n return true;\n } catch (...) {\n return false;\n }\n}\nCommand parse(const std::vector<std::string>& words)\n{\n Command command;\n struct Entry {\n const char* word;\n Op op;\n int arguments;\n };\n const std::array<Entry, 11> entries = {{\n {"open", Op::open, 2},\n {"close", Op::close, 1},\n {"reserve", Op::reserve, 2},\n {"gate", Op::gate, 2},\n {"ungate", Op::ungate, 2},\n {"release", Op::release, 2},\n {"activate", Op::activate, 2},\n {"deactivate", Op::deactivate, 2},\n {"arm-p", Op::arm_p, 2},\n {"arm-e", Op::arm_e, 2},\n {"clear", Op::clear, 1}\n }};\n for (const Entry& entry : entries) {\n if (words.front() != entry.word) {\n continue;\n }\n if (words.size() != static_cast<std::size_t>(entry.arguments + 1)) {\n return command;\n }\n int first = 0;\n int second = 0;\n if (!integer(words[1], first)) {\n return command;\n }\n if (entry.arguments == 2 && !integer(words[2], second)) {\n return command;\n }\n command.op = entry.op;\n command.a = first;\n command.b = second;\n return command;\n }\n return command;\n}\nvoid print(const activation::Observation& observation)\n{\n std::cout << "{\\"rc\\":" << observation.rc;\n std::cout << ",\\"token\\":" << observation.token;\n std::cout << ",\\"providers\\":[";\n bool comma = false;\n for (const auto& view : observation.providers) {\n if (comma) {\n std::cout << \',\';\n }\n comma = true;\n std::cout << \'[\' << view.handles << \',\' << view.prepared << \',\';\n std::cout << view.enabled << \',\' << view.prepare_on << \',\';\n std::cout << view.prepare_off << \',\' << view.enable_on << \',\';\n std::cout << view.enable_off << \',\' << view.prepare_calls << \',\';\n std::cout << view.enable_calls << \',\' << view.pending_prepare << \',\';\n std::cout << view.pending_enable << \']\';\n }\n std::cout << "],\\"handles\\":[";\n comma = false;\n for (const auto& view : observation.handles) {\n if (comma) {\n std::cout << \',\';\n }\n comma = true;\n std::cout << \'[\' << view.token << \',\' << view.client << \',\';\n std::cout << view.provider << \',\' << view.prepared << \',\';\n std::cout << view.enabled << \']\';\n }\n std::cout << "],\\"events\\":[";\n comma = false;\n for (const auto& event : observation.events) {\n if (comma) {\n std::cout << \',\';\n }\n comma = true;\n std::cout << \'[\' << event.kind << \',\' << event.provider << \',\';\n std::cout << event.token << \']\';\n }\n std::cout << "]}\\n";\n}\n}\nint main(int argc, char** argv)\n{\n activation::Engine engine;\n if (argc == 2 && std::string(argv[1]) == "--api-probe") {\n print(engine.observe());\n activation::Engine other;\n const std::array<activation::Command, 7> commands = {{\n {activation::Op::open, 0, 0},\n {activation::Op::reserve, 1, 2},\n {activation::Op::invalid, 1, 99},\n {activation::Op::close, 1, 999},\n {activation::Op::release, 1, 2},\n {activation::Op::close, 1, -999},\n {activation::Op::clear, 0, -999}\n }};\n for (const auto& command : commands) {\n const int returned = engine.execute(command);\n std::cout << returned << \'\\n\';\n print(engine.observe());\n if (command.op == activation::Op::reserve) {\n auto populated = engine.observe();\n if (!populated.providers.empty()) {\n populated.providers[0].prepared = 999;\n }\n if (!populated.handles.empty()) {\n populated.handles[0].prepared = 999;\n }\n populated.events.clear();\n print(engine.observe());\n }\n }\n auto detached = engine.observe();\n detached.rc = 99;\n detached.token = 77;\n if (!detached.providers.empty()) {\n detached.providers[0].prepared = 999;\n }\n detached.handles.clear();\n detached.events.clear();\n print(engine.observe());\n print(engine.observe());\n print(other.observe());\n const int other_result = other.execute({activation::Op::open, 7, 3});\n std::cout << other_result << \'\\n\';\n print(other.observe());\n print(engine.observe());\n return 0;\n }\n std::string line;\n while (std::getline(std::cin, line)) {\n std::istringstream stream(line);\n std::vector<std::string> words;\n std::string word;\n while (stream >> word) {\n words.push_back(word);\n }\n if (words.empty()) {\n continue;\n }\n const int returned = engine.execute(parse(words));\n std::cout << returned << \'\\n\';\n print(engine.observe());\n }\n return 0;\n}\n'
|
|
|
|
import copy
|
|
import json
|
|
from pathlib import Path
|
|
import random
|
|
import re
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
|
|
NAMES = ('activation.hpp', 'provider.cpp', 'handles.cpp', 'transaction.cpp')
|
|
ARITY = {'open': 2, 'close': 1, 'reserve': 2, 'gate': 2, 'ungate': 2,
|
|
'release': 2, 'activate': 2, 'deactivate': 2, 'arm-p': 2, 'arm-e': 2, 'clear': 1}
|
|
|
|
class Model:
|
|
def __init__(self):
|
|
self.providers = [[0] * 11 for _ in range(4)]
|
|
self.handles = {}
|
|
self.next_token = 1
|
|
self.rc = self.token = 0
|
|
self.events = []
|
|
|
|
def observe(self):
|
|
return copy.deepcopy(dict(rc=self.rc, token=self.token,
|
|
providers=self.providers,
|
|
handles=[self.handles[key] for key in sorted(self.handles)],
|
|
events=self.events))
|
|
|
|
def primitive(self, handle, kind):
|
|
provider = self.providers[handle[2]]
|
|
if kind in (1, 3):
|
|
counter, pending, total, owned, edge = ((7, 9, 1, 3, 3) if kind == 1 else (8, 10, 2, 4, 5))
|
|
provider[counter] += 1
|
|
if provider[pending]:
|
|
provider[pending] -= 1
|
|
if provider[pending] == 0:
|
|
self.events.append([kind + 1, handle[2], handle[0]])
|
|
return False
|
|
if provider[total] == 0:
|
|
provider[edge] += 1
|
|
provider[total] += 1
|
|
handle[owned] += 1
|
|
else:
|
|
total, owned, edge = ((2, 4, 6) if kind == 5 else (1, 3, 4))
|
|
provider[total] -= 1
|
|
handle[owned] -= 1
|
|
if provider[total] == 0:
|
|
provider[edge] += 1
|
|
self.events.append([kind, handle[2], handle[0]])
|
|
return True
|
|
|
|
def execute(self, line):
|
|
words = line.split()
|
|
if not words:
|
|
return None
|
|
self.events = []
|
|
self.token = 0
|
|
op = words[0]
|
|
self.rc = -1
|
|
if op not in ARITY or len(words) != ARITY[op] + 1:
|
|
return self.observe()
|
|
if any(re.fullmatch(r'[+-]?[0-9]+', word) is None for word in words[1:]):
|
|
return self.observe()
|
|
args = [int(word) for word in words[1:]]
|
|
if any(number < -(2**31) or number >= 2**31 for number in args):
|
|
return self.observe()
|
|
a, b = (args + [0])[:2]
|
|
if op == 'open':
|
|
if not 0 <= a <= 7 or not 0 <= b <= 3:
|
|
return self.observe()
|
|
if len(self.handles) == 8:
|
|
self.rc = -3
|
|
return self.observe()
|
|
self.token = self.next_token
|
|
self.next_token += 1
|
|
self.handles[self.token] = [self.token, a, b, 0, 0]
|
|
self.providers[b][0] += 1
|
|
self.rc = 0
|
|
return self.observe()
|
|
if op in ('arm-p', 'arm-e', 'clear'):
|
|
if not 0 <= a <= 3 or op != 'clear' and not 1 <= b <= 16:
|
|
return self.observe()
|
|
provider = self.providers[a]
|
|
if op == 'clear':
|
|
provider[9:11] = [0, 0]
|
|
else:
|
|
provider[9 if op == 'arm-p' else 10] = b
|
|
self.rc = 0
|
|
return self.observe()
|
|
handle = self.handles.get(a)
|
|
if handle is None:
|
|
self.rc = -2
|
|
return self.observe()
|
|
if op == 'close':
|
|
if handle[3] or handle[4]:
|
|
self.rc = -5
|
|
else:
|
|
self.providers[handle[2]][0] -= 1
|
|
del self.handles[a]
|
|
self.rc = 0
|
|
return self.observe()
|
|
if not 0 <= b <= 16:
|
|
return self.observe()
|
|
p, e = handle[3:5]
|
|
allowed = {'reserve': p+b <= 16, 'activate': p+b <= 16,
|
|
'gate': p-e >= b, 'release': p-e >= b,
|
|
'ungate': e >= b, 'deactivate': e >= b}[op]
|
|
if not allowed:
|
|
self.rc = -4
|
|
return self.observe()
|
|
self.rc = 0
|
|
if op in ('reserve', 'gate', 'activate'):
|
|
layers = {'reserve': [1], 'gate': [3], 'activate': [1, 3]}[op]
|
|
journal = []
|
|
for kind in layers:
|
|
for _ in range(b):
|
|
if self.primitive(handle, kind):
|
|
journal.append(6 if kind == 1 else 5)
|
|
else:
|
|
for undo in reversed(journal):
|
|
self.primitive(handle, undo)
|
|
self.rc = -7
|
|
return self.observe()
|
|
else:
|
|
layers = {'ungate': [5], 'release': [6], 'deactivate': [5, 6]}[op]
|
|
for kind in layers:
|
|
for _ in range(b):
|
|
self.primitive(handle, kind)
|
|
return self.observe()
|
|
|
|
|
|
def cases():
|
|
yield 'empty', []
|
|
yield 'smoke', ['open 0 0', 'activate 1 2', 'deactivate 1 2', 'close 1']
|
|
yield 'ownership', ['open 0 0', 'open 1 0', 'reserve 1 4', 'arm-e 0 2',
|
|
'gate 2 1', 'reserve 2 2', 'gate 2 2', 'gate 1 3',
|
|
'gate 1 3', 'ungate 1 2', 'gate 1 1', 'release 1 2',
|
|
'ungate 2 1', 'release 2 1', 'close 2']
|
|
yield 'ordinary', ['open 7 3', 'open 7 3', 'open 0 1', 'reserve 1 4',
|
|
'gate 1 2', 'reserve 2 1', 'gate 2 1', 'ungate 1 1',
|
|
'release 1 1', 'deactivate 2 1', 'close 2', 'activate 3 3',
|
|
'deactivate 1 1', 'release 1 1', 'close 1', 'deactivate 3 3', 'close 3']
|
|
yield 'slots', ([f'open {i % 8} {i % 4}' for i in range(8)] +
|
|
['open 0 0', 'open -1 0', 'open 0 4', 'close 4', 'open 0 0',
|
|
'close 4', 'close 9', 'open 7 3', 'close -1', 'close 0'])
|
|
ops = ['reserve', 'gate', 'ungate', 'release', 'activate', 'deactivate']
|
|
yield 'validation', (['arm-p -1 1', 'arm-e 4 1', 'arm-p 0 0', 'arm-e 0 17',
|
|
'clear -1', 'open -1 0', 'open 8 0', 'open 0 -1', 'open 0 4',
|
|
'open 0 0', 'arm-p 0 16', 'arm-e 0 16'] +
|
|
[f'{op} {token} {n}' for op in ops for token,n in
|
|
[(1,0),(1,-1),(1,17),(99,-1),(0,0)]] +
|
|
['arm-p 0 -1', 'arm-e 0 -1', 'clear 4', 'clear 0', 'close 1'])
|
|
yield 'capacity', ['open 0 0', 'reserve 1 16', 'reserve 1 1', 'activate 1 1',
|
|
'gate 1 16', 'gate 1 1', 'release 1 1', 'close 1',
|
|
'ungate 1 16', 'release 1 16', 'activate 1 16',
|
|
'deactivate 1 17', 'deactivate 1 16', 'close 1', 'open 0 0']
|
|
for stage in ('p', 'e'):
|
|
for nth in (1, 2, 4, 16):
|
|
n = 16 if nth == 16 else 4
|
|
baseline = [] if n == 16 else ['reserve 1 3', 'gate 1 1']
|
|
yield f'activate-{stage}-{nth}', (['open 0 0', 'open 1 0', 'open 2 2',
|
|
'activate 2 2', 'reserve 3 2'] + baseline +
|
|
[f'arm-{stage} 0 {nth}', f'activate 1 {n}',
|
|
f'activate 1 {n}', 'gate 3 2', 'clear 0',
|
|
'deactivate 2 2', 'close 2'])
|
|
for nth in (1, 3, 16):
|
|
n = 16 if nth == 16 else 4
|
|
yield f'reserve-failure-{nth}', ['open 0 1', 'open 1 1', 'reserve 2 2',
|
|
f'arm-p 1 {nth}', f'reserve 1 {n}',
|
|
f'reserve 1 {n}', f'release 1 {n}', 'clear 1']
|
|
yield f'gate-failure-{nth}', ['open 0 1', 'reserve 1 16', f'arm-e 1 {nth}',
|
|
f'gate 1 {n}', f'gate 1 {n}', f'ungate 1 {n}',
|
|
'release 1 16', 'close 1']
|
|
yield 'preserve-own-shares', ['open 3 0', 'open 4 0', 'activate 2 2',
|
|
'reserve 1 4', 'gate 1 1', 'arm-p 0 3',
|
|
'reserve 1 4', 'arm-e 0 2', 'gate 1 3',
|
|
'gate 1 3', 'ungate 1 4', 'release 1 4',
|
|
'deactivate 2 2', 'close 1', 'close 2']
|
|
yield 'same-client-ownership', ['open 5 2', 'open 5 2', 'reserve 1 3',
|
|
'arm-e 2 1', 'gate 2 1', 'gate 1 2',
|
|
'gate 1 2', 'ungate 1 2', 'release 1 3',
|
|
'close 1', 'close 2']
|
|
yield 'maximum-session', [line for token in range(1,301)
|
|
for line in ('open 0 0', f'close {token}')]
|
|
yield 'pending-and-reuse', ['arm-p 3 4', 'arm-e 3 2', 'open 0 3', 'reserve 1 2',
|
|
'arm-p 3 3', 'arm-e 3 1', 'gate 1 1', 'gate 1 1',
|
|
'ungate 1 1', 'release 1 2', 'close 1', 'open 1 3',
|
|
'activate 2 2', 'activate 2 2', 'clear 3', 'activate 2 1',
|
|
'open 2 0', 'arm-p 0 1', 'reserve 3 1', 'reserve 2 0',
|
|
'deactivate 2 1', 'close 2']
|
|
yield 'syntax', [' ', '\t', 'wrong', 'open', 'open 0 0 extra', 'OPEN 0 0',
|
|
'open +0000 -0', 'reserve 1 +0002', 'gate 1 1x', 'gate 1 +',
|
|
'gate 1 --1', 'gate 1 2147483648', 'gate 1 -2147483649',
|
|
'close 1 0', 'clear', 'close -2147483648', 'gate 1 +01',
|
|
'ungate 1 1', 'release 1 2', 'close 1']
|
|
for seed in (71823, 912991, 20261027):
|
|
rng = random.Random(seed)
|
|
model = Model()
|
|
lines = []
|
|
for index in range(250):
|
|
if index < 6 or not model.handles:
|
|
line = f'open {rng.randrange(8)} {rng.randrange(4)}'
|
|
elif index % 23 == 0:
|
|
line = f'arm-{rng.choice(("p", "e"))} {rng.randrange(4)} {rng.choice((1,2,3,16))}'
|
|
elif index % 31 == 0:
|
|
line = f'clear {rng.randrange(4)}'
|
|
else:
|
|
token = rng.choice(list(model.handles))
|
|
h = model.handles[token]
|
|
op = rng.choice(ops + ['close', 'open'])
|
|
if op == 'close':
|
|
line = f'close {token}'
|
|
elif op == 'open':
|
|
line = f'open {rng.randrange(8)} {rng.randrange(4)}'
|
|
else:
|
|
choices = [0, 1, 2, 16, -1, 17]
|
|
if op in ('gate', 'release'): choices += [h[3]-h[4]] * 3
|
|
elif op in ('deactivate', 'ungate'): choices += [h[4]] * 3
|
|
else: choices += [16-h[3]] * 3
|
|
line = f'{op} {token} {rng.choice(choices)}'
|
|
lines.append(line)
|
|
model.execute(line)
|
|
yield f'seed-{seed}', lines
|
|
|
|
|
|
def equal(actual, expected):
|
|
if type(actual) is not type(expected):
|
|
return False
|
|
if isinstance(expected, dict):
|
|
return actual.keys() == expected.keys() and all(equal(actual[k], v) for k,v in expected.items())
|
|
if isinstance(expected, list):
|
|
return len(actual) == len(expected) and all(equal(a,b) for a,b in zip(actual,expected))
|
|
return actual == expected
|
|
|
|
|
|
def observations(data):
|
|
return [json.loads(line) for line in data.decode('utf-8').splitlines() if line.strip()]
|
|
|
|
|
|
def run(argv, stdin=b''):
|
|
completed = subprocess.run(argv, input=stdin, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
|
timeout=15)
|
|
if completed.returncode:
|
|
diagnostic = completed.stderr.decode('utf-8', 'replace')
|
|
raise AssertionError('child exit ' + str(completed.returncode) + ': ' + diagnostic[-1000:].replace('\n', ' | '))
|
|
return completed.stdout
|
|
|
|
|
|
def main():
|
|
candidate = Path(sys.argv[1])
|
|
source_bytes = {name: (candidate / name).read_bytes() for name in NAMES}
|
|
test_cases = []
|
|
for label, lines in cases():
|
|
model = Model()
|
|
expected = []
|
|
for line in lines:
|
|
value = model.execute(line)
|
|
if value is not None:
|
|
expected.extend([value['rc'], value])
|
|
test_cases.append((label, ('\n'.join(lines)+'\n').encode('ascii'), expected))
|
|
api_model = Model()
|
|
api_expected = [api_model.observe()]
|
|
for line in ['open 0 0', 'reserve 1 2', 'wrong', 'close 1',
|
|
'release 1 2', 'close 1', 'clear 0']:
|
|
value = api_model.execute(line)
|
|
api_expected.extend([value['rc'], value])
|
|
if line == 'reserve 1 2':
|
|
api_expected.append(api_model.observe())
|
|
api_expected.extend([api_model.observe(), api_model.observe(), Model().observe()])
|
|
other_model = Model()
|
|
other_value = other_model.execute('open 7 3')
|
|
api_expected.extend([other_value['rc'], other_value, api_model.observe()])
|
|
scratch = Path(tempfile.mkdtemp(prefix='provider-activation-', dir=Path.cwd()))
|
|
for name, data in source_bytes.items():
|
|
(scratch / name).write_bytes(data)
|
|
(scratch / 'harness.cpp').write_text(HARNESS, encoding='utf-8')
|
|
executable = str(scratch / 'observe')
|
|
command = ['g++', '-std=c++17', '-O2', '-Wall', '-Wextra', '-pedantic',
|
|
str(scratch / 'harness.cpp'), *(str(scratch / name) for name in NAMES[1:]),
|
|
'-o', executable]
|
|
# All scratch writes precede the first child. Only captured bytes are used from here on.
|
|
run(command)
|
|
actual = observations(run([executable, '--api-probe']))
|
|
if not equal(actual, api_expected):
|
|
raise AssertionError('API snapshot independence')
|
|
count = len(actual)
|
|
for label, payload, expected in test_cases:
|
|
actual = observations(run([executable], payload))
|
|
if not equal(actual, expected):
|
|
mismatch = next((i for i,(a,b) in enumerate(zip(actual,expected)) if not equal(a,b)), min(len(actual),len(expected)))
|
|
raise AssertionError(label + ' observation ' + str(mismatch))
|
|
count += len(expected)
|
|
print('checked ' + str(count) + ' observations')
|
|
print('PASS')
|
|
|
|
if __name__ == '__main__':
|
|
try:
|
|
main()
|
|
except (AssertionError, ValueError, UnicodeDecodeError, subprocess.TimeoutExpired) as error:
|
|
print('FAIL: ' + str(error))
|
|
raise SystemExit(1)
|