Files
ziran/tests/host_array_test.c
waozi 96c23c7de6 Use defining modules for portable host type identity
Resolve record and enum names through their defining module when writing the
host interface, including container element names. Match package-qualified
module names against plain host module names without accepting other modules.
Update native host assertions and the generated bootstrap lowerer.

Local tests: host records, slices, arrays, capability, VM payloads and browser
lifecycle passed; check-bootstrap passed. Full check initially 275/281; the two
wire-name assertion failures now pass. Remaining failures were unrelated
placement/debug output and a stale generated site page.

Reported-by: Breeze task codex-breeze-breeze-crash-recovery-ui
2026-11-16 16:13:27 -03:00

120 lines
4.3 KiB
C

#include "ziran_host.h"
#include <assert.h>
#include <string.h>
static int malformed;
static int calls;
static HostValue boxes[2];
static HostField fields[2][2];
static HostValue sums[2], sum_cells[2][2];
/* A [2][2]u8 argument, checked against the values Answer stores. */
static void
expect_grid(const HostValue *grid, int scale)
{
assert(grid->kind == HOST_ARRAY && strcmp(grid->type, "[2][2]u8") == 0);
assert(grid->length == 2);
for(int i = 0; i < 2; i++) {
const HostValue *row = &grid->elements[i];
assert(row->kind == HOST_ARRAY && strcmp(row->type, "[2]u8") == 0);
assert(row->length == 2);
for(int j = 0; j < 2; j++) {
assert(row->elements[j].kind == HOST_UNSIGNED);
assert(row->elements[j].bits == (uint64_t)((i * 2 + j + 1) * scale));
}
}
}
static int
host_call(void *context, const char *module, const char *function,
const HostValue *args, int arg_count, HostValue *result)
{
(void)context;
assert(strcmp(module, "arrays") == 0);
if(strcmp(function, "AddGrid") == 0) {
assert(arg_count == 2);
assert(args[0].kind == HOST_RECORD && args[0].field_count == 1);
assert(strcmp(args[0].fields[0].name, "cells") == 0);
expect_grid(&args[0].fields[0].value, 1);
expect_grid(&args[1], 10);
calls++;
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++)
sum_cells[i][j] = (HostValue){.kind = HOST_UNSIGNED, .type = "u8",
.bits = (uint64_t)((i * 2 + j + 1) * 11)};
sums[i] = (HostValue){.kind = HOST_ARRAY, .type = "[2]u8",
.elements = sum_cells[i], .length = 2};
}
result->kind = HOST_ARRAY;
result->length = 2;
result->elements = sums;
return 1;
}
assert(arg_count == 1);
assert(args[0].kind == HOST_ARRAY);
assert(args[0].field_count == 0 && args[0].fields == NULL);
calls++;
if(strcmp(function, "EchoEmpty") == 0) {
assert(strcmp(args[0].type, "[0]s32") == 0);
assert(args[0].length == 0 && args[0].elements == NULL);
result->kind = HOST_ARRAY;
result->length = malformed == 1 ? 1 : 0;
result->elements = NULL;
return 1;
}
assert(strcmp(function, "EchoBoxes") == 0);
assert(strcmp(args[0].type, "[2]arrays.Box") == 0);
assert(args[0].length == 2 && args[0].elements != NULL);
for(int i = 0; i < 2; i++) {
const HostValue *box = &args[0].elements[i];
assert(box->kind == HOST_RECORD);
assert(strcmp(box->type, "arrays.Box") == 0);
assert(box->field_count == 2);
assert(strcmp(box->fields[0].name, "empty") == 0);
assert(box->fields[0].value.kind == HOST_ARRAY);
assert(strcmp(box->fields[0].value.type, "[0]s32") == 0);
assert(box->fields[0].value.length == 0);
assert(box->fields[0].value.elements == NULL);
assert(strcmp(box->fields[1].name, "value") == 0);
assert(box->fields[1].value.integer == (i == 0 ? 40 : 2));
fields[i][0] = (HostField){"empty", {
.kind = HOST_ARRAY, .type = "[0]s32"}};
fields[i][1] = (HostField){"value", {
.kind = HOST_INTEGER, .type = "s32", .integer = i == 0 ? 40 : 2}};
boxes[i] = (HostValue){.kind = HOST_RECORD, .type = "arrays.Box",
.fields = fields[i], .field_count = 2};
}
if(malformed == 2)
fields[1][0].value.kind = HOST_INTEGER;
result->kind = HOST_ARRAY;
result->length = malformed == 3 ? 1 : 2;
result->elements = boxes;
return 1;
}
int
main(int argc, char **argv)
{
assert(argc == 2);
Bundle *bundle = BundleOpen(argv[1]);
assert(bundle != NULL);
assert(BundleCapabilityCount(bundle) == 3);
HostBinding bindings[3] = {
{"arrays", "EchoEmpty", host_call, NULL},
{"arrays", "EchoBoxes", host_call, NULL},
{"arrays", "AddGrid", host_call, NULL}
};
long long result = 0;
int has_result = 0;
assert(BundleRun(bundle, bindings, 3, &result, &has_result));
assert(has_result && result == 42 && calls == 3);
for(malformed = 1; malformed <= 3; malformed++)
assert(!BundleRun(bundle, bindings, 3, &result, &has_result));
malformed = 0;
assert(BundleRun(bundle, bindings, 3, &result, &has_result));
assert(has_result && result == 42);
BundleClose(bundle);
return 0;
}