interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / kernel / ui / tests / allen_ui_test.c

1369 lines · 68603 bytes Raw Blame History
1/* allen_ui_test.c -- host test for Allen's home (ui/allen_ui.c with the night
2 * sky, ui/night.c, and his presence, ui/mind.c), his typeface
3 * (ui/allen_font.c) and his first answers (svc/allen_reply.c). Runs the
4 * startup at 3840 x 2160 BGRX on a hand-moved clock, wakes Allen, types
5 * "Hello Allen", lets him answer, leaves him alone, saves a note, says goodbye
6 * (the stars fade, Allen folds to a line of light and it closes to black) and wakes him; checks the pixels at each step, checks that drawing only the changed parts gives the
7 * same picture as drawing everything, runs 1280 x 800 RGBX with and without
8 * three helper threads (the frames must match exactly), and writes PNG
9 * pictures to the directory given as argv[1] (if any) for a person to look at.
10 * Then the setup wizard (svc/setup_wizard.c) at 1920 x 1080: its choices,
11 * the lit one moving, the scan lines easing in, words held with a question.
12 * Final line: ALLEN_UI_TEST: PASS|FAIL (N checks). */
13#include <fcntl.h>
14#include <pthread.h>
15#include <stdint.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <unistd.h>
20#include <time.h>
21
22#include "../../svc/allen_intent.h"
23#include "../../svc/allen_reply.h"
24#include "../allen_font.h"
25#include "../allen_ui.h"
26#include "../flow.h"
27#include "../mind.h"
28#include "../night.h"
29#include "../sky_real.h"
30#include "../setup_view.h"
31
32static int checks, fails;
33static const char help_reply[] =
34 "I remember what you teach me and keep notes. Goodbye turns the display off, "
35 "or powers off if display sleep is unavailable.";
36static unsigned flow_marks;
37static void flow_mark(ck_rect r)
38{
39 if (r.w > 0 && r.h > 0)
40 flow_marks++;
41}
42#define CHECK(cond, what) \
43 do { \
44 checks++; \
45 if (!(cond)) { \
46 fails++; \
47 printf("FAIL %s\n", what); \
48 } \
49 } while (0)
50
51/* ---- a minimal PNG writer (stored deflate blocks), host only ---- */
52static uint32_t crc_table[256];
53static void crc_init(void)
54{
55 for (uint32_t n = 0; n < 256; n++) {
56 uint32_t c = n;
57 for (int k = 0; k < 8; k++)
58 c = c & 1 ? 0xedb88320u ^ (c >> 1) : c >> 1;
59 crc_table[n] = c;
60 }
61}
62static uint32_t crc(uint32_t c, const uint8_t *b, size_t n)
63{
64 for (size_t i = 0; i < n; i++)
65 c = crc_table[(c ^ b[i]) & 255] ^ (c >> 8);
66 return c;
67}
68static void be32(uint8_t *p, uint32_t v)
69{
70 p[0] = (uint8_t)(v >> 24), p[1] = (uint8_t)(v >> 16), p[2] = (uint8_t)(v >> 8), p[3] = (uint8_t)v;
71}
72static void chunk(FILE *f, const char *type, const uint8_t *data, size_t n)
73{
74 uint8_t h[8];
75 be32(h, (uint32_t)n);
76 memcpy(h + 4, type, 4);
77 fwrite(h, 1, 8, f);
78 if (n)
79 fwrite(data, 1, n, f);
80 uint32_t c = crc(0xffffffffu, (const uint8_t *)type, 4);
81 c = crc(c, data, n) ^ 0xffffffffu;
82 be32(h, c);
83 fwrite(h, 1, 4, f);
84}
85/* pixels: 0x00RRGGBB (format 1) rows of `pitch`; region x,y,w,h; every `step`th pixel */
86static void png(const char *path, const uint32_t *px, uint32_t pitch, int fmt, uint32_t x0, uint32_t y0, uint32_t w,
87 uint32_t h, uint32_t step)
88{
89 uint32_t ow = w / step, oh = h / step;
90 size_t raw_n = (size_t)oh * (1 + (size_t)ow * 3);
91 uint8_t *raw = malloc(raw_n);
92 size_t k = 0;
93 for (uint32_t y = 0; y < oh; y++) {
94 raw[k++] = 0;
95 for (uint32_t x = 0; x < ow; x++) {
96 uint32_t sr = 0, sg = 0, sb = 0;
97 for (uint32_t j = 0; j < step; j++)
98 for (uint32_t i = 0; i < step; i++) {
99 uint32_t c = px[((size_t)y0 + (size_t)y * step + j) * pitch + x0 + (size_t)x * step + i];
100 sr += c >> 16 & 255, sg += c >> 8 & 255, sb += c & 255;
101 }
102 uint8_t r = (uint8_t)(sr / (step * step)), g = (uint8_t)(sg / (step * step)), b = (uint8_t)(sb / (step * step));
103 if (fmt == 0) {
104 uint8_t t = r;
105 r = b;
106 b = t;
107 }
108 raw[k++] = r, raw[k++] = g, raw[k++] = b;
109 }
110 }
111 size_t blocks = raw_n / 65535 + 1;
112 size_t z_n = 2 + raw_n + blocks * 5 + 4;
113 uint8_t *z = malloc(z_n);
114 size_t o = 0;
115 z[o++] = 0x78, z[o++] = 0x01;
116 uint32_t a = 1, b = 0;
117 for (size_t i = 0; i < raw_n;) {
118 size_t n = raw_n - i > 65535 ? 65535 : raw_n - i;
119 z[o++] = i + n == raw_n;
120 z[o++] = (uint8_t)n, z[o++] = (uint8_t)(n >> 8), z[o++] = (uint8_t)~n, z[o++] = (uint8_t)(~n >> 8);
121 memcpy(z + o, raw + i, n);
122 for (size_t j = 0; j < n; j++) {
123 a = (a + raw[i + j]) % 65521;
124 b = (b + a) % 65521;
125 }
126 o += n;
127 i += n;
128 }
129 be32(z + o, b << 16 | a);
130 o += 4;
131 /* 0644, not fopen's 0666 & ~umask: a picture for a person, never world-writable. */
132 int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
133 FILE *f = fd < 0 ? NULL : fdopen(fd, "wb");
134 if (fd >= 0 && !f)
135 close(fd);
136 if (!f) {
137 printf("note: cannot write %s\n", path);
138 free(raw), free(z);
139 return;
140 }
141 fwrite("\x89PNG\r\n\x1a\n", 1, 8, f);
142 uint8_t ihdr[13];
143 be32(ihdr, ow), be32(ihdr + 4, oh);
144 ihdr[8] = 8, ihdr[9] = 2, ihdr[10] = 0, ihdr[11] = 0, ihdr[12] = 0;
145 chunk(f, "IHDR", ihdr, 13);
146 chunk(f, "IDAT", z, o);
147 chunk(f, "IEND", 0, 0);
148 fclose(f);
149 free(raw), free(z);
150 printf("note: wrote %s (%ux%u)\n", path, ow, oh);
151}
152
153/* ---- looking at a picture: counts over a rectangle of 0x00RRGGBB pixels ---- */
154struct pic {
155 const uint32_t *px;
156 uint32_t pitch;
157 int fmt; /* 0: red in the low byte (RGBX) */
158};
159static void rgb(const struct pic *p, uint32_t x, uint32_t y, int *r, int *g, int *b)
160{
161 uint32_t c = p->px[(size_t)y * p->pitch + x];
162 *r = c >> 16 & 255, *g = c >> 8 & 255, *b = c & 255;
163 if (p->fmt == 0) {
164 int t = *r;
165 *r = *b;
166 *b = t;
167 }
168}
169enum { BRIGHT, BLACK, LIT };
170static unsigned count(const struct pic *p, uint32_t x0, uint32_t y0, uint32_t w, uint32_t h, int kind)
171{
172 unsigned n = 0;
173 for (uint32_t y = y0; y < y0 + h; y++)
174 for (uint32_t x = x0; x < x0 + w; x++) {
175 int r, g, b;
176 rgb(p, x, y, &r, &g, &b);
177 if (kind == BRIGHT)
178 n += r > 150 && g > 150 && b > 150;
179 else if (kind == BLACK)
180 n += r == 0 && g == 0 && b == 0;
181 else
182 n += r > 60 || g > 60 || b > 90;
183 }
184 return n;
185}
186/* the light in a rectangle: r + g + b summed over every fourth pixel each way */
187static uint64_t light(const struct pic *p, uint32_t x0, uint32_t y0, uint32_t w, uint32_t h)
188{
189 uint64_t s = 0;
190 for (uint32_t y = y0; y < y0 + h; y += 4)
191 for (uint32_t x = x0; x < x0 + w; x += 4) {
192 int r, g, b;
193 rgb(p, x, y, &r, &g, &b);
194 s += (uint64_t)(r + g + b);
195 }
196 return s;
197}
198/* pixels of a and b (w x h, rows `pitch`) that differ by more than tol in any channel */
199static unsigned differ(const uint32_t *a, const uint32_t *b, uint32_t pitch, uint32_t w, uint32_t h, int tol)
200{
201 unsigned n = 0;
202 for (uint32_t y = 0; y < h; y++)
203 for (uint32_t x = 0; x < w; x++) {
204 uint32_t c = a[(size_t)y * pitch + x], d = b[(size_t)y * pitch + x];
205 for (int s = 0; s < 24; s += 8) {
206 int e = (int)(c >> s & 255) - (int)(d >> s & 255);
207 if (e > tol || e < -tol) {
208 n++;
209 break;
210 }
211 }
212 }
213 return n;
214}
215
216/* the frames are timed by the wall clock; ALLEN_UI_CLOCK=cpu times them by this thread's CPU time instead,
217 * which is the fairer figure on a busy host */
218static int cpu_clock;
219/* The injected frame clock runs at begin and after helpers/presentation,
220 * before the completed frame is published. No test-only renderer hook. */
221static unsigned black_probe, black_probe_calls, black_probe_early, wake_during_frame;
222static uint64_t real_us(void)
223{
224 if (black_probe) {
225 black_probe_calls++;
226 black_probe_early += ck_allen_ui_asleep() != 0;
227 }
228 if (wake_during_frame && !--wake_during_frame)
229 ck_allen_ui_sleep(0, 0);
230 struct timespec ts;
231 clock_gettime(cpu_clock ? CLOCK_THREAD_CPUTIME_ID : CLOCK_MONOTONIC, &ts);
232 return (uint64_t)ts.tv_sec * 1000000u + (uint64_t)ts.tv_nsec / 1000u;
233}
234
235/* the screen's clock in these checks: microseconds, moved by hand */
236static uint64_t now_us;
237static void run_to(uint32_t ms)
238{
239 for (; now_us <= (uint64_t)ms * 1000u; now_us += 16000)
240 ck_allen_ui_tick(now_us);
241}
242
243/* the frame drawn by changed parts against the whole screen drawn again at the same moment */
244static unsigned stale(uint32_t *fb, uint32_t *copy, uint32_t pitch, uint32_t w, uint32_t h)
245{
246 memcpy(copy, fb, (size_t)pitch * h * 4);
247 ck_allen_ui_refresh();
248 ck_allen_ui_tick(now_us - 16000);
249 return differ(fb, copy, pitch, w, h, 6);
250}
251
252/* The bare night sky as Allen's screen draws it (same scale and centre) with
253 * nothing in front of it, and the pixels in a rectangle of the screen that
254 * differ from it at the moment of the last frame: only what the screen added. */
255static ck_night bare_night;
256static int bare_init(uint32_t w, uint32_t h)
257{
258 uint32_t k = w * 256u / 1920u;
259 size_t nb = ck_night_mem_bytes(w, h, k);
260 return ck_night_init(&bare_night, w, h, k, (int32_t)w / 2, (int32_t)(h * 40 / 100), malloc(nb), nb);
261}
262static unsigned added(const struct pic *p, uint32_t *sky, uint32_t x0, uint32_t y0, uint32_t w, uint32_t h)
263{
264 ck_canvas cv = {sky, (int32_t)p->pitch, bare_night.h};
265 ck_rect r = {(int32_t)x0, (int32_t)y0, (int32_t)w, (int32_t)h};
266 ck_night_at(&bare_night, (uint32_t)((now_us - 16000) / 1000));
267 ck_night_base(&bare_night, &cv, &r);
268 ck_night_lights(&bare_night, &cv, &r, 0, 0);
269 return differ(p->px + (size_t)y0 * p->pitch + x0, sky + (size_t)y0 * p->pitch + x0, p->pitch, w, h, 24);
270}
271/* Visibility must come from the being, not unrelated stars or the retired
272 * warm/white broad discs. Use the same current native-sky baseline as added. */
273static unsigned cool_added(const struct pic *p, uint32_t *sky, uint32_t x0, uint32_t y0, uint32_t w, uint32_t h)
274{
275 ck_canvas cv = {sky, (int32_t)p->pitch, bare_night.h};
276 ck_rect area = {(int32_t)x0, (int32_t)y0, (int32_t)w, (int32_t)h};
277 ck_night_at(&bare_night, (uint32_t)((now_us - 16000) / 1000));
278 ck_night_base(&bare_night, &cv, &area);
279 ck_night_lights(&bare_night, &cv, &area, 0, 0);
280 unsigned visible = 0;
281 for (uint32_t y = y0; y < y0 + h; y++)
282 for (uint32_t x = x0; x < x0 + w; x++) {
283 int r, g, b;
284 rgb(p, x, y, &r, &g, &b);
285 unsigned base_blue = sky[(size_t)y * p->pitch + x] & 255u;
286 visible += b > g && g > r && b > (int)base_blue + 24;
287 }
288 return visible;
289}
290/* The starfield round Allen must stay the quiet sky: pixels of the ring
291 * ri < |p - (cx, cy)| <= ro, outside the words box and above the bar, that
292 * differ from the bare night (base + lights, no shimmer) by more than tol in
293 * any channel. The retired water ink (ck_flow_draw in compose) laid a blue
294 * aura here, banded on the physical iMac (L852); this counts its return. */
295static unsigned ring_lit(const struct pic *p, uint32_t *sky, uint32_t cx, uint32_t cy, uint32_t ri, uint32_t ro,
296 uint32_t wy, uint32_t wh, uint32_t by, int tol)
297{
298 uint32_t x0 = cx - ro, y0 = cy - ro;
299 ck_canvas cv = {sky, (int32_t)p->pitch, bare_night.h};
300 ck_rect area = {(int32_t)x0, (int32_t)y0, (int32_t)(2 * ro + 1), (int32_t)(2 * ro + 1)};
301 ck_night_at(&bare_night, (uint32_t)((now_us - 16000) / 1000));
302 ck_night_base(&bare_night, &cv, &area);
303 ck_night_lights(&bare_night, &cv, &area, 0, 0);
304 unsigned n = 0;
305 uint32_t W = p->pitch;
306 for (uint32_t y = y0; y <= cy + ro; y++)
307 for (uint32_t x = x0; x <= cx + ro; x++) {
308 int64_t dx = (int64_t)x - cx, dy = (int64_t)y - cy, d2 = dx * dx + dy * dy;
309 if (d2 <= (int64_t)ri * ri || d2 > (int64_t)ro * ro || y + 40 >= by)
310 continue;
311 if (y >= wy && y < wy + wh && x >= W / 4 && x < W * 3 / 4)
312 continue;
313 uint32_t c = p->px[(size_t)y * W + x], d = sky[(size_t)y * W + x];
314 for (int s = 0; s < 24; s += 8) {
315 int e = (int)(c >> s & 255) - (int)(d >> s & 255);
316 if (e > tol || e < -tol) {
317 n++;
318 break;
319 }
320 }
321 }
322 return n;
323}
324
325static volatile int helpers_run;
326static void *helper(void *arg)
327{
328 (void)arg;
329 while (helpers_run)
330 ck_allen_ui_help();
331 return 0;
332}
333
334/* ---- the setup wizard on the screen (svc/setup_wizard.c through ui/setup_view.h), 1920 x 1080 ---- */
335#define CHECKF(cond, ...) \
336 do { \
337 checks++; \
338 if (!(cond)) { \
339 fails++; \
340 printf("FAIL "); \
341 printf(__VA_ARGS__); \
342 printf("\n"); \
343 } \
344 } while (0)
345static sw_wizard wiz;
346static unsigned wiz_said;
347static void wiz_show(void) { ck_setup_show(sw_view_of(&wiz), &wiz_said, sw_step_of(&wiz) == SW_DONE, now_us); }
348static void wiz_key(int k, char c)
349{
350 sw_key(&wiz, k, c, (uint32_t)(now_us / 1000));
351 wiz_show();
352}
353static void wiz_run(uint32_t ms) /* the wizard's clock and the screen's together */
354{
355 uint32_t end = (uint32_t)(now_us / 1000) + ms;
356 while (now_us / 1000 < end) { /* as run_to: the last frame is 16 ms before now_us */
357 if (sw_tick(&wiz, (uint32_t)(now_us / 1000)))
358 wiz_show();
359 ck_allen_ui_tick(now_us);
360 now_us += 16000;
361 }
362}
363
364static void wizard_screens(const char *dir)
365{
366 char path[512];
367 static char text[4096];
368 FILE *f = fopen("native/kernel/svc/tests/fixtures/setup/games-pc.txt", "rb");
369 CHECKF(f != 0, "wizard: the games PC fixture opens (run from the repository root)");
370 if (!f)
371 return;
372 unsigned n = (unsigned)fread(text, 1, sizeof text, f);
373 fclose(f);
374 static si_inv inv;
375 si_init(&inv);
376 si_parse(&inv, text, n);
377 uint32_t W = 1920, H = 1080;
378 uint32_t *fb = calloc((size_t)W * H, 4), *copy = malloc((size_t)W * H * 4);
379 size_t need = ck_allen_ui_mem_bytes(W, H);
380 void *mem = malloc(need);
381 struct pic P = {fb, W, 1};
382 now_us = 0;
383 CHECKF(ck_allen_ui_start(fb, W, H, W, 1, mem, need, 1000000, 0, 0) == 0, "wizard: the window starts at 1920x1080");
384 run_to(9000);
385 ck_allen_ui_wake(0, now_us);
386 wiz_said = ~0u;
387 sw_start(&wiz, &inv, 1, 0, 0, (uint32_t)(now_us / 1000));
388 wiz_show();
389 wiz_run(7000);
390 CHECKF(sw_step_of(&wiz) == SW_INTENT, "wizard: hello, then the one question");
391 wiz_run(5000);
392 /* the row of choices sits where the bar is: three pills, the first lit */
393 uint32_t ry = H - 150 - 4, rh = 62;
394 unsigned row = count(&P, 0, ry, W, rh, BRIGHT);
395 CHECKF(row > 400, "wizard: the choices are drawn where the bar sits (%u bright)", row);
396 unsigned left = count(&P, 0, ry, W / 2 - 200, rh, LIT), right = count(&P, W / 2 + 200, ry, W / 2 - 200, rh, LIT);
397 CHECKF(left > right, "wizard: the first choice is the lit one (%u against %u)", left, right);
398 unsigned hint = count(&P, 400, H - 150 + 54 + 6, W - 800, 40, LIT);
399 CHECKF(hint > 50, "wizard: the lit choice's line is under the row (%u)", hint);
400 unsigned st = stale(fb, copy, W, W, H);
401 CHECKF(st < 40, "wizard: the question drawn by changed parts is the whole picture (%u differ)", st);
402 if (dir) {
403 snprintf(path, sizeof path, "%s/wizard-1-intent.png", dir);
404 png(path, fb, W, 1, 0, 0, W, H, 1);
405 }
406 wiz_key(SW_K_RIGHT, 0);
407 wiz_run(600);
408 unsigned left2 = count(&P, 0, ry, W / 2 - 200, rh, LIT), mid = count(&P, W / 2 - 200, ry, 400, rh, LIT);
409 CHECKF(left2 < left && mid > 0, "wizard: the light moves to the next choice (%u -> %u)", left, left2);
410 wiz_key(SW_K_LEFT, 0);
411 wiz_key(SW_K_ENTER, 0);
412 /* getting to know the computer: the lines come one after another */
413 wiz_run(1200);
414 uint32_t py = (uint32_t)(H * 40 / 100 + 250 + 40), ph = 220;
415 unsigned early = count(&P, 200, py, W - 400, ph, BRIGHT);
416 wiz_run(3000);
417 unsigned later = count(&P, 200, py, W - 400, ph, BRIGHT);
418 CHECKF(later > early + 200, "wizard: the scan's lines ease in one at a time (%u then %u)", early, later);
419 if (dir) {
420 snprintf(path, sizeof path, "%s/wizard-2-scan.png", dir);
421 png(path, fb, W, 1, 0, 0, W, H, 1);
422 }
423 wiz_run(4000);
424 CHECKF(sw_step_of(&wiz) == SW_CONSENT, "wizard: the consent comes by itself");
425 wiz_run(4000);
426 st = stale(fb, copy, W, W, H);
427 CHECKF(st < 40, "wizard: the consent drawn by changed parts is the whole picture (%u differ)", st);
428 if (dir) {
429 snprintf(path, sizeof path, "%s/wizard-3-consent.png", dir);
430 png(path, fb, W, 1, 0, 0, W, H, 1);
431 }
432 wiz_key(SW_K_ENTER, 0);
433 wiz_run(5000);
434 if (dir) {
435 snprintf(path, sizeof path, "%s/wizard-4-games.png", dir);
436 png(path, fb, W, 1, 0, 0, W, H, 1);
437 }
438 /* his words stay while the question is up, however long they wait */
439 unsigned words = count(&P, 300, (uint32_t)(H * 40 / 100 + 200), W - 600, 70, BRIGHT);
440 wiz_run(20000);
441 unsigned words_later = count(&P, 300, (uint32_t)(H * 40 / 100 + 200), W - 600, 70, BRIGHT);
442 CHECKF(words > 100 && words_later * 10 > words * 9, "wizard: Allen's words stay with the question (%u, %u)", words,
443 words_later);
444 wiz_key(SW_K_ENTER, 0);
445 wiz_key(SW_K_CHAR, '2'); /* choose another */
446 wiz_key(SW_K_CHAR, '2'); /* connect my own */
447 wiz_key(SW_K_CHAR, '1'); /* Claude */
448 for (const char *k = "sk-not-a-real-key"; *k; k++)
449 wiz_key(SW_K_CHAR, *k);
450 wiz_run(3000);
451 CHECKF(sw_step_of(&wiz) == SW_KEY, "wizard: at the key");
452 unsigned bar = count(&P, W / 2 - 360, H - 150, 720, 54, BRIGHT);
453 CHECKF(bar > 30, "wizard: the bar is back for typing, the key as dots (%u)", bar);
454 if (dir) {
455 snprintf(path, sizeof path, "%s/wizard-5-key.png", dir);
456 png(path, fb, W, 1, 0, 0, W, H, 1);
457 }
458 wiz_key(SW_K_ESC, 0);
459 wiz_key(SW_K_ESC, 0);
460 wiz_key(SW_K_ESC, 0);
461 wiz_run(4000);
462 CHECKF(sw_step_of(&wiz) == SW_READY, "wizard: ready");
463 if (dir) {
464 snprintf(path, sizeof path, "%s/wizard-6-ready.png", dir);
465 png(path, fb, W, 1, 0, 0, W, H, 1);
466 }
467 wiz_key(SW_K_ENTER, 0);
468 wiz_run(3000);
469 CHECKF(count(&P, 0, ry, W, rh, BRIGHT) < row / 4, "wizard: done, the choices have gone");
470 st = stale(fb, copy, W, W, H);
471 CHECKF(st < 40, "wizard: after the panel went, changed parts are the whole picture (%u differ)", st);
472 free(fb), free(copy), free(mem);
473}
474
475/* Compare the exact same native galaxy pixels, not an unrelated star patch.
476 * The annulus starts beyond every projected node plus the largest sharp
477 * primitive and its AA edge. Threads and travelling sparks stay between nodes. */
478static void mind_no_halo(const char *dir)
479{
480 enum { W = 3840, H = 2160, K = 512, CX = W / 2, CY = H * 40 / 100 };
481 size_t bytes = (size_t)W * H * sizeof(uint32_t);
482 uint32_t *background = malloc(bytes), *image = malloc(bytes);
483 size_t nb = ck_night_mem_bytes(W, H, K);
484 void *storage = malloc(nb);
485 static ck_night night;
486 CHECK(background && image && storage, "mind: native frame allocation");
487 if (!background || !image || !storage) {
488 free(background), free(image), free(storage);
489 return;
490 }
491 CHECK(!ck_night_init(&night, W, H, K, CX, CY, storage, nb), "mind: native galaxy init");
492 ck_canvas bg = {background, W, H}, cv = {image, W, H};
493 ck_rect clip = {0, 0, W, H};
494 ck_night_at(&night, 30000);
495 ck_night_base(&night, &bg, &clip);
496 ck_night_lights(&night, &bg, &clip, 0, 0);
497 for (int mode = CK_MIND_IDLE; mode <= CK_MIND_SPEAK; mode++) {
498 ck_mind mind;
499 ck_mind_init(&mind, CX, CY, K, 0);
500 ck_mind_mode(&mind, mode, 0);
501 ck_mind_voice(&mind, 256);
502 for (uint32_t t = 0; t <= 6000; t += 100)
503 ck_mind_at(&mind, t);
504 ck_mind_word(&mind, 6000);
505 ck_mind_key(&mind, 6000);
506 ck_mind_at(&mind, 6100);
507 int64_t radius = 0;
508 for (unsigned i = 0; i < CK_MIND_NODES; i++) {
509 int64_t dx = mind.px[i] - mind.hx, dy = mind.py[i] - mind.hy;
510 int64_t d = (int64_t)ck_isqrt((uint64_t)(dx * dx + dy * dy));
511 if (d > radius)
512 radius = d;
513 }
514 /* S(5) is the largest spark radius; two pixels cover rounding/AA. */
515 radius += 5 * K / 256 * 16 + 32;
516 int64_t outer = (int64_t)mind.R * 16 * 2;
517 memcpy(image, background, bytes);
518 ck_mind_draw(&mind, &cv, &clip);
519 unsigned annulus = 0, being = 0, control = 0;
520 for (int y = 0; y < H; y++)
521 for (int x = 0; x < W; x++) {
522 int64_t dx = x * 16 + 8 - mind.hx, dy = y * 16 + 8 - mind.hy;
523 int64_t d2 = dx * dx + dy * dy;
524 size_t p = (size_t)y * W + x;
525 int positive = 0;
526 for (int s = 0; s < 24; s += 8)
527 positive |= (image[p] >> s & 255) > (background[p] >> s & 255);
528 if (d2 > radius * radius && d2 < outer * outer)
529 annulus += positive;
530 if (d2 <= radius * radius)
531 being += positive;
532 }
533 CHECK(!annulus, "mind: no positive difference outside true being on identical galaxy");
534 CHECK(being > 0, "mind: sharp being still present");
535 if (dir) {
536 char path[512];
537 snprintf(path, sizeof path, "%s/mind-no-halo-%d.png", dir, mode);
538 png(path, image, W, 1, 0, 0, W, H, 1);
539 }
540 /* Historical whole-mind glow, kept only as a detector control. */
541 memcpy(image, background, bytes);
542 ck_glow(&cv, &clip, mind.hx, mind.hy, mind.R * 16 * 17 / 10,
543 CK_RGB(60, 74, 210), (52 + mind.heart / 8) * mind.alpha >> 8);
544 for (int y = 0; y < H; y++)
545 for (int x = 0; x < W; x++) {
546 int64_t dx = x * 16 + 8 - mind.hx, dy = y * 16 + 8 - mind.hy;
547 int64_t d2 = dx * dx + dy * dy;
548 size_t p = (size_t)y * W + x;
549 if (d2 > radius * radius && d2 < outer * outer)
550 control += image[p] != background[p];
551 }
552 CHECK(control > 0, "mind: historical glow positive control lights the annulus");
553 printf("MIND_NO_HALO mode=%d center=%d,%d inner16=%lld outer16=%lld annulus_added=%u being_added=%u old_glow_added=%u box=%d,%d,%d,%d\n",
554 mode, CX, CY, (long long)radius, (long long)outer, annulus, being, control,
555 mind.box.x, mind.box.y, mind.box.w, mind.box.h);
556 }
557 /* Isolate an actual native-sized cool point, not a broad flat disc.
558 * All other geometry is dark; this still calls the production renderer. */
559 ck_mind point;
560 ck_mind_init(&point, CX, CY, K, 0);
561 ck_mind_at(&point, 6000);
562 point.ne = point.nspark = 0;
563 point.hr = 0;
564 memset(point.lum, 0, sizeof point.lum);
565 point.px[0] = CX * 16 + 8;
566 point.py[0] = CY * 16 + 8;
567 point.near[0] = 0;
568 point.lum[0] = 256;
569 point.act[0] = 0;
570 point.box = clip;
571 memset(image, 0, bytes);
572 ck_mind_draw(&point, &cv, &clip);
573 uint32_t centre = image[(size_t)CY * W + CX];
574 CHECK(centre != 0, "mind: isolated point is present");
575 CHECK(centre == CK_RGB(32, 72, 144), "mind: unactivated point retains its full cool core");
576 unsigned footprint = 0;
577 for (size_t i = 0; i < (size_t)W * H; i++)
578 footprint += image[i] != 0;
579 CHECK(footprint > 0 && footprint <= 25, "mind: isolated point has bounded native support");
580 CHECK(image[(size_t)CY * W + CX + 1] == image[(size_t)CY * W + CX - 1] &&
581 image[(size_t)CY * W + CX + 1] == image[(size_t)(CY + 1) * W + CX],
582 "mind: native point edge coverage is symmetric");
583 CHECK(!image[(size_t)CY * W + CX + 2], "mind: no point light outside the native edge");
584 free(background), free(image), free(storage);
585}
586
587int main(int argc, char **argv)
588{
589 const char *dir = argc > 1 ? argv[1] : 0;
590 char path[512];
591 crc_init();
592 if (argc > 1 && !strcmp(argv[1], "--mind-no-halo")) {
593 mind_no_halo(argc > 2 ? argv[2] : 0);
594 printf("MIND_NO_HALO: %s (%d checks)\n", fails ? "FAIL" : "PASS", checks);
595 return fails ? 1 : 0;
596 }
597 mind_no_halo(0);
598 cpu_clock = getenv("ALLEN_UI_CLOCK") && !strcmp(getenv("ALLEN_UI_CLOCK"), "cpu");
599
600 /* ---- answers ---- */
601 CHECK(!strcmp(ck_allen_reply("Hello Allen"), "Hello Humanity."), "reply: Hello Allen");
602 CHECK(!strcmp(ck_allen_reply(" hello ALLEN!! "), "Hello Humanity."), "reply: case, spaces, punctuation folded");
603 CHECK(!strcmp(ck_allen_reply("hello allen."), "Hello Humanity."), "reply: trailing period");
604 CHECK(strcmp(ck_allen_reply("hello allenx"), "Hello Humanity."), "reply: near miss is not the first greeting");
605 CHECK(!strncmp(ck_allen_reply(""), "I don't know that yet.", 22), "reply: empty line");
606 CHECK(!strncmp(ck_allen_reply(0), "I don't know that yet.", 22), "reply: no line");
607 CHECK(!strncmp(ck_allen_reply("xz"), "I don't know that yet.", 22), "reply: unknown line");
608 CHECK(!strcmp(ck_allen_reply("who are you"), "I am Allen. I live in AIENOS."), "reply: who are you");
609 CHECK(ck_allen_reply("Goodbye, Allen!") == ck_allen_goodbye && ck_allen_reply("bye") == ck_allen_goodbye &&
610 ck_allen_reply("good bye") == ck_allen_goodbye && ck_allen_reply("goodbye now") != ck_allen_goodbye,
611 "reply: goodbye ends the session (and only goodbye)");
612 {
613 /* A conversation: what Allen is told stays for the session. */
614 ck_allen_facts facts = {4, 1, 1, 3840, 2160};
615 ck_allen_mind m;
616 ck_allen_mind_init(&m, &facts);
617 static const struct {
618 const char *say, *want; /* want: the answer starts with this */
619 uint64_t awake;
620 } talk[] = {
621 {"Hello Allen", "Hello Humanity.", 5},
622 {"what's my name?", "You haven't told me yet.", 6},
623 {"My name is drake", "Nice to meet you, Drake.", 7},
624 {"hi", "Hello, Drake. Good to see you.", 8},
625 {"what is my name", "You're Drake.", 9},
626 {"how are you?", "I'm running well, Drake. 4 cores awake", 10},
627 {"good thanks", "Glad to hear it.", 11},
628 {"i am tired", "Sorry to hear that.", 12},
629 {"what is 12 times 4", "12 times 4 is 48.", 13},
630 {"nine divided by two", "9 divided by 2 is 4 remainder 1.", 14},
631 {"3 minus 10", "3 minus 10 is minus 7.", 15},
632 {"what is 5 divided by zero", "Nobody can divide by zero", 16},
633 {"tell me about this computer", "This computer has 4 cores, a 3840 by 2160 screen, 1 hard drive and 1 SSD.",
634 17},
635 {"how long have you been awake?", "I've been awake for 2 minutes and 5 seconds.", 125},
636 {"what time is it", "I can't read the clock yet. I've been awake for 1 hour and 1 minute.", 3660},
637 {"who made you", "Drake built me", 3661},
638 {"what is aienos", "AIENOS is the operating system I live in", 3662},
639 {"tell me a joke", "", 3663},
640 {"thank you", "You're welcome, Drake.", 3664},
641 {"can you remember things", "Teach me like this: remember my favorite color is blue.", 3665},
642 {"help", help_reply, 3666},
643 {"i'm fine", "Glad to hear it.", 3667},
644 {"blorp", "I don't know that yet.", 3668},
645 };
646 int ok = 1;
647 for (size_t i = 0; i < sizeof talk / sizeof talk[0]; i++) {
648 const char *a = ck_allen_answer(&m, talk[i].say, talk[i].awake);
649 if (strncmp(a, talk[i].want, strlen(talk[i].want)) || strlen(a) >= 160) {
650 printf(" conversation line %zu: \"%s\" -> \"%s\"\n", i, talk[i].say, a);
651 ok = 0;
652 }
653 }
654 CHECK(ok, "reply: a whole conversation (name, how are you, sums, machine facts, awake time, help)");
655 }
656 {
657 /* His own name: asked for at the first meeting, kept, changed on request. */
658 ck_allen_mind m;
659 ck_allen_mind_init(&m, 0);
660 CHECK(!strcmp(ck_allen_welcome(&m), "Hello. I'm new here, and I have no name yet. What will you call me?"),
661 "self: a first meeting asks for his name");
662 CHECK(!strcmp(ck_allen_answer(&m, "Nova", 2), "Then I'm Nova. Hello Humanity.") && !strcmp(m.mem.self, "Nova") &&
663 m.changed && !m.naming,
664 "self: the next word is his name, saved");
665 m.changed = 0;
666 CHECK(!strcmp(ck_allen_answer(&m, "hello nova", 3), "Hello Humanity."), "self: greeted by his new name");
667 CHECK(!strcmp(ck_allen_answer(&m, "what is your name?", 4), "My name is Nova."), "self: says his name");
668 CHECK(!strcmp(ck_allen_answer(&m, "who are you", 5), "I am Nova. I live in AIENOS."), "self: who are you");
669 CHECK(!strcmp(ck_allen_answer(&m, "call yourself Sky", 6), "I'm Sky now.") && !strcmp(m.mem.self, "Sky") &&
670 m.changed,
671 "self: renamed on request");
672 CHECK(!strncmp(ck_allen_answer(&m, "your name is the best", 7), "One word, please", 16) &&
673 !strcmp(m.mem.self, "Sky"),
674 "self: a rename needs one word");
675 CHECK(!strncmp(ck_allen_answer(&m, "my name is sky", 8), "You haven't told me yet", 23) && !m.mem.name[0],
676 "self: the person cannot take his name");
677 /* no sky mode chosen yet, so the welcome also offers the two sky choices (allen_reply.c) */
678 CHECK(!strcmp(ck_allen_welcome(&m), "Hello. I'm Sky. The sky behind me can be the real sky above you right now, or a different part of the universe each time. Tell me which you'd like."),
679 "self: the next window opening says who he is, and offers the sky");
680 CHECK(!strcmp(ck_allen_answer(&m, "forget everything", 9),
681 "Done. I've forgotten everything you taught me, and your name.") &&
682 !strcmp(m.mem.self, "Sky"),
683 "self: forgetting what he was taught keeps his name");
684 ck_allen_mind_init(&m, 0);
685 ck_allen_welcome(&m);
686 CHECK(!strcmp(ck_allen_answer(&m, "what can you do", 1), help_reply) && !m.naming && !m.mem.self[0],
687 "self: any other line drops the question, he stays Allen");
688 CHECK(!strcmp(ck_allen_answer(&m, "what's your name", 2),
689 "My name is Allen. You can give me another: call yourself, then a name."),
690 "self: Allen by default, and how to change it");
691 ck_allen_mind_init(&m, 0);
692 ck_allen_welcome(&m);
693 CHECK(!strcmp(ck_allen_answer(&m, "allen", 1), "Allen it is. Hello Humanity.") && !m.mem.self[0] && !m.naming,
694 "self: Allen chosen stays the default");
695 ck_allen_mind_init(&m, 0);
696 ck_allen_welcome(&m);
697 CHECK(!strcmp(ck_allen_answer(&m, "Hello Allen", 1), "Hello Humanity.") && !m.naming,
698 "self: greeting him as Allen settles it");
699 CHECK(!strncmp(ck_allen_answer(&m, "xz", 2), "I don't know that yet.", 22) && !m.mem.self[0],
700 "self: after that a stray word is not a name");
701 }
702 {
703 /* What a line wants, and the job Allen writes for the far side. */
704 static const struct {
705 const char *say;
706 ck_allen_want want;
707 } wants[] = {
708 {"write a haiku about rain", CK_WANT_MAKE},
709 {"please make me a list of three birds", CK_WANT_MAKE},
710 {"can you code a function that adds two numbers", CK_WANT_MAKE},
711 {"i need a name for my boat", CK_WANT_MAKE},
712 {"explain how rain forms", CK_WANT_EXPLAIN},
713 {"what is a prime number", CK_WANT_EXPLAIN},
714 {"why is the sky blue", CK_WANT_EXPLAIN},
715 {"allen tell me about the moon", CK_WANT_EXPLAIN},
716 {"what does ephemeral mean", CK_WANT_EXPLAIN},
717 {"find my keys", CK_WANT_DO},
718 {"could you translate good morning to french", CK_WANT_DO},
719 {"summarize the news", CK_WANT_DO},
720 {"i had a long day", CK_WANT_CHAT},
721 {"tell me a joke", CK_WANT_CHAT},
722 {"", CK_WANT_CHAT},
723 };
724 int ok = 1;
725 for (size_t i = 0; i < sizeof wants / sizeof wants[0]; i++) {
726 char f[96];
727 ck_allen_fold(wants[i].say, f, sizeof f);
728 ck_allen_want got = ck_allen_want_of(f);
729 if (got != wants[i].want) {
730 printf(" want of \"%s\": %s, wanted %s\n", wants[i].say, ck_allen_want_name(got),
731 ck_allen_want_name(wants[i].want));
732 ok = 0;
733 }
734 }
735 CHECK(ok, "intent: make, explain, do and chat told apart");
736 ck_allen_mind m;
737 ck_allen_mind_init(&m, 0);
738 char job[1024];
739 unsigned n = ck_allen_job(&m, 7, "Write a haiku\tabout\nrain", job, sizeof job);
740 CHECK(n && n == strlen(job) && !strncmp(job, "JOB 7 make You are the far hands of Allen, a quiet presence", 59),
741 "intent: the job names the seq, the want and who he is");
742 CHECK(strstr(job, "the person's computer") && strstr(job, "Begin with one short sentence") &&
743 !strchr(job, '\n') && strstr(job, "\tWrite a haiku about rain") == job + n - 25,
744 "intent: the instruction fits the want; the line follows a tab, cleaned");
745 strcpy(m.mem.name, "Drake");
746 strcpy(m.mem.self, "Nova");
747 n = ck_allen_job(&m, 8, "why is the sky blue?", job, sizeof job);
748 CHECK(n && !strncmp(job, "JOB 8 explain You are the far hands of Nova", 43) && strstr(job, "on Drake's computer") &&
749 strstr(job, "three plain sentences"),
750 "intent: his name and the person's name go into the instruction");
751 CHECK(ck_allen_job(&m, 9, "hello", job, 100) == 0, "intent: too small a buffer is refused, not cut");
752 uint32_t seq = 0;
753 char text[160];
754 CHECK(ck_allen_answer_line("SAY 8 Sunlight scatters\\nin the air, blue most of all. ", &seq, text, sizeof text) &&
755 seq == 8 && !strcmp(text, "Sunlight scatters in the air, blue most of all."),
756 "intent: an answer line is read back, newlines and runs of blanks folded");
757 CHECK(!ck_allen_answer_line("HELLO courier", &seq, text, sizeof text) &&
758 !ck_allen_answer_line("SAY x nope", &seq, text, sizeof text) &&
759 !ck_allen_answer_line("SAYING 3 no", &seq, text, sizeof text),
760 "intent: other lines are not answers");
761 CHECK(ck_allen_answer_line("SAY 12 0123456789abcdef0123456789abcdef", &seq, text, 10) && !strcmp(text, "012345678"),
762 "intent: a long answer is cut to the buffer");
763 }
764
765 {
766 /* A fluid strip must sample the same global grid as a full frame,
767 * and a refresh cannot expose solver changes that were not marked. */
768 const int32_t w = 1920, h = 1080;
769 size_t bytes = (size_t)w * h * sizeof(uint32_t);
770 uint32_t *whole = calloc(1, bytes), *parts = calloc(1, bytes);
771 static ck_flow water;
772 ck_flow_init(&water, w, h, w / 2, h / 2, 118, w / 2, h, 0);
773 for (unsigned i = 0; i < CK_FLOW_CELLS; i++) {
774 water.d[i] = 65536 / 2 + 255;
775 water.warm[i] = i & 1 ? 256 : 0;
776 }
777 flow_marks = 0;
778 ck_flow_mark(&water, flow_mark);
779 CHECK(flow_marks > 0, "flow: changed solver cells publish their rendered snapshot");
780 ck_canvas full = {whole, w, h}, tiled = {parts, w, h};
781 ck_rect all = {0, 0, w, h};
782 ck_flow_draw(&water, &full, &all, 256);
783 for (int32_t y = 0; y < h; y += CK_FLOW_H)
784 for (int32_t x = 0; x < w; x += CK_FLOW_W) {
785 ck_rect r = {x, y, (int32_t)CK_FLOW_W, (int32_t)CK_FLOW_H};
786 ck_flow_draw(&water, &tiled, &r, 256);
787 }
788 CHECK(!memcmp(whole, parts, bytes),
789 "flow: exact grid remainder makes full and clipped draws byte-identical");
790 unsigned marks = flow_marks;
791 for (unsigned i = 0; i < CK_FLOW_CELLS; i++)
792 water.d[i]++;
793 ck_flow_mark(&water, flow_mark);
794 memset(parts, 0, bytes);
795 ck_flow_draw(&water, &tiled, &all, 256);
796 CHECK(flow_marks == marks && !memcmp(whole, parts, bytes),
797 "flow: below-threshold changes cannot leak into full refreshes");
798 for (unsigned i = 0; i < CK_FLOW_CELLS; i++)
799 water.d[i] = 0;
800 ck_flow_mark(&water, flow_mark);
801 memset(parts, 0, bytes);
802 ck_flow_draw(&water, &tiled, &all, 256);
803 int black = 1;
804 for (size_t i = 0; i < (size_t)w * h; i++)
805 black &= parts[i] == 0;
806 CHECK(flow_marks > marks && black, "flow: a published disappearance leaves no stale ink");
807 free(whole);
808 free(parts);
809 }
810
811 /* Display bytes are sRGB; coverage must weigh decoded light. These
812 * checks distinguish the transfer-table path from encoded-byte scaling. */
813 {
814 uint32_t ink = CK_RGB(234, 238, 252), white = CK_RGB(255, 255, 255);
815 uint32_t half = (ck_sky_srgb16[ck_sky_lin[255] * 128u >> 11] + 8u) >> 4;
816 uint32_t expected = CK_RGB(half, half, half);
817 CHECK(ck_scale(white, 128) == expected && ck_mix(0, white, 128) == expected &&
818 ck_add(0, white, 128) == expected && half > 128,
819 "paint: half light is encoded after weighting, not half an sRGB byte");
820 int endpoints = 1, stable = 1, monotone = 1;
821 uint32_t previous = 0;
822 for (uint32_t c = 0; c <= 255; c++) {
823 uint32_t color = CK_RGB(c, c, c);
824 endpoints &= ck_scale(color, 0) == 0 && ck_scale(color, 256) == color &&
825 ck_mix(ink, color, 0) == ink && ck_mix(ink, color, 256) == color &&
826 ck_add(0, color, 256) == color && ck_add(color, ink, 0) == color;
827 for (uint32_t a = 0; a <= 256; a++)
828 stable &= ck_mix(color, color, a) == color &&
829 ck_ch(ck_add(color, CK_RGB(0, 0, 255), a), 16) == c;
830 }
831 for (uint32_t a = 0; a <= 256; a++) {
832 uint32_t value = ck_scale(white, a);
833 monotone &= value >= previous;
834 previous = value;
835 }
836 CHECK(endpoints && stable, "paint: opaque/black endpoints and untouched channels remain exact");
837 CHECK(monotone, "paint: light fades monotonically all the way to pure black");
838
839 uint32_t hpx[64 * 64] = {0}, vpx[64 * 64] = {0}, thin[64 * 64] = {0};
840 ck_canvas hc = {hpx, 64, 64}, vc = {vpx, 64, 64}, tc = {thin, 64, 64};
841 ck_rect clip = {0, 0, 64, 64};
842 ck_thread(&hc, &clip, 4 * 16, 16 * 16 + 8, 48 * 16, 16 * 16 + 8, 32, white, 256, 256);
843 ck_thread(&vc, &clip, 16 * 16 + 8, 4 * 16, 16 * 16 + 8, 48 * 16, 32, white, 256, 256);
844 ck_thread(&tc, &clip, 4 * 16, 16 * 16 + 8, 48 * 16, 16 * 16 + 8, 16, white, 256, 256);
845 int transpose = 1;
846 for (unsigned y = 0; y < 64; y++)
847 for (unsigned x = 0; x < 64; x++)
848 transpose &= hpx[y * 64 + x] == vpx[x * 64 + y];
849 CHECK(transpose && hpx[16 * 64 + 20] == white && hpx[15 * 64 + 20] == expected &&
850 hpx[17 * 64 + 20] == expected && thin[16 * 64 + 20] == white &&
851 thin[15 * 64 + 20] == 0 && thin[17 * 64 + 20] == 0,
852 "paint: native-scaled threads widen symmetrically, including vertical edges");
853 }
854
855 /* ---- typeface ---- */
856 CHECK(ck_af_isqrt(0) == 0 && ck_af_isqrt(15) == 3 && ck_af_isqrt(16) == 4 &&
857 ck_af_isqrt(1000000007ull * 1000000007ull) == 1000000007ull,
858 "font: integer square root");
859 CHECK(ck_af_sin(0) == 0 && ck_af_sin(90) == 16384 && ck_af_sin(-90) == -16384 && ck_af_cos(180) == -16384 &&
860 ck_af_sin(450) == 16384,
861 "font: sine table");
862 static uint8_t pool[4 << 20];
863 ck_af_face face;
864 ck_af_face_init(&face, 30, 1250, pool, sizeof pool);
865 int all = 1, sym = 1;
866 for (int c = 33; c <= 126; c++) {
867 const ck_af_glyph *g = ck_af_glyph_get(&face, (char)c);
868 unsigned ink = 0;
869 if (!g || !g->alpha) {
870 all = 0;
871 continue;
872 }
873 for (size_t i = 0; i < (size_t)g->w * g->h; i++)
874 ink += g->alpha[i] > 128;
875 if (ink < 4)
876 all = 0;
877 }
878 const ck_af_glyph *o = ck_af_glyph_get(&face, 'o');
879 for (int y = 0; o && y < o->h; y++)
880 for (int x = 0; x < o->w; x++) {
881 int d = (int)o->alpha[y * o->w + x] - (int)o->alpha[y * o->w + (o->w - 1 - x)];
882 if (d > 40 || d < -40)
883 sym = 0;
884 }
885 CHECK(all, "font: every printable character rasterizes with ink");
886 CHECK(sym, "font: 'o' is left-right symmetric");
887 CHECK(ck_af_glyph_get(&face, ' ') != 0 && ck_af_advance(&face, ' ') > 0, "font: space advances");
888 CHECK(ck_af_measure(&face, "Hello Humanity.", 15) > ck_af_measure(&face, "Hello", 5), "font: measure grows");
889 uint8_t tiny[64];
890 ck_af_face small;
891 ck_af_face_init(&small, 30, 1250, tiny, sizeof tiny);
892 CHECK(ck_af_glyph_get(&small, 'W') == 0, "font: a full pool refuses instead of overrunning");
893
894 {
895 uint32_t pixels[64 * 64] = {0};
896 ck_canvas cv = {pixels, 64, 64};
897 ck_rect clip = {0, 0, 64, 64};
898 ck_letter(&cv, &clip, &face, 16 * 256, 48, 'o', CK_RGB(255, 255, 255), 128, 0);
899 int exact = 1;
900 unsigned fractional = 0;
901 for (int y = 0; o && y < o->h; y++)
902 for (int x = 0; x < o->w; x++) {
903 int px = 16 + o->ox + x, py = 48 + o->oy + y;
904 uint32_t cov = o->alpha[y * o->w + x], weight = (cov + (cov >> 7)) * 128 >> 8;
905 uint32_t level = (ck_sky_srgb16[ck_sky_lin[255] * weight >> 11] + 8u) >> 4;
906 if (px >= 0 && py >= 0 && px < 64 && py < 64)
907 exact &= pixels[py * 64 + px] == CK_RGB(level, level, level);
908 fractional += cov > 0 && cov < 255;
909 }
910 CHECK(exact && fractional, "font: real glyph AA coverage is composed as light");
911 uint32_t ink = CK_RGB(234, 238, 252);
912 for (unsigned i = 0; i < 64 * 64; i++)
913 pixels[i] = ink;
914 ck_letter(&cv, &clip, &face, 16 * 256, 48, 'o', ink, 256, 0);
915 exact = 1;
916 for (unsigned i = 0; i < 64 * 64; i++)
917 exact &= pixels[i] == ink;
918 CHECK(exact, "font: matching ink/background does not darken at AA edges");
919 }
920
921 /* ---- Allen's home at 3840 x 2160, BGRX ---- */
922 uint32_t W = 3840, H = 2160;
923 uint32_t *fb = calloc((size_t)W * H, 4), *copy = malloc((size_t)W * H * 4), *sky = malloc((size_t)W * H * 4);
924 size_t need = ck_allen_ui_mem_bytes(W, H);
925 void *mem = malloc(need);
926 struct pic P = {fb, W, 1};
927 CHECK(ck_allen_ui_start(fb, W, H, W, 1, mem, need - 1, 1000000, 0, real_us) == -1, "ui: short memory refused");
928 CHECK(ck_allen_ui_start(fb, W, H, W, 2, mem, need, 1000000, 0, real_us) == -1, "ui: unknown pixel format refused");
929 CHECK(bare_init(W, H) == 0, "night: the bare sky for the checks");
930 {
931 /* No pull and no shading must be the native kept picture, including
932 * single-pixel contrast and its final row/column. Dimming must not
933 * select a lower-resolution copy either. */
934 static ck_night n;
935 uint32_t source[64 * 64], background[64 * 64] = {0}, resolved[64 * 64], previous[64 * 64];
936 n.w = n.h = 64;
937 n.k = 256;
938 n.cx = n.cy = -64;
939 n.sky = background;
940 for (unsigned y = 0; y < 64; y++)
941 for (unsigned x = 0; x < 64; x++)
942 source[y * 64 + x] = (x + y) & 1 ? CK_RGB(255, 255, 255) : 0;
943 void *storage = malloc(ck_night_picture_bytes(64, 64));
944 ck_night_picture kept;
945 ck_night_picture_take(&n, source, storage, &kept);
946 ck_canvas cv = {resolved, 64, 64};
947 ck_rect clip = {0, 0, 64, 64};
948 ck_night_dusk d = {.crowd = 256, .keep = 256};
949 ck_night_fall(&n, &d);
950 ck_night_base_from(&n, &kept, &cv, &clip);
951 CHECK(!memcmp(source, resolved, sizeof source), "dusk: unwarped picture resolves every native pixel and edge");
952 uint32_t half = (ck_sky_srgb16[ck_sky_lin[255] * 128u >> 11] + 8u) >> 4;
953 CHECK(kept.level[1][0] == CK_RGB(half, half, half),
954 "dusk: minification averages light instead of darkening encoded contrast");
955 memcpy(previous, resolved, sizeof previous);
956 int monotone = 1, detail = 1;
957 for (uint32_t keep = 128; keep; keep >>= 1) {
958 d.keep = keep;
959 ck_night_fall(&n, &d);
960 ck_night_base_from(&n, &kept, &cv, &clip);
961 for (unsigned i = 0; i < 64 * 64; i++) {
962 monotone &= resolved[i] <= previous[i];
963 detail &= resolved[i] == ck_scale(source[i], keep);
964 }
965 memcpy(previous, resolved, sizeof previous);
966 }
967 CHECK(monotone && detail && resolved[1] != 0 && resolved[0] == 0,
968 "dusk: faint picture stays pixel-resolved, nonblack and monotonically dimming");
969 d.keep = 0;
970 ck_night_fall(&n, &d);
971 ck_night_base_from(&n, &kept, &cv, &clip);
972 int black = 1;
973 for (unsigned i = 0; i < 64 * 64; i++)
974 black &= resolved[i] == 0;
975 CHECK(black, "dusk: zero light resolves to pure black at every native pixel");
976 free(storage);
977
978 ck_night_at(&bare_night, CK_NIGHT_MERGE_MS + CK_NIGHT_RIPPLE_MS);
979 ck_canvas native = {copy, (int32_t)W, (int32_t)H};
980 ck_rect whole = {0, 0, (int32_t)W, (int32_t)H};
981 ck_night_base(&bare_night, &native, &whole);
982 CHECK(!memcmp(copy, bare_night.sky, (size_t)W * H * 4) &&
983 ck_night_faded(&bare_night, 0, 0, 0) == 256 &&
984 ck_night_faded(&bare_night, (int32_t)W - 1, (int32_t)H - 1, 255) == 256,
985 "night: settled 4K stars are native, unscaled and fully unfaded");
986 }
987 /* A catalogue-only reference catches diffuse fog both initially and
988 * after changing sky region. Keep the actual star positions and colour. */
989 {
990 ck_night_star tw[CK_NIGHT_TWINKLES];
991 unsigned ntw;
992 ck_canvas reference = {copy, (int32_t)W, (int32_t)H};
993 ck_sky_view original = bare_night.view, other;
994 ck_sky_view_at(&other, 83822000, -5391000, 80000, 0);
995 for (unsigned pass = 0; pass < 2; pass++) {
996 if (pass)
997 ck_night_view(&bare_night, &other);
998 memset(copy, 0, (size_t)W * H * 4);
999 ck_sky_draw_stars(&reference, bare_night.cx, bare_night.cy,
1000 &bare_night.view, tw, CK_NIGHT_TWINKLES, &ntw);
1001 CHECK(!memcmp(copy, bare_night.sky, (size_t)W * H * 4),
1002 "night: exactly catalogue stars on black, no diffuse fog");
1003 }
1004 ck_night_view(&bare_night, &original);
1005 }
1006 uint64_t t_init = real_us();
1007 CHECK(ck_allen_ui_start(fb, W, H, W, 1, mem, need, 1000000, 0, real_us) == 0, "ui: starts at 3840x2160");
1008 printf("note: the night made in %llu ms (this host)\n", (unsigned long long)((real_us() - t_init) / 1000));
1009 CHECK(ck_allen_ui_started() && !ck_allen_ui_on(), "ui: the night has begun; Allen is not awake yet");
1010 now_us = 0;
1011 run_to(0);
1012 CHECK(count(&P, 0, 0, W, H, LIT) == 0, "night: it opens in the dark");
1013 run_to(1800);
1014 unsigned early = count(&P, 0, 0, W, H, LIT);
1015 run_to(4200);
1016 unsigned later = count(&P, 0, 0, W, H, LIT);
1017 CHECK(early > 0 && later > early * 2, "night: the stars come out, the brightest first");
1018 CHECK(stale(fb, copy, W, W, H) == 0, "night: the fading sky is drawn whole");
1019 if (dir) {
1020 snprintf(path, sizeof path, "%s/night-1-stars.png", dir);
1021 png(path, fb, W, 1, 0, 0, W, H, 2);
1022 }
1023 uint32_t cx = W / 2, cy = H * 40 / 100, R = 236;
1024 run_to(8000);
1025 CHECK(count(&P, cx - 800, cy - 500, 1600, 1000, BLACK) > 2000, "night: two black holes, their shadows");
1026 CHECK(count(&P, 0, 0, W, H, BLACK) > W * H * 9 / 10, "night: clear black space between the stars and holes");
1027 CHECK(stale(fb, copy, W, W, H) < 40, "night: the bent starlight round the holes is redrawn as they move");
1028 if (dir) {
1029 snprintf(path, sizeof path, "%s/night-2-holes.png", dir);
1030 png(path, fb, W, 1, 0, 0, W, H, 2);
1031 snprintf(path, sizeof path, "%s/night-2-holes-detail.png", dir);
1032 png(path, fb, W, 1, cx - 900, cy - 560, 1800, 1120, 1);
1033 }
1034 run_to(11600);
1035 unsigned before = count(&P, cx - 300, cy - 300, 600, 600, BRIGHT);
1036 run_to(12100);
1037 CHECK(count(&P, cx - 300, cy - 300, 600, 600, BRIGHT) > before + 1500, "night: the merger flashes");
1038 run_to(12900);
1039 CHECK(stale(fb, copy, W, W, H) < 40, "night: the ripple is redrawn wherever it runs");
1040 if (dir) {
1041 snprintf(path, sizeof path, "%s/night-3-ripple.png", dir);
1042 png(path, fb, W, 1, 0, 0, W, H, 2);
1043 }
1044 run_to(16500);
1045 CHECK(count(&P, 0, 0, W, H, LIT) > 4000, "night: point stars remain when the last black hole dissolves");
1046 CHECK(cool_added(&P, sky, cx - R, cy - R, 2 * R, 2 * R) > 5000,
1047 "allen: formed, a visible cool network over the actual sky");
1048 uint32_t bx = W / 2 - 720, by = H - 300, bw = 1440, bh = 108;
1049 /* L852: the starfield round him is the quiet sky, with no water-ink aura */
1050 CHECK(ring_lit(&P, sky, cx, cy, R * 5 / 4, 3 * R, cy + 500 - 90, 5 * 80 + 60, by, 0) == 0,
1051 "allen: formed, the stars round him are the bare sky (no ink aura)");
1052 CHECK(added(&P, sky, 0, by - 40, W, H - by + 40) < 400, "allen: no bar before he is awake");
1053 CHECK(stale(fb, copy, W, W, H) < 40, "allen: drawn whole as he turns");
1054 if (dir) {
1055 snprintf(path, sizeof path, "%s/allen-1-formed.png", dir);
1056 png(path, fb, W, 1, 0, 0, W, H, 2);
1057 snprintf(path, sizeof path, "%s/allen-1-formed-detail.png", dir);
1058 png(path, fb, W, 1, cx - 600, cy - 500, 1200, 1000, 1);
1059 }
1060 /* awake: the bar comes up */
1061 const char *footer = "Type goodbye to turn the display off";
1062 ck_allen_ui_wake(footer, now_us);
1063 run_to(17800);
1064 CHECK(ck_allen_ui_on(), "ui: on");
1065 unsigned bar0 = added(&P, sky, bx, by, bw, bh);
1066 CHECK(bar0 > 2500, "bar: up once he is awake, with its hint");
1067 memcpy(copy, fb, (size_t)W * H * 4);
1068 ck_allen_ui_wake(0, now_us);
1069 ck_allen_ui_refresh();
1070 ck_allen_ui_tick(now_us - 16000);
1071 CHECK(memcmp(copy, fb, (size_t)W * H * 4) != 0, "bar: removing the footer changes the empty bar's picture");
1072 ck_allen_ui_wake(footer, now_us);
1073 ck_allen_ui_refresh();
1074 ck_allen_ui_tick(now_us - 16000);
1075 CHECK(!memcmp(copy, fb, (size_t)W * H * 4), "bar: restoring the footer restores exactly the empty bar's picture");
1076 const char *typed = "Hello Allen";
1077 for (unsigned i = 1; i <= 11; i++) {
1078 ck_allen_ui_input(typed, i, now_us);
1079 run_to((uint32_t)(now_us / 1000) + 120);
1080 }
1081 CHECK(count(&P, bx, by, bw, bh, BRIGHT) > 500, "bar: what is typed appears in it");
1082 memcpy(copy, fb, (size_t)W * H * 4);
1083 ck_allen_ui_wake(0, now_us);
1084 ck_allen_ui_refresh();
1085 ck_allen_ui_tick(now_us - 16000);
1086 CHECK(!memcmp(copy, fb, (size_t)W * H * 4), "bar: removing the footer changes no pixel while typing");
1087 ck_allen_ui_wake(footer, now_us);
1088 ck_allen_ui_refresh();
1089 ck_allen_ui_tick(now_us - 16000);
1090 CHECK(!memcmp(copy, fb, (size_t)W * H * 4), "bar: restoring the footer changes no pixel while typing");
1091 if (dir) {
1092 snprintf(path, sizeof path, "%s/allen-2-typing.png", dir);
1093 png(path, fb, W, 1, 0, 0, W, H, 2);
1094 }
1095 /* Enter: the line rises into him, he thinks, then answers under him */
1096 uint32_t wy = cy + 500 - 90, wh = 5 * 80 + 60;
1097 ck_allen_ui_said(typed, ck_allen_reply(typed), now_us);
1098 uint32_t said = (uint32_t)(now_us / 1000);
1099 run_to(said + 500);
1100 CHECK(stale(fb, copy, W, W, H) < 40, "talk: the rising line and the thoughts are redrawn");
1101 if (dir) {
1102 snprintf(path, sizeof path, "%s/allen-3-thinking.png", dir);
1103 png(path, fb, W, 1, 0, 0, W, H, 2);
1104 }
1105 run_to(said + 1300);
1106 CHECK(stale(fb, copy, W, W, H) < 40, "talk: speaking redraws the geometry and words cleanly");
1107 {
1108 ck_canvas reference = {copy, (int32_t)W, (int32_t)H};
1109 ck_rect whole = {0, 0, (int32_t)W, (int32_t)H};
1110 ck_night_at(&bare_night, (uint32_t)(now_us / 1000) - 16);
1111 ck_night_base(&bare_night, &reference, &whole);
1112 ck_night_lights(&bare_night, &reference, &whole, 0, 0);
1113 unsigned changed = 0;
1114 for (uint32_t y = 0; y < H; y++)
1115 for (uint32_t x = 0; x < W; x++)
1116 if (x < W / 4 || x >= W * 3 / 4)
1117 changed += fb[(size_t)y * W + x] != copy[(size_t)y * W + x];
1118 CHECK(changed == 0, "talk: side sky stays identical to quiet stars, with no voice glare or fog");
1119 }
1120 {
1121 unsigned lit = ring_lit(&P, sky, cx, cy, R * 5 / 4, 3 * R, wy, wh, by, 0);
1122 printf("note: L852 ring round Allen while he speaks: %u pixels off the bare sky\n", lit);
1123 CHECK(lit == 0, "talk: speaking pushes no ink aura over the stars round him");
1124 }
1125 run_to(said + 2600);
1126 {
1127 unsigned lit = ring_lit(&P, sky, cx, cy, R * 5 / 4, 3 * R, wy, wh, by, 0);
1128 printf("note: L852 ring round Allen under his answer: %u pixels off the bare sky\n", lit);
1129 CHECK(lit == 0, "talk: his answer leaves the stars round him as the bare sky");
1130 }
1131 CHECK(added(&P, sky, W / 4, wy, W / 2, wh) > 3000, "talk: Allen's answer appears under him");
1132 CHECK(count(&P, bx, by, bw, bh, BRIGHT) < 500, "talk: the bar is cleared for the next line");
1133 if (dir) {
1134 snprintf(path, sizeof path, "%s/allen-4-answer.png", dir);
1135 png(path, fb, W, 1, 0, 0, W, H, 2);
1136 snprintf(path, sizeof path, "%s/allen-4-answer-detail.png", dir);
1137 png(path, fb, W, 1, W / 4, cy - 400, W / 2, 1300, 1);
1138 }
1139 /* Remote work keeps THINK active rather than immediately scheduling an
1140 * answer. Exercise repeated dirty frames and the residual activity at the
1141 * transition to SPEAK; isolated fresh-frame fixtures cannot catch trails. */
1142 ck_allen_ui_think("why do stars shine", now_us);
1143 {
1144 uint32_t pending = (uint32_t)(now_us / 1000);
1145 static const uint32_t checkpoints[] = {500, 900, 1500, 2700, 4500};
1146 for (unsigned i = 0; i < sizeof checkpoints / sizeof checkpoints[0]; i++) {
1147 run_to(pending + checkpoints[i]);
1148 CHECKF(stale(fb, copy, W, W, H) < 40,
1149 "talk: sustained thinking leaves no stale light at checkpoint %u", checkpoints[i]);
1150 /* the typed line's motes fly from the bar up into him (draw_guest:
1151 * guest_area, from cy - S(40) = cy - 80 down at 4K, wider than the
1152 * ring): skip that path, check the sky above him */
1153 CHECKF(ring_lit(&P, sky, cx, cy, R * 5 / 4, 3 * R, cy - 80, by - (cy - 80), by, 0) == 0,
1154 "talk: thinking streams no ink aura over the stars round him at checkpoint %u", checkpoints[i]);
1155 }
1156 }
1157 ck_allen_ui_said(0, "The rendering fixture is answering.", now_us);
1158 said = (uint32_t)(now_us / 1000);
1159 run_to(said + 1300);
1160 CHECK(stale(fb, copy, W, W, H) < 40, "talk: thinking-to-speaking leaves no stale interior light");
1161 /* left alone: his words fade once read, then the bar; only Allen is left, waiting */
1162 run_to(said + 24000);
1163 CHECK(added(&P, sky, W / 4, wy, W / 2, wh) < 400, "rest: his words fade once read");
1164 CHECK(added(&P, sky, 0, by - 40, W, H - by + 40) < 400, "rest: the bar fades when nothing is in use");
1165 CHECK(cool_added(&P, sky, cx - R, cy - R, 2 * R, 2 * R) > 5000,
1166 "rest: Allen stays visible as a cool network, not a bright lamp");
1167 CHECK(stale(fb, copy, W, W, H) < 40, "rest: nothing is left behind on the screen");
1168 if (dir) {
1169 snprintf(path, sizeof path, "%s/allen-5-waiting.png", dir);
1170 png(path, fb, W, 1, 0, 0, W, H, 2);
1171 }
1172 ck_allen_ui_input("h", 1, now_us);
1173 run_to((uint32_t)(now_us / 1000) + 400);
1174 CHECK(added(&P, sky, bx, by, bw, bh) > 2500, "rest: a key brings the bar back");
1175 ck_allen_ui_input("", 0, now_us);
1176 {
1177 /* the notes constellation: shown when a note is saved, then it goes */
1178 static const char *const notes[] = {"the gate code is 4471", "the garage code is 9012",
1179 "the wifi password is Blue-Fox-88", "the garage door opener is in the truck",
1180 "the dentist is on friday at 10", "the truck needs new tires before the winter"};
1181 static ck_allen_notes ns;
1182 ns.n = 6;
1183 for (unsigned i = 0; i < 6; i++)
1184 ck_allen_note_set(&ns.note[i], notes[i], 1);
1185 uint8_t ea[32], eb[32];
1186 unsigned ne = 0;
1187 for (unsigned a = 0; a < 6; a++) /* point 0 is the newest note */
1188 for (unsigned b = a + 1; b < 6; b++)
1189 if (ck_allen_note_links(&ns.note[5 - a], &ns.note[5 - b]))
1190 ea[ne] = (uint8_t)a, eb[ne++] = (uint8_t)b;
1191 CHECK(ne >= 3, "map: the six notes link (garage, code, truck)");
1192 uint32_t mx = W * 84 / 100 - 380, my = H * 42 / 100 - 390, mw = 760, mh = 820;
1193 ck_allen_ui_map(6, ea, eb, ne, -1, 0, 6, ne);
1194 run_to((uint32_t)(now_us / 1000) + 1500);
1195 CHECK(added(&P, sky, mx, my, mw, mh) < 400, "map: not shown when nothing was saved");
1196 ck_allen_ui_map(6, ea, eb, ne, 0, ns.note[5].text, 6, ne);
1197 uint32_t m0 = (uint32_t)(now_us / 1000);
1198 run_to(m0 + 500);
1199 CHECK(stale(fb, copy, W, W, H) < 40, "map: the spark from Allen to the note is redrawn");
1200 run_to(m0 + 1500);
1201 CHECK(added(&P, sky, mx, my, mw, mh) > 3000, "map: the note just saved glows");
1202 if (dir) {
1203 snprintf(path, sizeof path, "%s/allen-6-notes.png", dir);
1204 png(path, fb, W, 1, 0, 0, W, H, 2);
1205 snprintf(path, sizeof path, "%s/allen-6-notes-detail.png", dir);
1206 png(path, fb, W, 1, mx, my, mw, mh, 1);
1207 }
1208 ck_allen_ui_map(6, ea, eb, ne, 3, "a very long note that cannot possibly fit on the one line under the map", 6,
1209 ne);
1210 run_to(m0 + 3000);
1211 CHECK(added(&P, sky, mx, my, mw, mh) > 3000, "map: a long label is cut to fit, not overrun");
1212 run_to(m0 + 15000);
1213 CHECK(added(&P, sky, mx, my, mw, mh) < 400, "map: it goes again after a while");
1214 }
1215 /* ---- sleep: fixed stars fade; Allen folds into a line, which closes to black ---- */
1216 {
1217 memcpy(copy, fb, (size_t)W * H * 4);
1218 ck_allen_ui_sleep(1, now_us);
1219 uint32_t s0 = (uint32_t)(now_us / 1000);
1220 static const uint32_t at[6] = {400, 900, 1300, 1800, 2200, 2600};
1221 unsigned nf = 0, saved = 0, lit400 = 0, displaced = 0;
1222 unsigned widths[3] = {0}, outside_line = 0;
1223 uint64_t tail_light = UINT64_MAX;
1224 unsigned rises = 0;
1225 for (; now_us <= (uint64_t)(s0 + 3000) * 1000u; now_us += 16000) {
1226 black_probe = !ck_allen_ui_asleep();
1227 int drawn = ck_allen_ui_tick(now_us);
1228 black_probe = 0;
1229 if (!drawn)
1230 continue;
1231 uint32_t d = (uint32_t)(now_us / 1000) - s0;
1232 nf++;
1233 if (d >= 1800) {
1234 uint64_t v = light(&P, 0, 0, W, H);
1235 rises += v > tail_light;
1236 tail_light = v;
1237 }
1238 if (saved < 6 && d >= at[saved]) {
1239 if (saved == 0) {
1240 for (uint32_t y = 0; y < H; y++)
1241 for (uint32_t x = 0; x < W; x++) {
1242 uint32_t v = fb[(size_t)y * W + x] & 0xffffffu;
1243 lit400 += v != 0;
1244 if (abs((int)x - (int)cx) > 2 * (int)R || abs((int)y - (int)cy) > 2 * (int)R)
1245 displaced += v != 0 && !(copy[(size_t)y * W + x] & 0xffffffu);
1246 }
1247 }
1248 if (saved >= 3) {
1249 for (uint32_t x = 0; x < W; x++)
1250 widths[saved - 3] += (fb[(size_t)cy * W + x] & 255u) > 4;
1251 for (uint32_t y = 0; y < H; y++)
1252 if (abs((int)y - (int)cy) > 8)
1253 for (uint32_t x = 0; x < W; x++)
1254 outside_line += (fb[(size_t)y * W + x] & 0xffffffu) != 0;
1255 }
1256 if (dir) {
1257 snprintf(path, sizeof path, "%s/allen-8-exit-%u.png", dir, saved + 1);
1258 png(path, fb, W, 1, 0, 0, W, H, 2);
1259 }
1260 saved++;
1261 }
1262 }
1263 CHECK(saved == 6 && nf >= 170, "exit: all six phases rendered on the animation timeline");
1264 CHECK(lit400 > 4000, "exit: stars remain visible early rather than cutting to black");
1265 CHECK(displaced == 0, "exit: fading stars stay in place, with no whirlpool warp");
1266 CHECK(widths[0] > widths[1] && widths[1] > widths[2] && widths[2] > 0,
1267 "exit: the light line closes smoothly toward a visible final point");
1268 CHECK(outside_line == 0, "exit: after the star fade only the thin central line remains");
1269 CHECK(rises == 0, "exit: the closing line never flashes brighter again");
1270 CHECK(black_probe_calls > 0 && black_probe_early == 0,
1271 "dusk: frame-begin/end observers cannot see unpublished black completion");
1272 CHECK(ck_allen_ui_asleep(), "exit: asleep once the light line has closed");
1273 CHECK(count(&P, 0, 0, W, H, BLACK) == W * H, "exit: final frame is pure black, every pixel");
1274 if (dir) {
1275 snprintf(path, sizeof path, "%s/allen-8-exit-closed.png", dir);
1276 png(path, fb, W, 1, 0, 0, W, H, 2);
1277 }
1278 memcpy(copy, fb, (size_t)W * H * 4);
1279 run_to(s0 + 4600);
1280 CHECK(differ(fb, copy, W, W, H, 0) == 0, "sleep: nothing moves while asleep");
1281 uint32_t df, ds, da;
1282 ck_allen_ui_dusk_stats(&df, &ds, &da);
1283 printf("note: the dusk: %u frames at 3840x2160 on one core of this host: average %u us, slowest %u us (%s)\n",
1284 df, da, ds, cpu_clock ? "CPU time of this thread" : "wall clock");
1285 CHECK(df >= 170 && da > 0, "exit: its frames are counted");
1286 /* the dawn: not a cut, and the sky is back in under a second, with nothing of the dusk left */
1287 ck_allen_ui_sleep(1, now_us);
1288 CHECK(!ck_allen_ui_asleep(), "sleep: a new request clears the previous black acknowledgement immediately");
1289 ck_allen_ui_refresh();
1290 ck_allen_ui_tick(now_us - 16000);
1291 CHECK(ck_allen_ui_asleep(), "sleep: a repeated request is acknowledged after a newly completed black frame");
1292 /* Queue a wake from the closing clock callback, after drawing but
1293 * before completion publication. The stale sleep must not win. */
1294 wake_during_frame = 2;
1295 ck_allen_ui_refresh();
1296 ck_allen_ui_tick(now_us - 16000);
1297 CHECK(!wake_during_frame && !ck_allen_ui_asleep(),
1298 "dawn: a wake queued before frame commit cannot be overwritten by that sleeping frame");
1299 uint32_t w0 = (uint32_t)(now_us / 1000);
1300 run_to(w0 + 300);
1301 CHECK(!ck_allen_ui_asleep(), "dawn: awake after ck_allen_ui_sleep(0)");
1302 uint64_t part = light(&P, 0, 0, W, H);
1303 CHECK(part > 0, "dawn: the sky is coming back a third of the way in");
1304 run_to(w0 + 1000);
1305 uint64_t full = light(&P, 0, 0, W, H);
1306 CHECK(full > part * 3 / 2, "dawn: the sky is back, brighter than a third of the way in");
1307 CHECK(stale(fb, copy, W, W, H) < 40, "dawn: nothing of the dusk is left behind on the screen");
1308 CHECK(cool_added(&P, sky, cx - R, cy - R, 2 * R, 2 * R) > 5000, "dawn: the visible cool network returns");
1309 }
1310 uint32_t frames, slowest, average;
1311 ck_allen_ui_stats(&frames, &slowest, &average);
1312 printf("note: %u frames at 3840x2160 on one core of this host: average %u us, slowest %u us (%s)\n", frames,
1313 average, slowest, cpu_clock ? "CPU time of this thread" : "wall clock");
1314 CHECK(frames > 1000, "ui: frames counted");
1315
1316 /* Padded RGBX alone/with helpers, then the identical timeline in BGRX. */
1317 uint32_t w2 = 1280, h2 = 800, pitch2 = 1344;
1318 uint32_t *fb2[3];
1319 size_t need2 = ck_allen_ui_mem_bytes(w2, h2);
1320 void *mem2 = malloc(need2);
1321 for (int run = 0; run < 3; run++) {
1322 fb2[run] = calloc((size_t)pitch2 * h2, 4);
1323 CHECK(ck_allen_ui_start(fb2[run], w2, h2, pitch2, run == 2, mem2, need2, 1000000, 0, 0) == 0,
1324 "ui: starts at 1280x800");
1325 pthread_t th[3];
1326 helpers_run = run == 1;
1327 for (int i = 0; run == 1 && i < 3; i++)
1328 pthread_create(&th[i], 0, helper, 0);
1329 now_us = 0;
1330 run_to(15000);
1331 ck_allen_ui_wake("footer", now_us);
1332 ck_allen_ui_said("Hello Allen", "Hello Humanity.", now_us);
1333 run_to(18000);
1334 helpers_run = 0;
1335 for (int i = 0; run == 1 && i < 3; i++)
1336 pthread_join(th[i], 0);
1337 }
1338 struct pic P2 = {fb2[0], pitch2, 0};
1339 unsigned padding_clean = 1;
1340 for (uint32_t y = 0; y < h2; y++)
1341 for (uint32_t x = w2; x < pitch2; x++)
1342 padding_clean &= fb2[0][(size_t)y * pitch2 + x] == 0;
1343 unsigned format_exact = 1, colored = 0;
1344 for (uint32_t y = 0; y < h2; y++)
1345 for (uint32_t x = 0; x < w2; x++) {
1346 size_t p = (size_t)y * pitch2 + x;
1347 uint32_t bgr = fb2[2][p];
1348 uint32_t rgb = (bgr & 0xff00ff00u) | ((bgr & 0xffu) << 16) | ((bgr >> 16) & 0xffu);
1349 format_exact &= fb2[0][p] == rgb;
1350 colored |= (bgr & 0xffu) != ((bgr >> 16) & 0xffu);
1351 }
1352 CHECK(format_exact && colored, "ui: RGBX swaps red/blue exactly against an asymmetric BGRX frame");
1353 uint32_t c2y = h2 * 40 / 100;
1354 CHECK(count(&P2, w2 / 4, c2y + 120, w2 / 2, 200, BRIGHT) > 100, "ui: Allen answers at 1280x800");
1355 CHECK(padding_clean, "ui: nothing written past the visible width");
1356 CHECK(memcmp(fb2[0], fb2[1], (size_t)pitch2 * h2 * 4) == 0, "ui: three helper cores draw exactly the same frames");
1357 if (dir) {
1358 snprintf(path, sizeof path, "%s/allen-1280x800.png", dir);
1359 uint32_t *tmp = malloc((size_t)w2 * h2 * 4);
1360 for (uint32_t y = 0; y < h2; y++)
1361 memcpy(tmp + (size_t)y * w2, fb2[0] + (size_t)y * pitch2, w2 * 4);
1362 png(path, tmp, w2, 0, 0, 0, w2, h2, 1);
1363 free(tmp);
1364 }
1365 free(fb), free(copy), free(sky), free(mem), free(fb2[0]), free(fb2[1]), free(fb2[2]), free(mem2);
1366 wizard_screens(dir);
1367 printf("ALLEN_UI_TEST: %s (%d checks%s)\n", fails ? "FAIL" : "PASS", checks, fails ? ", some failed" : "");
1368 return fails ? 1 : 0;
1369}