aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / ui / tests / allen_ui_stream_test.c
| 1 | /* allen_ui_stream_test.c -- a streamed answer must keep the letters already on screen (L853). The |
| 2 | * courier hands each later fragment over as the whole text so far, through |
| 3 | * ck_allen_ui_said_more, which joins it to the words showing. ck_allen_ui_said |
| 4 | * still starts a fresh reply: the "restart" arm shows that old path blanks the |
| 5 | * strip, so the checks can see the defect. Allen cannot speak until he has |
| 6 | * formed (FORMED_MS + 300), so every arm waits for that before the first words. |
| 7 | * One arm per run: allen_ui_stream_test join|glide|restart|mismatch|pending|faded|coalesced */ |
| 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <string.h> |
| 11 | #include <stdint.h> |
| 12 | #include "ui/allen_ui.h" |
| 13 | |
| 14 | static uint64_t now_us; |
| 15 | static uint32_t *fb, *copy; |
| 16 | static uint32_t W = 1920, H = 1080; |
| 17 | |
| 18 | static uint32_t now_ms(void) { return (uint32_t)(now_us / 1000); } |
| 19 | |
| 20 | static void run_to(uint32_t ms) |
| 21 | { |
| 22 | for (; now_us <= (uint64_t)ms * 1000u; now_us += 16000) |
| 23 | ck_allen_ui_tick(now_us); |
| 24 | } |
| 25 | |
| 26 | /* Pixels in the strip under Allen where his words go. Settled letters are |
| 27 | * near white; faint() also counts letters that are still coming in. */ |
| 28 | static unsigned strip(int faint) |
| 29 | { |
| 30 | unsigned n = 0; |
| 31 | uint32_t cy = H * 40 / 100; |
| 32 | for (uint32_t y = cy + 216; y < cy + 456 && y < H; y++) |
| 33 | for (uint32_t x = W / 2 - 460; x < W / 2 + 460; x++) { |
| 34 | uint32_t c = fb[(size_t)y * W + x]; |
| 35 | unsigned r = c & 255, g = c >> 8 & 255, b = c >> 16 & 255; |
| 36 | if (faint ? r > 40 : r > 200 && g > 200 && b > 200) |
| 37 | n++; |
| 38 | } |
| 39 | return n; |
| 40 | } |
| 41 | |
| 42 | static unsigned ink(void) { return strip(0); } |
| 43 | static unsigned faint(void) { return strip(1); } |
| 44 | |
| 45 | /* Pixels where the last frame, drawn only where it changed, differs from the |
| 46 | * same frame drawn whole. */ |
| 47 | static unsigned stale(void) |
| 48 | { |
| 49 | memcpy(copy, fb, (size_t)W * H * 4); |
| 50 | ck_allen_ui_refresh(); |
| 51 | ck_allen_ui_tick(now_us - 16000); |
| 52 | unsigned n = 0; |
| 53 | for (size_t i = 0; i < (size_t)W * H; i++) { |
| 54 | uint32_t a = fb[i], b = copy[i]; |
| 55 | for (int s = 0; s < 24; s += 8) { |
| 56 | int e = (int)(a >> s & 255) - (int)(b >> s & 255); |
| 57 | if (e > 6 || e < -6) { |
| 58 | n++; |
| 59 | break; |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | return n; |
| 64 | } |
| 65 | |
| 66 | static void fail(const char *why) |
| 67 | { |
| 68 | printf("ALLEN_UI_STREAM: FAIL %s\n", why); |
| 69 | exit(1); |
| 70 | } |
| 71 | |
| 72 | static unsigned base; |
| 73 | |
| 74 | static unsigned above(unsigned from, unsigned n) { return n > from ? n - from : 0; } |
| 75 | |
| 76 | static void boot(void) |
| 77 | { |
| 78 | fb = calloc((size_t)W * H, 4); |
| 79 | copy = malloc((size_t)W * H * 4); |
| 80 | size_t need = ck_allen_ui_mem_bytes(W, H); |
| 81 | void *mem = malloc(need); |
| 82 | if (!fb || !copy || !mem || ck_allen_ui_start(fb, W, H, W, 1, mem, need, 1000000, 0, 0)) |
| 83 | fail("the window did not start"); |
| 84 | run_to(9000); |
| 85 | ck_allen_ui_wake(0, now_us); |
| 86 | run_to(16000); /* formed at 15300; the first words wait until 15600 */ |
| 87 | base = ink(); |
| 88 | } |
| 89 | |
| 90 | /* The first fragment, said and settled: the ink it leaves. */ |
| 91 | static unsigned first(const char *p1) |
| 92 | { |
| 93 | uint32_t t0 = now_ms(); |
| 94 | ck_allen_ui_said(0, p1, now_us); |
| 95 | run_to(t0 + 2800); |
| 96 | unsigned a = above(base, ink()); |
| 97 | printf("note: first fragment ink %u\n", a); |
| 98 | if (a < 400) |
| 99 | fail("the first fragment left too little ink to judge a restart"); |
| 100 | return a; |
| 101 | } |
| 102 | |
| 103 | /* Lets the words on screen go: their fade (450 ms) has begun once the white |
| 104 | * is nearly gone, and once it ends they are cleared. */ |
| 105 | static void until_gone(void) |
| 106 | { |
| 107 | uint32_t t0 = now_ms(); |
| 108 | while (above(base, ink()) >= 40) { |
| 109 | run_to(now_ms() + 32); |
| 110 | if (now_ms() > t0 + 16000) |
| 111 | fail("the first answer did not leave the screen"); |
| 112 | } |
| 113 | run_to(now_ms() + 600); |
| 114 | } |
| 115 | |
| 116 | /* The next 500 ms after the call just made: did the strip go (nearly) blank? */ |
| 117 | static int blanks(unsigned a) |
| 118 | { |
| 119 | uint32_t at = now_ms(); |
| 120 | unsigned low = a; |
| 121 | for (uint32_t dt = 16; dt <= 500; dt += 16) { |
| 122 | run_to(at + dt); |
| 123 | unsigned n = above(base, ink()); |
| 124 | low = n < low ? n : low; |
| 125 | } |
| 126 | printf("note: lowest ink %u of %u\n", low, a); |
| 127 | return low * 4 < a; |
| 128 | } |
| 129 | |
| 130 | /* Joins whole after a, sampling every frame: never blanks, partial redraw |
| 131 | * equals full redraw while lines glide, and the new letters arrive. */ |
| 132 | static void joins(unsigned a, const char *whole) |
| 133 | { |
| 134 | ck_allen_ui_said_more(whole, now_us); |
| 135 | uint32_t at = now_ms(); |
| 136 | unsigned worst = 0; |
| 137 | for (uint32_t dt = 16; dt <= 700; dt += 16) { |
| 138 | run_to(at + dt); |
| 139 | unsigned n = above(base, ink()); |
| 140 | if (n * 10 < a * 9) { |
| 141 | printf("note: ink %u at +%u ms, was %u\n", n, dt, a); |
| 142 | fail("a later fragment blanked the letters already showing"); |
| 143 | } |
| 144 | unsigned st = stale(); |
| 145 | worst = st > worst ? st : worst; |
| 146 | } |
| 147 | printf("note: worst stale %u while the words joined\n", worst); |
| 148 | if (worst >= 40) |
| 149 | fail("joining words left a partial redraw"); |
| 150 | run_to(at + 3000); |
| 151 | unsigned b = above(base, ink()); |
| 152 | printf("note: joined ink %u\n", b); |
| 153 | if (b * 10 <= a * 13) |
| 154 | fail("the later fragment did not add letters"); |
| 155 | } |
| 156 | |
| 157 | int main(int argc, char **argv) |
| 158 | { |
| 159 | const char *mode = argc > 1 ? argv[1] : "join"; |
| 160 | const char *p1 = "Gravity pulls them together because"; |
| 161 | char full[160], more[192], glide[160]; |
| 162 | snprintf(full, sizeof full, "%s %s", p1, "they orbit a common center of mass."); |
| 163 | snprintf(more, sizeof more, "%s %s", full, "It takes time."); |
| 164 | /* At 1920 wide the first line holds about 79 letters: one short word |
| 165 | * still fits on it, the rest wraps, so that line glides less than it grows. */ |
| 166 | const char *g1 = "Gravity pulls them together because they orbit a common center of"; |
| 167 | snprintf(glide, sizeof glide, "%s %s", g1, argc > 2 ? argv[2] : "mass, notwithstanding everything."); |
| 168 | boot(); |
| 169 | printf("note: baseline ink %u at %u ms\n", base, now_ms()); |
| 170 | |
| 171 | if (!strcmp(mode, "join")) { |
| 172 | joins(first(p1), full); |
| 173 | } else if (!strcmp(mode, "glide")) { |
| 174 | joins(first(g1), glide); |
| 175 | } else if (!strcmp(mode, "restart")) { |
| 176 | /* the old call with the same text: the defect, visible to these checks */ |
| 177 | unsigned a = first(p1); |
| 178 | ck_allen_ui_said(0, full, now_us); |
| 179 | if (!blanks(a)) |
| 180 | fail("the old call did not blank the strip; this check cannot see the defect"); |
| 181 | } else if (!strcmp(mode, "mismatch")) { |
| 182 | /* longer than the words showing but not their continuation: a fresh reply */ |
| 183 | unsigned a = first(p1); |
| 184 | ck_allen_ui_said_more("Gravity pulls them apart when nothing else holds them.", now_us); |
| 185 | if (!blanks(a)) |
| 186 | fail("a reply that is not a continuation stayed glued on"); |
| 187 | } else if (!strcmp(mode, "pending")) { |
| 188 | /* The second fragment comes before the first has started to speak. It |
| 189 | * joins the waiting words: their start stays (a fresh reply would wait |
| 190 | * another 500 ms), nothing restarts, and all of it is said. The ink one |
| 191 | * fragment leaves is measured first, then left to go. */ |
| 192 | unsigned a = first(p1); |
| 193 | until_gone(); |
| 194 | uint32_t t0 = now_ms(); |
| 195 | ck_allen_ui_said(0, p1, now_us); |
| 196 | run_to(t0 + 200); |
| 197 | ck_allen_ui_said_more(full, now_us); |
| 198 | unsigned quiet = faint(); |
| 199 | run_to(t0 + 650); |
| 200 | unsigned early = above(quiet, faint()); |
| 201 | printf("note: letters 650 ms after the first fragment %u\n", early); |
| 202 | if (early < 15) |
| 203 | fail("an early continuation delayed the first letters"); |
| 204 | unsigned most = early; |
| 205 | for (uint32_t dt = 666; dt <= 4800; dt += 16) { |
| 206 | run_to(t0 + dt); |
| 207 | unsigned n = above(quiet, faint()); |
| 208 | if (n * 4 < most) |
| 209 | fail("an early continuation restarted the letters"); |
| 210 | most = n > most ? n : most; |
| 211 | } |
| 212 | unsigned b = above(base, ink()); |
| 213 | printf("note: whole answer ink %u, one fragment %u\n", b, a); |
| 214 | if (b * 10 <= a * 13) |
| 215 | fail("an early continuation did not say all of the answer"); |
| 216 | } else if (!strcmp(mode, "faded")) { |
| 217 | first(p1); |
| 218 | until_gone(); |
| 219 | ck_allen_ui_said_more(full, now_us); |
| 220 | uint32_t at = now_ms(); |
| 221 | run_to(at + 400); |
| 222 | if (above(base, ink()) > 200) |
| 223 | fail("a continuation after the words were gone painted at once"); |
| 224 | run_to(at + 3000); |
| 225 | printf("note: fresh ink %u\n", above(base, ink())); |
| 226 | if (above(base, ink()) < 400) |
| 227 | fail("a continuation after the words were gone did not start fresh"); |
| 228 | } else if (!strcmp(mode, "coalesced")) { |
| 229 | /* A fresh reply and its continuation arrive between two frames. The |
| 230 | * continuation belongs to that reply, not to the words still showing, |
| 231 | * even though those words begin it: the old words go. */ |
| 232 | unsigned a = first(p1); |
| 233 | ck_allen_ui_said(0, full, now_us); |
| 234 | ck_allen_ui_said_more(more, now_us); |
| 235 | if (!blanks(a)) |
| 236 | fail("a continuation of a reply not yet shown was glued to the old words"); |
| 237 | } else |
| 238 | fail("unknown mode"); |
| 239 | printf("ALLEN_UI_STREAM: PASS %s\n", mode); |
| 240 | return 0; |
| 241 | } |