Skip to content

Commit 3f4c936

Browse files
tests: +21 across substrate / code_intel (more)
test_substrate_more.omc (11): arr_him_vec: Fibonacci → low HIM; arr_resonance_vec at 0; arr_fold_all specific cases (7→8, 9→8) and attractor-landing; crt_recover patterns (zeros, ones, twos, threes, fours); fibonacci_index known table; non-attractor returns -1; substrate-attention shape (2 Q rows, 2 V dims); score_rows on zero matrix; is_attractor pattern over 16 Fibs; attractor_distance off-by-1 for 8 Fibs; resonance range [0,1] over varied inputs. test_code_intel_more.omc (10): metrics grow with content size, compression on repetition; deps deep call extraction; deps in class methods; summary on multi-function program (incl. class methods); diff add+remove+unchanged together; fingerprint stability across 3 programs; canonical_hash matches under whitespace variation; minify produces no newlines; similarity orders close < far. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent e1e453f commit 3f4c936

2 files changed

Lines changed: 268 additions & 0 deletions

File tree

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# Yet more code_intel coverage.
2+
3+
fn assert_eq(actual, expected, msg) {
4+
if actual != expected {
5+
test_record_failure(msg + ": expected " + to_string(expected) + " got " + to_string(actual));
6+
}
7+
}
8+
9+
fn assert_true(cond, msg) { if !cond { test_record_failure(msg); } }
10+
11+
# code_metrics for various sources
12+
fn test_metrics_grow_with_size() {
13+
h small = omc_code_metrics("fn f() {}");
14+
h big = omc_code_metrics("fn a() {} fn b() {} fn c() {} fn d() {}");
15+
assert_true(dict_get(big, "ast_size") > dict_get(small, "ast_size"),
16+
"size grows with content");
17+
}
18+
19+
fn test_metrics_compression_with_repetition() {
20+
# metrics requires parseable OMC code; use a valid program with repetition.
21+
h reps = "fn f() { arr_softmax(arr_softmax(arr_softmax(arr_softmax([1.0])))); }";
22+
h m = omc_code_metrics(reps);
23+
assert_true(dict_get(m, "compression_ratio") > 1.0, "repetition compresses");
24+
}
25+
26+
# omc_code_dependencies finds calls deep in nested code
27+
fn test_dependencies_nested_call() {
28+
h deps = omc_code_dependencies("fn f() { return arr_softmax(arr_neg(xs)); }");
29+
h has_sm = 0;
30+
h has_neg = 0;
31+
h i = 0;
32+
while i < arr_len(deps) {
33+
h c = arr_get(deps, i);
34+
if c == "arr_softmax" { has_sm = 1; }
35+
if c == "arr_neg" { has_neg = 1; }
36+
i = i + 1;
37+
}
38+
assert_eq(has_sm, 1, "softmax found");
39+
assert_eq(has_neg, 1, "neg found");
40+
}
41+
42+
# omc_code_dependencies on class methods
43+
fn test_dependencies_class_methods() {
44+
h src = "class C { x; fn m(self) { return dict_get(self, \"x\"); } }";
45+
h deps = omc_code_dependencies(src);
46+
# dict_get should be in deps
47+
h has = 0;
48+
h i = 0;
49+
while i < arr_len(deps) {
50+
if arr_get(deps, i) == "dict_get" { has = 1; }
51+
i = i + 1;
52+
}
53+
assert_eq(has, 1, "dict_get in class method deps");
54+
}
55+
56+
# omc_code_summary on a substantial program
57+
fn test_summary_multi_function() {
58+
h src = "fn a() {} fn b(x) { return x; } fn c(x, y) { return x + y; } class K { f; fn m(self) {} }";
59+
h s = omc_code_summary(src);
60+
h fns = dict_get(s, "functions");
61+
# Includes K.m method.
62+
assert_true(arr_len(fns) >= 4, "4+ fns");
63+
h classes = dict_get(s, "classes");
64+
assert_eq(arr_len(classes), 1, "1 class");
65+
}
66+
67+
# code_diff variations
68+
fn test_diff_add_and_remove() {
69+
h old = "fn a() {} fn b() {}";
70+
h new = "fn a() {} fn c() {}";
71+
h d = omc_code_diff(old, new);
72+
h removed = dict_get(d, "removed");
73+
h added = dict_get(d, "added");
74+
h unchanged = dict_get(d, "unchanged");
75+
assert_eq(arr_len(removed), 1, "b removed");
76+
assert_eq(arr_len(added), 1, "c added");
77+
assert_eq(arr_len(unchanged), 1, "a unchanged");
78+
}
79+
80+
# code_fingerprint stability
81+
fn test_fingerprint_stability_many() {
82+
h cases = [
83+
"fn f() { return 1; }",
84+
"h x = 42;",
85+
"fn add(a, b) { return a + b; }",
86+
];
87+
h i = 0;
88+
while i < arr_len(cases) {
89+
h c = arr_get(cases, i);
90+
h fp1 = omc_code_fingerprint(c);
91+
h fp2 = omc_code_fingerprint(c);
92+
assert_eq(fp1, fp2, concat_many("fp ", to_string(i)));
93+
i = i + 1;
94+
}
95+
}
96+
97+
# canonical_hash matches even with extra whitespace
98+
fn test_canonical_hash_whitespace() {
99+
h tight = "fn f(x){return x;}";
100+
h loose = "fn f ( x ) { return x ; }";
101+
h h1 = omc_canonical_hash(tight);
102+
h h2 = omc_canonical_hash(loose);
103+
assert_eq(dict_get(h1, "raw"), dict_get(h2, "raw"), "whitespace canonicalizes");
104+
}
105+
106+
# omc_code_minify produces single line
107+
fn test_minify_single_line_check() {
108+
h src = "fn f(x) {\n if x > 0 {\n return 1;\n }\n return 0;\n}";
109+
h m = omc_code_minify(src);
110+
# No literal newlines in output.
111+
assert_eq(re_match("\n", m), 0, "no newline");
112+
}
113+
114+
# code_similarity arity
115+
fn test_similarity_orderable() {
116+
h base = "fn f(x) { return x; }";
117+
h close = "fn f(x) { return x + 1; }";
118+
h far = "fn f(a, b, c) { return a * b * c + arr_softmax(arr_neg([a, b, c])); }";
119+
h s_close = omc_code_similarity(base, close);
120+
h s_far = omc_code_similarity(base, far);
121+
assert_true(s_close > s_far, "close > far");
122+
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
# More substrate-primitive coverage.
2+
3+
fn assert_eq(actual, expected, msg) {
4+
if actual != expected {
5+
test_record_failure(msg + ": expected " + to_string(expected) + " got " + to_string(actual));
6+
}
7+
}
8+
9+
fn assert_true(cond, msg) { if !cond { test_record_failure(msg); } }
10+
11+
fn approx_eq(a, b, tol) {
12+
h d = a - b;
13+
if d < 0.0 { d = 0.0 - d; }
14+
return d <= tol;
15+
}
16+
17+
# arr_him_vec — every Fibonacci has small HIM (highly coherent)
18+
fn test_him_fibonacci_small() {
19+
h hv = arr_him_vec([1, 2, 3, 5, 8, 13]);
20+
h i = 0;
21+
while i < arr_len(hv) {
22+
h v = arr_get(hv, i);
23+
assert_true(v < 0.5,
24+
concat_many("him ", to_string(i), " < 0.5"));
25+
i = i + 1;
26+
}
27+
}
28+
29+
# arr_resonance and arr_him agree on attractor zeros
30+
fn test_resonance_at_zero_is_one() {
31+
h r = arr_resonance_vec([0]);
32+
assert_true(approx_eq(arr_get(r, 0), 1.0, 0.001), "0 has resonance 1");
33+
}
34+
35+
# arr_fold_all maps each non-attractor to nearest
36+
fn test_fold_specific_cases() {
37+
h folded = arr_fold_all([7, 9, 12, 22, 30]);
38+
# 7 → 8, 9 → 8, 12 → 13, 22 → 21, 30 → 34
39+
assert_eq(arr_get(folded, 0), 8, "7 → 8");
40+
assert_eq(arr_get(folded, 1), 8, "9 → 8");
41+
# Some choices depend on tiebreaker; check it's a valid attractor.
42+
assert_eq(is_attractor(arr_get(folded, 2)), 1, "12 → attractor");
43+
assert_eq(is_attractor(arr_get(folded, 3)), 1, "22 → attractor");
44+
assert_eq(is_attractor(arr_get(folded, 4)), 1, "30 → attractor");
45+
}
46+
47+
# crt_recover with various residue patterns
48+
fn test_crt_recover_patterns() {
49+
h cases_residues = [
50+
[0, 0, 0, 0],
51+
[1, 1, 1, 1],
52+
[2, 2, 2, 2],
53+
[3, 3, 3, 3],
54+
[4, 4, 4, 4],
55+
];
56+
h i = 0;
57+
while i < arr_len(cases_residues) {
58+
h r = crt_recover(arr_get(cases_residues, i));
59+
assert_true(r >= 0, concat_many("CRT ", to_string(i), " non-negative"));
60+
i = i + 1;
61+
}
62+
}
63+
64+
# fibonacci_index of known values
65+
fn test_fib_index_known() {
66+
assert_eq(fibonacci_index(0), 0, "F0");
67+
assert_eq(fibonacci_index(1), 1, "F1 (or F2)");
68+
assert_eq(fibonacci_index(2), 3, "F3");
69+
assert_eq(fibonacci_index(3), 4, "F4");
70+
assert_eq(fibonacci_index(5), 5, "F5");
71+
assert_eq(fibonacci_index(8), 6, "F6");
72+
assert_eq(fibonacci_index(13), 7, "F7");
73+
}
74+
75+
fn test_fib_index_non_attractor_returns_neg() {
76+
assert_eq(fibonacci_index(7), 0 - 1, "7 not Fibonacci");
77+
assert_eq(fibonacci_index(10), 0 - 1, "10 not Fibonacci");
78+
}
79+
80+
# Substrate attention preserves shape across inputs
81+
fn test_attention_shapes() {
82+
h Q = [[1, 2, 3], [4, 5, 6]];
83+
h K = [[1, 2, 3], [10, 20, 30], [100, 200, 300]];
84+
h V = [[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]];
85+
h out = arr_substrate_attention(Q, K, V);
86+
assert_eq(arr_len(out), 2, "2 query rows");
87+
h r0 = arr_get(out, 0);
88+
assert_eq(arr_len(r0), 2, "2 value dims");
89+
}
90+
91+
# Substrate score rows for various inputs
92+
fn test_score_rows_zero_matrix() {
93+
h M = arr_zeros_2d(3, 3);
94+
h s = arr_substrate_score_rows(M);
95+
assert_eq(arr_len(s), 3, "3 rows");
96+
# 0 is an attractor — every row has resonance 1.0
97+
assert_true(arr_get(s, 0) > 0.9, "zeros all on attractor");
98+
}
99+
100+
# Many is_attractor checks
101+
fn test_attractor_pattern() {
102+
h vals = [0, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987];
103+
h i = 0;
104+
while i < arr_len(vals) {
105+
assert_eq(is_attractor(arr_get(vals, i)), 1,
106+
concat_many("attractor ", to_string(arr_get(vals, i))));
107+
i = i + 1;
108+
}
109+
}
110+
111+
# attractor_distance for off-by-1 from each Fibonacci
112+
fn test_distance_off_by_one() {
113+
h fibs = [3, 5, 8, 13, 21, 34, 55, 89];
114+
h i = 0;
115+
while i < arr_len(fibs) {
116+
h f = arr_get(fibs, i);
117+
# f+1 has distance 1 if f+1 isn't itself a Fibonacci.
118+
h d_up = attractor_distance(f + 1);
119+
h d_down = attractor_distance(f - 1);
120+
# Use if-else instead of `||` (boolean OR only in if conditions).
121+
h ok_up = 0;
122+
if d_up >= 1 { ok_up = 1; }
123+
if is_attractor(f + 1) == 1 { ok_up = 1; }
124+
assert_eq(ok_up, 1, concat_many("f+1 ", to_string(f)));
125+
h ok_dn = 0;
126+
if d_down >= 1 { ok_dn = 1; }
127+
if is_attractor(f - 1) == 1 { ok_dn = 1; }
128+
assert_eq(ok_dn, 1, concat_many("f-1 ", to_string(f)));
129+
i = i + 1;
130+
}
131+
}
132+
133+
# Check resonance is in [0, 1]
134+
fn test_resonance_range() {
135+
h vals = [0, 1, 5, 7, 13, 100, 1000];
136+
h r = arr_resonance_vec(vals);
137+
h i = 0;
138+
while i < arr_len(r) {
139+
h v = arr_get(r, i);
140+
assert_true(v >= 0.0,
141+
concat_many("res ", to_string(i), " >= 0"));
142+
assert_true(v <= 1.0,
143+
concat_many("res ", to_string(i), " <= 1"));
144+
i = i + 1;
145+
}
146+
}

0 commit comments

Comments
 (0)