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

243 lines
7.8 KiB
C++

#include "attribute.hpp"
#include <array>
#include <charconv>
#include <iostream>
#include <limits>
#include <sstream>
#include <string>
#include <vector>
namespace {
std::vector<std::string> split(const std::string& line)
{
std::vector<std::string> fields;
size_t first = 0;
while (true) {
size_t next = line.find('\t', first);
if (next == std::string::npos) {
fields.push_back(line.substr(first));
break;
}
fields.push_back(line.substr(first, next - first));
first = next + 1;
}
return fields;
}
long long number(const std::string& text)
{
long long value = 0;
const char* first = text.data();
const char* last = first + text.size();
auto converted = std::from_chars(first, last, value);
if (converted.ec != std::errc() || converted.ptr != last) {
throw std::invalid_argument("number");
}
return value;
}
int nibble(char ch)
{
if (ch >= '0' && ch <= '9') {
return ch - '0';
}
if (ch >= 'a' && ch <= 'f') {
return ch - 'a' + 10;
}
throw std::invalid_argument("hex");
}
std::string unhex(const std::string& encoded)
{
if (encoded.size() % 2 != 0) {
throw std::invalid_argument("hex");
}
std::string bytes;
for (size_t i = 0; i < encoded.size(); i += 2) {
int value = nibble(encoded[i]) * 16 + nibble(encoded[i + 1]);
bytes.push_back(static_cast<char>(value));
}
return bytes;
}
std::string hex(const char* bytes, size_t count)
{
const char* alphabet = "0123456789abcdef";
std::string encoded;
for (size_t i = 0; i < count; ++i) {
unsigned char value = static_cast<unsigned char>(bytes[i]);
encoded.push_back(alphabet[value >> 4]);
encoded.push_back(alphabet[value & 15]);
}
return encoded;
}
void result(long long rc)
{
std::cout << "[\"R\"," << rc << "]\n";
}
void fields(const attr::Snapshot& captured)
{
std::cout << ',' << captured.value;
std::cout << ',' << captured.epoch;
std::cout << ',' << captured.generation;
std::cout << ',' << captured.minimum;
std::cout << ',' << captured.maximum;
std::cout << ',' << (captured.writable ? 1 : 0);
}
attr::Snapshot sentinel()
{
return attr::Snapshot{777, 888, 999, -123, 456, false};
}
void captured(attr::Service& service, int slot)
{
auto state = sentinel();
int rc = service.snapshot(slot, state);
std::cout << "[\"S\"," << rc;
fields(state);
std::cout << "]\n";
}
void shown(attr::Service& service, int slot, size_t capacity, bool null)
{
std::vector<char> bytes(32, '#');
int rc = service.show(slot, null ? nullptr : bytes.data(), capacity);
std::cout << "[\"H\"," << rc << ",\"";
std::cout << hex(bytes.data(), bytes.size()) << "\"]\n";
}
attr::Callback listener(attr::Service& service, int mode, int64_t target)
{
return [&service, mode, target](const attr::Notice& notice) {
auto state = sentinel();
int rc = service.snapshot(notice.slot, state);
std::array<char, 32> bytes;
bytes.fill('#');
int show_rc = service.show(notice.slot, bytes.data(), bytes.size());
int action = -999;
std::array<int64_t, 4> probes{-999, -999, -999, -999};
if (mode == 1) {
action = service.cancel(target);
}
if (mode == 2) {
action = service.cancel(notice.token);
}
if (mode == 3) {
probes[0] = service.create(7, "callback", 0, 1, 0, true);
probes[1] = service.drop(notice.slot);
probes[2] = service.store(notice.slot, "0");
probes[3] = service.watch(notice.slot, [](const attr::Notice&) {});
}
std::cout << "[\"N\"," << notice.slot << ',' << notice.token;
std::cout << ',' << notice.epoch << ',' << notice.generation;
std::cout << ',' << rc;
fields(state);
std::cout << ',' << show_rc << ",\"";
std::cout << hex(bytes.data(), bytes.size()) << "\"," << action;
for (auto probe : probes) {
std::cout << ',' << probe;
}
std::cout << ',' << service.active_count();
std::cout << ',' << service.watcher_count();
std::cout << ',' << service.lookup("counter") << "]\n";
};
}
void execute(attr::Service& service, const std::vector<std::string>& f)
{
const std::string& op = f.at(0);
if (op == "I" && f.size() == 4) {
attr::Service first;
attr::Service second;
int initial_first = number(f[1]);
int initial_second = number(f[2]);
int first_rc = first.create(0, "same", INT32_MIN, INT32_MAX, initial_first, true);
int second_rc = second.create(0, "same", INT32_MIN, INT32_MAX, initial_second, true);
size_t delivered = 0;
int64_t token = first.watch(0, [&delivered](const attr::Notice&) { ++delivered; });
int write_rc = first.store(0, unhex(f[3]));
auto first_state = sentinel();
auto second_state = sentinel();
int read_first = first.snapshot(0, first_state);
int read_second = second.snapshot(0, second_state);
std::cout << "[\"I\"," << first_rc << ',' << second_rc << ',' << token;
std::cout << ',' << write_rc << ',' << delivered << ',' << read_first;
fields(first_state);
std::cout << ',' << read_second;
fields(second_state);
std::cout << ',' << first.watcher_count() << ',' << second.watcher_count() << "]\n";
return;
}
if (op == "C" && f.size() == 7) {
result(service.create(number(f[1]), f[2], number(f[3]),
number(f[4]), number(f[5]), number(f[6]) != 0));
return;
}
if (op == "D" && f.size() == 2) {
result(service.drop(number(f[1])));
return;
}
if (op == "L" && f.size() == 2) {
result(service.lookup(f[1]));
return;
}
if (op == "S" && f.size() == 2) {
captured(service, number(f[1]));
return;
}
if (op == "H" && f.size() == 4) {
shown(service, number(f[1]), number(f[2]), number(f[3]) != 0);
return;
}
if (op == "W" && f.size() == 3) {
result(service.store(number(f[1]), unhex(f[2])));
return;
}
if (op == "B" && f.size() == 4) {
int mode = number(f[2]);
attr::Callback callback;
if (mode >= 0) {
callback = listener(service, mode, number(f[3]));
}
result(service.watch(number(f[1]), callback));
return;
}
if (op == "X" && f.size() == 2) {
result(service.cancel(number(f[1])));
return;
}
if (op == "A" && f.size() == 1) {
std::cout << "[\"A\"," << service.active_count();
std::cout << ',' << service.watcher_count() << "]\n";
return;
}
if (op == "P" && f.size() == 2) {
int32_t value = 777;
int rc = attr::parse_value(unhex(f[1]), value);
std::cout << "[\"P\"," << rc << ',' << value << "]\n";
return;
}
if (op == "F" && f.size() == 4) {
std::array<char, 32> bytes;
bytes.fill('#');
char* destination = number(f[3]) ? nullptr : bytes.data();
int rc = attr::format_value(number(f[1]), destination, number(f[2]));
std::cout << "[\"H\"," << rc << ",\"";
std::cout << hex(bytes.data(), bytes.size()) << "\"]\n";
return;
}
if (op == "V" && f.size() == 2) {
result(attr::valid_name(unhex(f[1])) ? 1 : 0);
return;
}
throw std::invalid_argument("command");
}
}
int main()
{
attr::Service service;
std::string line;
while (std::getline(std::cin, line)) {
try {
auto command = split(line);
execute(service, command);
} catch (const std::exception&) {
std::cerr << "invalid console command\n";
return 2;
}
}
return 0;
}