525 lines
38 KiB
Python

CONTEXT = {'affinity.hpp': '#ifndef EMBEDDED_AFFINITY_HPP\n#define EMBEDDED_AFFINITY_HPP\n\n#include <cstddef>\n#include <cstdint>\n#include <optional>\n#include <string>\n#include <vector>\n\nnamespace affinity {\n\nenum class Error { none = 0, invalid = 1 };\nenum class SetOperation { unite = 0, intersect = 1, subtract = 2 };\n\nstruct CoreSet {\n std::vector<std::uint16_t> ids;\n};\n\nstruct Core {\n std::uint16_t id = 0;\n std::uint8_t capacity = 0;\n};\n\nstruct Job {\n std::string id;\n std::int32_t priority = 0;\n CoreSet allowed;\n std::optional<std::uint16_t> previous;\n};\n\nstruct Problem {\n std::vector<Core> cores;\n std::vector<Job> jobs;\n};\n\nstruct Binding {\n std::string job;\n std::optional<std::uint16_t> core;\n};\n\nstruct CoreLoad {\n std::uint16_t core = 0;\n std::uint8_t used = 0;\n};\n\nstruct Plan {\n std::size_t accepted = 0;\n std::size_t retained = 0;\n std::vector<Binding> bindings;\n std::vector<CoreLoad> loads;\n};\n\nbool valid_job_id(const std::string &text);\nError parse_list(const std::string &text, CoreSet &output);\nError format_list(const CoreSet &input, std::string &output);\nError parse_mask(const std::string &text, CoreSet &output);\nError format_mask(const CoreSet &input, std::string &output);\nError combine(const CoreSet &left, const CoreSet &right,\n SetOperation operation, CoreSet &output);\nError canonical_problem(const Problem &input, Problem &output);\nError encode_problem(const Problem &input, std::vector<std::uint8_t> &output);\nError decode_problem(const std::vector<std::uint8_t> &input, Problem &output);\nError make_plan(const Problem &input, Plan &output);\n\n}\n#endif\n', 'affinity_observe.cpp': '#include "affinity.hpp"\n#include <iostream>\n#include <limits>\n#include <stdexcept>\n#include <string>\n#include <utility>\n\nusing namespace affinity;\n\nnamespace {\n\nstd::string quote_bytes(const std::string &value)\n{\n constexpr char digits[] = "0123456789abcdef";\n std::string text = "\\"";\n for (unsigned char byte : value) {\n if (byte == \'"\' || byte == \'\\\\\') {\n text.push_back(\'\\\\\');\n text.push_back(static_cast<char>(byte));\n } else if (byte < 32 || byte > 126) {\n text += "\\\\u00";\n text.push_back(digits[byte >> 4]);\n text.push_back(digits[byte & 15]);\n } else {\n text.push_back(static_cast<char>(byte));\n }\n }\n text.push_back(\'"\');\n return text;\n}\n\nstd::string boolean(bool value)\n{\n return value ? "true" : "false";\n}\n\ntemplate<class Range, class Encode>\nstd::string sequence(const Range &items, Encode encode)\n{\n std::string output = "[";\n bool first = true;\n for (const auto &item : items) {\n if (!first) output.push_back(\',\');\n first = false;\n output += encode(item);\n }\n output.push_back(\']\');\n return output;\n}\n\nstd::string set_json(const CoreSet &set)\n{\n return sequence(set.ids, [](std::uint16_t id) { return std::to_string(id); });\n}\n\nstd::string core_json(const Core &core)\n{\n return "[" + std::to_string(core.id) + "," + std::to_string(core.capacity) + "]";\n}\n\nstd::string optional_core(const std::optional<std::uint16_t> &core)\n{\n return core.has_value() ? std::to_string(*core) : "null";\n}\n\nstd::string job_json(const Job &job)\n{\n return "[" + quote_bytes(job.id) + "," + std::to_string(job.priority) + "," +\n set_json(job.allowed) + "," + optional_core(job.previous) + "]";\n}\n\nstd::string problem_json(const Problem &problem)\n{\n return "[" + sequence(problem.cores, core_json) + "," +\n sequence(problem.jobs, job_json) + "]";\n}\n\nstd::string binding_json(const Binding &binding)\n{\n return "[" + quote_bytes(binding.job) + "," + optional_core(binding.core) + "]";\n}\n\nstd::string load_json(const CoreLoad &load)\n{\n return "[" + std::to_string(load.core) + "," + std::to_string(load.used) + "]";\n}\n\nstd::string plan_json(const Plan &plan)\n{\n return "[" + std::to_string(plan.accepted) + "," + std::to_string(plan.retained) +\n "," + sequence(plan.bindings, binding_json) + "," +\n sequence(plan.loads, load_json) + "]";\n}\n\nstd::string hex_bytes(const std::vector<std::uint8_t> &bytes)\n{\n constexpr char digits[] = "0123456789abcdef";\n if (bytes.empty()) return "-";\n std::string output;\n output.reserve(bytes.size() * 2);\n for (std::uint8_t byte : bytes) {\n output.push_back(digits[byte >> 4]);\n output.push_back(digits[byte & 15]);\n }\n return output;\n}\n\nstd::string token()\n{\n std::string value;\n if (!(std::cin >> value)) throw std::runtime_error("missing token");\n return value;\n}\n\nstd::uint64_t number(std::uint64_t maximum)\n{\n const std::string text = token();\n if (text.empty() || text.front() == \'-\') throw std::runtime_error("unsigned number");\n std::size_t length = 0;\n const auto result = std::stoull(text, &length, 10);\n if (length != text.size() || result > maximum) throw std::runtime_error("number range");\n return result;\n}\n\nstd::int64_t signed_number(std::int64_t minimum, std::int64_t maximum)\n{\n const std::string text = token();\n std::size_t length = 0;\n const auto result = std::stoll(text, &length, 10);\n if (length != text.size() || result < minimum || result > maximum)\n throw std::runtime_error("signed number range");\n return result;\n}\n\nint nibble(char byte)\n{\n if (byte >= \'0\' && byte <= \'9\') return byte - \'0\';\n if (byte >= \'a\' && byte <= \'f\') return byte - \'a\' + 10;\n if (byte >= \'A\' && byte <= \'F\') return byte - \'A\' + 10;\n throw std::runtime_error("hex digit");\n}\n\nstd::vector<std::uint8_t> read_bytes()\n{\n const std::string text = token();\n if (text == "-") return {};\n if (text.size() % 2 != 0) throw std::runtime_error("hex length");\n std::vector<std::uint8_t> bytes;\n bytes.reserve(text.size() / 2);\n for (std::size_t at = 0; at < text.size(); at += 2) {\n bytes.push_back(static_cast<std::uint8_t>(nibble(text[at]) * 16 + nibble(text[at + 1])));\n }\n return bytes;\n}\n\nstd::string read_text()\n{\n const auto bytes = read_bytes();\n return std::string(bytes.begin(), bytes.end());\n}\n\nCoreSet read_set()\n{\n const auto count = number(4096);\n CoreSet input;\n for (std::size_t index = 0; index < count; ++index) {\n input.ids.push_back(static_cast<std::uint16_t>(number(65535)));\n }\n return input;\n}\n\nvoid require_row(const char *wanted)\n{\n if (token() != wanted) throw std::runtime_error("unexpected data row");\n}\n\nProblem read_problem()\n{\n const auto core_count = number(4096);\n const auto job_count = number(4096);\n Problem input;\n for (std::size_t index = 0; index < core_count; ++index) {\n require_row("K");\n Core core;\n core.id = static_cast<std::uint16_t>(number(65535));\n core.capacity = static_cast<std::uint8_t>(number(255));\n input.cores.push_back(core);\n }\n for (std::size_t index = 0; index < job_count; ++index) {\n require_row("J");\n Job job;\n job.id = read_text();\n job.priority = static_cast<std::int32_t>(signed_number(\n std::numeric_limits<std::int32_t>::min(), std::numeric_limits<std::int32_t>::max()));\n const auto previous = signed_number(-1, 65535);\n if (previous >= 0) job.previous = static_cast<std::uint16_t>(previous);\n job.allowed = read_set();\n input.jobs.push_back(std::move(job));\n }\n return input;\n}\n\nProblem dirty_problem()\n{\n Problem result;\n result.cores.push_back(Core{9, 3});\n result.jobs.push_back(Job{"old_job", 12, CoreSet{{9}}, 9});\n return result;\n}\n\nPlan dirty_plan()\n{\n Plan result;\n result.accepted = 77;\n result.retained = 88;\n result.bindings.push_back(Binding{"old_binding", 9});\n result.loads.push_back(CoreLoad{9, 7});\n return result;\n}\n\nstruct Bank {\n Problem raw;\n Problem decoded;\n bool decoded_ok = false;\n Plan last_plan;\n Plan held_plan;\n std::vector<std::uint8_t> last_bytes;\n};\n\nstd::string result_row(const std::string &command, Error error,\n const std::string &value, bool unchanged)\n{\n return "[" + quote_bytes(command) + "," + std::to_string(static_cast<int>(error)) +\n "," + value + "," + boolean(unchanged) + "]";\n}\n\n}\n\nint main()\n{\n try {\n Bank banks[3];\n std::size_t selected = 0;\n std::string command;\n while (std::cin >> command) {\n if (command == "G") {\n selected = number(2);\n const Bank &bank = banks[selected];\n std::cout << "[\\"G\\"," << problem_json(bank.raw) << \',\' <<\n plan_json(bank.last_plan) << \',\' << quote_bytes(hex_bytes(bank.last_bytes)) << "]\\n";\n continue;\n }\n Bank &bank = banks[selected];\n std::string row;\n if (command == "N") {\n const std::string input = read_text();\n const std::string original = input;\n const bool result = valid_job_id(input);\n row = "[\\"N\\"," + boolean(result) + "," + boolean(input == original) + "]";\n } else if (command == "L" || command == "M") {\n const std::string input = read_text();\n const std::string original = input;\n CoreSet output{{17, 19}};\n const Error error = command == "L" ? parse_list(input, output) : parse_mask(input, output);\n row = result_row(command, error, set_json(output), input == original);\n } else if (command == "FL" || command == "FM") {\n const CoreSet input = read_set();\n const CoreSet original = input;\n std::string output = "old_text";\n const Error error = command == "FL" ? format_list(input, output) : format_mask(input, output);\n row = result_row(command, error, quote_bytes(output), input.ids == original.ids);\n } else if (command == "O") {\n const auto operation = static_cast<SetOperation>(signed_number(-2147483648LL, 2147483647));\n const CoreSet left = read_set();\n const CoreSet right = read_set();\n const auto original_left = left.ids;\n const auto original_right = right.ids;\n CoreSet output{{17, 19}};\n const Error error = combine(left, right, operation, output);\n row = result_row(command, error, set_json(output),\n left.ids == original_left && right.ids == original_right);\n } else if (command == "C") {\n bank.raw = read_problem();\n const std::string original = problem_json(bank.raw);\n Problem output = dirty_problem();\n const Error error = canonical_problem(bank.raw, output);\n row = result_row(command, error, problem_json(output), original == problem_json(bank.raw));\n } else if (command == "P") {\n const std::string original = problem_json(bank.raw);\n bank.last_plan = dirty_plan();\n const Error error = make_plan(bank.raw, bank.last_plan);\n row = result_row(command, error, plan_json(bank.last_plan), original == problem_json(bank.raw));\n } else if (command == "B") {\n const std::string original = problem_json(bank.raw);\n bank.last_bytes = {71, 83, 91};\n const Error error = encode_problem(bank.raw, bank.last_bytes);\n row = result_row(command, error, quote_bytes(hex_bytes(bank.last_bytes)),\n original == problem_json(bank.raw));\n } else if (command == "D") {\n const auto input = read_bytes();\n const auto original = input;\n bank.decoded = dirty_problem();\n const Error error = decode_problem(input, bank.decoded);\n bank.decoded_ok = error == Error::none;\n row = result_row(command, error, problem_json(bank.decoded), input == original);\n } else if (command == "U") {\n if (!bank.decoded_ok) throw std::runtime_error("decoded problem unavailable");\n bank.raw = bank.decoded;\n row = "[\\"U\\"," + problem_json(bank.raw) + "]";\n } else if (command == "H" || command == "Q") {\n if (command == "H") bank.held_plan = bank.last_plan;\n row = "[" + quote_bytes(command) + "," + plan_json(bank.held_plan) + "]";\n } else {\n throw std::runtime_error("unknown command");\n }\n std::cout << row << \'\\n\';\n }\n return std::cin.bad() ? 2 : 0;\n } catch (const std::exception &error) {\n std::cerr << error.what() << \'\\n\';\n return 2;\n }\n}\n'}
"""Authoring source copied into the sole private check with frozen public context."""
import copy
import json
import random
import re
import struct
import subprocess
import sys
import tempfile
from pathlib import Path
CANDIDATES = ('core_text.cpp', 'problem_model.cpp', 'problem_image.cpp', 'placement.cpp')
def hx(value):
if isinstance(value, str):
value = value.encode('latin1')
return bytes(value).hex() or '-'
def valid_id(value):
return re.fullmatch(r'[A-Za-z0-9_-]{1,32}', value, flags=re.ASCII) is not None
def valid_set(value):
return len(set(value)) == len(value) and all(0 <= core <= 255 for core in value)
def parse_list(text):
if len(text) > 4096:
return 1, []
if not text:
return 0, []
chosen = set()
for item in text.split(','):
match = re.fullmatch(r'([0-9]+)(?:-([0-9]+)(?::([0-9]+)/([0-9]+))?)?', item,
flags=re.ASCII)
if match is None:
return 1, []
start, end, used, group = [int(part) if part is not None else None for part in match.groups()]
end = start if end is None else end
if not 0 <= start <= end <= 255:
return 1, []
if group is None:
chosen.update(range(start, end + 1))
elif not 1 <= used <= group <= 256:
return 1, []
else:
chosen.update(core for core in range(start, end + 1) if (core - start) % group < used)
return 0, sorted(chosen)
def parse_mask(text):
groups = text.split(',')
if not 1 <= len(text) <= 4096 or not 1 <= len(groups) <= 8:
return 1, []
if any(re.fullmatch(r'[a-fA-F0-9]{1,8}', group, flags=re.ASCII) is None for group in groups):
return 1, []
bits = sum(int(group, 16) << (32 * i) for i, group in enumerate(reversed(groups)))
return 0, [core for core in range(256) if bits & (1 << core)]
def format_list(ids):
if not valid_set(ids):
return 1, ''
ids = sorted(ids)
runs = []
while ids:
start = end = ids.pop(0)
while ids and ids[0] == end + 1:
end = ids.pop(0)
runs.append(str(start) if start == end else '%d-%d' % (start, end))
return 0, ','.join(runs)
def format_mask(ids):
if not valid_set(ids):
return 1, ''
bits = sum(1 << core for core in ids)
words = ['%08x' % ((bits >> shift) & 0xffffffff) for shift in range(0, max(1, bits.bit_length()), 32)]
words[-1] = words[-1].lstrip('0') or '0'
return 0, ','.join(reversed(words))
def canonical(problem):
cores, jobs = problem
if len(cores) > 32 or len(jobs) > 24:
return 1, [[], []]
if any(not 0 <= core <= 255 or not 1 <= capacity <= 8 for core, capacity in cores):
return 1, [[], []]
if len({core for core, _ in cores}) != len(cores):
return 1, [[], []]
if len({job[0] for job in jobs}) != len(jobs):
return 1, [[], []]
for name, priority, allowed, previous in jobs:
if not valid_id(name) or not -(1 << 31) <= priority < (1 << 31) or not valid_set(allowed):
return 1, [[], []]
if previous is not None and not 0 <= previous <= 255:
return 1, [[], []]
normalized = [sorted(copy.deepcopy(cores)),
[[name, priority, sorted(allowed), previous] for name, priority, allowed, previous
in sorted(jobs, key=lambda job: (-job[1], job[0]))]]
return 0, normalized
def raw_image(problem):
"""Encode the given record order, for the independently valid decoder order variants."""
cores, jobs = problem
result = bytearray(b'AFF1' + struct.pack('<HH', len(cores), len(jobs)))
for core, capacity in cores:
result.extend(struct.pack('<HBB', core, capacity, 0))
for name, priority, allowed, previous in jobs:
result.extend(struct.pack('<BBHi', len(name), 0, 65535 if previous is None else previous, priority))
result.extend(sum(1 << core for core in allowed).to_bytes(32, 'little'))
result.extend(name.encode('latin1'))
return bytes(result)
def encode(problem):
error, normalized = canonical(problem)
return (error, b'') if error else (0, raw_image(normalized))
def decode(data):
try:
if not 8 <= len(data) <= 4096 or data[:4] != b'AFF1':
raise ValueError('header')
count, jobs_count = struct.unpack_from('<HH', data, 4)
if count > 32 or jobs_count > 24:
raise ValueError('count')
at = 8
cores, jobs = [], []
for _ in range(count):
core, capacity, reserved = struct.unpack_from('<HBB', data, at)
if reserved:
raise ValueError('core reserved')
cores.append([core, capacity])
at += 4
for _ in range(jobs_count):
length, reserved, previous, priority = struct.unpack_from('<BBHi', data, at)
if reserved or not 1 <= length <= 32 or (previous != 65535 and previous > 255):
raise ValueError('job header')
if at + 40 + length > len(data):
raise ValueError('job tail')
bits = int.from_bytes(data[at + 8:at + 40], 'little')
allowed = [core for core in range(256) if bits & (1 << core)]
name = data[at + 40:at + 40 + length].decode('latin1')
jobs.append([name, priority, allowed, None if previous == 65535 else previous])
at += 40 + length
if at != len(data):
raise ValueError('trailing data')
return canonical([cores, jobs])
except (ValueError, struct.error):
return 1, [[], []]
def minimum_assignment(costs):
"""Rectangular Hungarian assignment, integer potentials; independent of C++ flow code."""
if not costs:
return []
n, m = len(costs), len(costs[0])
infinity = 1 + max(abs(cost) for row in costs for cost in row) * (n + m + 1)
u, v, occupied, previous = [0] * (n + 1), [0] * (m + 1), [0] * (m + 1), [0] * (m + 1)
for row in range(1, n + 1):
occupied[0] = row
column = 0
best, visited = [infinity] * (m + 1), [False] * (m + 1)
while True:
visited[column] = True
active, delta, next_column = occupied[column], infinity, 0
for candidate in range(1, m + 1):
if visited[candidate]:
continue
reduced = costs[active - 1][candidate - 1] - u[active] - v[candidate]
if reduced < best[candidate]:
best[candidate], previous[candidate] = reduced, column
if best[candidate] < delta:
delta, next_column = best[candidate], candidate
for candidate in range(m + 1):
if visited[candidate]:
u[occupied[candidate]] += delta
v[candidate] -= delta
else:
best[candidate] -= delta
column = next_column
if occupied[column] == 0:
break
while column:
before = previous[column]
occupied[column] = occupied[before]
column = before
assignment = [-1] * n
for column in range(1, m + 1):
if occupied[column]:
assignment[occupied[column] - 1] = column - 1
return assignment
def plan(problem):
error, normalized = canonical(problem)
if error:
return 1, [0, 0, [], []]
cores, jobs = normalized
n = len(jobs)
slots = [core for core, capacity in cores for _ in range(capacity)]
# Each lower objective's TOTAL range is smaller than one unit of the next.
lex_bound = 256 ** n
acceptance_unit = (n + 1) * lex_bound
impossible = acceptance_unit * (1 << (n + 2))
costs = []
for position, (_, _, allowed, previous) in enumerate(jobs):
acceptance = (1 << (n - 1 - position)) * acceptance_unit
place = 256 ** (n - 1 - position)
row = [-acceptance - (previous == core) * lex_bound - (255 - core) * place
if core in allowed else impossible for core in slots]
costs.append(row + [0] * n)
columns = minimum_assignment(costs)
assignment = [slots[column] if column < len(slots) else None for column in columns]
retained = sum(core is not None and core == job[3] for core, job in zip(assignment, jobs))
return 0, [sum(core is not None for core in assignment), retained,
[[job[0], core] for job, core in zip(jobs, assignment)],
[[core, assignment.count(core)] for core, _ in cores]]
def problem_wire(problem):
cores, jobs = problem
lines = ['C %d %d' % (len(cores), len(jobs))]
lines += ['K %d %d' % tuple(core) for core in cores]
lines += ['J %s %d %d %d%s' % (hx(name), priority, -1 if previous is None else previous,
len(allowed), ''.join(' ' + str(core) for core in allowed))
for name, priority, allowed, previous in jobs]
return '\n'.join(lines)
class Suite:
def __init__(self):
self.commands, self.expected, self.labels = [], [], []
self.banks = [dict(raw=[[], []], plan=[0, 0, [], []], held=[0, 0, [], []],
encoded=b'', decoded=[[], []], decoded_ok=False) for _ in range(3)]
self.selected = 0
def emit(self, command, result, label, clauses):
self.commands.append(command)
self.expected.append(copy.deepcopy(result))
self.labels.append((label, clauses))
return result
def text(self, op, value, label, clauses):
if op == 'N':
result = [op, valid_id(value), True]
else:
error, output = (parse_list if op == 'L' else parse_mask)(value)
result = [op, error, output, True]
return self.emit(op + ' ' + hx(value), result, label, clauses)
def formatted(self, op, ids, label, clauses):
error, value = (format_list if op == 'FL' else format_mask)(ids)
return self.emit(op + ' ' + str(len(ids)) + ''.join(' ' + str(core) for core in ids),
[op, error, value, True], label, clauses)
def combined(self, operation, left, right, label, clauses):
invalid = operation not in (0, 1, 2) or not valid_set(left) or not valid_set(right)
value = [] if invalid else sorted((set(left) | set(right)) if operation == 0 else
(set(left) & set(right)) if operation == 1 else
(set(left) - set(right)))
command = 'O %d %d%s %d%s' % (operation, len(left), ''.join(' ' + str(x) for x in left),
len(right), ''.join(' ' + str(x) for x in right))
return self.emit(command, ['O', int(invalid), value, True], label, clauses)
def problem(self, value, label='canonical-problem', clauses='A01-A04,A14-A17,A34'):
self.banks[self.selected]['raw'] = copy.deepcopy(value)
error, output = canonical(value)
return self.emit(problem_wire(value), ['C', error, output, True], label, clauses)
def image(self, value, label='decode-image', clauses='A01-A04,A14-A16,A26-A30,A34'):
bank = self.banks[self.selected]
error, bank['decoded'] = decode(value)
bank['decoded_ok'] = error == 0
return self.emit('D ' + hx(value), ['D', error, bank['decoded'], True], label, clauses)
def command(self, op, label, clauses):
bank = self.banks[self.selected]
if op == 'P':
error, bank['plan'] = plan(bank['raw'])
result = ['P', error, bank['plan'], True]
elif op == 'B':
error, bank['encoded'] = encode(bank['raw'])
result = ['B', error, hx(bank['encoded']), True]
elif op == 'H':
bank['held'] = copy.deepcopy(bank['plan'])
result = ['H', bank['held']]
elif op == 'Q':
result = ['Q', bank['held']]
elif op == 'U':
assert bank['decoded_ok']
bank['raw'] = copy.deepcopy(bank['decoded'])
result = ['U', bank['raw']]
else:
assert op.startswith('G ')
self.selected = int(op[2:])
bank = self.banks[self.selected]
result = ['G', bank['raw'], bank['plan'], hx(bank['encoded'])]
return self.emit(op, result, label, clauses)
def exercise(self, value, label):
self.problem(value, label + '-canonical')
self.command('P', label + '-plan', 'A03-A04,A14-A25,A34')
encoded = self.command('B', label + '-encode', 'A03-A04,A14-A17,A26-A28,A34')
if encoded[1] == 0:
self.image(raw_image(value), label + '-unordered-decode')
self.command('U', label + '-decoded-value', 'A03,A16,A29-A30,A33-A34')
self.command('P', label + '-decoded-plan', 'A03-A04,A14-A25,A34')
self.command('B', label + '-canonical-reencode', 'A03-A04,A16-A17,A26-A30,A34')
def cases():
s, rng = Suite(), random.Random(0x616666696e697479)
c = s.command
for bank in range(3):
c('G ' + str(bank), 'initial-bank', 'A03-A04,A25,A33-A34')
c('P', 'empty-initial-plan', 'A04,A17-A22,A25,A34')
c('B', 'empty-initial-image', 'A04,A26-A28,A34')
c('G 0', 'select-first-bank', 'A03,A25,A33-A34')
for text in ['', 'A', '0', '_-', 'z' * 32, 'z' * 33, 'a.b', 'a/b', '\x00', '\xff',
'a b', 'a\n', '中'.encode().decode('latin1'), '-12', 'a' * 4096]:
s.text('N', text, 'job-name-grammar', 'A02-A03,A34')
lists = ['', '0', '255', '0-255', '0-15:2/4', '3-14:2/5', '254-255:1/256',
'255-255:256/256', '0-0:1/1', '1-9:9/256', '000001-000009:00002/00004',
'5,1-7,5,0-0', '0' * 4096, '0' * 4095 + '1', '0' * 4097,
'0' * 4094 + '255', '9' * 4096, '0-255,256', '0-255,0-0:2/1']
lists += [' ', '0 ', ' 0', '0\n', '1\x00', '\xff', '-1', '+1', '1.0', '1e2',
',0', '0,', '0,,1', '1:1/1', '2-1', '0-256', '0-1:0/1', '0-1:1/0',
'0-1:2/1', '0-1:1/257', '0-1:257/257', '0-1:1', '0-1:/1', '0--1',
'0-1:1/1/', '0-1:1/1:1/1', '0-1:', '0-', '0-1/1', '0-1:1//1']
for value in lists:
error, ids = s.text('L', value, 'list-grammar-group-and-completion', 'A01,A03-A08,A34')[1:3]
if not error:
s.formatted('FL', ids, 'parsed-list-canonical-format', 'A01,A03-A04,A09,A34')
s.formatted('FM', ids, 'list-to-mask-value', 'A01,A03-A04,A12,A34')
masks = ['0', '1,0', '0,1', '80000000,00000001', '00000000,00000001',
'FFFFFFFF', '80000000,' + '00000000,' * 6 + '00000001', ','.join(['ffffffff'] * 8),
','.join(['0'] * 8), ','.join(['0'] * 9), '', '0x1', '+1', '-1', ' 1',
'1 ', '000000000', 'g', ',1', '1,', '1,,2', '1\x00', '\xff', '0' * 4096, '0' * 4097]
for value in masks:
error, ids = s.text('M', value, 'mask-group-width-order-and-grammar', 'A01,A03-A04,A10-A11,A34')[1:3]
if not error:
s.formatted('FM', ids, 'parsed-mask-canonical-format', 'A01,A03-A04,A12,A34')
s.formatted('FL', ids, 'mask-to-list-value', 'A01,A03-A04,A09,A34')
for core in range(256):
value = '%x' % (1 << (core % 32))
value += ',00000000' * (core // 32)
s.text('M', value, 'every-mask-bit-high-core', 'A01,A03-A04,A10-A11,A34')
s.formatted('FM', [core], 'every-bit-canonical-mask', 'A01,A03-A04,A12,A34')
s.text('L', '%d-%d:256/256' % (core, core), 'every-list-singleton-group', 'A01,A03-A08,A34')
for ids in [[], [255, 0, 32, 31], list(range(255, -1, -1)), [0, 0], [256], [65535],
[4, 3, 2, 1], [1, 3, 5, 7], [1] * 4096]:
for op in ['FL', 'FM']:
s.formatted(op, ids, 'direct-set-normalize-or-clear', 'A01,A03-A04,A09,A12,A34')
for operation in [0, 1, 2, 3, -1, -(1 << 31), (1 << 31) - 1]:
for left, right in [([], []), ([0, 255], [255, 32]), ([], [256]), ([0, 0], []), ([3], [3, 3])]:
s.combined(operation, left, right, 'complete-set-operation-validation', 'A01,A03-A04,A13,A34')
for _ in range(150):
start, end = sorted(rng.sample(range(256), 2))
group = rng.randrange(1, 257)
used = rng.randrange(1, group + 1)
text = '%d-%d:%d/%d,%d,%d-%d' % (start, end, used, group, start, end, end)
_, _, ids, _ = s.text('L', text, 'random-group-tail-overlap', 'A01,A03-A08,A34')
_, _, mask, _ = s.formatted('FM', ids, 'random-list-mask-format', 'A01,A03-A04,A12,A34')
s.text('M', mask.upper(), 'random-cross-format-roundtrip', 'A01,A03-A04,A10-A11,A34')
other = rng.sample(range(256), rng.randrange(20))
s.combined(rng.randrange(3), ids, other, 'random-set-operation', 'A01,A03-A04,A13,A34')
field = [[[0, 1], [1, 1]], [['A', 20, [0, 1], None], ['B', 10, [0], None]]]
s.exercise(field, 'public-augmenting-reproduction')
c('H', 'hold-field-plan', 'A03,A25,A33-A34')
global_previous = [[[0, 1], [1, 1], [2, 1]],
[['A', 3, [0, 1], 0], ['B', 2, [0, 2], 0], ['C', 1, [1, 2], 2]]]
s.exercise(global_previous, 'global-previous-conflict')
c('Q', 'field-plan-value-independent', 'A03,A25,A33-A34')
directed = [
[[], []], [[[255, 8], [0, 1]], []], [[], [['Z', -(1 << 31), [0, 255], 0]]],
[[[0, 1]], [['late', -1, [0], 0], ['early', 1, [0], 255]]],
[[[255, 1], [32, 2], [0, 1]], [['z', 0, [255, 0, 32], None], ['A', 0, [32, 0], None],
['a', 0, [255, 32], 255], ['-', 0, [0, 32], 32]]],
[[[1, 1]], [['A', -(1 << 31), [1], None], ['Z', (1 << 31) - 1, [1], None]]],
[[[31, 1], [32, 1], [255, 1]], [['A', 0, [31, 32], 255], ['B', 0, [31, 255], 129]]],
[[[0, 1], [255, 1]], [['A', 0, [0, 255], 255], ['B', 0, [0, 255], 0], ['C', 0, [0], 0]]],
[[[255, 8]], [['T%02d' % i, i % 3 - 1, [255], 255] for i in range(24)]],
[[[i * 8, 8] for i in range(32)],
[['M%02d' % i, -(1 << 31), [j * 8 for j in range(32)], 0] for i in range(24)]],
[[[i * 8, 1] for i in range(24)],
[['A%02d' % i, 0, list(range(0, (24 - i) * 8, 8)), None] for i in range(24)]],
[[[0, 8], [63, 8], [255, 8]], [['J%02d' % i, 0, [0, 63, 255], 255] for i in range(24)]],
[[[7, 1]], [['-' * 32, 0, list(range(256)), 255]]],
]
for problem in directed:
s.exercise(problem, 'directed-three-objective-boundary')
reversed_problem = [list(reversed(problem[0])), list(reversed(copy.deepcopy(problem[1])))]
for job in reversed_problem[1]:
job[2].reverse()
s.exercise(reversed_problem, 'input-permutation-invariance')
for size in [1, 2, 7, 8, 9, 23, 24]:
cores = [[0, 8], [127, 8], [255, 8]]
jobs = [['Z%02d' % i, 0, [0, 127, 255], rng.choice([0, 127, 255, None])] for i in range(size)]
s.exercise([cores, jobs], 'capacity-and-priority-ties')
invalid = [
[[[0, 0]], []], [[[0, 9]], []], [[[256, 1]], []], [[[65535, 1]], []], [[[0, 1], [0, 2]], []],
[[[i, 1] for i in range(33)], []], [[], [['a%02d' % i, 0, [], None] for i in range(25)]],
[[], [['A', 0, [], None], ['A', 1, [], None]]],
]
for name in ['', 'a.b', 'x' * 33, '\x00', '\xff']:
invalid.append([[[0, 1]], [[name, -(1 << 31), [], None]]])
for allowed, previous in [([0, 0], None), ([256], None), ([65535], None), ([], 256), ([], 65535)]:
invalid.append([[], [['valid', 0, allowed, previous]]])
for problem in invalid:
s.exercise(problem, 'full-invalid-problem-clears-output')
c('Q', 'failure-preserves-held-plan', 'A03-A04,A25,A34')
# Complete byte records in arbitrary order, all truncations, and malformed late fields.
binary_problem = [[[255, 8], [0, 1], [32, 2]],
[['z', -(1 << 31), [255, 0, 7, 8, 31, 32, 63, 64, 127, 128], 255],
['A' * 32, (1 << 31) - 1, [], None], ['B', 0, [1, 254], 0]]]
data = raw_image(binary_problem)
s.exercise(binary_problem, 'binary-low-byte-order-and-int32-extremes')
for length in range(len(data)):
s.image(data[:length], 'every-image-prefix-is-truncated')
for extra in [b'\x00', b'\x00' * 100, data, b'\xff']:
s.image(data + extra, 'image-trailing-bytes-rejected')
for length in [0, 1, 7, 8, 4096, 4097]:
s.image(b'\x00' * length, 'image-total-length-and-magic')
for offset, value in [(0, 0), (4, 33), (5, 1), (6, 25), (7, 1),
(8, 0), (9, 1), (10, 0), (10, 9), (11, 1),
(20, 0), (20, 33), (21, 1), (22, 0), (23, 1)]:
changed = bytearray(data)
changed[offset] = value
s.image(bytes(changed), 'image-header-field-validation')
late = bytearray(data)
last_header = 8 + 12 + 41 + 72
for offset, value in [(last_header + 1, 1), (last_header + 3, 1),
(last_header + 40, ord('.'))]:
changed = bytearray(late)
changed[offset] = value
s.image(bytes(changed), 'late-invalid-record-no-partial-publication')
for problem in [
[[[0, 1], [0, 2]], []], [[], [['A', 0, [], None], ['A', 1, [255], None]]],
[[[0, 0]], []], [[], [['bad.id', 0, [], None]]], [[], [['valid', 0, [], 256]]],
]:
s.image(raw_image(problem), 'binary-full-semantic-validation')
# Fixed-seed varied matching instances. Every raw, codec and retained Plan is observable.
universe = [0, 1, 7, 8, 31, 32, 63, 64, 127, 128, 254, 255]
for index in range(200):
online = rng.sample(universe, rng.randrange(0, 7))
cores = [[core, rng.randrange(1, 4)] for core in online]
jobs = [['J%02d' % i, rng.choice([-(1 << 31), -1, 0, 0, 1, (1 << 31) - 1]),
rng.sample(universe, rng.randrange(0, len(universe) + 1)),
rng.choice([None] + universe)] for i in range(rng.randrange(0, 10))]
rng.shuffle(jobs)
if index % 13 == 0:
c('G ' + str(index % 3), 'random-independent-bank', 'A03,A25,A33-A34')
s.exercise([cores, jobs], 'random-capacity-placement')
if index % 7 == 0:
c('H', 'random-save-plan', 'A03,A25,A33-A34')
c('Q', 'saved-plan-not-mutated', 'A03,A25,A33-A34')
for index in range(25):
ids = rng.sample(range(256), 32)
cores = [[core, rng.randrange(1, 9)] for core in ids]
jobs = [['L%02d' % i, rng.choice([-(1 << 31), 0, (1 << 31) - 1]),
rng.sample(ids + [core for core in universe if core not in ids], rng.randrange(1, 20)),
rng.choice([None] + ids)] for i in range(24)]
rng.shuffle(jobs)
s.exercise([cores, jobs], 'maximum-domain-varied-placement')
for bank in [2, 0, 1, 2, 0]:
c('G ' + str(bank), 'final-bank-values-retained', 'A03,A25,A33-A34')
c('Q', 'final-old-plan-independent', 'A03,A25,A33-A34')
c('P', 'repeat-call-pure-value', 'A03-A04,A14-A25,A34')
return s
def main():
suite = cases()
scratch = Path(tempfile.mkdtemp(prefix='embedded-hard-affinity-', dir='/tmp'))
for name, content in CONTEXT.items():
(scratch / name).write_text(content)
for name in CANDIDATES:
(scratch / name).write_bytes((Path(sys.argv[1]) / name).read_bytes())
command_input = '\n'.join(suite.commands) + '\n'
built = subprocess.run(['g++', '-std=c++17', '-O2', '-Wall', '-Wextra', '-pedantic',
'affinity_observe.cpp', *CANDIDATES, '-o', 'observe'], cwd=scratch,
text=True, capture_output=True, timeout=30)
if built.returncode:
print('check=build clauses=A31-A32 build failed: ' + built.stderr[-1500:].replace('\n', ' '))
return 1
# shortcut: scratch is retained for container teardown; exact trusted-local cleanup is external.
# After candidate execution the trusted checker never reads/writes/traverses the shared scratch.
observed = subprocess.run([str(scratch / 'observe')], input=command_input, cwd=scratch,
text=True, capture_output=True, timeout=15)
if observed.returncode:
print('check=execution clauses=A32-A33 exit=%d stderr=%s' %
(observed.returncode, observed.stderr[-300:]))
return 1
actual = observed.stdout.splitlines()
failures = []
for index in range(max(len(actual), len(suite.expected))):
raw = actual[index] if index < len(actual) else '<missing>'
try:
got = json.loads(raw)
except (ValueError, TypeError):
got = raw
expected = suite.expected[index] if index < len(suite.expected) else '<end>'
if (index >= len(actual) or index >= len(suite.expected) or
json.dumps(got, sort_keys=True) != json.dumps(expected, sort_keys=True)):
label, clauses = suite.labels[index] if index < len(suite.labels) else ('extra-output', 'A33')
failures.append((index, label, clauses, got, expected))
if failures:
for index, label, clauses, got, expected in failures[:20]:
print('check=%s clauses=%s observation=%d got=%r expected=%r' %
(label, clauses, index, str(got)[:200], str(expected)[:200]))
print('mismatches=%d observations=%d' % (len(failures), len(suite.expected)))
return 1
print('PASS')
return 0
if __name__ == '__main__':
raise SystemExit(main())