|
| 1 | +//! Substrate-into-core (v1.8.x) conformance — locks the new primitives' behavior so future |
| 2 | +//! interpreter restructuring can't silently regress them. Each test evals real OMC through the |
| 3 | +//! parser + interpreter and asserts the contract. If one fails: fix the regression, don't relax it. |
| 4 | +
|
| 5 | +use omnimcode_core::interpreter::Interpreter; |
| 6 | +use omnimcode_core::parser::Parser; |
| 7 | +use omnimcode_core::value::Value; |
| 8 | + |
| 9 | +fn run(source: &str) -> Result<Value, String> { |
| 10 | + let mut parser = Parser::new(source); |
| 11 | + let stmts = parser.parse()?; |
| 12 | + let mut interp = Interpreter::new(); |
| 13 | + interp.execute(stmts)?; |
| 14 | + interp |
| 15 | + .get_var_for_testing("__result__") |
| 16 | + .ok_or_else(|| "no __result__ variable".to_string()) |
| 17 | +} |
| 18 | + |
| 19 | +fn int(src: &str) -> i64 { |
| 20 | + run(src).unwrap().to_int() |
| 21 | +} |
| 22 | + |
| 23 | +// ── content-addressing (Phase 1.1) ── |
| 24 | +#[test] |
| 25 | +fn haddr_face_in_range_and_deterministic() { |
| 26 | + assert_eq!(int("__result__ = haddr_face(\"fibonacci\");"), int("__result__ = haddr_face(\"fibonacci\");")); |
| 27 | + let f = int("__result__ = haddr_face(\"gcd\");"); |
| 28 | + assert!((0..12).contains(&f), "face out of range: {f}"); |
| 29 | +} |
| 30 | + |
| 31 | +// ── O(1) semantic equality + content-addressed heap (Phase 2) ── |
| 32 | +#[test] |
| 33 | +fn same_value_structural_equality() { |
| 34 | + assert_eq!(int("__result__ = same_value([1,2,3], [1,2,3]);"), 1); |
| 35 | + assert_eq!(int("__result__ = same_value([1,2,3], [1,2,4]);"), 0); |
| 36 | + assert_eq!(int("__result__ = same_value({\"a\":1}, {\"a\":1});"), 1); |
| 37 | +} |
| 38 | + |
| 39 | +#[test] |
| 40 | +fn cas_round_trip_and_dedup() { |
| 41 | + assert_eq!(int("h k = cas_put([10,20,30]); __result__ = cas_get(k)[1];"), 20); |
| 42 | + // identical content → same key |
| 43 | + assert_eq!(int("h a = cas_put([7,7]); h b = cas_put([7,7]); __result__ = same_value(a,b);"), 1); |
| 44 | +} |
| 45 | + |
| 46 | +// ── @memo (Phase 2.2): correctness + the purity gate ── |
| 47 | +#[test] |
| 48 | +fn memo_matches_plain() { |
| 49 | + let src = r#" |
| 50 | + @memo |
| 51 | + fn mf(n) { if n < 2 { return n; } return mf(n-1) + mf(n-2); } |
| 52 | + fn pf(n) { if n < 2 { return n; } return pf(n-1) + pf(n-2); } |
| 53 | + __result__ = same_value(mf(27), pf(27)); |
| 54 | + "#; |
| 55 | + assert_eq!(int(src), 1); |
| 56 | + assert_eq!(int("@memo\nfn mf(n){ if n<2 {return n;} return mf(n-1)+mf(n-2);}\n__result__ = mf(40);"), 102334155); |
| 57 | +} |
| 58 | + |
| 59 | +#[test] |
| 60 | +fn memo_refuses_impure() { |
| 61 | + let r = run("@memo\nfn bad(n){ print(n); return n; }\n__result__ = bad(1);"); |
| 62 | + assert!(r.is_err(), "@memo on an impure fn must be refused"); |
| 63 | +} |
| 64 | + |
| 65 | +// ── locality similarity + dispatch (Phase 1.2 / 3.1) ── |
| 66 | +#[test] |
| 67 | +fn locality_orders_by_content_and_routes() { |
| 68 | + // near-variant more similar than unrelated (×1000, integer compare to avoid float fmt) |
| 69 | + let src = r#" |
| 70 | + h near = locality_sim("quicksort","quick_sort"); |
| 71 | + h far = locality_sim("quicksort","zzzzzzzz"); |
| 72 | + __result__ = near > far; |
| 73 | + "#; |
| 74 | + assert_eq!(int(src), 1); |
| 75 | + let route = run("fn quicksort(a){return a;} __result__ = nearest_fn(\"quicksrt\");").unwrap(); |
| 76 | + assert_eq!(route.to_string(), "quicksort"); |
| 77 | +} |
| 78 | + |
| 79 | +// ── verify-gated self-modification (Phase 3) ── |
| 80 | +#[test] |
| 81 | +fn fn_swap_verified_accepts_good_rejects_bad() { |
| 82 | + let good = r#" |
| 83 | + fn target(n) { return 0 - 1; } |
| 84 | + h cand = "fn target(n) { return n * n; }"; |
| 85 | + h ok = fn_swap_verified("target", cand, "target(5) == 25"); |
| 86 | + __result__ = ok["accepted"]; |
| 87 | + "#; |
| 88 | + assert_eq!(int(good), 1); |
| 89 | + let bad = r#" |
| 90 | + fn target(n) { return n * n; } |
| 91 | + h cand = "fn target(n) { return n + 1; }"; |
| 92 | + h r = fn_swap_verified("target", cand, "target(5) == 25"); |
| 93 | + __result__ = r["accepted"]; |
| 94 | + "#; |
| 95 | + assert_eq!(int(bad), 0, "a candidate failing its test must be rejected"); |
| 96 | + // and rolled back: target still squares |
| 97 | + let rollback = r#" |
| 98 | + fn target(n) { return n * n; } |
| 99 | + h cand = "fn target(n) { return n + 1; }"; |
| 100 | + fn_swap_verified("target", cand, "target(5) == 25"); |
| 101 | + __result__ = target(6); |
| 102 | + "#; |
| 103 | + assert_eq!(int(rollback), 36, "rejected candidate must be rolled back"); |
| 104 | +} |
| 105 | + |
| 106 | +// ── correct-by-construction synthesis (Phase 4) ── |
| 107 | +#[test] |
| 108 | +fn gen_omc_is_valid_by_construction() { |
| 109 | + // a generated program parses (code_parse_check ok) for several seeds |
| 110 | + for seed in [1, 7, 42, 256, 2026] { |
| 111 | + let src = format!("h p = gen_omc({seed}); h c = code_parse_check(p); __result__ = c[\"ok\"];"); |
| 112 | + assert_eq!(int(&src), 1, "gen_omc({seed}) did not parse"); |
| 113 | + } |
| 114 | + // same address → same program (gen_at determinism) |
| 115 | + assert_eq!(int("__result__ = same_value(gen_at(\"x\"), gen_at(\"x\"));"), 1); |
| 116 | +} |
| 117 | + |
| 118 | +// ── HBit dual-band at the Value level (Phase 6) ── |
| 119 | +#[test] |
| 120 | +fn dualband_rides_through_arithmetic_alpha_exact() { |
| 121 | + // phi_shadow(10) → β = nearest attractor (8); β rides through +3 → 11; α stays exact 13 |
| 122 | + assert_eq!(int("h s = phi_shadow(10) + 3; __result__ = s;"), 13); |
| 123 | + assert_eq!(int("h s = phi_shadow(10) + 3; __result__ = bands(s)[1];"), 11); |
| 124 | + // on-lattice computation has zero divergence; off-lattice is positive |
| 125 | + assert_eq!(int("__result__ = value_divergence(phi_shadow(8) * 7);"), 0); |
| 126 | + assert!(int("__result__ = value_divergence((phi_shadow(50)+1)*3);") > 0); |
| 127 | + // ordinary (single-band) values are perfectly in tune and unchanged |
| 128 | + assert_eq!(int("__result__ = harmony(7 + 3);"), 1000); |
| 129 | + assert_eq!(int("__result__ = 7 + 3;"), 10); |
| 130 | +} |
| 131 | + |
| 132 | +#[test] |
| 133 | +fn hbit_gate_separates_in_tune_from_divergent() { |
| 134 | + assert_eq!(int("__result__ = hbit_divergence(8, 8);"), 0); |
| 135 | + assert!(int("__result__ = hbit_divergence(8, 977);") > 500); |
| 136 | +} |
| 137 | + |
| 138 | +// ── CRT positional encoding (Phase 1.3) ── |
| 139 | +#[test] |
| 140 | +fn crt_pe_is_periodic_over_lcm() { |
| 141 | + assert_eq!(int("__result__ = same_value(crt_pe(0), crt_pe(10920));"), 1); |
| 142 | + assert_eq!(int("__result__ = same_value(crt_pe(7), crt_pe(8));"), 0); |
| 143 | +} |
0 commit comments