amos 8b7616bfd8 Batch 22 (20261027-C-hwe-hard-22): add 63 calibrated HWE hard-design tasks
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>
2026-10-27 16:14:57 +08:00

157 lines
7.2 KiB
C++

#include "latch.hpp"
#include <iostream>
#include <limits>
#include <string>
#include <vector>
namespace {
using release::Count;
using release::Id;
std::vector<std::string> fields(const std::string &line)
{
std::vector<std::string> result;
std::size_t start = 0;
for (;;) {
std::size_t end = line.find('\t', start);
result.push_back(end == std::string::npos ? line.substr(start) : line.substr(start, end-start));
if (end == std::string::npos) return result;
start = end + 1;
}
}
bool number(const std::string &text, Count &value)
{
if (text.empty()) return false;
for (unsigned char byte : text) if (byte < '0' || byte > '9') return false;
Count result = 0;
Count limit = std::numeric_limits<Count>::max();
for (unsigned char byte : text) {
unsigned digit = byte - '0';
if (result > limit / 10 || (result == limit / 10 && digit > limit % 10)) return false;
result = result * 10 + digit;
}
value = result;
return true;
}
int nibble(char c)
{
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
return -1;
}
bool unhex(const std::string &text, std::string &out)
{
out.clear();
if (text == "-") return true;
if (text.empty() || text.size() % 2) return false;
for (std::size_t i = 0; i < text.size(); i += 2) {
int a = nibble(text[i]), b = nibble(text[i+1]);
if (a < 0 || b < 0) return false;
out.push_back(static_cast<char>(a * 16 + b));
}
return true;
}
std::string hex(const std::string &value)
{
if (value.empty()) return "-";
const char *digits = "0123456789abcdef";
std::string out;
for (unsigned char byte : value) {
out.push_back(digits[byte >> 4]);
out.push_back(digits[byte & 15]);
}
return out;
}
void raw_snapshot(const release::Snapshot &v)
{
std::cout << v.target << ' ' << v.remaining << ' ' << v.open << ' ' << v.revision
<< ' ' << v.broadcasts << ' ' << v.participants << ' ' << v.completed
<< ' ' << v.waiting << ' ' << v.released << ' ' << v.canceled
<< ' ' << v.queued << ' ' << v.next_ticket;
}
}
int main()
{
release::Latch objects[2];
unsigned selected = 0;
std::string line;
while (std::getline(std::cin, line)) {
if (std::cin.eof()) return 2;
if (line.find('\0') != std::string::npos) return 2;
auto args = fields(line);
const auto &op = args[0];
release::Latch &latch = objects[selected];
Count a = 0, b = 0, c = 0;
std::string x, y, z;
if (op == "U" && args.size() == 2 && number(args[1], a) && a < 2) {
selected = static_cast<unsigned>(a);
std::cout << "U " << selected << '\n';
} else if ((op == "R" || op == "C") && args.size() == 2 && number(args[1], a)) {
std::cout << op << ' ' << (op == "R" ? latch.reset(a) : latch.count_down(a)) << '\n';
} else if ((op == "J" || op == "D" || op == "W" || op == "X" || op == "P" || op == "A") &&
args.size() == 2 && number(args[1], a) && a <= UINT32_MAX) {
Id id = static_cast<Id>(a);
int result = op == "J" ? latch.join(id) : op == "D" ? latch.complete(id) :
op == "W" ? latch.await(id) : op == "X" ? latch.cancel(id) :
op == "P" ? latch.poll(id) : latch.participant(id);
std::cout << op << ' ' << result << '\n';
} else if (op == "E" && args.size() == 4 && number(args[1], a) && a <= 2 &&
number(args[2], b) && b <= UINT32_MAX && number(args[3], c)) {
release::WaitInfo info{static_cast<release::WaitState>(a), c};
int result = latch.inspect(static_cast<Id>(b), info);
std::cout << "E " << result << ' ' << static_cast<unsigned>(info.state) << ' ' << info.ticket << '\n';
} else if (op == "T" && args.size() == 4 && number(args[1], a) && a <= 2 &&
number(args[2], b) && unhex(args[3], x)) {
release::WaitInfo info{static_cast<release::WaitState>(a), b};
int result = release::parse_waiter(x, info);
std::cout << "T " << result << ' ' << static_cast<unsigned>(info.state) << ' ' << info.ticket << '\n';
} else if (op == "V" && args.size() == 3 && number(args[1], a) && a <= UINT32_MAX && number(args[2], b)) {
release::WaitInfo info{release::WaitState::canceled, b};
int result = latch.inspect(static_cast<Id>(a), info);
std::cout << "V " << result << ' ' << static_cast<unsigned>(info.state) << ' ' << info.ticket << '\n';
} else if (op == "N" && args.size() == 2 && number(args[1], a) && a <= UINT32_MAX) {
Id id = static_cast<Id>(a);
int result = latch.take(id);
std::cout << "N " << result << ' ' << id << '\n';
} else if (op == "Q" && args.size() == 1) {
auto value = latch.snapshot();
std::cout << "Q ";
raw_snapshot(value);
std::cout << ' ' << hex(release::format_snapshot(value)) << '\n';
} else if (op == "I" && args.size() == 4 && unhex(args[1], x) && unhex(args[2], y) && unhex(args[3], z)) {
std::cout << "I " << release::initialize(latch, x, y, z) << '\n';
} else if (op == "H" && args.size() == 4 && number(args[1], a) && number(args[2], b) && unhex(args[3], x)) {
int result = release::parse_number(x, a, b);
std::cout << "H " << result << ' ' << b << '\n';
} else if (op == "L" && args.size() == 2 && unhex(args[1], x)) {
std::vector<Id> ids{900, 901};
int result = release::parse_ids(x, ids);
std::cout << "L " << result << ' ' << ids.size();
for (Id id : ids) std::cout << ' ' << id;
std::cout << '\n';
} else if (op == "B" && args.size() == 3 && number(args[1], b) && unhex(args[2], x)) {
release::WaitInfo info{release::WaitState::canceled, b};
int result = release::parse_waiter(x, info);
std::cout << "B " << result << ' ' << static_cast<unsigned>(info.state) << ' ' << info.ticket << '\n';
} else if (op == "G" && args.size() == 3 && number(args[1], a) && a <= 2 && number(args[2], b)) {
release::WaitInfo value{static_cast<release::WaitState>(a), b};
std::cout << "G " << hex(release::format_waiter(value)) << '\n';
} else if (op == "O" && args.size() >= 2 && number(args[1], a) && a == args.size()-2) {
std::vector<Id> ids;
for (std::size_t i = 2; i < args.size(); ++i) {
if (!number(args[i], b) || b > UINT32_MAX) return 2;
ids.push_back(static_cast<Id>(b));
}
std::cout << "O " << hex(release::format_ids(ids)) << '\n';
} else if (op == "F" && args.size() == 13) {
Count v[12];
for (unsigned i = 0; i < 12; ++i) if (!number(args[i+1], v[i])) return 2;
release::Snapshot s{v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7],v[8],v[9],v[10],v[11]};
std::cout << "F " << hex(release::format_snapshot(s)) << '\n';
} else {
return 2;
}
}
return std::cin.bad() ? 2 : 0;
}