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 / canvas_test.c

658 lines · 32157 bytes Raw Blame History
1/* canvas_test.c -- host test for Allen's canvas: the scene parser
2 * (svc/allen_scene.c) and the renderer (ui/canvas.c) inside his home
3 * (ui/allen_ui.c).
4 * 1. The parser on fixed scenes: every kind, the colon and no-colon forms,
5 * the limits (lines, things, bytes, field room), unknown lines dropped
6 * quietly, plain words still spoken, thinking and fences ignored.
7 * 2. A fuzz pass: random bytes, mutated scenes and word soup, each in a
8 * buffer of exactly its length (so a sanitizer sees any overrun); the
9 * parsed scene must stay inside its limits, printable, NUL-terminated;
10 * some of them are also laid out and drawn.
11 * 3. Frames at 3840 x 2160: Allen forms, each sample scene arrives after a
12 * line; drawing only what changed must match drawing everything; the
13 * things light the sky beside him; pictures to argv[1].
14 * 4. With --replies DIR: every NN-slug.txt file there (raw replies from the model) is
15 * parsed, counted and drawn the same way (live-*.png).
16 * Frame times (this host, one core) are printed for the plain answers and
17 * the scenes. Final line: CANVAS_TEST: PASS|FAIL (N checks). */
18#include <fcntl.h>
19#include <dirent.h>
20#include <stdint.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <time.h>
25
26#include "../../svc/allen_scene.h"
27#include "../allen_font.h"
28#include "../allen_ui.h"
29#include "../canvas.h"
30#include "../night.h"
31
32static int checks, fails;
33#define CHECK(cond, what) \
34 do { \
35 checks++; \
36 if (!(cond)) { \
37 fails++; \
38 printf("FAIL %s\n", what); \
39 } \
40 } while (0)
41
42/* ---- a minimal PNG writer (stored deflate blocks), host only ---- */
43static uint32_t crc_table[256];
44static void crc_init(void)
45{
46 for (uint32_t n = 0; n < 256; n++) {
47 uint32_t c = n;
48 for (int k = 0; k < 8; k++)
49 c = c & 1 ? 0xedb88320u ^ (c >> 1) : c >> 1;
50 crc_table[n] = c;
51 }
52}
53static uint32_t crc(uint32_t c, const uint8_t *b, size_t n)
54{
55 for (size_t i = 0; i < n; i++)
56 c = crc_table[(c ^ b[i]) & 255] ^ (c >> 8);
57 return c;
58}
59static void be32(uint8_t *p, uint32_t v)
60{
61 p[0] = (uint8_t)(v >> 24), p[1] = (uint8_t)(v >> 16), p[2] = (uint8_t)(v >> 8), p[3] = (uint8_t)v;
62}
63static void chunk(FILE *f, const char *type, const uint8_t *data, size_t n)
64{
65 uint8_t h[8];
66 be32(h, (uint32_t)n);
67 memcpy(h + 4, type, 4);
68 fwrite(h, 1, 8, f);
69 if (n)
70 fwrite(data, 1, n, f);
71 uint32_t c = crc(0xffffffffu, (const uint8_t *)type, 4);
72 c = crc(c, data, n) ^ 0xffffffffu;
73 be32(h, c);
74 fwrite(h, 1, 4, f);
75}
76static void png(const char *path, const uint32_t *px, uint32_t pitch, uint32_t x0, uint32_t y0, uint32_t w, uint32_t h,
77 uint32_t step)
78{
79 uint32_t ow = w / step, oh = h / step;
80 size_t raw_n = (size_t)oh * (1 + (size_t)ow * 3);
81 uint8_t *raw = malloc(raw_n);
82 size_t k = 0;
83 for (uint32_t y = 0; y < oh; y++) {
84 raw[k++] = 0;
85 for (uint32_t x = 0; x < ow; x++) {
86 uint32_t sr = 0, sg = 0, sb = 0;
87 for (uint32_t j = 0; j < step; j++)
88 for (uint32_t i = 0; i < step; i++) {
89 uint32_t c = px[((size_t)y0 + (size_t)y * step + j) * pitch + x0 + (size_t)x * step + i];
90 sr += c >> 16 & 255, sg += c >> 8 & 255, sb += c & 255;
91 }
92 raw[k++] = (uint8_t)(sr / (step * step)), raw[k++] = (uint8_t)(sg / (step * step)),
93 raw[k++] = (uint8_t)(sb / (step * step));
94 }
95 }
96 size_t blocks = raw_n / 65535 + 1, o = 0;
97 uint8_t *z = malloc(2 + raw_n + blocks * 5 + 4);
98 z[o++] = 0x78, z[o++] = 0x01;
99 uint32_t a = 1, b = 0;
100 for (size_t i = 0; i < raw_n;) {
101 size_t n = raw_n - i > 65535 ? 65535 : raw_n - i;
102 z[o++] = i + n == raw_n;
103 z[o++] = (uint8_t)n, z[o++] = (uint8_t)(n >> 8), z[o++] = (uint8_t)~n, z[o++] = (uint8_t)(~n >> 8);
104 memcpy(z + o, raw + i, n);
105 for (size_t j = 0; j < n; j++) {
106 a = (a + raw[i + j]) % 65521;
107 b = (b + a) % 65521;
108 }
109 o += n;
110 i += n;
111 }
112 be32(z + o, b << 16 | a);
113 o += 4;
114 int f_fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
115 FILE *f = f_fd < 0 ? NULL : fdopen(f_fd, "wb");
116 if (f) {
117 fwrite("\x89PNG\r\n\x1a\n", 1, 8, f);
118 uint8_t ihdr[13];
119 be32(ihdr, ow), be32(ihdr + 4, oh);
120 ihdr[8] = 8, ihdr[9] = 2, ihdr[10] = 0, ihdr[11] = 0, ihdr[12] = 0;
121 chunk(f, "IHDR", ihdr, 13);
122 chunk(f, "IDAT", z, o);
123 chunk(f, "IEND", 0, 0);
124 fclose(f);
125 printf("note: wrote %s (%ux%u)\n", path, ow, oh);
126 }
127 free(raw), free(z);
128}
129
130/* ---- the parser's promises, checked on any scene ---- */
131static int sane(const ck_scene *s)
132{
133 if (s->nthings > CK_SCENE_THINGS || s->used > CK_SCENE_TEXT)
134 return 0;
135 for (unsigned i = 0; i < s->used; i++)
136 if (s->text[i] && (s->text[i] < 32 || s->text[i] > 126))
137 return 0;
138 if (s->has_say && (s->say >= s->used || strlen(s->text + s->say) > 159))
139 return 0;
140 for (unsigned t = 0; t < s->nthings; t++) {
141 const ck_scene_thing *th = &s->thing[t];
142 if (th->kind < CK_THING_NOTE || th->kind > CK_THING_VIDEO || th->nf == 0 || th->nf > CK_SCENE_FIELDS)
143 return 0;
144 for (unsigned f = 0; f < th->nf; f++) {
145 if (th->f[f] >= s->used || !memchr(s->text + th->f[f], 0, s->used - th->f[f]))
146 return 0;
147 if (strlen(s->text + th->f[f]) > 243)
148 return 0;
149 }
150 }
151 return 1;
152}
153
154static int parse_str(const char *in, ck_scene *s, ck_scene_stats *st) { return ck_scene_parse(in, strlen(in), s, st); }
155
156static uint64_t rng = 0x9e3779b97f4a7c15ull;
157static uint32_t rnd(void)
158{
159 rng ^= rng << 13, rng ^= rng >> 7, rng ^= rng << 17;
160 return (uint32_t)(rng >> 16);
161}
162
163/* ---- the screen ---- */
164static uint64_t real_us(void)
165{
166 struct timespec ts;
167 clock_gettime(CLOCK_MONOTONIC, &ts);
168 return (uint64_t)ts.tv_sec * 1000000u + (uint64_t)ts.tv_nsec / 1000u;
169}
170static uint64_t now_us;
171struct timing {
172 uint64_t sum, n, worst;
173};
174static struct timing *timing;
175static void run_to(uint32_t ms)
176{
177 for (; now_us <= (uint64_t)ms * 1000u; now_us += 16000) {
178 uint64_t a = real_us();
179 int drew = ck_allen_ui_tick(now_us);
180 uint64_t d = real_us() - a;
181 if (drew && timing) {
182 timing->sum += d, timing->n++;
183 if (d > timing->worst)
184 timing->worst = d;
185 }
186 }
187}
188static unsigned differ(const uint32_t *a, const uint32_t *b, size_t n, int tol)
189{
190 unsigned c = 0;
191 for (size_t i = 0; i < n; i++)
192 for (int s = 0; s < 24; s += 8) {
193 int e = (int)(a[i] >> s & 255) - (int)(b[i] >> s & 255);
194 if (e > tol || e < -tol) {
195 c++;
196 break;
197 }
198 }
199 return c;
200}
201static unsigned stale(uint32_t *fb, uint32_t *copy, size_t n)
202{
203 memcpy(copy, fb, n * 4);
204 ck_allen_ui_refresh();
205 struct timing *keep = timing;
206 timing = 0;
207 ck_allen_ui_tick(now_us - 16000);
208 timing = keep;
209 return differ(fb, copy, n, 6);
210}
211/* pixels in a rectangle brighter than the night by more than tol in any channel */
212static unsigned lit(const uint32_t *fb, uint32_t pitch, uint32_t x0, uint32_t y0, uint32_t w, uint32_t h, int tol)
213{
214 unsigned n = 0;
215 for (uint32_t y = y0; y < y0 + h; y++)
216 for (uint32_t x = x0; x < x0 + w; x++) {
217 uint32_t c = fb[(size_t)y * pitch + x];
218 n += (int)(c >> 16 & 255) > 6 + tol || (int)(c >> 8 & 255) > 8 + tol || (int)(c & 255) > 18 + tol;
219 }
220 return n;
221}
222/* text-bright pixels (type) in a rectangle */
223static unsigned bright(const uint32_t *fb, uint32_t pitch, uint32_t x0, uint32_t y0, uint32_t w, uint32_t h)
224{
225 unsigned n = 0;
226 for (uint32_t y = y0; y < y0 + h; y++)
227 for (uint32_t x = x0; x < x0 + w; x++) {
228 uint32_t c = fb[(size_t)y * pitch + x];
229 n += (c >> 16 & 255) > 150 && (c >> 8 & 255) > 150 && (c & 255) > 150;
230 }
231 return n;
232}
233
234struct sample {
235 const char *name, *line, *scene;
236};
237static const struct sample samples[] = {
238 {"sum", "what's 17% of 240",
239 "mood calm\nsay Seventeen percent of 240 is 40.8.\nnumber right: 40.8 | 17% of 240\n"
240 "note left quiet: How | 240 x 0.17 = 40.8, a tenth (24) plus seven hundredths (16.8)."},
241 {"reminder", "remind me to call mom at 6",
242 "mood dawn\nsay I'll keep this in front of you: call Mom at six.\ntimer right warm: 6:00 pm | Call Mom"},
243 {"friend", "tell my friend Sam I'm running late",
244 "say I'll tell Sam you're running late.\nperson to right: Sam | Running late, I'll be there soon."},
245 {"video", "play a video about black holes",
246 "mood deep\nsay A film about black holes will play here once I can reach one.\n"
247 "video right: Black holes, explained | a short film\n"
248 "note left: While it comes | A black hole is gravity so strong that not even light gets out."},
249 {"beautiful", "show me something beautiful",
250 "mood clear lively\nsay Look up a moment. Here is something old and quiet.\n"
251 "quote left: We are made of star-stuff. | Carl Sagan\npicture right: The Pleiades rising at midnight"},
252 {"walk", "what's the weather like for a walk",
253 "mood dawn\nsay I can't see outside yet. Here is what to check before you go.\n"
254 "list right: Before a walk | Look out of a window | A layer for the wind | Water and your keys"},
255 {"focus", "give me 25 minutes to focus",
256 "say Twenty-five minutes, starting now. I'll keep the time.\ntimer above: 25m | Focus"},
257};
258#define NSAMPLES (sizeof samples / sizeof samples[0])
259
260static uint32_t W = 3840, H = 2160;
261static unsigned base_lit, base_type; /* the same regions with no things, after a plain answer */
262static uint32_t *fb, *cpy;
263
264/* A line, the model's reply to it, and what it paints: checks, picture, the frame times in *tm. */
265static void show(const char *tag, const char *line, const char *reply, size_t len, const char *dir, int expect_things,
266 struct timing *tm)
267{
268 char path[512], what[160];
269 uint32_t t0 = (uint32_t)(now_us / 1000);
270 timing = tm;
271 ck_allen_ui_think(line, now_us);
272 run_to(t0 + 1400); /* he thinks; the old things go */
273 ck_allen_ui_scene(reply, (unsigned)len, now_us);
274 uint32_t t1 = (uint32_t)(now_us / 1000);
275 run_to(t1 + 900); /* the spark flies, the first thing opens */
276 snprintf(what, sizeof what, "%s: drawn whole while things arrive", tag);
277 CHECK(stale(fb, cpy, (size_t)W * H) < 60, what);
278 run_to(t1 + 3600);
279 snprintf(what, sizeof what, "%s: drawn whole once they rest", tag);
280 CHECK(stale(fb, cpy, (size_t)W * H) < 60, what);
281 run_to(t1 + 5400); /* and Allen has finished speaking */
282 timing = 0;
283 if (expect_things) {
284 uint32_t cx = W / 2, cy = H * 40 / 100;
285 unsigned side = lit(fb, W, 200, cy - 700, cx - 200 - 400, 1300, 30) + lit(fb, W, cx + 400, cy - 700, W - cx - 600, 1300, 30) +
286 lit(fb, W, cx - 600, 80, 1200, cy - 80 - 280, 30);
287 unsigned type = bright(fb, W, 200, 80, cx - 600, 1800) + bright(fb, W, cx + 400, 80, W - cx - 600, 1800) +
288 bright(fb, W, cx - 600, 80, 1200, cy - 80 - 280);
289 printf("note: %s: %u lit, %u type (no things: %u, %u)\n", tag, side, type, base_lit, base_type);
290 snprintf(what, sizeof what, "%s: things light the sky beside or above Allen (%u lit, %u type; no things: %u, %u)", tag,
291 side, type, base_lit, base_type);
292 CHECK(type > base_type + 2500, what);
293 (void)side;
294 } else if (!strncmp(tag, "plain", 5)) {
295 uint32_t cx = W / 2, cy = H * 40 / 100;
296 base_lit = lit(fb, W, 200, cy - 700, cx - 200 - 400, 1300, 30) + lit(fb, W, cx + 400, cy - 700, W - cx - 600, 1300, 30) +
297 lit(fb, W, cx - 600, 80, 1200, cy - 80 - 280, 30);
298 base_type = bright(fb, W, 200, 80, cx - 600, 1800) + bright(fb, W, cx + 400, 80, W - cx - 600, 1800) +
299 bright(fb, W, cx - 600, 80, 1200, cy - 80 - 280);
300 }
301 if (dir) {
302 snprintf(path, sizeof path, "%s/%s.png", dir, tag);
303 png(path, fb, W, 0, 0, W, H, 2);
304 }
305}
306
307static char *slurp(const char *path, size_t *n)
308{
309 FILE *f = fopen(path, "rb");
310 if (!f)
311 return 0;
312 fseek(f, 0, SEEK_END);
313 long m = ftell(f);
314 fseek(f, 0, SEEK_SET);
315 char *b = malloc((size_t)m + 1);
316 *n = fread(b, 1, (size_t)m, f);
317 b[*n] = 0;
318 fclose(f);
319 return b;
320}
321
322static void no_mark(ck_rect r) { (void)r; }
323
324static int by_name(const void *a, const void *b) { return strcmp(*(char *const *)a, *(char *const *)b); }
325
326int main(int argc, char **argv)
327{
328 const char *dir = 0, *replies = 0;
329 unsigned fuzz_n = 300000;
330 int parse_only = 0;
331 for (int i = 1; i < argc; i++) {
332 if (!strcmp(argv[i], "--replies") && i + 1 < argc)
333 replies = argv[++i];
334 else if (!strcmp(argv[i], "--parse-only"))
335 parse_only = 1;
336 else if (!strcmp(argv[i], "--fuzz") && i + 1 < argc)
337 fuzz_n = (unsigned)strtoul(argv[++i], 0, 10);
338 else
339 dir = argv[i];
340 }
341 crc_init();
342 static ck_scene s;
343 ck_scene_stats st;
344
345 /* ---- 1. the parser on fixed scenes ---- */
346 CHECK(parse_str("mood dawn lively\nsay Seventeen percent of 240 is 40.8.\nnumber right warm: 40.8 | 17% of 240\n"
347 "note left: How | 240 x 0.17 = 40.8\n",
348 &s, &st) &&
349 s.tone == CK_TONE_DAWN && s.water == CK_WATER_LIVELY && !strcmp(ck_scene_say(&s), "Seventeen percent of 240 is 40.8.") &&
350 s.nthings == 2 && s.thing[0].kind == CK_THING_NUMBER && s.thing[0].place == CK_PLACE_RIGHT && s.thing[0].warm &&
351 !strcmp(ck_scene_field(&s, &s.thing[0], 0), "40.8") && !strcmp(ck_scene_field(&s, &s.thing[0], 1), "17% of 240") &&
352 s.thing[1].kind == CK_THING_NOTE && s.thing[1].place == CK_PLACE_LEFT && ck_scene_clean(&st, &s) && st.kept == 4,
353 "scene: mood, say and two things, read whole");
354 {
355 static const char *const kinds[] = {"note: a | b", "list: Title | one | two", "number: 42",
356 "quote: Be here. | Ram", "person: Sam | late", "timer: 25m | Focus",
357 "picture: the sea", "video: rain | 3 min"};
358 int ok = 1;
359 for (unsigned k = 0; k < 8; k++) {
360 char b[96];
361 snprintf(b, sizeof b, "say ok\n%s", kinds[k]);
362 if (!parse_str(b, &s, &st) || s.nthings != 1 || s.thing[0].kind != k + 1 || !ck_scene_clean(&st, &s)) {
363 printf(" kind %u: \"%s\" things %u rejected %u\n", k + 1, kinds[k], s.nthings, st.rejected);
364 ok = 0;
365 }
366 }
367 CHECK(ok, "scene: every kind of thing");
368 }
369 CHECK(parse_str("Say: Hi there.\nNUMBER 6:00 pm | dinner\ntimer 6:00 pm | call", &s, &st) && s.nthings == 2 &&
370 !strcmp(ck_scene_say(&s), "Hi there.") && !strcmp(ck_scene_field(&s, &s.thing[0], 0), "6:00 pm") &&
371 !strcmp(ck_scene_field(&s, &s.thing[1], 1), "call") && ck_scene_clean(&st, &s),
372 "scene: any case, and the forms without a colon after the words");
373 CHECK(parse_str("person from: Ana | Landed safe, see you soon\nsay Ana wrote.", &s, &st) && s.thing[0].way == CK_WAY_FROM &&
374 parse_str("note to: x | y\nsay a", &s, &st) && st.unknown == 1 && s.nthings == 1,
375 "scene: to and from are a person's; elsewhere an unknown word, ignored");
376 CHECK(parse_str("say hi\nwindow left: a | b\nbutton: OK\nnote: fine", &s, &st) && s.nthings == 1 && st.rejected == 2 &&
377 !ck_scene_clean(&st, &s),
378 "scene: lines it does not know are dropped and counted, never shown");
379 CHECK(parse_str("Sunlight scatters in the air, blue most of all. That is why the sky is blue.", &s, &st) &&
380 !strcmp(ck_scene_say(&s), "Sunlight scatters in the air, blue most of all. That is why the sky is blue.") &&
381 !s.nthings && st.rejected == 1,
382 "scene: plain words, no scene: Allen still says them");
383 CHECK(parse_str("<think>\nthe user wants a sum\nnote: nope\n</think>\n```\nsay 2.\nnumber: 2\n```", &s, &st) &&
384 s.nthings == 1 && !strcmp(ck_scene_say(&s), "2.") && st.lines == 2 && st.tidied == 2,
385 "scene: thinking is skipped, fences taken off (and counted)");
386 CHECK(parse_str("- say a\n* note: b\n1. list: c | d\n**number**: 5", &s, &st) && s.nthings == 3 && s.has_say &&
387 st.tidied == 4 && !st.rejected,
388 "scene: list marks and bold taken off, counted as tidied");
389 {
390 char b[4096];
391 unsigned n = 0;
392 n += (unsigned)snprintf(b + n, sizeof b - n, "say first\n");
393 for (int i = 0; i < 15; i++) {
394 int w = snprintf(b + n, sizeof b - n, "note: line %d\n", i);
395 if (w < 0 || (size_t)w >= sizeof b - n)
396 break; /* cannot happen at 4096 bytes; parse_str's line count then fails the CHECK */
397 n += (unsigned)w;
398 }
399 CHECK(parse_str(b, &s, &st) && s.nthings == CK_SCENE_THINGS && st.lines == 16 && st.over == 4 + 7,
400 "scene: at most 12 lines and 4 things");
401 memset(b, 'a', sizeof b);
402 memcpy(b, "say ", 4);
403 b[sizeof b - 1] = 0;
404 CHECK(!parse_str(b, &s, &st) && st.truncated && !s.has_say, "scene: past 2 KiB is not read; the cut line is dropped");
405 {
406 static const char nul[] = "say hi\nnote: x\0note: hidden";
407 CHECK(ck_scene_parse(nul, sizeof nul - 1, &s, &st) == 1 && s.nthings == 1, "scene: a NUL ends the reply");
408 }
409 char lng[600];
410 memset(lng, 0, sizeof lng);
411 strcpy(lng, "say ok\nnumber: 1 | ");
412 for (int i = 0; i < 40; i++)
413 strcat(lng, "word ");
414 parse_str(lng, &s, &st);
415 const char *cap = ck_scene_field(&s, &s.thing[0], 1);
416 CHECK(strlen(cap) <= 64 && !strcmp(cap + strlen(cap) - 3, "...") && st.cut == 1, "scene: a field too long is cut at a word, with ...");
417 }
418 CHECK(parse_str("say caf\xc3\xa9 \x01\x7f ok\tthen", &s, &st) && !strcmp(ck_scene_say(&s), "caf ok then") && st.cut == 1,
419 "scene: only printable ASCII is kept");
420 CHECK(parse_str("say a\nnumber: lots | of things\nperson: Sam", &s, &st) && !s.nthings && st.rejected == 2,
421 "scene: a number with no digits, a person with no message: dropped");
422 CHECK(parse_str("say one\nsay two\nmood deep\nmood dawn", &s, &st) && !strcmp(ck_scene_say(&s), "one") && s.tone == CK_TONE_DEEP &&
423 st.over == 2,
424 "scene: the first say and mood count");
425 CHECK(ck_scene_duration_ms("25m") == 1500000 && ck_scene_duration_ms("1h 30m") == 5400000 &&
426 ck_scene_duration_ms("90 seconds") == 90000 && ck_scene_duration_ms("in 10 minutes") == 600000 &&
427 ck_scene_duration_ms("1.5 hours") == 5400000 && ck_scene_duration_ms("6:00 pm") == 0 &&
428 ck_scene_duration_ms("soon") == 0 && ck_scene_duration_ms("25 parsecs") == 0 &&
429 ck_scene_duration_ms("30 hours") == 0 && ck_scene_duration_ms("") == 0 &&
430 ck_scene_duration_ms("99999999999 s") == 0,
431 "scene: a timer's length (and what is not one)");
432 {
433 char w[] = "say hi\\nnote: a \\\\ b";
434 size_t n = ck_scene_unwire(w, strlen(w));
435 CHECK(n == strlen("say hi\nnote: a \\ b") && !strcmp(w, "say hi\nnote: a \\ b"), "scene: the courier's one-line form unwired");
436 }
437 CHECK(!ck_scene_parse(0, 10, &s, &st) && !ck_scene_parse("", 0, &s, 0) && !ck_scene_parse("\n\n \n", 5, &s, &st),
438 "scene: nothing in, nothing to show");
439
440 /* ---- 2. fuzz ---- */
441 {
442 static const char *const soup[] = {"say", "mood", "note", "list", "number", "quote", "person", "timer",
443 "picture", "video", ":", "|", " ", "\n", "left", "right", "above", "warm",
444 "quiet", "to", "from", "bloom", "rise", "fade", "dawn", "deep", "lively",
445 "12", "6:00", "25m", "```", "<think>", "</think>", "-", "**", "\xe2\x80\x94",
446 "\t", "\r", "||||", "::::", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"};
447 unsigned nsoup = sizeof soup / sizeof soup[0], bad = 0, shown = 0, things = 0, rejected = 0, lines = 0;
448 unsigned drawn = 0;
449 char *buf = malloc(4096);
450 /* a small screen for drawing fuzzed scenes */
451 static uint8_t pv[2 << 20], pb[2 << 20], ps[2 << 20];
452 static ck_af_face fv, fbd, fs;
453 uint32_t k = 170, cw = 1280, ch = 800;
454 ck_af_face_init(&fv, 19 * k / 256, 1000, pv, sizeof pv);
455 ck_af_face_init(&fbd, 15 * k / 256, 1150, pb, sizeof pb);
456 ck_af_face_init(&fs, 11 * k / 256, 1250, ps, sizeof ps);
457 for (int c = CK_AF_FIRST; c <= CK_AF_LAST; c++)
458 ck_af_glyph_get(&fv, (char)c), ck_af_glyph_get(&fbd, (char)c), ck_af_glyph_get(&fs, (char)c);
459 static ck_allen_canvas cv0;
460 uint32_t *px = calloc((size_t)cw * ch, 4);
461 ck_canvas pic = {px, (int32_t)cw, (int32_t)ch};
462 CHECK(ck_allen_canvas_init(&cv0, (int32_t)cw, (int32_t)ch, k, (int32_t)cw / 2, (int32_t)ch * 40 / 100, &fv, &fbd, &fs) == 0,
463 "fuzz: a small canvas to draw on");
464 uint32_t tt = 1000;
465 for (unsigned it = 0; it < fuzz_n; it++) {
466 size_t n = 0;
467 unsigned mode = rnd() % 3;
468 if (mode == 0) { /* random bytes */
469 n = rnd() % 3000;
470 if (n > 4096)
471 n = 4096;
472 for (size_t i = 0; i < n; i++)
473 buf[i] = (char)(rnd() & 0xff);
474 } else if (mode == 1) { /* a sample, mutated */
475 const char *base = samples[rnd() % NSAMPLES].scene;
476 n = strlen(base);
477 memcpy(buf, base, n);
478 for (unsigned m = rnd() % 12; m; m--) {
479 unsigned op = rnd() % 4, at = n ? rnd() % (unsigned)n : 0;
480 if (op == 0 && n)
481 buf[at] = (char)(rnd() & 0xff);
482 else if (op == 1 && n < 4000)
483 memmove(buf + at + 1, buf + at, n - at), buf[at] = "|:\n \x00"[rnd() % 5], n++;
484 else if (op == 2 && n)
485 memmove(buf + at, buf + at + 1, n - at - 1), n--;
486 else if (n && n * 2 < 4000) /* the whole thing again */
487 memcpy(buf + n, buf, n), n *= 2;
488 }
489 } else { /* word soup */
490 unsigned words = rnd() % 200;
491 for (unsigned w = 0; w < words; w++) {
492 const char *x = soup[rnd() % nsoup];
493 size_t xl = strlen(x);
494 if (n + xl >= 4096)
495 break;
496 memcpy(buf + n, x, xl), n += xl;
497 }
498 }
499 char *exact = malloc(n ? n : 1); /* exactly n bytes: reading past them is caught */
500 memcpy(exact, buf, n);
501 int r = ck_scene_parse(exact, n, &s, &st);
502 free(exact);
503 if (!sane(&s))
504 bad++;
505 shown += r != 0, things += s.nthings, rejected += st.rejected, lines += st.lines;
506 if (s.nthings && (it % 97 == 0)) { /* drawn too */
507 ck_allen_canvas_show(&cv0, &s, tt);
508 for (uint32_t d = 0; d <= 3200; d += 400) {
509 ck_allen_canvas_at(&cv0, tt + d, (int32_t)cw * 8, (int32_t)ch * 6, 0, no_mark);
510 }
511 ck_rect all = {0, 0, (int32_t)cw, (int32_t)ch};
512 ck_allen_canvas_draw(&cv0, &pic, &all);
513 drawn++;
514 tt += 5000;
515 }
516 }
517 free(buf);
518 free(px);
519 printf("note: fuzz %u replies: %u gave something to show, %u things, %u lines read, %u lines dropped, %u drawn, %u broke a limit\n",
520 fuzz_n, shown, things, lines, rejected, drawn, bad);
521 CHECK(bad == 0, "fuzz: every parsed scene stays within its limits, printable and NUL-terminated");
522 CHECK(fuzz_n < 1000 || (shown > fuzz_n / 10 && drawn > fuzz_n / 400), "fuzz: the fuzz reached real scenes and drew them");
523 }
524
525 if (parse_only) {
526 printf("CANVAS_TEST: %s (%d checks%s, parser and fuzz only)\n", fails ? "FAIL" : "PASS", checks,
527 fails ? ", some failed" : "");
528 return fails ? 1 : 0;
529 }
530 /* ---- 3. frames at 3840 x 2160 ---- */
531 fb = calloc((size_t)W * H, 4), cpy = malloc((size_t)W * H * 4);
532 size_t need = ck_allen_ui_mem_bytes(W, H);
533 void *mem = malloc(need);
534 CHECK(ck_allen_ui_start(fb, W, H, W, 1, mem, need, 1000000, 0, real_us) == 0, "ui: starts at 3840x2160 with the canvas");
535 struct timing t_start = {0}, t_plain = {0}, t_scene = {0}, t_live = {0};
536 timing = &t_start;
537 now_us = 0;
538 run_to(16500);
539 ck_allen_ui_wake("Type goodbye and this computer rests; any key wakes it", now_us);
540 run_to(17800);
541 timing = 0;
542 /* plain answers first: the cost of the frames without things */
543 static const char *const plain[][2] = {{"hello allen", "Hello Humanity."},
544 {"how are you", "I'm running well. 4 cores awake."},
545 {"what is 12 times 4", "12 times 4 is 48."}};
546 for (unsigned i = 0; i < 3; i++) {
547 char tag[32];
548 snprintf(tag, sizeof tag, "plain-%u", i + 1);
549 show(tag, plain[i][0], plain[i][1], strlen(plain[i][1]), 0, 0, &t_plain);
550 }
551 for (unsigned i = 0; i < NSAMPLES; i++) {
552 char tag[48];
553 snprintf(tag, sizeof tag, "canvas-%u-%s", i + 1, samples[i].name);
554 show(tag, samples[i].line, samples[i].scene, strlen(samples[i].scene), dir, 1, &t_scene);
555 if (i == 0 && dir) { /* the arrival itself, a moment in */
556 ck_allen_ui_think("what's 17% of 240", now_us);
557 run_to((uint32_t)(now_us / 1000) + 1400);
558 ck_allen_ui_scene(samples[0].scene, (unsigned)strlen(samples[0].scene), now_us);
559 uint32_t ts = (uint32_t)(now_us / 1000);
560 char path[512];
561 static const uint32_t at[3] = {700, 850, 1050};
562 for (unsigned q = 0; q < 3; q++) { /* the spark on its way from his heart, at full size */
563 run_to(ts + at[q]);
564 snprintf(path, sizeof path, "%s/canvas-0-arriving-%u.png", dir, q + 1);
565 png(path, fb, W, W / 2 - 200, H * 40 / 100 - 520, 1800, 800, 1);
566 }
567 snprintf(path, sizeof path, "%s/canvas-0-arriving.png", dir);
568 png(path, fb, W, 0, 0, W, H, 2);
569 run_to((uint32_t)(now_us / 1000) + 2800);
570 }
571 }
572 /* left alone, the things stay a while and then go; Allen is alone again */
573 {
574 uint32_t t0 = (uint32_t)(now_us / 1000);
575 run_to(t0 + 30000);
576 CHECK(ck_allen_ui_started(), "rest: still running");
577 ck_allen_ui_think("thanks", now_us);
578 run_to(t0 + 30000 + 1200);
579 CHECK(stale(fb, cpy, (size_t)W * H) < 60, "rest: things leaving are drawn whole");
580 ck_allen_ui_said(0, "You're welcome.", now_us);
581 run_to(t0 + 30000 + 4000);
582 uint32_t cx = W / 2;
583 unsigned left = bright(fb, W, 200, 80, cx - 600, 1800) + bright(fb, W, cx + 400, 80, W - cx - 600, 1800) +
584 bright(fb, W, cx - 600, 80, 1200, H * 40 / 100 - 80 - 280);
585 char what[160];
586 snprintf(what, sizeof what, "rest: a new line sends the things away; only Allen and his words stay (%u type, %u with none)",
587 left, base_type);
588 CHECK(left < base_type + 2500, what);
589 if (dir) {
590 char path[512];
591 snprintf(path, sizeof path, "%s/canvas-9-gone.png", dir);
592 png(path, fb, W, 0, 0, W, H, 2);
593 }
594 }
595
596 /* ---- 4. real replies from the model ---- */
597 if (replies) {
598 DIR *d = opendir(replies);
599 char *names[64];
600 unsigned nn = 0, clean = 0, used = 0, parsed = 0;
601 struct dirent *e;
602 while (d && (e = readdir(d)) && nn < 64) {
603 size_t l = strlen(e->d_name);
604 if (l > 4 && !strcmp(e->d_name + l - 4, ".txt") && e->d_name[0] >= '0' && e->d_name[0] <= '9') /* NN-slug.txt */
605 names[nn++] = strdup(e->d_name);
606 }
607 if (d)
608 closedir(d);
609 qsort(names, nn, sizeof names[0], by_name);
610 CHECK(nn > 0, "live: replies to read");
611 for (unsigned i = 0; i < nn; i++) {
612 char path[1024], ask_path[1024], tag[128];
613 snprintf(path, sizeof path, "%s/%s", replies, names[i]);
614 snprintf(ask_path, sizeof ask_path, "%s/ask-%s", replies, names[i]);
615 size_t n = 0, an = 0;
616 char *b = slurp(path, &n), *ask = slurp(ask_path, &an);
617 if (!b)
618 continue;
619 int r = ck_scene_parse(b, n, &s, &st);
620 int ok = ck_scene_clean(&st, &s);
621 parsed += r != 0, clean += ok, used += s.nthings > 0;
622 printf("live %s: %s; lines %u kept %u dropped %u tidied %u over %u cut %u; things %u:", names[i],
623 ok ? "CLEAN" : "not clean", st.lines, st.kept, st.rejected, st.tidied, st.over, st.cut, s.nthings);
624 for (unsigned t = 0; t < s.nthings; t++)
625 printf(" %s", ck_scene_kind_name(s.thing[t].kind));
626 printf("\n");
627 CHECK(sane(&s), "live: the reply's scene stays within its limits");
628 snprintf(tag, sizeof tag, "live-%.*s", (int)(strlen(names[i]) - 4), names[i]);
629 if (ask) {
630 char *nl = strchr(ask, '\n');
631 if (nl)
632 *nl = 0;
633 }
634 show(tag, ask ? ask : "a question", b, n, dir, 0, &t_live);
635 free(b), free(ask), free(names[i]);
636 }
637 printf("LIVE: %u replies, %u parsed cleanly, %u used the canvas (at least one thing), %u gave something to show\n",
638 nn, clean, used, parsed);
639 }
640 uint32_t frames, slowest, average;
641 ck_allen_ui_stats(&frames, &slowest, &average);
642 printf("note: frames at 3840x2160 on one core of this host (us): whole run avg %u worst %u over %u frames\n", average,
643 slowest, frames);
644 printf("note: startup avg %llu worst %llu (%llu frames); plain answers avg %llu worst %llu (%llu); "
645 "scenes avg %llu worst %llu (%llu)",
646 (unsigned long long)(t_start.n ? t_start.sum / t_start.n : 0), (unsigned long long)t_start.worst,
647 (unsigned long long)t_start.n, (unsigned long long)(t_plain.n ? t_plain.sum / t_plain.n : 0),
648 (unsigned long long)t_plain.worst, (unsigned long long)t_plain.n,
649 (unsigned long long)(t_scene.n ? t_scene.sum / t_scene.n : 0), (unsigned long long)t_scene.worst,
650 (unsigned long long)t_scene.n);
651 if (t_live.n)
652 printf("; live avg %llu worst %llu (%llu)", (unsigned long long)(t_live.sum / t_live.n),
653 (unsigned long long)t_live.worst, (unsigned long long)t_live.n);
654 printf("\n");
655 free(fb), free(cpy), free(mem);
656 printf("CANVAS_TEST: %s (%d checks%s)\n", fails ? "FAIL" : "PASS", checks, fails ? ", some failed" : "");
657 return fails ? 1 : 0;
658}