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

232 lines
6.5 KiB
C

#include "fork_model.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
static int number(const char *word, int *value)
{
unsigned int result = 0;
size_t index = 0;
int negative = 0;
if (word == NULL || word[0] == '\0') {
return 0;
}
if (word[0] == '-') {
negative = 1;
index = 1;
if (word[index] == '\0') return 0;
}
for (; word[index] != '\0'; ++index) {
unsigned int digit;
if (word[index] < '0' || word[index] > '9') {
return 0;
}
digit = (unsigned int)(word[index] - '0');
if (result > (1000000U - digit) / 10) {
return 0;
}
result = result * 10 + digit;
}
*value = negative ? -(int)result : (int)result;
return 1;
}
static int nibble(char value)
{
if (value >= '0' && value <= '9') {
return value - '0';
}
if (value >= 'a' && value <= 'f') {
return value - 'a' + 10;
}
if (value >= 'A' && value <= 'F') {
return value - 'A' + 10;
}
return -1;
}
static int decode(const char *word, unsigned char *bytes, size_t *length)
{
size_t count;
size_t index;
if (strcmp(word, "-") == 0) {
*length = 0;
return 1;
}
count = strlen(word);
if (count % 2 != 0 || count > 2 * (FM_BUFFER_CAP + 1)) {
return 0;
}
for (index = 0; index < count / 2; ++index) {
int high = nibble(word[2 * index]);
int low = nibble(word[2 * index + 1]);
if (high < 0 || low < 0) {
return 0;
}
bytes[index] = (unsigned char)((high << 4) | low);
}
*length = count / 2;
return 1;
}
static void hex(const unsigned char *bytes, size_t length)
{
size_t index;
putchar('"');
for (index = 0; index < length; ++index) {
printf("%02x", (unsigned int)bytes[index]);
}
putchar('"');
}
static void observe(const struct fm_model *model, int result)
{
int process;
int stream;
int description;
printf("{\"rc\":%d,\"capacity\":%zu,\"processes\":[", result, model->capacity);
for (process = 0; process < FM_PROCESS_CAP; ++process) {
const struct fm_process *entry = &model->processes[process];
if (process != 0) {
putchar(',');
}
printf("[%d,%d,%u,[", entry->used, entry->alive, entry->code);
for (stream = 0; stream < FM_STREAM_CAP; ++stream) {
const struct fm_stream *view = &entry->streams[stream];
if (stream != 0) {
putchar(',');
}
printf("[%d,", view->description);
hex(view->pending, view->length);
putchar(']');
}
printf("]]");
}
printf("],\"descriptions\":[");
for (description = 0; description < FM_DESCRIPTION_CAP; ++description) {
const struct fm_description *entry = &model->descriptions[description];
if (description != 0) {
putchar(',');
}
printf("[%zu,%u,%u,", entry->offset, entry->references, entry->commits);
hex(entry->bytes, entry->length);
putchar(']');
}
printf("]}\n");
}
static int command(struct fm_model *model, char **words, size_t count)
{
int args[3] = {0, 0, 0};
unsigned char bytes[FM_BUFFER_CAP + 1];
size_t length;
size_t index;
size_t numeric = count - 1;
int writing = strcmp(words[0], "WRITE") == 0;
if (writing) {
if (count != 4) return -1;
numeric = 2;
}
if (numeric > 3) {
return -1;
}
for (index = 0; index < numeric; ++index) {
if (!number(words[index + 1], &args[index])) {
return -1;
}
}
if (count == 2 && strcmp(words[0], "INIT") == 0) {
return fm_init(model, args[0]);
}
if (count == 4 && strcmp(words[0], "OPEN") == 0) {
return fm_open(model, (int)args[0], (int)args[1], (int)args[2]);
}
if (count == 4 && strcmp(words[0], "NULL") == 0) {
return fm_write(model, args[0], args[1], NULL, (size_t)args[2]);
}
if (count == 4 && writing && decode(words[3], bytes, &length)) {
return fm_write(model, (int)args[0], (int)args[1], bytes, length);
}
if (count == 3 && strcmp(words[0], "FLUSH") == 0) {
return fm_flush(model, (int)args[0], (int)args[1]);
}
if (count == 3 && strcmp(words[0], "CLOSE") == 0) {
return fm_close(model, (int)args[0], (int)args[1]);
}
if (count == 4 && strcmp(words[0], "SEEK") == 0) {
return fm_seek(model, (int)args[0], (int)args[1], args[2]);
}
if (count == 3 && strcmp(words[0], "FORK") == 0) {
return fm_fork(model, (int)args[0], (int)args[1]);
}
if (count == 4 && strcmp(words[0], "EXIT") == 0) {
return fm_exit(model, (int)args[0], (int)args[1], args[2]);
}
return -1;
}
int main(void)
{
struct fm_model models[2];
struct fm_model *model = &models[0];
char *line = NULL;
size_t allocation = 0;
ssize_t length;
fm_init(&models[0], FM_FILE_CAP);
fm_init(&models[1], FM_FILE_CAP);
while ((length = getline(&line, &allocation, stdin)) >= 0) {
char *words[6];
size_t count = 0;
char *cursor = line;
int result;
if (length > 0 && line[length - 1] == '\n') {
line[--length] = '\0';
}
if (memchr(line, '\0', (size_t)length) != NULL) {
free(line);
return 2;
}
while (*cursor != '\0') {
while (*cursor == ' ' || *cursor == '\t') {
++cursor;
}
if (*cursor == '\0') {
break;
}
if (count == 6) {
free(line);
return 2;
}
words[count++] = cursor;
while (*cursor != '\0' && *cursor != ' ' && *cursor != '\t') {
++cursor;
}
if (*cursor != '\0') {
*cursor++ = '\0';
}
}
if (count == 0) {
continue;
}
if (strcmp(words[0], "USE") == 0) {
int selected;
if (count != 2 || !number(words[1], &selected) || selected < 0 || selected > 1) {
free(line);
return 2;
}
model = &models[selected];
result = FM_OK;
} else {
result = command(model, words, count);
}
if (result < 0) {
free(line);
return 2;
}
observe(model, result);
}
free(line);
return ferror(stdin) ? 2 : 0;
}