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>
481 lines
26 KiB
Python
481 lines
26 KiB
Python
CONTEXT = '#include "group.hpp"\n#include <iostream>\n#include <limits>\n#include <sstream>\n#include <string>\n#include <vector>\n\nnamespace {\nusing group::Word;\nstd::vector<std::string> words(const std::string &line)\n{\n std::vector<std::string> result;\n std::string word;\n for (char c : line) {\n if (c == \' \' || c == \'\\t\') {\n if (!word.empty()) {\n result.push_back(word);\n word.clear();\n }\n } else {\n word.push_back(c);\n }\n }\n if (!word.empty()) result.push_back(word);\n return result;\n}\n\nbool number(const std::string &text, Word &out)\n{\n if (text.empty()) return false;\n for (char c : text) if (c < \'0\' || c > \'9\') return false;\n Word result = 0;\n const Word maximum = std::numeric_limits<Word>::max();\n for (char c : text) {\n const Word digit = static_cast<Word>(c - \'0\');\n if (result > (maximum - digit) / 10) return false;\n result = result * 10 + digit;\n }\n out = result;\n return true;\n}\n\nint nibble(char c)\n{\n if (c >= \'0\' && c <= \'9\') return c - \'0\';\n if (c >= \'a\' && c <= \'f\') return c - \'a\' + 10;\n if (c >= \'A\' && c <= \'F\') return c - \'A\' + 10;\n return -1;\n}\n\nbool unhex(const std::string &text, std::string &out)\n{\n out.clear();\n if (text == "-") return true;\n if (text.size() % 2 != 0) return false;\n for (std::size_t i = 0; i < text.size(); i += 2) {\n const int high = nibble(text[i]);\n const int low = nibble(text[i + 1]);\n if (high < 0 || low < 0) return false;\n out.push_back(static_cast<char>(high * 16 + low));\n }\n return true;\n}\n\nWord raw_hash(const std::string &text)\n{\n Word hash = 14695981039346656037ULL;\n for (unsigned char c : text) {\n hash ^= c;\n hash *= 1099511628211ULL;\n }\n return hash;\n}\n\nWord raw_items(const std::vector<group::Item> &items)\n{\n std::string bytes;\n for (const auto &item : items) {\n for (Word value : {item.id, item.value}) {\n for (unsigned shift = 0; shift < 64; shift += 8) {\n bytes.push_back(static_cast<char>((value >> shift) & 255));\n }\n }\n }\n return raw_hash(bytes);\n}\n\nvoid unchanged(Word before, Word after)\n{\n std::cout << \'[\' << before << \',\' << after << "]\\n";\n}\n\nvoid receipt(int rc, const group::Receipt &r)\n{\n std::cout << \'[\' << rc << \',\' << r.ticket << \',\' << r.count\n << \',\' << r.total << \',\' << r.digest << "]\\n";\n}\n\nvoid info(const group::JournalInfo &s)\n{\n std::cout << s.batches << \',\' << s.items << \',\' << s.total\n << \',\' << s.attempts << \',\' << s.failures;\n}\n\nbool execute(group::Engine &e, group::Journal &j, const std::vector<std::string> &w)\n{\n const std::string &op = w[0];\n std::vector<Word> a;\n std::string text;\n const bool text_op = op == "N" || op == "T";\n for (std::size_t i = 1; i < w.size(); ++i) {\n if (text_op && i == 1) {\n if (!unhex(w[i], text)) return false;\n } else {\n Word value = 0;\n if (!number(w[i], value)) return false;\n a.push_back(value);\n }\n }\n int rc = 0;\n if (op == "C" && a.size() == 2) {\n rc = e.configure({a[0], a[1]});\n } else if (op == "A" && a.size() == 2) {\n rc = e.enqueue({a[0], a[1]});\n } else if (op == "X" && a.empty()) {\n rc = e.close();\n } else if (op == "F" && a.size() == 1) {\n rc = e.fail_next(a[0]);\n } else if (op == "Z" && a.empty()) {\n e.reset();\n } else if ((op == "P" && a.size() == 1) || (op == "R" && a.size() == 2)) {\n const Word n = a.back();\n group::Receipt r{n, n, n, n};\n rc = op == "P" ? e.pump(r) : e.read(a[0], r);\n receipt(rc, r);\n return true;\n } else if (op == "Q" && a.size() == 2) {\n group::Item item{a[1], a[1]};\n rc = e.peek(a[0], item);\n std::cout << \'[\' << rc << \',\' << item.id << \',\' << item.value << "]\\n";\n return true;\n } else if (op == "S" && a.empty()) {\n const group::Status s = e.status();\n std::cout << \'[\' << s.active << \',\' << s.width << \',\' << s.capacity\n << \',\' << s.closing << \',\' << s.queued << \',\' << s.accepted\n << \',\' << s.generation << \',\' << s.ready << \',\' << s.next_ticket << \',\';\n info(s.journal);\n std::cout << "]\\n";\n return true;\n } else if (op == "N" && a.size() == 2) {\n const Word before = raw_hash(text);\n Word value = a[1];\n rc = group::parse_decimal(text, a[0], value);\n std::cout << \'[\' << rc << \',\' << value << "]\\n";\n unchanged(before, raw_hash(text));\n return true;\n } else if (op == "T" && a.size() == 1) {\n const Word before = raw_hash(text);\n group::Config config{a[0], a[0]};\n rc = group::decode_config(text, config);\n std::cout << \'[\' << rc << \',\' << config.width << \',\' << config.capacity << "]\\n";\n unchanged(before, raw_hash(text));\n return true;\n } else if (op == "V" && a.size() == 2) {\n rc = group::validate_config({a[0], a[1]});\n } else if (op == "K" && a.size() == 3 && a[2] <= 1) {\n std::cout << \'[\' << group::take_count(a[0], a[1], a[2] != 0) << "]\\n";\n return true;\n } else if (op == "JF" && a.size() == 1) {\n rc = j.fail_next(a[0]);\n } else if (op == "JZ" && a.empty()) {\n j.reset();\n } else if (op == "JS" && a.empty()) {\n std::cout << \'[\';\n info(j.info());\n std::cout << "]\\n";\n return true;\n } else if (op == "JR" && a.size() == 2) {\n group::Receipt r{a[1], a[1], a[1], a[1]};\n rc = j.read(a[0], r);\n receipt(rc, r);\n return true;\n } else if ((op == "JC" || op == "B" || op == "D" || op == "H") && a.size() >= 3\n && a[2] <= 64 && a.size() == 3 + 2 * a[2]) {\n std::vector<group::Item> items;\n for (std::size_t k = 3; k < a.size(); k += 2) items.push_back({a[k], a[k + 1]});\n const Word before = raw_items(items);\n if (op == "B") {\n rc = group::validate_items(a[0], items);\n std::cout << \'[\' << rc << "]\\n";\n } else if (op == "H") {\n std::cout << \'[\' << group::digest_items(items) << "]\\n";\n } else if (op == "D") {\n const auto r = group::summarize(a[0], items);\n receipt(0, r);\n } else {\n group::Receipt r{a[1], a[1], a[1], a[1]};\n rc = j.commit(a[0], items, r);\n receipt(rc, r);\n }\n unchanged(before, raw_items(items));\n return true;\n } else if (op == "E" && a.size() >= 2 && a[0] <= 64 && a[1] <= 64\n && a.size() == 2 + 2 * (a[0] + a[1])) {\n std::vector<group::Item> left;\n std::vector<group::Item> right;\n for (std::size_t k = 2; k < 2 + 2 * a[0]; k += 2) left.push_back({a[k], a[k + 1]});\n for (std::size_t k = 2 + 2 * a[0]; k < a.size(); k += 2) right.push_back({a[k], a[k + 1]});\n const Word before_left = raw_items(left);\n const Word before_right = raw_items(right);\n std::cout << \'[\' << group::same_items(left, right) << "]\\n";\n unchanged(before_left, raw_items(left));\n unchanged(before_right, raw_items(right));\n return true;\n } else {\n return false;\n }\n std::cout << \'[\' << rc << "]\\n";\n return true;\n}\n}\n\nint main()\n{\n group::Engine engines[2];\n group::Journal journals[2];\n Word selected = 0;\n std::string line;\n while (std::getline(std::cin, line)) {\n const auto w = words(line);\n if (w.empty() || w[0][0] == \'#\') continue;\n if (w[0] == "U") {\n if (w.size() != 2 || !number(w[1], selected) || selected > 1) return 2;\n std::cout << \'[\' << selected << "]\\n";\n } else if (!execute(engines[selected], journals[selected], w)) {\n return 2;\n }\n }\n return std::cin.bad() ? 2 : 0;\n}\n'
|
||
import json
|
||
from pathlib import Path
|
||
import random
|
||
import re
|
||
import subprocess
|
||
import sys
|
||
import tempfile
|
||
|
||
# CONTEXT is prepended when assembling the task, before any candidate child exists.
|
||
MASK = 2**64 - 1
|
||
MAX_ID = 1000000
|
||
MAX_VALUE = 1000000000
|
||
CANDIDATES = ['group.hpp', 'policy.cpp', 'text.cpp', 'journal.cpp', 'engine.cpp']
|
||
|
||
|
||
def digest(data):
|
||
result = 14695981039346656037
|
||
for byte in data:
|
||
result = ((result ^ byte) * 1099511628211) & MASK
|
||
return result
|
||
|
||
|
||
def item_hash(items):
|
||
return digest(b''.join(x.to_bytes(8, 'little') for item in items for x in item))
|
||
|
||
|
||
def summary(ticket, items):
|
||
return [ticket, len(items), sum(v for _, v in items) & MASK, item_hash(items)]
|
||
|
||
|
||
def config_rc(width, capacity):
|
||
return 0 if 1 <= width <= 16 and width <= capacity <= 64 else -34
|
||
|
||
|
||
def batch_rc(ticket, items):
|
||
if not 1 <= len(items) <= 16:
|
||
return -22
|
||
if not 1 <= ticket <= MAX_ID or any(not 1 <= i <= MAX_ID or v > MAX_VALUE for i, v in items):
|
||
return -34
|
||
return -17 if len({i for i, _ in items}) != len(items) else 0
|
||
|
||
|
||
def count(queued, width, closing):
|
||
if not 1 <= width <= 16 or queued > 64:
|
||
return 0
|
||
return min(queued, width) if closing or queued >= width else 0
|
||
|
||
|
||
def decimal(text, maximum, nonce):
|
||
if re.fullmatch(b'[0-9]+', text) is None:
|
||
return [-22, nonce]
|
||
stripped = text.lstrip(b'0') or b'0'
|
||
if len(stripped) > 20:
|
||
return [-34, nonce]
|
||
value = int(stripped)
|
||
return [0, value] if value <= maximum else [-34, nonce]
|
||
|
||
|
||
def configuration(text, nonce):
|
||
match = re.fullmatch(b'[ \t]*width[ \t]*=[ \t]*([0-9]+)[ \t]*;[ \t]*capacity[ \t]*=[ \t]*([0-9]+)[ \t]*', text)
|
||
if match is None:
|
||
return [-22, nonce, nonce]
|
||
width = decimal(match[1], MASK, 0)
|
||
capacity = decimal(match[2], MASK, 0)
|
||
if width[0] or capacity[0] or config_rc(width[1], capacity[1]):
|
||
return [-34, nonce, nonce]
|
||
return [0, width[1], capacity[1]]
|
||
|
||
|
||
class Journal:
|
||
def __init__(self):
|
||
self.entries = []
|
||
self.attempts = 0
|
||
self.failures = 0
|
||
|
||
def fail(self, value):
|
||
if value > 8:
|
||
return -34
|
||
self.failures = value
|
||
return 0
|
||
|
||
def commit(self, ticket, items, nonce):
|
||
rc = batch_rc(ticket, items)
|
||
if rc:
|
||
return [rc] + [nonce] * 4
|
||
for old_ticket, old_items in self.entries:
|
||
if old_ticket == ticket:
|
||
return ([0] + summary(ticket, old_items)) if old_items == items else [-17] + [nonce] * 4
|
||
self.attempts += 1
|
||
if self.failures:
|
||
self.failures -= 1
|
||
return [-11] + [nonce] * 4
|
||
self.entries.append((ticket, list(items)))
|
||
return [0] + summary(ticket, items)
|
||
|
||
def read(self, index, nonce):
|
||
return [0] + summary(*self.entries[index]) if index < len(self.entries) else [-2] + [nonce] * 4
|
||
|
||
def info(self):
|
||
return [len(self.entries), sum(len(x) for _, x in self.entries), sum(v for _, x in self.entries for _, v in x), self.attempts, self.failures]
|
||
|
||
|
||
class Engine:
|
||
def __init__(self):
|
||
self.active = 0
|
||
self.width = 0
|
||
self.capacity = 0
|
||
self.closing = 0
|
||
self.queue = []
|
||
self.accepted = set()
|
||
self.generation = 0
|
||
self.ticket = 1
|
||
self.journal = Journal()
|
||
|
||
def configure(self, width, capacity):
|
||
rc = config_rc(width, capacity)
|
||
if rc:
|
||
return rc
|
||
if self.active:
|
||
return -16
|
||
self.active, self.width, self.capacity = 1, width, capacity
|
||
return 0
|
||
|
||
def enqueue(self, ident, value):
|
||
if not self.active:
|
||
return -19
|
||
if not 1 <= ident <= MAX_ID or value > MAX_VALUE:
|
||
return -34
|
||
if ident in self.accepted:
|
||
return -17
|
||
if self.closing:
|
||
return -32
|
||
if len(self.queue) == self.capacity:
|
||
return -28
|
||
self.queue.append((ident, value))
|
||
self.accepted.add(ident)
|
||
self.generation += 1
|
||
return 0
|
||
|
||
def close(self):
|
||
if not self.active:
|
||
return -19
|
||
if not self.closing:
|
||
self.generation += 1
|
||
self.closing = 1
|
||
return 0
|
||
|
||
def pump(self, nonce):
|
||
if not self.active:
|
||
return [-19] + [nonce] * 4
|
||
size = count(len(self.queue), self.width, self.closing)
|
||
if not size:
|
||
return [2 if self.closing and not self.queue else 1] + [nonce] * 4
|
||
result = self.journal.commit(self.ticket, self.queue[:size], nonce)
|
||
if result[0] == 0:
|
||
del self.queue[:size]
|
||
self.ticket += 1
|
||
return result
|
||
|
||
def status(self):
|
||
return [self.active, self.width, self.capacity, self.closing, len(self.queue), len(self.accepted), self.generation, int(bool(self.active and count(len(self.queue), self.width, self.closing))), self.ticket] + self.journal.info()
|
||
|
||
|
||
class Scenario:
|
||
def __init__(self):
|
||
self.engines = [Engine(), Engine()]
|
||
self.journals = [Journal(), Journal()]
|
||
self.selected = 0
|
||
self.lines = []
|
||
self.outputs = []
|
||
self.labels = []
|
||
|
||
def emit(self, output, label):
|
||
self.outputs.append(output)
|
||
self.labels.append(label)
|
||
|
||
def add(self, op, *args):
|
||
label = f'{len(self.lines)}:{op}'
|
||
serialized = [a.hex() or '-' if isinstance(a, bytes) else str(a) for a in args]
|
||
self.lines.append(' '.join([op] + serialized))
|
||
e, j = self.engines[self.selected], self.journals[self.selected]
|
||
result = None
|
||
if op == 'U':
|
||
self.selected = args[0]
|
||
result = [self.selected]
|
||
elif op == 'C':
|
||
result = [e.configure(*args)]
|
||
elif op == 'A':
|
||
result = [e.enqueue(*args)]
|
||
elif op == 'X':
|
||
result = [e.close()]
|
||
elif op == 'F':
|
||
result = [e.journal.fail(args[0]) if e.active else -19]
|
||
elif op == 'P':
|
||
result = e.pump(args[0])
|
||
elif op == 'Q':
|
||
index, n = args
|
||
result = [-19, n, n] if not e.active else ([0] + list(e.queue[index]) if index < len(e.queue) else [-2, n, n])
|
||
elif op == 'R':
|
||
result = e.journal.read(*args) if e.active else [-19] + [args[1]] * 4
|
||
elif op == 'S':
|
||
result = e.status()
|
||
elif op == 'Z':
|
||
self.engines[self.selected] = Engine()
|
||
result = [0]
|
||
elif op in ('N', 'T'):
|
||
result = decimal(*args) if op == 'N' else configuration(*args)
|
||
self.emit(result, label)
|
||
h = digest(args[0])
|
||
self.emit([h, h], label + ':input')
|
||
return
|
||
elif op == 'V':
|
||
result = [config_rc(*args)]
|
||
elif op == 'K':
|
||
result = [count(*args)]
|
||
elif op == 'JF':
|
||
result = [j.fail(args[0])]
|
||
elif op == 'JZ':
|
||
self.journals[self.selected] = Journal()
|
||
result = [0]
|
||
elif op == 'JS':
|
||
result = j.info()
|
||
elif op == 'JR':
|
||
result = j.read(*args)
|
||
elif op in ('JC', 'B', 'D', 'H'):
|
||
ticket, n, size = args[:3]
|
||
items = list(zip(args[3::2], args[4::2]))
|
||
assert len(items) == size
|
||
if op == 'JC':
|
||
result = j.commit(ticket, items, n)
|
||
elif op == 'B':
|
||
result = [batch_rc(ticket, items)]
|
||
elif op == 'D':
|
||
result = [0] + summary(ticket, items)
|
||
else:
|
||
result = [item_hash(items)]
|
||
self.emit(result, label)
|
||
h = item_hash(items)
|
||
self.emit([h, h], label + ':input')
|
||
return
|
||
elif op == 'E':
|
||
lsize, rsize = args[:2]
|
||
left = list(zip(args[2:2+2*lsize:2], args[3:2+2*lsize:2]))
|
||
right = list(zip(args[2+2*lsize::2], args[3+2*lsize::2]))
|
||
assert len(right) == rsize
|
||
self.emit([int(left == right)], label)
|
||
for items in (left, right):
|
||
h = item_hash(items)
|
||
self.emit([h, h], label + ':input')
|
||
return
|
||
else:
|
||
raise ValueError(op)
|
||
self.emit(result, label)
|
||
|
||
def batch(self, op, ticket, nonce, items):
|
||
self.add(op, ticket, nonce, len(items), *(n for item in items for n in item))
|
||
|
||
|
||
def cases():
|
||
s = Scenario()
|
||
# Initial state, output preservation, inactive/invalid combinations.
|
||
for instance in (0, 1):
|
||
s.add('U', instance)
|
||
s.add('S')
|
||
for n in (0, 37, MASK):
|
||
s.add('P', n)
|
||
s.add('Q', MASK, n)
|
||
s.add('R', 0, n)
|
||
s.add('A', 0, MASK)
|
||
s.add('F', MASK)
|
||
s.add('X')
|
||
# Public helpers are observed without going through Engine.
|
||
for width in (0, 1, 2, 3, 4, 7, 16, 17, MASK):
|
||
for capacity in (0, 1, 3, 4, 16, 63, 64, 65, MASK):
|
||
s.add('V', width, capacity)
|
||
for queued in (0, 1, 2, 3, 4, 7, 15, 16, 17, 63, 64, 65, MASK):
|
||
for closing in (0, 1):
|
||
s.add('K', queued, width, closing)
|
||
text_cases = [b'', b'0', b'00', b'1', b'9', b'10', b'16', str(MASK).encode(), str(MASK+1).encode(), b'0'*20000+b'16', b'9'*20000, b'9'*20000+b'x', b'+1', b'-0', b' 1', b'1 ', b'1\n', b'1\r', b'\v1', b'\f1', b'1\x00', b'1\x001', b'\xff', '12'.encode()]
|
||
for maximum in (0, 1, 16, 64, 10**9, MASK):
|
||
for text in text_cases:
|
||
for n in (0, 29, MASK):
|
||
s.add('N', text, maximum, n)
|
||
config_cases = [b'width=4;capacity=8', b'\t width \t=0001 ; capacity=64\t ', b'width=16;capacity=16', b'width=0;capacity=64', b'width=17;capacity=64', b'width=4;capacity=3', b'width=4;capacity=65', b'width=1;capacity=0', b'width=18446744073709551616;capacity=64', b'width=9'*1+b'0'*20000+b';capacity=x', b'width='+b'0'*20000+b'16;capacity='+b'0'*20000+b'64', b'width=4;capacity=8\n', b'width=4;capacity=8\x00', b'width=4;capacity=8\r', b'width=4;capacity=8;', b'capacity=8;width=4', b'width=4;width=8', b'width=4;capacity=+8', b'width=4;capacity=8x', b'width=4;capacity=\v8', b'width=4;capacity=\f8', b'width=4;capacity=8;extra=0', b'width=4\n;capacity=8', b'width= 4 0;capacity=64', b'width==4;capacity=8', b'', b'width=4;capacity=8#x']
|
||
config_cases += [b'width='+b'9'*20000+b';capacity=64', b'width=1;capacity='+b'9'*20000, b'width=1\x00;capacity=8', b'Width=1;capacity=8', b'width=1;capacity=\xff', b'width=1;capacity=8\n\n']
|
||
for text in config_cases:
|
||
for n in (0, 41, MASK):
|
||
s.add('T', text, n)
|
||
batches = [[], [(1, 0)], [(1, 10), (2, 11)], [(1, 1), (1, 1)], [(0, 0)], [(MAX_ID+1, 0)], [(1, MAX_VALUE+1)], [(1, 0), (1, 0), (2, MASK)], [(i+1, MAX_VALUE) for i in range(16)], [(i+1, 1) for i in range(17)]]
|
||
for ticket in (0, 1, MAX_ID, MAX_ID+1, MASK):
|
||
for items in batches:
|
||
s.batch('B', ticket, 0, items)
|
||
for items in batches + [[(MASK, MASK)] * 64]:
|
||
s.batch('D', MASK, 0, items)
|
||
s.batch('H', 0, 0, items)
|
||
comparisons = [([], []), ([], [(1, 0)]), ([(1, 0)], []), ([(1, 0)], [(1, 0)]), ([(1, 0)], [(1, 1)]), ([(1, 0)], [(2, 0)]), ([(1, 0), (2, 1)], [(2, 1), (1, 0)])]
|
||
for left, right in comparisons:
|
||
s.add('E', len(left), len(right), *(n for item in left+right for n in item))
|
||
# Direct backend: shape/range/duplicate validation does not consume failures.
|
||
for instance in (0, 1):
|
||
s.add('U', instance)
|
||
s.add('JZ')
|
||
for n in (0, 73, MASK):
|
||
s.add('JR', 0, n)
|
||
s.add('JF', 8)
|
||
s.add('JF', 9)
|
||
for ticket in (0, 1, MASK):
|
||
for items in batches:
|
||
if batch_rc(ticket, items):
|
||
for n in (0, 77, MASK):
|
||
s.batch('JC', ticket, n, items)
|
||
s.add('JS')
|
||
for n in (0, 79, MASK):
|
||
s.batch('JC', 1, n, [(1, 3), (2, 5)])
|
||
s.add('JS')
|
||
s.add('JF', 0)
|
||
s.batch('JC', 1, 13, [(1, 3), (2, 5)])
|
||
s.add('JF', 2)
|
||
s.batch('JC', 1, 15, [(1, 3), (2, 5)])
|
||
s.batch('JC', 1, 17, [(2, 5), (1, 3)])
|
||
s.batch('JC', 1, 19, [(1, 4), (2, 5)])
|
||
for n in (0, 83, MASK):
|
||
s.batch('JC', 1, n, [(1, 4), (2, 5)])
|
||
s.batch('JC', 1, n, [(1, MASK)])
|
||
s.batch('JC', 1, n, [])
|
||
s.add('JS')
|
||
s.batch('JC', 2, 23, [(1, 0)])
|
||
s.add('JS')
|
||
s.batch('JC', 2, 0, [(1, 0)])
|
||
s.add('JS')
|
||
s.batch('JC', 2, MASK, [(1, 0)])
|
||
s.batch('JC', 3, MASK, [(1, MAX_VALUE)])
|
||
s.add('JS')
|
||
s.add('JR', 0, 31)
|
||
s.add('JR', 1, 31)
|
||
s.add('JR', 2, MASK)
|
||
s.add('JR', 3, MASK)
|
||
# Both standalone backends hold live state before interleaved changes.
|
||
for instance in (0, 1):
|
||
s.add('U', instance)
|
||
s.add('JZ')
|
||
s.batch('JC', 100+instance, 7, [(17, 900+instance)])
|
||
s.add('JF', instance+1)
|
||
s.add('JS')
|
||
s.add('U', 0)
|
||
s.add('JR', 0, MASK)
|
||
s.add('JS')
|
||
s.add('U', 1)
|
||
s.add('JR', 0, 0)
|
||
s.add('JZ')
|
||
s.add('JS')
|
||
s.add('U', 0)
|
||
s.add('JR', 0, 31)
|
||
s.add('JS')
|
||
s.batch('JC', 103, 0, [(18, 42)])
|
||
s.add('JS')
|
||
s.add('U', 1)
|
||
s.add('JS')
|
||
s.batch('JC', 103, MASK, [(18, 999)])
|
||
s.add('JS')
|
||
s.add('U', 0)
|
||
s.add('JS')
|
||
s.add('JR', 0, MASK)
|
||
s.batch('JC', 103, MASK, [(18, 42)])
|
||
s.add('JS')
|
||
# Fully open groups, partial closing groups, failure retry and exact prefix sizes.
|
||
for width in (1, 2, 3, 4, 7, 16):
|
||
for amount in sorted({0, max(0, width-1), width, width+1, 2*width-1, 2*width+1, 64}):
|
||
s.add('U', width % 2)
|
||
s.add('Z')
|
||
s.add('C', width, 64)
|
||
s.add('C', 0, 0)
|
||
s.add('C', width, 64)
|
||
for i in range(amount):
|
||
s.add('A', i+1, (i*100003+width) % (MAX_VALUE+1))
|
||
if amount == 64:
|
||
s.add('A', 777777, 0)
|
||
s.add('S')
|
||
for n in (0, 53, MASK):
|
||
s.add('Q', 0, n)
|
||
s.add('Q', amount, n)
|
||
s.add('F', 2)
|
||
s.add('P', 61)
|
||
s.add('S')
|
||
s.add('X')
|
||
s.add('X')
|
||
s.add('A', 0, MASK)
|
||
s.add('A', 1, MASK)
|
||
s.add('A', 1, 1)
|
||
s.add('A', MAX_ID, 0)
|
||
s.add('F', 9)
|
||
for k in range(68):
|
||
s.add('P', (k*998244353) & MASK)
|
||
s.add('S')
|
||
for index in (0, 1, amount//width, MASK):
|
||
for n in (0, 101, MASK):
|
||
s.add('R', index, n)
|
||
s.add('Z')
|
||
s.add('C', width, width)
|
||
s.add('S')
|
||
# Capacity failure, accepted-id lifetime and simultaneous independent objects.
|
||
for selected in (0, 1):
|
||
s.add('U', selected)
|
||
s.add('Z')
|
||
s.add('C', selected+1, selected+2)
|
||
for i in range(selected+2):
|
||
s.add('A', MAX_ID-i, MAX_VALUE)
|
||
s.add('A', 0, MASK)
|
||
s.add('A', MAX_ID, 0)
|
||
s.add('A', 12, 0)
|
||
s.add('P', MASK)
|
||
s.add('A', MAX_ID, 7)
|
||
s.add('A', 12, 0)
|
||
s.add('S')
|
||
rng = random.Random(0xBA7C22)
|
||
for i in range(700):
|
||
if i % 11 == 0:
|
||
s.add('U', rng.randrange(2))
|
||
if i % 97 == 0:
|
||
s.add('Z')
|
||
width = rng.choice([1, 3, 5, 16])
|
||
s.add('C', width, rng.randrange(width, 65))
|
||
op = rng.choice(['A', 'A', 'A', 'P', 'Q', 'F', 'X', 'R', 'S'])
|
||
if op == 'A':
|
||
s.add(op, rng.choice([0, MAX_ID+1, rng.randrange(1, 60)]), rng.choice([0, MAX_VALUE, MAX_VALUE+1, rng.randrange(MAX_VALUE+1)]))
|
||
elif op in ('Q', 'R'):
|
||
s.add(op, rng.randrange(20), rng.getrandbits(64))
|
||
elif op == 'P':
|
||
s.add(op, rng.getrandbits(64))
|
||
elif op == 'F':
|
||
s.add(op, rng.randrange(11))
|
||
else:
|
||
s.add(op)
|
||
s.add('S')
|
||
return s
|
||
|
||
|
||
def equal(actual, expected):
|
||
return type(actual) is type(expected) and (all(equal(a, b) for a, b in zip(actual, expected)) and len(actual) == len(expected) if isinstance(expected, list) else actual == expected)
|
||
|
||
|
||
def main():
|
||
scenario = cases()
|
||
# Capture the complete candidate and write every scratch file before the first child.
|
||
candidate = Path(sys.argv[1])
|
||
content = {name: (candidate / name).read_bytes() for name in CANDIDATES}
|
||
scratch = Path(tempfile.mkdtemp(prefix='group-observe-', dir=Path.cwd()))
|
||
for name, data in content.items():
|
||
(scratch / name).write_bytes(data)
|
||
(scratch / 'console.cpp').write_text(CONTEXT)
|
||
command = ['g++', '-std=c++17', '-O2', '-Wall', '-Wextra', '-pedantic', *(str(scratch / name) for name in CANDIDATES if name.endswith('.cpp')), str(scratch / 'console.cpp'), '-o', str(scratch / 'observe')]
|
||
built = subprocess.run(command, cwd=scratch, capture_output=True, timeout=35)
|
||
if built.returncode:
|
||
print(built.stderr.decode('utf-8', 'replace')[-2500:], file=sys.stderr)
|
||
raise AssertionError('candidate build failed')
|
||
payload = ('\n'.join(scenario.lines) + '\n').encode('ascii')
|
||
result = subprocess.run([str(scratch / 'observe')], cwd=scratch, input=payload, capture_output=True, timeout=20)
|
||
if result.returncode:
|
||
print(result.stderr.decode('utf-8', 'replace')[-2500:], file=sys.stderr)
|
||
raise AssertionError('observer exited unsuccessfully')
|
||
try:
|
||
outputs = [json.loads(line) for line in result.stdout.decode('utf-8').splitlines()]
|
||
except (UnicodeError, ValueError) as error:
|
||
raise AssertionError('invalid observation encoding or JSON') from error
|
||
if len(outputs) != len(scenario.outputs):
|
||
raise AssertionError(f'observation count {len(outputs)} != {len(scenario.outputs)}')
|
||
for actual, expected, label in zip(outputs, scenario.outputs, scenario.labels):
|
||
if not equal(actual, expected):
|
||
print(f'{label}: observed={actual!r} expected={expected!r}', file=sys.stderr)
|
||
raise AssertionError('behavior differs from the public contract')
|
||
print('PASS')
|
||
|
||
|
||
if __name__ == '__main__':
|
||
try:
|
||
main()
|
||
except (AssertionError, OSError, subprocess.SubprocessError) as error:
|
||
print('FAIL: ' + str(error))
|
||
raise SystemExit(1)
|