Artifact 48c4f2f9941226bd125dc86fac85d84d1fe622dec229a1ed78d14becc92b4118:
- File
test/unit.c
— part of check-in
[6d70dcc76a]
at
2026-11-12 19:36:41
on branch trunk
— Phase 1: the config parser, the no-change run and the speed bench
suit check parses suit.conf strictly and prints a canonical dump. Any unknown kind or key, duplicate, cycle or dangling name rejects the whole file with its line number.
suit run compares a stamp against the files it names and exits without writing, allocating or printing when nothing changed.
suit bench times suit run against an empty program built the same way and against /usr/bin/true, interleaved, and fails the build when the no-change run costs more than 1 ms over the empty program or 1 MiB more memory. It measured 0.65 to 0.94 ms over, quiet and under load. A system binary starts 0.6 to 1.5 ms faster than any built binary on macOS, so it is shown for information and is not the baseline.
94 unit checks, 100 shell cases and a libFuzzer target for the parser. (user: dmestas, size: 7060) [annotate] [blame] [check-ins using]
// Unit tests for the config parser's API. The shell tests in run.sh cover the // command line; these cover the parser directly, including the limits. #include "conf.h" #include <stdio.h> #include <stdlib.h> #include <string.h> static Conf conf; static int checks; static int failures; #define CHECK(cond) \ do { \ checks++; \ if (!(cond)) { \ failures++; \ printf("FAIL unit %s:%d: %s\n", __FILE__, __LINE__, #cond); \ } \ } while (0) static bool parse(const char *text, ConfError *e) { return conf_parse(&conf, text, strlen(text), e); } static void expect_error(const char *text, int line, const char *part) { ConfError e; bool ok = parse(text, &e); CHECK(!ok); CHECK(e.line == line); CHECK(strstr(e.msg, part) != nullptr); if (ok || e.line != line || strstr(e.msg, part) == nullptr) printf(" wanted line %d with '%s', got line %d '%s'\n", line, part, e.line, ok ? "(parsed)" : e.msg); } static void test_valid(void) { ConfError e; CHECK(parse("", &e)); CHECK(conf.nsections == 0); CHECK(parse("# only a comment\n\n \n", &e)); CHECK(parse("[harness a]", &e)); // no trailing newline CHECK(conf.nsections == 1); CHECK(parse("[harness a]\r\nrecord = x\r\n", &e)); CHECK(strcmp(conf.entries[0].val, "x") == 0); } static void test_comments(void) { ConfError e; CHECK(parse("[harness a]\nrecord = x # note\nevent E = a#b\n", &e)); CHECK(strcmp(conf.entries[0].val, "x") == 0); CHECK(strcmp(conf.entries[1].val, "a#b") == 0); CHECK(parse(" # indented\n[harness a] # header\n", &e)); } static void test_lists(void) { ConfError e; CHECK(parse("[outfit a]\nskills = x\t y z* \n", &e)); const char *cur = conf.entries[0].val; const char *w; size_t n; CHECK(conf_list_next(&cur, &w, &n) && n == 1 && w[0] == 'x'); CHECK(conf_list_next(&cur, &w, &n) && n == 1 && w[0] == 'y'); CHECK(conf_list_next(&cur, &w, &n) && n == 2 && memcmp(w, "z*", 2) == 0); CHECK(!conf_list_next(&cur, &w, &n)); } static void test_errors(void) { expect_error("[bogus x]\n", 1, "unknown section kind"); expect_error("[harness a]\nnope = 1\n", 2, "unknown key"); expect_error("[harness a]\n[harness a]\n", 2, "duplicate section"); expect_error("[outfit a]\nonly = h\nonly = h\n", 3, "duplicate key"); expect_error("record = x\n", 1, "before any section"); expect_error("[harness a\n", 1, "malformed"); expect_error("[outfit a]\ninclude = b\n[outfit b]\ninclude = a\n", 2 + 2, "include cycle: a -> b -> a"); expect_error("[outfit a]\ninclude = a\n", 2, "include cycle: a -> a"); expect_error("[outfit a]\ninclude = zz\n", 2, "unknown outfit"); expect_error("[wear user]\noutfits = zz\n", 2, "unknown outfit"); expect_error("[outfit a]\nonly = zz\n", 2, "unknown harness"); } static void test_wear_headers(void) { ConfError e; CHECK(parse("[wear enterprise]\n[wear user]\n[wear user machine=m]\n", &e)); CHECK(parse("[wear shared g/o/n]\n[wear personal g/o/n machine=m]\n", &e)); expect_error("[wear shared]\n", 1, "needs a TARGET"); expect_error("[wear personal]\n", 1, "needs a TARGET"); expect_error("[wear enterprise g/o/n]\n", 1, "takes no TARGET"); expect_error("[wear user g/o/n]\n", 1, "takes no TARGET"); expect_error("[wear enterprise machine=m]\n", 1, "machine="); expect_error("[wear shared g/o/n machine=m]\n", 1, "machine="); expect_error("[wear galaxy]\n", 1, "unknown scope"); expect_error("[wear user]\npublish = yes\n", 2, "publish"); } static char *repeat_lines(const char *fmt, int n, size_t *len) { char *s = malloc((size_t)n * 64 + 1); size_t off = 0; for (int i = 1; i <= n; i++) off += (size_t)snprintf(s + off, 64, fmt, i); *len = off; return s; } static bool parse_n(const char *fmt, int n, ConfError *e) { size_t len; char *s = repeat_lines(fmt, n, &len); bool ok = conf_parse(&conf, s, len, e); free(s); return ok; } static void test_limits(void) { ConfError e; CHECK(parse_n("[harness h%d]\n", 64, &e)); CHECK(!parse_n("[harness h%d]\n", 65, &e) && e.line == 65); CHECK(parse_n("[outfit o%d]\n", 512, &e)); CHECK(!parse_n("[outfit o%d]\n", 513, &e) && e.line == 513); CHECK(parse_n("[wear personal g/o/n%d]\n", 256, &e)); CHECK(!parse_n("[wear personal g/o/n%d]\n", 257, &e) && e.line == 257); CHECK(parse_n("[upstream u%d]\n", 128, &e)); CHECK(!parse_n("[upstream u%d]\n", 129, &e) && e.line == 129); // 256 needs, then 257. Needs name a harness, so lead with one. size_t len; char *s = repeat_lines("[need h n%d]\n", 257, &len); char *text = malloc(len + 32); int head = snprintf(text, 32, "[harness h]\n"); memcpy(text + head, s, len); CHECK(!conf_parse(&conf, text, (size_t)head + len, &e) && e.line == 258); CHECK(strstr(e.msg, "too many need") != nullptr); free(s); free(text); // 8192 entries fit, the 8193rd does not. char *big = malloc(8200 * 16); size_t off = (size_t)snprintf(big, 32, "[harness h]\n"); for (int i = 0; i < 8192; i++) off += (size_t)snprintf(big + off, 16, "record = r\n"); CHECK(conf_parse(&conf, big, off, &e)); off += (size_t)snprintf(big + off, 16, "record = r\n"); CHECK(!conf_parse(&conf, big, off, &e) && e.line == 8194); free(big); } static void test_line_and_file(void) { ConfError e; static char line[CONF_FILE_MAX + 64]; memset(line, '#', CONF_LINE_MAX); line[CONF_LINE_MAX] = '\n'; CHECK(conf_parse(&conf, line, CONF_LINE_MAX + 1, &e)); memset(line, '#', CONF_LINE_MAX + 1); line[CONF_LINE_MAX + 1] = '\n'; CHECK(!conf_parse(&conf, line, CONF_LINE_MAX + 2, &e) && e.line == 1); memset(line, '\n', CONF_FILE_MAX); CHECK(conf_parse(&conf, line, CONF_FILE_MAX, &e)); CHECK(!conf_parse(&conf, line, CONF_FILE_MAX + 1, &e) && e.line == 0); CHECK(!conf_parse(&conf, "[harness a]\nrecord = x\0y\n", 24, &e) && e.line == 2); } static void test_dump_is_stable(void) { ConfError e; CHECK(parse("[outfit b]\n[outfit a]\nskills = z y\n", &e)); char *a = nullptr; size_t alen = 0; FILE *f = open_memstream(&a, &alen); conf_dump(&conf, f); fclose(f); CHECK(strcmp(a, "outfit a\noutfit a skills = z y\noutfit b\n") == 0); free(a); } int main(void) { test_valid(); test_comments(); test_lists(); test_errors(); test_wear_headers(); test_limits(); test_line_and_file(); test_dump_is_stable(); printf("unit: %d checks, %d failed\n", checks, failures); return failures == 0 ? 0 : 1; }