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_est_json.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_est_json.c` 271 lines ยท 12738 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 strict-JSON receipt rules (est-json-1, docs/estimation/RECEIPT_JSON.md). 18 * Synthetic statistics only; opens no run data. 19 * test_est_json run the unit checks 20 * test_est_json --validate F exit 0 when file F is strict RFC 8259 JSON, else 1 21 * jq is NOT used as the validator: jq 1.7 accepts the bare tokens inf and nan. */ 22 #include 23 #include 24 #include 25 #include 26 27 #include "est3c_common.h" 28 #include "est_replay.h" 29 #include "sha256.h" 30 31 static int fails, checks; 32 #define CHECK(c, ...) do { checks++; if (!(c)) { fails++; fprintf(stderr, "FAIL %s:%d: ", __FILE__, __LINE__); fprintf(stderr, __VA_ARGS__); fputc('\n', stderr); } } while (0) 33 34 /* ---------------------------------------------------------- strict validator */ 35 typedef struct { const char *p, *end; int depth; } jp; 36 static void ws(jp *j) { while (j->p < j->end && (*j->p == ' ' || *j->p == '\t' || *j->p == '\n' || *j->p == '\r')) j->p++; } 37 static int lit(jp *j, const char *w) { size_t n = strlen(w); if ((size_t)(j->end - j->p) < n || memcmp(j->p, w, n)) return 0; j->p += n; return 1; } 38 static int isd(const jp *j) { return j->p < j->end && *j->p >= '0' && *j->p <= '9'; } 39 static int jstring(jp *j) 40 { 41 if (j->p >= j->end || *j->p != '"') return 0; 42 j->p++; 43 while (j->p < j->end) { 44 unsigned char c = (unsigned char)*j->p++; 45 if (c == '"') return 1; 46 if (c < 0x20) return 0; 47 if (c == '\\') { 48 if (j->p >= j->end) return 0; 49 char e = *j->p++; 50 if (e == 'u') { for (int k = 0; k < 4; k++) { if (j->p >= j->end || !strchr("0123456789abcdefABCDEF", *j->p)) return 0; j->p++; } } 51 else if (!strchr("\"\\/bfnrt", e)) return 0; 52 } 53 } 54 return 0; 55 } 56 static int jnumber(jp *j) 57 { 58 if (j->p < j->end && *j->p == '-') j->p++; 59 if (!isd(j)) return 0; 60 if (*j->p == '0') j->p++; else while (isd(j)) j->p++; 61 if (j->p < j->end && *j->p == '.') { j->p++; if (!isd(j)) return 0; while (isd(j)) j->p++; } 62 if (j->p < j->end && (*j->p == 'e' || *j->p == 'E')) { 63 j->p++; if (j->p < j->end && (*j->p == '+' || *j->p == '-')) j->p++; 64 if (!isd(j)) return 0; 65 while (isd(j)) j->p++; 66 } 67 return 1; 68 } 69 static int jvalue(jp *j) 70 { 71 if (++j->depth > 64) return 0; 72 ws(j); 73 int ok = 0; 74 if (j->p >= j->end) ok = 0; 75 else if (*j->p == '{') { 76 j->p++; ws(j); 77 if (j->p < j->end && *j->p == '}') { j->p++; ok = 1; } 78 else for (;;) { 79 ws(j); if (!jstring(j)) break; 80 ws(j); if (j->p >= j->end || *j->p++ != ':') break; 81 if (!jvalue(j)) break; 82 ws(j); if (j->p >= j->end) break; 83 if (*j->p == ',') { j->p++; continue; } 84 if (*j->p == '}') { j->p++; ok = 1; } 85 break; 86 } 87 } else if (*j->p == '[') { 88 j->p++; ws(j); 89 if (j->p < j->end && *j->p == ']') { j->p++; ok = 1; } 90 else for (;;) { 91 if (!jvalue(j)) break; 92 ws(j); if (j->p >= j->end) break; 93 if (*j->p == ',') { j->p++; continue; } 94 if (*j->p == ']') { j->p++; ok = 1; } 95 break; 96 } 97 } else if (*j->p == '"') ok = jstring(j); 98 else if (*j->p == 't') ok = lit(j, "true"); 99 else if (*j->p == 'f') ok = lit(j, "false"); 100 else if (*j->p == 'n') ok = lit(j, "null"); 101 else ok = jnumber(j); 102 j->depth--; 103 return ok; 104 } 105 static int strict_json(const char *s, size_t n) 106 { 107 jp j = { s, s + n, 0 }; 108 if (!jvalue(&j)) return 0; 109 ws(&j); 110 return j.p == j.end; 111 } 112 static char *slurp(const char *path, size_t *n) 113 { 114 FILE *f = fopen(path, "rb"); 115 if (!f) return NULL; 116 size_t cap = 1 << 16, len = 0; char *b = malloc(cap); 117 for (;;) { 118 if (len + 1 >= cap) b = realloc(b, cap *= 2); 119 size_t r = fread(b + len, 1, cap - len - 1, f); 120 if (!r) break; 121 len += r; 122 } 123 fclose(f); b[len] = 0; *n = len; 124 return b; 125 } 126 127 /* -------------------------------------------------------------------- tests */ 128 static void fill_ok(c3_stats *st) 129 { 130 memset(st, 0, sizeof *st); 131 st->n = 2651; st->logscore = -1.5; st->width_mean = 800; st->width_median = 300; st->calibrated = 1; st->first_fail = -1; st->ten_n = 2641; 132 for (int i = 0; i < C3_ST_COUNT; i++) { st->value[i] = 0.5; st->band_lo[i] = 0.25; st->band_hi[i] = 0.75; st->gated[i] = 1; st->pass[i] = 1; st->sub_n[i] = 600; } 133 st->value[C3_ST_N] = 2651; st->band_lo[C3_ST_N] = 2000; st->band_hi[C3_ST_N] = INFINITY; /* the unbounded threshold */ 134 } 135 136 static char *emit(const c3_stats *st, int ten_mode, double ls10, size_t *bad, size_t *len) 137 { 138 char *buf = NULL; size_t n = 0; 139 FILE *o = open_memstream(&buf, &n); 140 fputs("{\n", o); 141 *bad = c3_json_stats(o, "X", st, ten_mode, ls10); 142 fputs("\n}\n", o); 143 fclose(o); 144 *len = n; 145 return buf; 146 } 147 148 static int has(const char *s, const char *t) { return strstr(s, t) != NULL; } 149 /* bare inf / nan tokens outside strings: the emitted text never holds them at all */ 150 static int bare_nonfinite(const char *s) 151 { 152 return has(s, "inf") || has(s, "nan") || has(s, "Inf") || has(s, "NaN"); 153 } 154 155 static void test_validator(void) 156 { 157 const char *good[] = { "{}", "[1,2.5e-3,-0,\"a\\u00e9\"]", "{\"a\":null,\"b\":[true,false]}", " 7 " }; 158 const char *bad[] = { "{\"a\": inf}", "{\"a\": nan}", "{\"a\": -inf}", "[Infinity]", "[NaN]", "{\"a\":1,}", "[01]", "[1.]", "{\"a\":1} x", "" }; 159 for (size_t i = 0; i < sizeof good / sizeof *good; i++) CHECK(strict_json(good[i], strlen(good[i])), "validator rejected %s", good[i]); 160 for (size_t i = 0; i < sizeof bad / sizeof *bad; i++) CHECK(!strict_json(bad[i], strlen(bad[i])), "validator accepted %s", bad[i]); 161 } 162 163 static void test_finite_and_unbounded(void) 164 { 165 c3_stats st; size_t bad, n; 166 fill_ok(&st); 167 char *s = emit(&st, C3_TEN_MEASURED, -4.25, &bad, &n); 168 CHECK(strict_json(s, n), "finite block is not strict JSON"); 169 CHECK(!bare_nonfinite(s), "finite block holds inf/nan"); 170 CHECK(bad == 0 && c3_stats_invalid(&st, C3_TEN_MEASURED, -4.25) == 0, "finite block counted invalid fields"); 171 CHECK(has(s, "\"n_scored\": {\"value\": 2651, \"value_status\": \"ok\", \"lo\": 2000, \"lo_unbounded\": false, \"hi\": null, \"hi_unbounded\": true"), "unbounded threshold not null + hi_unbounded true"); 172 CHECK(has(s, "\"coverage50\": {\"value\": 0.5, \"value_status\": \"ok\", \"lo\": 0.25, \"lo_unbounded\": false, \"hi\": 0.75, \"hi_unbounded\": false"), "finite threshold changed"); 173 CHECK(has(s, "\"ten_log_score\": -4.25, \"ten_log_score_status\": \"ok\""), "measured ten-step score changed"); 174 CHECK(has(s, "\"invalid_fields\": []"), "invalid_fields not empty"); 175 free(s); 176 /* a lower bound of -inf is unbounded too */ 177 st.band_lo[C3_ST_BIAS] = -INFINITY; 178 s = emit(&st, C3_TEN_MEASURED, -4.25, &bad, &n); 179 CHECK(strict_json(s, n) && bad == 0 && has(s, "\"lo\": null, \"lo_unbounded\": true"), "-inf lower bound not marked unbounded"); 180 free(s); 181 } 182 183 static void test_unavailable_baseline(void) 184 { 185 c3_stats st; size_t bad, n; 186 fill_ok(&st); 187 char *s = emit(&st, C3_TEN_UNAVAILABLE, NAN, &bad, &n); 188 CHECK(strict_json(s, n) && !bare_nonfinite(s), "baseline block not strict JSON"); 189 CHECK(has(s, "\"ten_log_score\": null, \"ten_log_score_status\": \"unavailable\""), "unavailable baseline score not null + status"); 190 CHECK(bad == 0 && c3_stats_invalid(&st, C3_TEN_UNAVAILABLE, NAN) == 0, "an unavailable baseline score was counted invalid"); 191 free(s); 192 s = emit(&st, C3_TEN_ABSENT, 0, &bad, &n); 193 CHECK(strict_json(s, n) && !has(s, "ten_log_score"), "v3 (no ten-step field) block carries a ten-step field"); 194 free(s); 195 } 196 197 static void test_nonfinite_measured_fails_closed(void) 198 { 199 c3_stats st; size_t bad, n; char *s; 200 /* measured ten-step score of S not finite */ 201 fill_ok(&st); 202 s = emit(&st, C3_TEN_MEASURED, -INFINITY, &bad, &n); 203 CHECK(strict_json(s, n) && !bare_nonfinite(s), "invalid ten-step block not strict JSON"); 204 CHECK(bad == 1 && has(s, "\"ten_log_score_status\": \"invalid\"") && has(s, "\"invalid_fields\": [\"ten_log_score\"]"), "non-finite ten-step score not marked invalid"); 205 CHECK(c3_stats_invalid(&st, C3_TEN_MEASURED, NAN) == 1, "NaN measured ten-step score not counted"); 206 free(s); 207 /* mean log score, width and a gated statistic value non-finite */ 208 fill_ok(&st); 209 st.logscore = -INFINITY; st.width_mean = NAN; st.value[C3_ST_LAG1] = NAN; st.pass[C3_ST_LAG1] = 0; st.calibrated = 0; 210 s = emit(&st, C3_TEN_UNAVAILABLE, NAN, &bad, &n); 211 CHECK(strict_json(s, n) && !bare_nonfinite(s), "invalid block not strict JSON"); 212 CHECK(bad == 3 && c3_stats_invalid(&st, C3_TEN_UNAVAILABLE, NAN) == 3, "want 3 invalid fields, have %zu", bad); 213 CHECK(has(s, "\"mean_log_score\": null") && has(s, "\"lag1_z\": {\"value\": null, \"value_status\": \"invalid\""), "invalid values not null + invalid status"); 214 CHECK(has(s, "\"stats.lag1_z.value\"") && has(s, "\"width80_mean_mc\"") && has(s, "\"mean_log_score\""), "invalid_fields misses a field"); 215 free(s); 216 /* a NaN threshold is not "unbounded": invalid */ 217 fill_ok(&st); st.band_hi[C3_ST_COV95] = NAN; 218 s = emit(&st, C3_TEN_UNAVAILABLE, NAN, &bad, &n); 219 CHECK(strict_json(s, n) && bad == 1 && has(s, "\"hi\": null, \"hi_unbounded\": false"), "NaN threshold not invalid"); 220 free(s); 221 /* an ungated statistic with no steps is unmeasured, not invalid */ 222 fill_ok(&st); st.value[C3_ST_REG_IDLE] = NAN; st.gated[C3_ST_REG_IDLE] = 0; st.pass[C3_ST_REG_IDLE] = 0; 223 s = emit(&st, C3_TEN_UNAVAILABLE, NAN, &bad, &n); 224 CHECK(strict_json(s, n) && bad == 0 && has(s, "\"regime_idle_cov95\": {\"value\": null, \"value_status\": \"unmeasured\""), "ungated NaN not unmeasured"); 225 free(s); 226 /* the gate itself: band() fails a non-finite value (est3c_common.c band) */ 227 c3_stats j; memset(&j, 0, sizeof j); 228 j.n = 0; j.logscore = -INFINITY; c3_judge(&j); 229 CHECK(!j.calibrated, "an empty (all-NaN) statistics block judged calibrated"); 230 CHECK(c3_stats_invalid(&j, C3_TEN_UNAVAILABLE, NAN) > 0, "an empty statistics block not counted invalid"); 231 } 232 233 static void test_num(void) 234 { 235 char *buf = NULL; size_t n = 0; FILE *o = open_memstream(&buf, &n); 236 c3_json_num(o, 0.1); fputc(' ', o); c3_json_num(o, INFINITY); fputc(' ', o); c3_json_num(o, -INFINITY); fputc(' ', o); c3_json_num(o, NAN); 237 fclose(o); 238 CHECK(!strcmp(buf, "0.`10000000000000001` null null null"), "c3_json_num wrote '%s'", buf); 239 free(buf); 240 } 241 242 /* the committed v5 receipt: its bytes must still hash to its name, and it is the known strict-JSON offender */ 243 static void test_original_untouched(const char *root) 244 { 245 static const char *name = "receipt-`7f0bb7018d066bc438e7a8a9bf87c0d45615f60be2d029b6ab5194168e3db3d1`.json"; 246 char path[1400]; snprintf(path, sizeof path, "%s/docs/estimation/receipts/est-v5/%s", root, name); 247 size_t n; char *b = slurp(path, &n); 248 CHECK(b != NULL, "committed v5 receipt not found at %s (a missing input fails, it is never skipped)", path); 249 if (!b) return; 250 uint8_t dg[32]; char hex[65]; 251 sha256_hash((const uint8_t *)b, n, dg); est_hex(dg, 32, hex); 252 CHECK(!strncmp(name + 8, hex, 64), "the committed v5 receipt no longer matches its digest name (%s)", hex); 253 CHECK(!strict_json(b, n), "validator accepted the committed v5 receipt (it contains bare inf/nan)"); 254 free(b); 255 } 256 257 int main(int argc, char **argv) 258 { 259 if (argc == 3 && !strcmp(argv[1], "--validate")) { 260 size_t n; char *b = slurp(argv[2], &n); 261 if (!b) { fprintf(stderr, "cannot read %s\n", argv[2]); return 2; } 262 int ok = strict_json(b, n); 263 free(b); 264 if (!ok) fprintf(stderr, "not strict JSON: %s\n", argv[2]); 265 return ok ? 0 : 1; 266 } 267 test_validator(); test_num(); test_finite_and_unbounded(); test_unavailable_baseline(); test_nonfinite_measured_fails_closed(); 268 test_original_untouched(argc > 1 ? argv[1] : "."); 269 printf("test_est_json: %d checks, %d failed\n", checks, fails); 270 return fails ? 1 : 0; 271 }