aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / svc / tests / allen_code_test.c
| 1 | /* allen_code_test.c -- host test of code from the Spark (svc/allen_code.c) |
| 2 | * and Allen's coding requests (svc/allen_intent.c): recorded replies turned |
| 3 | * into wire lines by the courier itself (allen-courier --split, made by |
| 4 | * scripts/ck_allen_code_test.sh) are taken whole, in any order, with a piece |
| 5 | * lost and asked again, with a piece spoiled, and against hostile lines; the |
| 6 | * wait for a slow or silent Spark; the rows and the scroll the screen uses. |
| 7 | * |
| 8 | * usage: allen_code_test <fixtures dir> <wire dir> |
| 9 | * fixtures: <name>.txt, a model reply; wire: <name>.wire, its lines. */ |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | |
| 14 | #include "allen_code.h" |
| 15 | #include "allen_intent.h" |
| 16 | #include "allen_reply.h" |
| 17 | |
| 18 | static int failed, checks; |
| 19 | #define CHECK(c, ...) \ |
| 20 | do { \ |
| 21 | checks++; \ |
| 22 | if (!(c)) { \ |
| 23 | failed++; \ |
| 24 | printf("FAIL %s:%d: ", __FILE__, __LINE__); \ |
| 25 | printf(__VA_ARGS__); \ |
| 26 | printf("\n"); \ |
| 27 | } \ |
| 28 | } while (0) |
| 29 | |
| 30 | static char *slurp(const char *dir, const char *name, const char *ext) |
| 31 | { |
| 32 | char path[512]; |
| 33 | snprintf(path, sizeof path, "%s/%s%s", dir, name, ext); |
| 34 | FILE *f = fopen(path, "rb"); |
| 35 | if (!f) { |
| 36 | printf("FAIL cannot open %s\n", path); |
| 37 | exit(1); |
| 38 | } |
| 39 | fseek(f, 0, SEEK_END); |
| 40 | long n = ftell(f); |
| 41 | fseek(f, 0, SEEK_SET); |
| 42 | char *b = malloc((size_t)n + 1); |
| 43 | if (fread(b, 1, (size_t)n, f) != (size_t)n) |
| 44 | exit(1); |
| 45 | b[n] = 0; |
| 46 | fclose(f); |
| 47 | return b; |
| 48 | } |
| 49 | |
| 50 | /* the wire's lines, split in place */ |
| 51 | static unsigned lines_of(char *s, char **out, unsigned max) |
| 52 | { |
| 53 | unsigned n = 0; |
| 54 | while (*s && n < max) { |
| 55 | out[n++] = s; |
| 56 | char *e = strchr(s, '\n'); |
| 57 | if (!e) |
| 58 | break; |
| 59 | *e = 0; |
| 60 | s = e + 1; |
| 61 | } |
| 62 | return n; |
| 63 | } |
| 64 | |
| 65 | /* The code in a fixture reply as the courier keeps it: the first fenced |
| 66 | * block, a newline at its end (the fixtures hold no tabs or odd bytes). */ |
| 67 | static unsigned code_of(const char *reply, char *out, unsigned cap) |
| 68 | { |
| 69 | const char *a = strstr(reply, "```"); |
| 70 | if (!a) |
| 71 | return 0; |
| 72 | a = strchr(a, '\n') + 1; |
| 73 | const char *e = a; |
| 74 | while (*e && !(e[0] == '`' && e[1] == '`' && e[2] == '`' && (e == a || e[-1] == '\n'))) |
| 75 | e++; |
| 76 | while (e > a && e[-1] == '\n') |
| 77 | e--; |
| 78 | unsigned n = (unsigned)(e - a); |
| 79 | if (n + 2 > cap) |
| 80 | return 0; |
| 81 | memcpy(out, a, n); |
| 82 | out[n++] = '\n'; |
| 83 | out[n] = 0; |
| 84 | return n; |
| 85 | } |
| 86 | |
| 87 | /* the kernel's own CODE lines for text, for the hostile cases (the same shape as the courier's) */ |
| 88 | static unsigned make_lines(uint32_t seq, const char *text, uint32_t bytes, const char *lang, char lines[][320], |
| 89 | unsigned max) |
| 90 | { |
| 91 | uint32_t n = (bytes + CK_CODE_PIECE - 1) / CK_CODE_PIECE, crc = ck_code_crc32(text, bytes); |
| 92 | for (uint32_t i = 0; i < n && i < max; i++) { |
| 93 | int k = snprintf(lines[i], 320, "CODE %u %u/%u %u %08x %s ", seq, i + 1, n, bytes, crc, lang); |
| 94 | for (uint32_t j = i * CK_CODE_PIECE; j < bytes && j < (i + 1) * CK_CODE_PIECE; j++) { |
| 95 | char c = text[j]; |
| 96 | if (c == '\n') |
| 97 | lines[i][k++] = '\\', lines[i][k++] = 'n'; |
| 98 | else if (c == '\\') |
| 99 | lines[i][k++] = '\\', lines[i][k++] = '\\'; |
| 100 | else |
| 101 | lines[i][k++] = c; |
| 102 | } |
| 103 | lines[i][k] = 0; |
| 104 | } |
| 105 | return n; |
| 106 | } |
| 107 | |
| 108 | static ck_code_rx rx; |
| 109 | |
| 110 | /* a recorded reply taken whole: in order, then backwards; the closing SAY read as the kernel does */ |
| 111 | static void test_whole(const char *fix, const char *wire_dir, const char *name, unsigned min_pieces, |
| 112 | const char *lang) |
| 113 | { |
| 114 | char *reply = slurp(fix, name, ".txt"), *wire = slurp(wire_dir, name, ".wire"); |
| 115 | static char want[CK_CODE_MAX + 2]; |
| 116 | unsigned wn = code_of(reply, want, sizeof want); |
| 117 | static char *l[CK_CODE_PIECES + 4]; |
| 118 | unsigned n = lines_of(wire, l, CK_CODE_PIECES + 4); |
| 119 | CHECK(n >= min_pieces + 1, "%s: %u lines", name, n); |
| 120 | for (int pass = 0; pass < 2; pass++) { |
| 121 | ck_code_begin(&rx, 1); |
| 122 | for (unsigned k = 0; k + 1 < n; k++) { |
| 123 | unsigned i = pass ? n - 2 - k : k; |
| 124 | CHECK(ck_code_take(&rx, l[i]) == 1, "%s: line %u refused", name, i); |
| 125 | } |
| 126 | CHECK(ck_code_state(&rx) == CK_CODE_WHOLE, "%s pass %d: state %d", name, pass, ck_code_state(&rx)); |
| 127 | CHECK(rx.bytes == wn && !memcmp(rx.text, want, wn), "%s: the code differs (%u vs %u bytes)", name, rx.bytes, |
| 128 | wn); |
| 129 | CHECK(!strcmp(rx.lang, lang), "%s: language %s", name, rx.lang); |
| 130 | } |
| 131 | /* the closing SAY: Allen's sentence */ |
| 132 | uint32_t seq; |
| 133 | unsigned part, total; |
| 134 | char text[160]; |
| 135 | CHECK(ck_code_take(&rx, l[n - 1]) == 0, "%s: the SAY is not a CODE line", name); |
| 136 | CHECK(ck_allen_say_line(l[n - 1], &seq, &part, &total, text, sizeof text) && seq == 1 && part == 1 && total == 1 && |
| 137 | text[0], |
| 138 | "%s: closing line %s", name, l[n - 1]); |
| 139 | printf("ok %s: %u pieces, %u bytes, %u lines, crc %08x, \"%s\"\n", name, rx.n, rx.bytes, |
| 140 | ck_code_lines(rx.text, rx.bytes), rx.crc, text); |
| 141 | /* what the courier sent is what the kernel would send: the same lines from the same code */ |
| 142 | static char mine[CK_CODE_PIECES][320]; |
| 143 | unsigned m = make_lines(1, want, wn, lang[0] ? lang : "-", mine, CK_CODE_PIECES); |
| 144 | int same = m == n - 1; |
| 145 | for (unsigned i = 0; same && i < m; i++) |
| 146 | same = !strcmp(mine[i], l[i]); |
| 147 | CHECK(same, "%s: the courier's lines and the kernel's shape differ", name); |
| 148 | free(reply); |
| 149 | free(wire); |
| 150 | } |
| 151 | |
| 152 | /* a piece lost on the way: the state says which, AGAIN brings the whole set again */ |
| 153 | static void test_lost(const char *wire_dir) |
| 154 | { |
| 155 | char *wire = slurp(wire_dir, "long", ".wire"); |
| 156 | static char *l[CK_CODE_PIECES + 4]; |
| 157 | unsigned n = lines_of(wire, l, CK_CODE_PIECES + 4), lost = (n - 1) / 2 + 1; |
| 158 | ck_code_begin(&rx, 1); |
| 159 | for (unsigned i = 0; i + 1 < n; i++) |
| 160 | if (i + 1 != lost) |
| 161 | ck_code_take(&rx, l[i]); |
| 162 | CHECK(ck_code_state(&rx) == CK_CODE_PART, "lost: state %d", ck_code_state(&rx)); |
| 163 | CHECK(ck_code_missing(&rx) == lost, "lost: missing %u, want %u", ck_code_missing(&rx), lost); |
| 164 | /* AGAIN: the kernel starts the pieces over and the courier sends them all */ |
| 165 | ck_code_begin(&rx, 1); |
| 166 | for (unsigned i = 0; i + 1 < n; i++) |
| 167 | ck_code_take(&rx, l[i]); |
| 168 | CHECK(ck_code_state(&rx) == CK_CODE_WHOLE, "lost then again: state %d", ck_code_state(&rx)); |
| 169 | printf("ok lost piece %u of %u: missing said, whole after AGAIN\n", lost, n - 1); |
| 170 | /* a piece spoiled on the way (its length kept): the checksum catches it */ |
| 171 | char *spoil = l[lost - 1] + strlen(l[lost - 1]) - 1; /* a letter of the code, not an escape's */ |
| 172 | while (!(*spoil >= 'a' && *spoil <= 'z' && spoil[-1] != '\\')) |
| 173 | spoil--; |
| 174 | char was = *spoil; |
| 175 | *spoil = was == 'x' ? 'y' : 'x'; |
| 176 | ck_code_begin(&rx, 1); |
| 177 | for (unsigned i = 0; i + 1 < n; i++) |
| 178 | ck_code_take(&rx, l[i]); |
| 179 | CHECK(ck_code_state(&rx) == CK_CODE_BAD, "spoiled: state %d", ck_code_state(&rx)); |
| 180 | *spoil = was; |
| 181 | printf("ok a spoiled piece: the checksum refuses the whole\n"); |
| 182 | free(wire); |
| 183 | } |
| 184 | |
| 185 | static void test_hostile(void) |
| 186 | { |
| 187 | static char text[CK_CODE_MAX + 1], lines[CK_CODE_PIECES][320]; |
| 188 | for (unsigned i = 0; i < 300; i++) |
| 189 | text[i] = (char)(i % 10 == 9 ? '\n' : 'a' + i % 26); |
| 190 | unsigned n = make_lines(9, text, 300, "c", lines, CK_CODE_PIECES); |
| 191 | struct { |
| 192 | const char *line, *why; |
| 193 | } bad[] = { |
| 194 | {"CODE 8 1/3 300 00000000 c x", "another job"}, |
| 195 | {"CODE 9 0/3 300 00000000 c x", "piece 0"}, |
| 196 | {"CODE 9 4/3 300 00000000 c x", "piece past the count"}, |
| 197 | {"CODE 9 1/2 300 00000000 c x", "a count that does not fit the length"}, |
| 198 | {"CODE 9 1/1 0 00000000 c ", "no bytes"}, |
| 199 | {"CODE 9 1/257 32769 00000000 c x", "past the limit"}, |
| 200 | {"CODE 9 1/3 300 0000000g c x", "a checksum that is not hex"}, |
| 201 | {"CODE 9 1/3 300 00000000 C x", "a language in capitals"}, |
| 202 | {"CODE 9 1/3 300 00000000 c", "no piece"}, |
| 203 | {"CODE 9 3/3 300 00000000 c ab\\tcd", "an unknown escape"}, |
| 204 | {"CODE 9 3/3 300 00000000 c ab\x01", "a control byte"}, |
| 205 | {"CODE 9 3/3 300 00000000 c ab", "a last piece too short"}, |
| 206 | {"CODE 9 1/3 300 00000000 c xyz", "a piece too short"}, |
| 207 | {"CODE 9 3/3 300 00000000 averyveryverylonglanguagename x", "a language too long"}, |
| 208 | {"CODE 9999999999 1/3 300 00000000 c x", "a seq past 32 bits"}, |
| 209 | {"CODE 00000000009 1/3 300 00000000 c x", "a seq of eleven digits"}, |
| 210 | {"CODE 9 1/3 300 00000000 c x", "two spaces"}, |
| 211 | }; |
| 212 | ck_code_begin(&rx, 9); |
| 213 | for (unsigned i = 0; i < sizeof bad / sizeof bad[0]; i++) |
| 214 | CHECK(ck_code_take(&rx, bad[i].line) == -1, "hostile line taken: %s", bad[i].why); |
| 215 | CHECK(rx.refused == sizeof bad / sizeof bad[0] && rx.got == 0 && ck_code_state(&rx) == CK_CODE_NONE, |
| 216 | "refused %u got %u", rx.refused, rx.got); |
| 217 | CHECK(ck_code_take(&rx, "SAY 9 1/1 hello") == 0 && ck_code_take(&rx, "CODEX 9") == 0 && ck_code_take(&rx, 0) == 0, |
| 218 | "lines that are not CODE are 0"); |
| 219 | /* a head that disagrees with the first piece's */ |
| 220 | ck_code_take(&rx, lines[0]); |
| 221 | char other[320]; |
| 222 | snprintf(other, sizeof other, "%s", lines[1]); |
| 223 | char *crc = strstr(other, " 300 ") + 5; |
| 224 | crc[0] = crc[0] == '0' ? '1' : '0'; |
| 225 | CHECK(ck_code_take(&rx, other) == -1, "a disagreeing head is refused"); |
| 226 | for (unsigned i = 1; i < n; i++) |
| 227 | ck_code_take(&rx, lines[i]); |
| 228 | ck_code_take(&rx, lines[1]); /* a piece twice: no harm */ |
| 229 | CHECK(ck_code_state(&rx) == CK_CODE_WHOLE && rx.got == n, "whole after the hostile ones: state %d got %u", |
| 230 | ck_code_state(&rx), rx.got); |
| 231 | /* more lines than the limit: refused whole even with a right checksum */ |
| 232 | unsigned b = 0; |
| 233 | for (unsigned i = 0; i <= CK_CODE_LINES; i++) |
| 234 | text[b++] = 'x', text[b++] = '\n'; |
| 235 | n = make_lines(10, text, b, "-", lines, CK_CODE_PIECES); |
| 236 | ck_code_begin(&rx, 10); |
| 237 | for (unsigned i = 0; i < n; i++) |
| 238 | ck_code_take(&rx, lines[i]); |
| 239 | CHECK(ck_code_state(&rx) == CK_CODE_BAD, "801 lines: state %d", ck_code_state(&rx)); |
| 240 | CHECK(rx.lang[0] == 0, "\"-\" is no language"); |
| 241 | /* the most code there may be: 32768 bytes, all backslashes, every line within the kernel's 320 */ |
| 242 | memset(text, '\\', CK_CODE_MAX); |
| 243 | for (unsigned i = 63; i < CK_CODE_MAX; i += 64) |
| 244 | text[i] = '\n'; |
| 245 | n = make_lines(4000000000u, text, CK_CODE_MAX, "objective-c++", lines, CK_CODE_PIECES); |
| 246 | ck_code_begin(&rx, 4000000000u); |
| 247 | int fit = 1; |
| 248 | for (unsigned i = 0; i < n; i++) |
| 249 | fit &= strlen(lines[i]) < 320 && ck_code_take(&rx, lines[i]) == 1; |
| 250 | CHECK(fit && n == CK_CODE_PIECES && ck_code_state(&rx) == CK_CODE_WHOLE, "the largest code: %u pieces state %d", n, |
| 251 | ck_code_state(&rx)); |
| 252 | printf("ok %u hostile lines refused, a disagreeing head refused, 801 lines refused, 32768 bytes taken\n", |
| 253 | (unsigned)(sizeof bad / sizeof bad[0])); |
| 254 | } |
| 255 | |
| 256 | static void test_wait(void) |
| 257 | { |
| 258 | ck_code_wait w; |
| 259 | ck_code_wait_begin(&w, 1000); |
| 260 | CHECK(ck_code_wait_tick(&w, 1000 + 19999) == CK_CODE_WAIT_NOTHING, "quiet before 20 s"); |
| 261 | CHECK(ck_code_wait_tick(&w, 1000 + 20000) == CK_CODE_WAIT_SLOW, "slow at 20 s"); |
| 262 | CHECK(ck_code_wait_tick(&w, 1000 + 20001) == CK_CODE_WAIT_NOTHING, "slow said once"); |
| 263 | CHECK(ck_code_wait_tick(&w, 1000 + 30000) == CK_CODE_WAIT_GONE, "silent 30 s: gone (the courier is down)"); |
| 264 | /* a busy Spark keeps the job: WAIT every 2 s */ |
| 265 | ck_code_wait_begin(&w, 0); |
| 266 | int gone = 0, slow = 0; |
| 267 | for (uint64_t t = 0; t <= 600000; t += 100) { |
| 268 | if (t % 2000 == 0) |
| 269 | ck_code_wait_heard(&w, t); |
| 270 | int k = ck_code_wait_tick(&w, t); |
| 271 | gone += k == CK_CODE_WAIT_GONE; |
| 272 | slow += k == CK_CODE_WAIT_SLOW; |
| 273 | } |
| 274 | CHECK(!gone && slow == 1, "ten busy minutes: gone %d slow %d", gone, slow); |
| 275 | printf("ok the wait: slow once at 20 s, gone after 30 s of silence, kept while the Spark says WAIT\n"); |
| 276 | } |
| 277 | |
| 278 | static void test_rows(void) |
| 279 | { |
| 280 | const char *t = "short\nthis line is longer than ten\n\nabcdefghijklmnopqrstuvwxyz\nend"; |
| 281 | uint32_t st[32]; |
| 282 | uint16_t ln[32], no[32]; |
| 283 | uint32_t r = ck_code_rows(t, (uint32_t)strlen(t), 10, st, ln, no, 32); |
| 284 | /* short | this line | is longer | than ten | (blank) | abcdefghij | klmnopqrst | uvwxyz | end */ |
| 285 | CHECK(r == 9, "rows %u", r); |
| 286 | CHECK(no[0] == 1 && no[1] == 2 && no[2] == 0 && no[3] == 0 && no[4] == 3 && no[5] == 4 && no[6] == 0 && |
| 287 | no[8] == 5, |
| 288 | "line numbers %u %u %u %u %u %u %u %u", no[0], no[1], no[2], no[3], no[4], no[5], no[6], no[8]); |
| 289 | CHECK(ln[1] == 10 && !memcmp(t + st[1], "this line ", 10) && ln[4] == 0 && ln[5] == 10, "cuts"); |
| 290 | for (uint32_t i = 0; i < r; i++) |
| 291 | CHECK(ln[i] <= 10, "row %u too long", i); |
| 292 | CHECK(ck_code_rows(t, (uint32_t)strlen(t), 10, st, ln, no, 3) == 3, "rows stop at max"); |
| 293 | CHECK(ck_code_scroll(0, -1, 100, 30) == 0 && ck_code_scroll(0, 29, 100, 30) == 29 && |
| 294 | ck_code_scroll(60, 29, 100, 30) == 70 && ck_code_scroll(5, 1, 20, 30) == 0 && |
| 295 | ck_code_scroll(70, -100000, 100, 30) == 0, |
| 296 | "scroll clamps"); |
| 297 | printf("ok rows wrap at a space within reach, numbered by line; the scroll stays in the code\n"); |
| 298 | } |
| 299 | |
| 300 | static void test_intent(void) |
| 301 | { |
| 302 | static const char *const yes[] = {"make me a calculator", "Write a snake game in Python.", |
| 303 | "please write a python script that renames my photos", |
| 304 | "can you build me a todo app", "code me a timer", "Allen, make a pong game in C", |
| 305 | "I need a program that sorts numbers", "create a website for my bakery", 0}; |
| 306 | static const char *const no[] = {"what is my name", "write a poem about python", "make me laugh", "hello", |
| 307 | "remember my favorite color is blue", "tell me a story about a snake", |
| 308 | "write me a letter to my mother", "what is a calculator", "make it quieter", 0}; |
| 309 | char f[256]; |
| 310 | for (const char *const *s = yes; *s; s++) { |
| 311 | ck_allen_fold(*s, f, sizeof f); |
| 312 | CHECK(ck_allen_wants_code(f), "a coding request missed: %s", *s); |
| 313 | } |
| 314 | for (const char *const *s = no; *s; s++) { |
| 315 | ck_allen_fold(*s, f, sizeof f); |
| 316 | CHECK(!ck_allen_wants_code(f), "not a coding request: %s", *s); |
| 317 | } |
| 318 | static const char *const more[] = {"now add a square root key", "add a reset button", "make it use colors", |
| 319 | "also show the history", "change the title to Sums", 0}; |
| 320 | static const char *const not_more[] = {"what time is it", "who are you", "goodbye", 0}; |
| 321 | for (const char *const *s = more; *s; s++) { |
| 322 | ck_allen_fold(*s, f, sizeof f); |
| 323 | CHECK(ck_allen_code_more(f), "a change missed: %s", *s); |
| 324 | } |
| 325 | for (const char *const *s = not_more; *s; s++) { |
| 326 | ck_allen_fold(*s, f, sizeof f); |
| 327 | CHECK(!ck_allen_code_more(f), "not a change: %s", *s); |
| 328 | } |
| 329 | static ck_allen_mind m; |
| 330 | char job[512]; |
| 331 | unsigned n = ck_allen_code_job(&m, 5, "make me a calculator", 0, job, sizeof job); |
| 332 | CHECK(n && !strncmp(job, "JOB 5 code ", 11) && strstr(job, " for code.\tmake me a calculator") && |
| 333 | strlen(job) == n, |
| 334 | "job line: %s", job); |
| 335 | n = ck_allen_code_job(&m, 6, "now add a square root key\x01", 1, job, sizeof job); |
| 336 | CHECK(n && !strncmp(job, "JOB 6 code-more ", 16) && strstr(job, "\tnow add a square root key ") && |
| 337 | !strchr(job, '\x01'), |
| 338 | "code-more job line: %s", job); |
| 339 | CHECK(ck_allen_code_job(&m, 7, "make me a calculator", 0, job, 20) == 0, "a job that does not fit is 0"); |
| 340 | printf("ok coding requests told from talk (%u kinds), changes to shown code, the job lines\n", |
| 341 | (unsigned)(sizeof yes / sizeof yes[0] + sizeof no / sizeof no[0] - 2)); |
| 342 | } |
| 343 | |
| 344 | int main(int argc, char **argv) |
| 345 | { |
| 346 | if (argc != 3) { |
| 347 | fprintf(stderr, "usage: %s <fixtures dir> <wire dir>\n", argv[0]); |
| 348 | return 2; |
| 349 | } |
| 350 | test_whole(argv[1], argv[2], "short", 1, "python"); |
| 351 | test_whole(argv[1], argv[2], "long", 100, "c"); |
| 352 | test_lost(argv[2]); |
| 353 | test_hostile(); |
| 354 | test_wait(); |
| 355 | test_rows(); |
| 356 | test_intent(); |
| 357 | /* courier down is the wait's silence (test_wait); the kernel says so before it asks when no Spark said hello */ |
| 358 | printf("%s (%d checks, %d failed)\n", failed ? "AIENOS_ALLEN_CODE: FAIL" : "AIENOS_ALLEN_CODE: PASS", checks, failed); |
| 359 | return failed != 0; |
| 360 | } |