/* SPDX-License-Identifier: LicenseRef-Starship-1.0 * Copyright (c) 2023-2026 Robert A. James */ /* test_fabric_walk.c -- mechanism A (docs/v4.0.0/plans/2026-11-17-the-fabric-step-cost.md 4): the fabric * keeps the places that hold a node, and a step walks them, not every place. Their order is the places' * order, as before: v4_fabric_first and v4_fabric_next give the occupied places in ascending number, and * the capacity when there are no more. Checked: empty; nodes added, removed and added again (the order kept); * a node removed while the walk is at it (the walk goes on to the next); one node in sixteen places (a walk * of one, the boot's case); and grown room (the walk kept). */ #include "v4/fabric.h" #include #include static int failures = 0, checks = 0; #define CHECK(c,...) do{checks++; if(!(c)){failures++; printf("FAIL %s:%d: ",__FILE__,__LINE__); printf(__VA_ARGS__); printf("\n");}}while(0) #define PB ((v4_cell)(V4_NODE_WORDS - 24u)) static v4_fabric_node pool[6]; static v4_place places[16], more[24]; static v4_fabric f; /* the walk as a string of place numbers, e.g. "0 2 " */ static const char *walk(void) { static char s[128]; unsigned i, n = 0, guard = 0; s[0] = 0; for (i = v4_fabric_first(&f); i < f.capacity && guard < 64; i = v4_fabric_next(&f, i), guard++) n += (unsigned)sprintf(s + n, "%u ", i); return s; } int main(void) { unsigned k; printf("v4 fabric walk tests\n"); v4_fabric_init(&f, places, 4); CHECK(v4_fabric_first(&f) == f.capacity, "an empty fabric: nothing to walk"); for (k = 0; k < 3; k++) (void)v4_fabric_add(&f, &pool[k], PB); CHECK(strcmp(walk(), "0 1 2 ") == 0, "three nodes: walked 0 1 2 (%s)", walk()); (void)v4_fabric_remove(&f, 1); CHECK(strcmp(walk(), "0 2 ") == 0, "the middle one removed: 0 2 (%s)", walk()); CHECK(v4_fabric_add(&f, &pool[3], PB) == 1 && strcmp(walk(), "0 1 2 ") == 0, "one added takes place 1, and the order is kept (%s)", walk()); CHECK(v4_fabric_add(&f, &pool[4], PB) == 3 && strcmp(walk(), "0 1 2 3 ") == 0, "and one more at the end (%s)", walk()); (void)v4_fabric_remove(&f, 0); CHECK(strcmp(walk(), "1 2 3 ") == 0, "the first removed: 1 2 3 (%s)", walk()); /* removed while the walk is at it: the walk goes on to the next, as the loop over every place did */ (void)v4_fabric_remove(&f, 2); CHECK(v4_fabric_next(&f, 2) == 3, "a removed place's next is the next that holds a node (%u)", v4_fabric_next(&f, 2)); (void)v4_fabric_remove(&f, 3); CHECK(v4_fabric_next(&f, 2) == f.capacity && v4_fabric_next(&f, 1) == f.capacity, "and none after: the capacity"); (void)v4_fabric_remove(&f, 1); CHECK(v4_fabric_first(&f) == f.capacity, "all removed: nothing to walk (%s)", walk()); /* the boot's case: Hera alone in sixteen places */ v4_fabric_init(&f, places, 16); (void)v4_fabric_add(&f, &pool[0], PB); CHECK(strcmp(walk(), "0 ") == 0, "one node in sixteen places: a walk of one (%s)", walk()); CHECK(v4_fabric_step(&f) == 0 && pool[0].n.reading, "and the step runs it: blocked reading its ports"); /* more room: the walk is kept, and a node added in it is walked */ (void)v4_fabric_add(&f, &pool[1], PB); v4_fabric_grow(&f, more, 24); CHECK(strcmp(walk(), "0 1 ") == 0, "grown: the walk kept (%s)", walk()); for (k = 2; k < 6; k++) (void)v4_fabric_add(&f, &pool[k], PB); CHECK(strcmp(walk(), "0 1 2 3 4 5 ") == 0, "and the nodes added after are walked (%s)", walk()); printf(" %d checks, %d failures\n", checks, failures); return failures != 0; }