Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/tools/estimation/test_est3c_tools.c # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / tools / estimation / test_est3c_tools.c` 365 lines ยท 23365 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /* Tests for the EST-3c v3 tools (est3c_fit, est3c_eval, est3c_common) on 18 * synthetic data only. Built with -DC3_AS_LIB -DC3_TEST_BUILD: both tools run 19 * in-process, and the dirty flag, the D1 folder and the D2 identity come from 20 * the environment (C3_TEST_DIRTY, C3_TEST_D1_DIR, C3_TEST_D2_{RAW,MARKS,SCHED}_SHA), 21 * as do the compiled params SHA (C3_TEST_PARAMS_SHA), the receipt folder 22 * (C3_TEST_RECEIPT_DIR) and, for est3c_fit, the protocol SHA (C3_TEST_PROTOCOL_SHA). 23 * Usage: test_est3c_tools (workdir must not exist) */ 24 #include 25 #include 26 #include 27 #include 28 #include 29 #include 30 #include 31 #include 32 33 #include "est3c_common.h" 34 #include "sha256.h" 35 36 int c3_fit_main(int argc, char **argv); 37 int c3_eval_main(int argc, char **argv); 38 39 static int fails, checks; 40 #define CHECK(c, ...) do { checks++; if (!(c)) { fails++; fprintf(stderr, "FAIL %s:%d: ", __FILE__, __LINE__); fprintf(stderr, __VA_ARGS__); fputc('\n', stderr); } } while (0) 41 42 static char W[1024]; 43 44 static int run(int (*fn)(int, char **), ...) 45 { 46 char *av[32]; int n = 0; va_list ap; 47 av[n++] = (char *)"tool"; 48 va_start(ap, fn); 49 for (char *s; n < 31 && (s = va_arg(ap, char *)); ) av[n++] = s; 50 va_end(ap); 51 av[n] = NULL; 52 fflush(stdout); 53 return fn(n, av); 54 } 55 56 static void synth(const char *dir, uint64_t seed, size_t lines, double foreign, size_t gap_every, int drop, size_t spike) 57 { 58 c3_synth s; memset(&s, 0, sizeof s); 59 s.seed = seed; s.lines = lines; s.foreign = foreign; s.gap_every = gap_every; s.drop_end_mark = drop; s.spike_every = spike; 60 if (c3_synth_write(dir, &s)) { fprintf(stderr, "synthetic write failed: %s\n", dir); exit(1); } 61 } 62 63 static void resums(const char *dir) 64 { 65 static const char *const names[3] = { "machine-state.ndjson", "machine-state-marks.txt", "schedule.txt" }; 66 if (c3_write_sums(dir, names, 3)) { fprintf(stderr, "sums failed\n"); exit(1); } 67 } 68 69 static char *slurp(const char *p, size_t *len) 70 { 71 FILE *f = fopen(p, "rb"); if (!f) return NULL; 72 fseek(f, 0, SEEK_END); long n = ftell(f); fseek(f, 0, SEEK_SET); 73 char *b = malloc((size_t)n + 1); 74 if (!b || fread(b, 1, (size_t)n, f) != (size_t)n) { fclose(f); free(b); return NULL; } 75 fclose(f); b[n] = 0; if (len) *len = (size_t)n; return b; 76 } 77 78 /* copy src to dst replacing every line that starts with `key ` by `repl` */ 79 static void copy_edit(const char *src, const char *dst, const char *key, const char *repl, const char *key2, const char *repl2) 80 { 81 FILE *in = fopen(src, "r"), *out = fopen(dst, "w"); 82 if (!in || !out) { fprintf(stderr, "copy_edit open failed\n"); exit(1); } 83 char line[8192]; size_t kl = strlen(key), k2 = key2 ? strlen(key2) : 0; 84 while (fgets(line, sizeof line, in)) { 85 if (!strncmp(line, key, kl) && line[kl] == ' ') fprintf(out, "%s\n", repl); 86 else if (key2 && !strncmp(line, key2, k2) && line[k2] == ' ') fprintf(out, "%s\n", repl2); 87 else fputs(line, out); 88 } 89 fclose(in); fclose(out); 90 } 91 92 static int count_receipts(const char *dir, char *first, size_t cap) 93 { 94 DIR *d = opendir(dir); if (!d) return -1; 95 int n = 0; struct dirent *e; 96 while ((e = readdir(d))) if (!strncmp(e->d_name, "receipt-", 8)) { if (!n && first) snprintf(first, cap, "%s/%s", dir, e->d_name); n++; } 97 closedir(d); return n; 98 } 99 100 static void path(char *out, size_t cap, const char *a) { snprintf(out, cap, "%s/%s", W, a); } 101 102 static void test_ticks(void) 103 { 104 char d[1200], f[1300]; path(d, sizeof d, "ticks"); 105 synth(d, 5, 100, 0.0, 7, 0, 0); 106 snprintf(f, sizeof f, "%s/machine-state.ndjson", d); 107 est_file ef; char err[256]; 108 CHECK(est_file_load(f, &ef, err, sizeof err) == 0, "load: %s", err); 109 c3_ticks tk; 110 CHECK(c3_ticks_build(&ef, &tk, err, sizeof err) == 0, "ticks: %s", err); 111 /* lines 7, 14, ..., 98 come after a 3 s gap: 14 gaps, 2 inserted ticks each */ 112 CHECK(tk.inserted == 28, "inserted %zu (want 28)", tk.inserted); 113 CHECK(tk.n == 128, "ticks %zu (want 128)", tk.n); 114 size_t ins_ok = 0; 115 for (size_t i = 0; i + 2 < tk.n; i++) 116 if (tk.t[i].inserted && tk.t[i + 1].inserted && !tk.t[i + 2].inserted && !tk.t[i].present && tk.t[i + 2].line % 7 == 0) ins_ok++; 117 CHECK(ins_ok == 14, "inserted pairs before gap lines %zu (want 14)", ins_ok); 118 CHECK(memcmp(tk.t[0].ev.b, tk.t[1].ev.b, sizeof tk.t[0].ev.b) != 0, "distinct evidence digests"); 119 c3_ticks_free(&tk); est_file_free(&ef); 120 } 121 122 static void pre(const char *name, uint64_t seed, size_t lines, double foreign, size_t gap_every, int drop, size_t spike, c3_pre *p) 123 { 124 char d[1200], err[256]; path(d, sizeof d, name); 125 synth(d, seed, lines, foreign, gap_every, drop, spike); 126 if (c3_precheck(d, p, err, sizeof err)) { fprintf(stderr, "precheck %s: %s\n", name, err); exit(1); } 127 } 128 129 static void test_precheck(void) 130 { 131 c3_pre p; 132 pre("pre-ok", 3, 2100, 0.0, 0, 0, 0, &p); 133 CHECK(p.valid && p.ok_marks && p.sched_trials > 0, "clean synthetic run VALID (%s)", p.why); 134 CHECK(fabs(p.foreign_mean) < 0.2, "foreign mean %.3f near 0", p.foreign_mean); 135 pre("pre-short", 3, 1500, 0.0, 0, 0, 0, &p); 136 CHECK(!p.valid && !p.ok_lines && p.ok_gaps, "short file INCONCLUSIVE on lines only (%s)", p.why); 137 pre("pre-gaps", 3, 2100, 0.0, 20, 0, 0, &p); 138 CHECK(!p.valid && !p.ok_gaps && p.ok_lines && p.ok_foreign_mean, "5 %% gaps INCONCLUSIVE on gaps (%s; %.4f)", p.why, p.gap_big_frac); 139 pre("pre-gaps-ok", 3, 2100, 0.0, 100, 0, 0, &p); 140 CHECK(p.valid && p.gaps_big > 0, "1 %% gaps stays VALID (%s)", p.why); 141 pre("pre-dropend", 3, 2100, 0.0, 0, 1, 0, &p); 142 CHECK(!p.valid && !p.ok_marks, "dropped end mark INCONCLUSIVE (%s)", p.why); 143 /* an extra begin/end pair */ 144 char d[1200], m[1300]; path(d, sizeof d, "pre-extra"); 145 synth(d, 3, 2100, 0.0, 0, 0, 0); 146 snprintf(m, sizeof m, "%s/machine-state-marks.txt", d); 147 FILE *f = fopen(m, "a"); fprintf(f, "`1790009999`.`000000000` begin load-L6-999\n1790010000.`000000000` end load-L6-999 exit 0\n"); fclose(f); 148 resums(d); 149 char err[256]; 150 CHECK(c3_precheck(d, &p, err, sizeof err) == 0 && !p.valid && !p.ok_marks, "extra marks pair INCONCLUSIVE (%s)", p.why); 151 /* a missing pair: drop the first begin and end lines */ 152 path(d, sizeof d, "pre-missing"); 153 synth(d, 3, 2100, 0.0, 0, 0, 0); 154 snprintf(m, sizeof m, "%s/machine-state-marks.txt", d); 155 char *b = slurp(m, NULL); char *nl = b ? strchr(b, '\n') : NULL; nl = nl ? strchr(nl + 1, '\n') : NULL; 156 if (nl) { f = fopen(m, "w"); fputs(nl + 1, f); fclose(f); } 157 free(b); resums(d); 158 CHECK(c3_precheck(d, &p, err, sizeof err) == 0 && !p.valid && !p.ok_marks, "missing marks pair INCONCLUSIVE (%s)", p.why); 159 pre("pre-foreign", 3, 2100, 2.0, 0, 0, 0, &p); 160 CHECK(!p.valid && !p.ok_foreign_mean, "foreign 2.0 INCONCLUSIVE on mean (%s; %.3f)", p.why, p.foreign_mean); 161 pre("pre-foreign-ok", 3, 2100, 1.2, 0, 0, 0, &p); 162 CHECK(p.valid, "foreign 1.2 stays VALID (%s; %.3f)", p.why, p.foreign_mean); 163 pre("pre-tail", 3, 2100, 0.0, 0, 0, 5, &p); 164 CHECK(!p.valid && p.ok_foreign_mean && !p.ok_foreign_tail, "spikes INCONCLUSIVE on tail only (%s; mean %.3f tail %.3f)", p.why, p.foreign_mean, p.foreign_over3_frac); 165 } 166 167 static void test_select(void) 168 { 169 int av[6] = { 0, 1, 1, 1, 1, 1 }, ps[6] = { 0, 1, 1, 1, 1, 1 }; 170 double ls[6] = { 0, -1.000, -1.000, -0.995, -1.200, -0.990 }; 171 CHECK(c3_select(av, ps, ls) == 2 - 1, "F1 within 0.01 of best F5 is preferred (got %d)", c3_select(av, ps, ls)); 172 ps[1] = 0; 173 CHECK(c3_select(av, ps, ls) == 2, "F2 next earliest within 0.01 (got %d)", c3_select(av, ps, ls)); 174 ls[2] = -1.0105; ls[3] = -1.02; 175 CHECK(c3_select(av, ps, ls) == 5, "best F5 wins when no earlier family is within 0.01 (got %d)", c3_select(av, ps, ls)); 176 ps[5] = 0; 177 CHECK(c3_select(av, ps, ls) == 2, "non-passers are ignored (got %d)", c3_select(av, ps, ls)); 178 av[2] = 0; av[3] = 0; av[4] = 0; 179 CHECK(c3_select(av, ps, ls) == 0, "no passer: PHASE_A_FAIL (0)"); 180 CHECK(c3_grid_take(0, -2.0, 0.0) && !c3_grid_take(1, -1.0, -1.0) && c3_grid_take(1, -0.9, -1.0) && !c3_grid_take(0, NAN, 0.0), 181 "grid tie keeps the first point; non-finite points are skipped"); 182 } 183 184 static void test_e0(void) 185 { 186 uint32_t c[EST_PRED_N]; memset(c, 0, sizeof c); 187 double e[10] = { 0, 0, 0, 0, 0, 0, 100, 100, -100, 90000 }; 188 c3_e0_counts(e, 10, c); 189 CHECK(c[EST_PRED_K] == 6 && c[EST_PRED_K + 1] == 2 && c[EST_PRED_K - 1] == 1 && c[EST_PRED_N - 1] == 1, "E0 counts, outside value clipped to the edge bin"); 190 static c3_model m; 191 c3_make_e0(&m, c, 10); 192 double s = 0; for (int i = 0; i < EST_PRED_N; i++) s += m.e0_one[i]; 193 CHECK(fabs(s - 1.0) < 1e-12, "E0 pmf sums to 1 (%.17g)", s); 194 CHECK(fabs(m.e0_one[EST_PRED_K] - (6.0 + 1.0 / 801.0) / 11.0) < 1e-15 && fabs(m.e0_one[EST_PRED_K + 5] - (1.0 / 801.0) / 11.0) < 1e-15, "E0 one-pseudo-observation smoothing (c+1/801)/(N+1)"); 195 CHECK(m.id == 0, "E0 id"); 196 } 197 198 static void hexof(const char *dir, const char *name, char out[65]) 199 { 200 char p[1300]; snprintf(p, sizeof p, "%s/%s", dir, name); 201 if (est_sha_file_hex(p, out)) { fprintf(stderr, "sha %s\n", p); exit(1); } 202 } 203 204 /* the compiled params SHA stand-in: the SHA-256 of p */ 205 static void use_params(const char *p) 206 { 207 char h[65]; 208 if (est_sha_file_hex(p, h)) { fprintf(stderr, "sha %s\n", p); exit(1); } 209 setenv("C3_TEST_PARAMS_SHA", h, 1); 210 } 211 212 static void test_tools(void) 213 { 214 char d1[1200], d2[1200], dh[1200], d2s[1200], raw1[1300], mk1[1300], raw2[1300], mk2[1300], rawh[1300], mkh[1300]; 215 char psyn[1200], pbind[1200], pfail[1200], pbad[1200], psel[1200], pscr[1200], pdup[1200], o1[1200], o2[1200], o3[1200]; 216 path(d1, sizeof d1, C3_D1_ID); path(d2, sizeof d2, "run-d2"); path(dh, sizeof dh, "x" C3_HELDOUT_TAG "1"); path(d2s, sizeof d2s, "run-d2-short"); 217 synth(d1, 11, 2300, 0.0, 0, 0, 0); synth(d2, 12, 2300, 0.0, 0, 0, 0); synth(dh, 13, 2100, 0.0, 0, 0, 0); synth(d2s, 14, 1200, 0.0, 0, 0, 0); 218 snprintf(raw1, sizeof raw1, "%s/machine-state.ndjson", d1); snprintf(mk1, sizeof mk1, "%s/machine-state-marks.txt", d1); 219 snprintf(raw2, sizeof raw2, "%s/machine-state.ndjson", d2); snprintf(mk2, sizeof mk2, "%s/machine-state-marks.txt", d2); 220 snprintf(rawh, sizeof rawh, "%s/machine-state.ndjson", dh); snprintf(mkh, sizeof mkh, "%s/machine-state-marks.txt", dh); 221 path(psyn, sizeof psyn, "params-syn.txt"); path(pbind, sizeof pbind, "params-bind.txt"); 222 path(pfail, sizeof pfail, "params-fail.txt"); path(pbad, sizeof pbad, "params-bad.txt"); 223 path(psel, sizeof psel, "params-sel.txt"); path(pscr, sizeof pscr, "params-scr.txt"); path(pdup, sizeof pdup, "params-dup.txt"); 224 path(o1, sizeof o1, "out1"); path(o2, sizeof o2, "out2"); path(o3, sizeof o3, "out3"); 225 mkdir(o1, 0755); mkdir(o2, 0755); mkdir(o3, 0755); 226 227 /* fit guards */ 228 setenv("C3_TEST_DIRTY", "1", 1); 229 CHECK(run(c3_fit_main, "--raw", raw1, "--marks", mk1, "--out", pbind, (char *)NULL) == 2, "fit refuses a dirty tree"); 230 CHECK(run(c3_fit_main, "--raw", rawh, "--marks", mkh, "--out", psyn, "--synthetic-test", (char *)NULL) == 2, "fit refuses a held-out path even in synthetic mode"); 231 setenv("C3_TEST_DIRTY", "0", 1); 232 CHECK(run(c3_fit_main, "--raw", raw2, "--marks", mk2, "--out", pbind, (char *)NULL) == 2, "binding fit refuses data outside D1"); 233 setenv("C3_TEST_PROTOCOL_SHA", "unknown", 1); 234 CHECK(run(c3_fit_main, "--raw", raw1, "--marks", mk1, "--out", pbind, "--grid-stride", "4", (char *)NULL) == 2, "binding fit refuses protocol SHA unknown"); 235 setenv("C3_TEST_PROTOCOL_SHA", "`1111111111111111111111111111111111111111111111111111111111111111`", 1); 236 CHECK(run(c3_fit_main, "--raw", raw1, "--marks", mk1, "--out", pbind, "--grid-stride", "4", (char *)NULL) == 2, "binding fit refuses a compiled protocol SHA that differs from the doc on disk"); 237 unsetenv("C3_TEST_PROTOCOL_SHA"); 238 { struct stat sb; CHECK(stat(pbind, &sb) != 0, "refused binding fits write no params file"); } 239 CHECK(run(c3_fit_main, "--raw", raw1, "--marks", mk1, "--out", psyn, "--synthetic-test", "--grid-stride", "4", (char *)NULL) == 0, "synthetic fit runs"); 240 CHECK(run(c3_fit_main, "--raw", raw1, "--marks", mk1, "--out", psyn, "--synthetic-test", "--grid-stride", "4", (char *)NULL) == 1, "fit never overwrites params"); 241 CHECK(run(c3_fit_main, "--raw", raw1, "--marks", mk1, "--out", pbind, "--grid-stride", "4", (char *)NULL) == 0, "binding-mode fit on the fake D1 runs"); 242 c3_params P; char err[512]; 243 CHECK(c3_params_read(pbind, &P, err, sizeof err) == 0, "params read: %s", err); 244 CHECK(P.synthetic == 0 && P.tool_dirty == 0 && P.e0_n > 2000, "params identity (synthetic %d dirty %d e0_n %llu)", P.synthetic, P.tool_dirty, (unsigned long long)P.e0_n); 245 CHECK(P.phase_a_pass && P.selected >= 1, "synthetic iid data passes the in-sample screen (selected %d)", P.selected); 246 char h[65]; hexof(d1, "machine-state.ndjson", h); 247 CHECK(!strcmp(h, P.fit_raw_sha), "params record the D1 raw SHA"); 248 copy_edit(pbind, pfail, "phase_a", "phase_a PHASE_A_FAIL", "selected", "selected none"); 249 250 /* eval refusals; C3_TEST_PARAMS_SHA / C3_TEST_RECEIPT_DIR stand in for the compiled params SHA and receipt folder */ 251 char r2[65], m2[65], s2[65]; hexof(d2, "machine-state.ndjson", r2); hexof(d2, "machine-state-marks.txt", m2); hexof(d2, "schedule.txt", s2); 252 setenv("C3_TEST_D1_DIR", d1, 1); setenv("C3_TEST_D2_RAW_SHA", r2, 1); setenv("C3_TEST_D2_MARKS_SHA", m2, 1); setenv("C3_TEST_D2_SCHED_SHA", s2, 1); 253 setenv("C3_TEST_RECEIPT_DIR", o1, 1); use_params(pbind); 254 setenv("C3_TEST_DIRTY", "1", 1); 255 CHECK(run(c3_eval_main, "--recorded", "--dir", d2, "--params", pbind, "--outdir", o1, (char *)NULL) == 2, "eval refuses a dirty tree"); 256 setenv("C3_TEST_DIRTY", "0", 1); 257 setenv("C3_TEST_D2_RAW_SHA", "absent", 1); 258 CHECK(run(c3_eval_main, "--recorded", "--dir", d2, "--params", pbind, "--outdir", o1, (char *)NULL) == 2, "eval refuses without a compiled D2 identity"); 259 setenv("C3_TEST_D2_RAW_SHA", r2, 1); 260 setenv("C3_TEST_D2_SCHED_SHA", "absent", 1); 261 CHECK(run(c3_eval_main, "--recorded", "--dir", d2, "--params", pbind, "--outdir", o1, (char *)NULL) == 2, "eval refuses without a compiled D2 schedule SHA"); 262 setenv("C3_TEST_D2_SCHED_SHA", "`2222222222222222222222222222222222222222222222222222222222222222`", 1); 263 CHECK(run(c3_eval_main, "--recorded", "--dir", d2, "--params", pbind, "--outdir", o1, (char *)NULL) == 2, "eval refuses a D2 schedule.txt with another SHA"); 264 setenv("C3_TEST_D2_SCHED_SHA", s2, 1); 265 setenv("C3_TEST_PARAMS_SHA", "absent", 1); 266 CHECK(run(c3_eval_main, "--recorded", "--dir", d2, "--params", pbind, "--outdir", o1, (char *)NULL) == 2, "eval refuses without a compiled params SHA"); 267 use_params(psyn); 268 CHECK(run(c3_eval_main, "--recorded", "--dir", d2, "--params", pbind, "--outdir", o1, (char *)NULL) == 2, "eval refuses a params file whose SHA differs from the compiled one"); 269 use_params(pbind); 270 CHECK(run(c3_eval_main, "--recorded", "--dir", d2, "--params", pbind, "--outdir", o2, (char *)NULL) == 2, "eval refuses an outdir other than the receipt folder"); 271 setenv("C3_TEST_RECEIPT_DIR", "/nonexistent-est3c-receipts", 1); 272 CHECK(run(c3_eval_main, "--recorded", "--dir", d2, "--params", pbind, "--outdir", o1, (char *)NULL) == 2, "eval refuses when the receipt folder does not resolve"); 273 setenv("C3_TEST_RECEIPT_DIR", o1, 1); 274 /* selection must be reproduced from the params' own screen results */ 275 char sel_other[32], scr_line[64], scr_key[32]; 276 snprintf(sel_other, sizeof sel_other, "selected F%d", P.selected % 5 + 1); 277 copy_edit(pbind, psel, "selected", sel_other, NULL, NULL); 278 use_params(psel); 279 CHECK(run(c3_eval_main, "--recorded", "--dir", d2, "--params", psel, "--outdir", o1, (char *)NULL) == 2, "eval refuses an edited `selected` (%s)", sel_other); 280 snprintf(scr_key, sizeof scr_key, "screen_result F%d", P.selected); 281 snprintf(scr_line, sizeof scr_line, "screen_result F%d pass 0 first_fail coverage50 n 0 logscore 0", P.selected); 282 copy_edit(pbind, pscr, scr_key, scr_line, NULL, NULL); 283 use_params(pscr); 284 CHECK(run(c3_eval_main, "--recorded", "--dir", d2, "--params", pscr, "--outdir", o1, (char *)NULL) == 2, "eval refuses a selected family that did not pass the screen"); 285 use_params(psyn); 286 CHECK(run(c3_eval_main, "--recorded", "--dir", d2, "--params", psyn, "--outdir", o1, (char *)NULL) == 2, "eval refuses synthetic params"); 287 use_params(pfail); 288 CHECK(run(c3_eval_main, "--recorded", "--dir", d2, "--params", pfail, "--outdir", o1, (char *)NULL) == 2, "eval refuses PHASE_A_FAIL params"); 289 use_params(pbind); 290 setenv("C3_TEST_D1_DIR", d2, 1); 291 CHECK(run(c3_eval_main, "--recorded", "--dir", d2, "--params", pbind, "--outdir", o1, (char *)NULL) == 2, "eval refuses params whose D1 SHA differs"); 292 setenv("C3_TEST_D1_DIR", d1, 1); 293 setenv("C3_TEST_D2_RAW_SHA", "`0000000000000000000000000000000000000000000000000000000000000000`", 1); 294 CHECK(run(c3_eval_main, "--recorded", "--dir", d2, "--params", pbind, "--outdir", o1, (char *)NULL) == 2, "eval refuses D2 data with another SHA"); 295 setenv("C3_TEST_D2_RAW_SHA", r2, 1); 296 CHECK(run(c3_eval_main, "--recorded", "--dir", d2s, "--params", pbind, "--outdir", o1, (char *)NULL) == 3, "eval refuses an INCONCLUSIVE D2"); 297 CHECK(count_receipts(o1, NULL, 0) == 0, "no receipt after refusals"); 298 299 /* the one scored run, then the second-run refusals (same folder, another folder) */ 300 CHECK(run(c3_eval_main, "--recorded", "--dir", d2, "--params", pbind, "--outdir", o1, (char *)NULL) == 0, "recorded run on synthetic D2"); 301 char rp[1400] = ""; 302 CHECK(count_receipts(o1, rp, sizeof rp) == 1, "exactly one receipt"); 303 size_t rl = 0; char *rc = slurp(rp, &rl); 304 CHECK(rc && strstr(rc, "\"verdict\": \"") && strstr(rc, "\"protocol_sha256\": \"" C3_PROTOCOL_SHA "\""), "receipt has verdict and protocol SHA"); 305 CHECK(rc && strstr(rc, "\"E0\"") && strstr(rc, "\"F1\"") && strstr(rc, "\"informational\""), "receipt scores S, F1, E0 and the informational block"); 306 if (rc) { 307 uint8_t dg[32]; char hx[65]; sha256_hash((const uint8_t *)rc, rl, dg); est_hex(dg, 32, hx); 308 CHECK(strstr(rp, hx) != NULL, "receipt is named by its SHA-256"); 309 } 310 free(rc); 311 CHECK(run(c3_eval_main, "--recorded", "--dir", d2, "--params", pbind, "--outdir", o1, (char *)NULL) == 2, "a second scored run is refused"); 312 CHECK(run(c3_eval_main, "--recorded", "--dir", d2, "--params", pbind, "--outdir", o3, (char *)NULL) == 2, "a second scored run into another folder is refused"); 313 CHECK(count_receipts(o3, NULL, 0) == 0 && count_receipts(o1, NULL, 0) == 1, "still exactly one recorded receipt"); 314 /* synthetic-test path */ 315 CHECK(run(c3_eval_main, "--recorded", "--synthetic-test", "--dir", d2, "--params", psyn, "--outdir", o2, (char *)NULL) == 0, "synthetic recorded run"); 316 CHECK(run(c3_eval_main, "--recorded", "--synthetic-test", "--dir", d1, "--params", psyn, "--outdir", o3, (char *)NULL) == 2, "synthetic mode refuses D1"); 317 CHECK(run(c3_eval_main, "--recorded", "--synthetic-test", "--dir", d2, "--params", psel, "--outdir", o3, (char *)NULL) == 2, "synthetic mode still refuses an edited `selected`"); 318 CHECK(run(c3_eval_main, "--precheck", "--dir", d2s, (char *)NULL) == 3, "precheck exit 3 on INCONCLUSIVE"); 319 copy_edit(pbind, pbad, "protocol_doc_sha256", "protocol_doc_sha256 `1111111111111111111111111111111111111111111111111111111111111111`", NULL, NULL); 320 use_params(pbad); setenv("C3_TEST_RECEIPT_DIR", o3, 1); 321 CHECK(run(c3_eval_main, "--recorded", "--dir", d2, "--params", pbad, "--outdir", o3, (char *)NULL) == 2, "eval refuses params from another protocol"); 322 323 /* parameter file: duplicate keys refused, distinct e0_count k accepted */ 324 FILE *pf; 325 pf = fopen(pdup, "w"); { size_t bl; char *b = slurp(pbind, &bl); if (b) { fputs(b, pf); free(b); } } fprintf(pf, "%s\n", sel_other); fclose(pf); 326 CHECK(c3_params_read(pdup, &P, err, sizeof err) != 0 && strstr(err, "duplicate"), "params with two selected lines refused (%s)", err); 327 pf = fopen(pdup, "w"); { size_t bl; char *b = slurp(pbind, &bl); if (b) { fputs(b, pf); free(b); } } fprintf(pf, "e0_count 0 0\n"); err[0] = 0; fclose(pf); 328 CHECK(c3_params_read(pbind, &P, err, sizeof err) == 0, "params with many distinct e0_count lines read: %s", err); 329 CHECK(c3_params_read(pdup, &P, err, sizeof err) != 0 && strstr(err, "duplicate"), "params with a repeated e0_count k refused (%s)", err); 330 pf = fopen(pdup, "w"); { size_t bl; char *b = slurp(pbind, &bl); if (b) { fputs(b, pf); free(b); } } fprintf(pf, "screen_result F1 pass 1\n"); fclose(pf); 331 CHECK(c3_params_read(pdup, &P, err, sizeof err) != 0 && strstr(err, "duplicate"), "params with a repeated screen_result refused (%s)", err); 332 } 333 334 /* section 6 rule 5: when both regimes fail, in-trial is named first */ 335 static void test_rule_order(void) 336 { 337 static c3_stats st; memset(&st, 0, sizeof st); 338 size_t n = 400; 339 st.n = n; st.cov95_step = calloc(n, sizeof(double)); st.width_step = calloc(n, sizeof(double)); st.regime_step = calloc(n, 1); 340 if (!st.cov95_step || !st.width_step || !st.regime_step) { fprintf(stderr, "oom\n"); exit(1); } 341 st.c.n = n; st.c.cov[0] = 0.50 * n; st.c.cov[1] = 0.80 * n; st.c.cov[2] = 0.95 * n; 342 for (int j = 0; j < 10; j++) st.c.pit[j] = 0.1 * n; 343 st.c.z_sum = 0; st.c.z_sq = (double)n; st.c.z_lag = 0; 344 for (size_t k = 0; k < n; k++) { st.regime_step[k] = (unsigned char)(k & 1); st.cov95_step[k] = (k & 1) ? 1.0 : 0.82; } 345 st.ten_n = 100; st.ten_cov = 95; 346 c3_judge(&st); 347 CHECK(!st.pass[C3_ST_REG_TRIAL] && !st.pass[C3_ST_REG_IDLE] && st.pass[C3_ST_Q0] && st.pass[C3_ST_LAG1], "both regimes fail, quarters pass"); 348 CHECK(st.first_fail == C3_ST_REG_TRIAL && !strcmp(c3_stat_name(st.first_fail), "regime_trial_cov95"), "first failure is in-trial (got %s)", st.first_fail >= 0 ? c3_stat_name(st.first_fail) : "none"); 349 c3_stats_free(&st); 350 } 351 352 int main(int argc, char **argv) 353 { 354 if (argc != 2) { fprintf(stderr, "usage: test_est3c_tools \n"); return 2; } 355 snprintf(W, sizeof W, "%s", argv[1]); 356 if (mkdir(W, 0755)) { fprintf(stderr, "workdir %s: %s\n", W, strerror(errno)); return 2; } 357 test_ticks(); printf("ticks: done\n"); 358 test_precheck(); printf("precheck: done\n"); 359 test_select(); printf("selection: done\n"); 360 test_e0(); printf("E0: done\n"); 361 test_rule_order(); printf("rule order: done\n"); 362 test_tools(); printf("tools: done\n"); 363 printf("test_est3c_tools: %d checks, %d failures\n", checks, fails); 364 return fails ? 1 : 0; 365 }