|
| 1 | +# Tests for substrate_embed, str_similarity, sleep, file_ls, omc_eval_file |
| 2 | +# Run with: omc --test examples/tests/test_substrate_embed_builtins.omc |
| 3 | + |
| 4 | +fn assert_eq(actual, expected, msg) { |
| 5 | + if actual != expected { |
| 6 | + test_record_failure(str_concat(msg, ": expected ", to_string(expected), " got ", to_string(actual))); |
| 7 | + } |
| 8 | +} |
| 9 | + |
| 10 | +fn assert_true(cond, msg) { |
| 11 | + if !cond { test_record_failure(msg); } |
| 12 | +} |
| 13 | + |
| 14 | +fn assert_false(cond, msg) { |
| 15 | + if cond { test_record_failure(msg); } |
| 16 | +} |
| 17 | + |
| 18 | +fn assert_near(a, b, tol, msg) { |
| 19 | + h diff = a - b |
| 20 | + if diff < 0.0 { diff = 0.0 - diff } |
| 21 | + if diff > tol { test_record_failure(str_concat(msg, ": |", to_string(a), " - ", to_string(b), "| > ", to_string(tol))); } |
| 22 | +} |
| 23 | + |
| 24 | +# ── substrate_embed ─────────────────────────────────────────────────────────── |
| 25 | + |
| 26 | +fn test_substrate_embed_returns_array() { |
| 27 | + h v = substrate_embed("hello world", 16) |
| 28 | + assert_eq(type_of(v), "array", "substrate_embed returns array") |
| 29 | +} |
| 30 | + |
| 31 | +fn test_substrate_embed_correct_dims() { |
| 32 | + h v = substrate_embed("test", 8) |
| 33 | + assert_eq(arr_len(v), 8, "substrate_embed respects dims arg") |
| 34 | +} |
| 35 | + |
| 36 | +fn test_substrate_embed_normalized() { |
| 37 | + h v = substrate_embed("normalize me", 16) |
| 38 | + h dot = 0.0 |
| 39 | + h i = 0 |
| 40 | + while i < arr_len(v) { |
| 41 | + h x = v[i] |
| 42 | + dot = dot + x * x |
| 43 | + i = i + 1 |
| 44 | + } |
| 45 | + assert_near(dot, 1.0, 0.01, "substrate_embed is L2-normalized") |
| 46 | +} |
| 47 | + |
| 48 | +fn test_substrate_embed_deterministic() { |
| 49 | + h a = substrate_embed("hello", 8) |
| 50 | + h b = substrate_embed("hello", 8) |
| 51 | + h match_all = true |
| 52 | + h i = 0 |
| 53 | + while i < arr_len(a) { |
| 54 | + if a[i] != b[i] { match_all = false } |
| 55 | + i = i + 1 |
| 56 | + } |
| 57 | + assert_true(match_all, "substrate_embed is deterministic") |
| 58 | +} |
| 59 | + |
| 60 | +fn test_substrate_embed_differs_for_different_texts() { |
| 61 | + h a = substrate_embed("apple", 8) |
| 62 | + h b = substrate_embed("orange", 8) |
| 63 | + h same = true |
| 64 | + h i = 0 |
| 65 | + while i < arr_len(a) { |
| 66 | + if a[i] != b[i] { same = false } |
| 67 | + i = i + 1 |
| 68 | + } |
| 69 | + assert_false(same, "substrate_embed differs for different texts") |
| 70 | +} |
| 71 | + |
| 72 | +# ── str_similarity ───────────────────────────────────────────────────────────── |
| 73 | + |
| 74 | +fn test_str_similarity_self_is_one() { |
| 75 | + h sim = str_similarity("hello world", "hello world") |
| 76 | + assert_near(sim, 1.0, 0.01, "str_similarity of identical strings is ~1.0") |
| 77 | +} |
| 78 | + |
| 79 | +fn test_str_similarity_in_range() { |
| 80 | + h sim = str_similarity("apple", "orange") |
| 81 | + assert_true(sim >= -1.0, "str_similarity >= -1") |
| 82 | + assert_true(sim <= 1.0, "str_similarity <= 1") |
| 83 | +} |
| 84 | + |
| 85 | +fn test_str_similarity_symmetric() { |
| 86 | + h s1 = str_similarity("hello world", "world hello") |
| 87 | + h s2 = str_similarity("world hello", "hello world") |
| 88 | + assert_near(s1, s2, 0.001, "str_similarity is symmetric") |
| 89 | +} |
| 90 | + |
| 91 | +# ── sleep ───────────────────────────────────────────────────────────────────── |
| 92 | + |
| 93 | +fn test_sleep_returns_null() { |
| 94 | + h result = sleep(0) |
| 95 | + assert_eq(result, null, "sleep(0) returns null") |
| 96 | +} |
| 97 | + |
| 98 | +fn test_sleep_short_delay() { |
| 99 | + h t0 = now_ms() |
| 100 | + sleep(50) |
| 101 | + h t1 = now_ms() |
| 102 | + h elapsed = t1 - t0 |
| 103 | + assert_true(elapsed >= 40, "sleep(50) waits at least ~40ms") |
| 104 | +} |
| 105 | + |
| 106 | +# ── file_ls ─────────────────────────────────────────────────────────────────── |
| 107 | + |
| 108 | +fn test_file_ls_returns_array() { |
| 109 | + h files = file_ls("examples/tests") |
| 110 | + assert_eq(type_of(files), "array", "file_ls returns array") |
| 111 | +} |
| 112 | + |
| 113 | +fn test_file_ls_nonempty() { |
| 114 | + h files = file_ls("examples/tests") |
| 115 | + assert_true(arr_len(files) > 0, "file_ls finds files in examples/tests") |
| 116 | +} |
| 117 | + |
| 118 | +fn test_file_ls_contains_known_file() { |
| 119 | + h files = file_ls("examples/tests") |
| 120 | + h found = false |
| 121 | + h i = 0 |
| 122 | + while i < arr_len(files) { |
| 123 | + if files[i] == "test_substrate_embed_builtins.omc" { found = true } |
| 124 | + i = i + 1 |
| 125 | + } |
| 126 | + assert_true(found, "file_ls finds this test file itself") |
| 127 | +} |
| 128 | + |
| 129 | +fn test_file_ls_sorted() { |
| 130 | + h files = file_ls("examples/tests") |
| 131 | + h sorted = true |
| 132 | + h i = 1 |
| 133 | + while i < arr_len(files) { |
| 134 | + if files[i] < files[i - 1] { sorted = false } |
| 135 | + i = i + 1 |
| 136 | + } |
| 137 | + assert_true(sorted, "file_ls returns sorted results") |
| 138 | +} |
| 139 | + |
| 140 | +# ── omc_eval_file ───────────────────────────────────────────────────────────── |
| 141 | + |
| 142 | +fn test_omc_eval_file_loads_functions() { |
| 143 | + omc_eval_file("examples/lib/cache.omc") |
| 144 | + h cache = lru_new(10) |
| 145 | + lru_put(cache, "key", "value") |
| 146 | + h got = lru_get(cache, "key") |
| 147 | + assert_eq(got, "value", "omc_eval_file loads lib file and makes functions available") |
| 148 | +} |
0 commit comments