interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / apps / translate / test.om

517 lines · 11783 bytes Raw Blame History
1note apps/translate/test.om: comprehensive pure-Omega test suite for aien-translate core.
2note Compose with: si/directives/om2.om om/tls/sha2.om apps/translate/lib.om apps/translate/test.om
3
4text tt_pass_all "PASS apps/translate: all 10 test suites passed (positive + negative controls)\n"
5text tt_fail_pre "FAIL apps/translate: check "
6text tt_nl "\n"
7
8text tt_l_en "en"
9text tt_l_eng "English"
10text tt_l_zhcn "zh-CN"
11text tt_l_zhtw "chinese_traditional"
12text tt_l_ja "ja"
13text tt_l_de "DEUTSCH"
14text tt_l_ms " ms "
15text tt_l_auto "auto"
16text tt_l_bad "klingon"
17text tt_l_bad2 "zz"
18
19text tt_src_keep "Aien runs on Aienux with Omega at https://aienos.com/docs. Run `aien start` for {user} and <g1>docs</g1> %s."
20text tt_trn_ok "Aien fonctionne sur Aienux avec Omega sur https://aienos.com/docs. Lancez `aien start` pour {user} et <g1>doku</g1> %s."
21text tt_trn_lost_keep "艾恩 fonctionne sur Aienux avec Omega sur https://aienos.com/docs. Lancez `aien start` pour {user} et <g1>doku</g1> %s."
22text tt_trn_lost_url "Aien fonctionne sur Aienux avec Omega. Lancez `aien start` pour {user} et <g1>doku</g1> %s."
23text tt_trn_lost_tag "Aien fonctionne sur Aienux avec Omega sur https://aienos.com/docs. Lancez `aien start` pour {user} et doku %s."
24text tt_src_subword "AIENOS boots cleanly."
25text tt_trn_subword "AIENOS demarre proprement."
26text tt_tidy_src "Run aien start"
27text tt_tidy_trn " Fuhren Sie `aien start` aus "
28text tt_tidy_exp "Fuhren Sie aien start aus"
29text tt_sw_link " Read the docs at https: "
30
31text tt_lc_ok "sure: [\"a\", \"b ] c\"] trailing"
32text tt_lc_open "[\"a\", \"b ] still inside\""
33
34text tt_req_ok "{\"from\":\"en\",\"to\":\"ja\",\"text\":\"Hello world. Second sentence!\",\"max_chunk\":16}"
35text tt_req_bad_lang "{\"from\":\"en\",\"to\":\"xx\",\"text\":\"Hello\"}"
36text tt_req_empty "{\"from\":\"en\",\"to\":\"ja\",\"text\":\" \"}"
37text tt_req_bad_json "{\"from\":\"en\",\"to\":\"ja\""
38
39text tt_c_a "alpha"
40text tt_c_b "beta"
41text tt_c_c "gamma"
42text tt_c_d "delta"
43text tt_c_va "ALPHA_JA"
44text tt_c_vb "BETA_JA"
45text tt_c_vc "GAMMA_JA"
46text tt_c_vd "DELTA_JA"
47
48fn tt_write_text s view uchar * to void
49n = len s
50w = write 1 s 0 n
51if lt w 0
52exit 99
53end
54end
55
56fn tt_fail code ulong to int
57tt_write_text tt_fail_pre
58buf = new uchar 24 0
59x = code
60k = ulong 0
61if eq x 0
62put buf 0 48
63k = 1
64else
65while gt x 0 bound 24
66put buf k uchar add 48 rem x 10
67x = div x 10
68k = add k 1
69end
70end
71outb = new uchar 24 0
72j = ulong 0
73while lt j k bound 24
74put outb j get buf sub sub k 1 j
75j = add j 1
76end
77w = write 1 outb 0 k
78if lt w 0
79exit 99
80end
81tt_write_text tt_nl
82ret int code
83end
84
85fn tt_put_tmp s view uchar * to void
86n = len s
87i = ulong 0
88while lt i n bound 512
89put tr_g_tmp_out i get s i
90i = add i 1
91end
92put tr_g_tmp_out_len 0 n
93end
94
95fn tt_tmp_eq s view uchar * to bool
96n = len s
97if ne get tr_g_tmp_out_len 0 n
98ret false
99end
100i = ulong 0
101while lt i n bound 512
102if ne get tr_g_tmp_out i get s i
103ret false
104end
105i = add i 1
106end
107ret true
108end
109
110note Check 1: ISO-639 and translate.js language allowlist validation + negative controls
111fn test_langs to bool
112if ne tr_lang_find tt_l_en 0 len tt_l_en 0
113ret false
114end
115if ne tr_lang_find tt_l_eng 0 len tt_l_eng 0
116ret false
117end
118if ne tr_lang_find tt_l_zhcn 0 len tt_l_zhcn 1
119ret false
120end
121if ne tr_lang_find tt_l_zhtw 0 len tt_l_zhtw 2
122ret false
123end
124if ne tr_lang_find tt_l_ja 0 len tt_l_ja 3
125ret false
126end
127if ne tr_lang_find tt_l_de 0 len tt_l_de 7
128ret false
129end
130if ne tr_lang_find tt_l_ms 0 len tt_l_ms 30
131ret false
132end
133if ne tr_lang_find_src tt_l_auto 0 len tt_l_auto TR_LANG_AUTO
134ret false
135end
136note Negative controls: unsupported language codes refused
137if ne tr_lang_find tt_l_bad 0 len tt_l_bad TR_LANG_INVALID
138ret false
139end
140if ne tr_lang_find tt_l_bad2 0 len tt_l_bad2 TR_LANG_INVALID
141ret false
142end
143if ne tr_lang_find tt_l_auto 0 len tt_l_auto TR_LANG_INVALID
144ret false
145end
146ret true
147end
148
149note Check 2: Multi-byte UTF-8 (1/2/3/4-byte code points) validation & codepoint counting
150fn test_utf8_valid_multi to bool
151note Build: "A" (1B) + e-acute (c3 a9, 2B) + CJK "日" (e6 97 a5, 3B) + emoji U+1F600 (f0 9f 98 80, 4B) = 10 bytes, 4 chars
152b = new uchar 16 0
153put b 0 65
154put b 1 0xc3
155put b 2 0xa9
156put b 3 0xe6
157put b 4 0x97
158put b 5 0xa5
159put b 6 0xf0
160put b 7 0x9f
161put b 8 0x98
162put b 9 0x80
163if ne tr_validate_text b 10 TR_OK
164ret false
165end
166if ne tr_utf8_chars b 10 4
167ret false
168end
169ret true
170end
171
172note Check 3: Negative controls for UTF-8, NUL, overlong, surrogate, >U+10FFFF, truncated
173fn test_utf8_refusals to bool
174b = new uchar 16 0
175note 3a: NUL byte embedded
176put b 0 65
177put b 1 0
178put b 2 66
179if ne tr_validate_text b 3 TR_ERR_NUL
180ret false
181end
182note 3b: Overlong 2-byte sequence (0xc0 0xaf)
183put b 0 0xc0
184put b 1 0xaf
185if ne tr_validate_text b 2 TR_ERR_UTF8
186ret false
187end
188note 3c: UTF-16 surrogate U+D800 (0xed 0xa0 0x80)
189put b 0 0xed
190put b 1 0xa0
191put b 2 0x80
192if ne tr_validate_text b 3 TR_ERR_UTF8
193ret false
194end
195note 3d: Codepoint > U+10FFFF (0xf4 0x90 0x80 0x80)
196put b 0 0xf4
197put b 1 0x90
198put b 2 0x80
199put b 3 0x80
200if ne tr_validate_text b 4 TR_ERR_UTF8
201ret false
202end
203note 3e: Truncated 4-byte emoji (3 of 4 bytes)
204put b 0 0xf0
205put b 1 0x9f
206put b 2 0x98
207if ne tr_validate_text b 3 TR_ERR_UTF8
208ret false
209end
210note 3f: Stray continuation byte
211put b 0 0x80
212if ne tr_validate_text b 1 TR_ERR_UTF8
213ret false
214end
215ret true
216end
217
218note Check 4: UTF-8 chunker NEVER splits 2/3/4-byte code points even under tight max_chunk=5
219fn test_utf8_chunker_boundaries to bool
220note 14 bytes: 2B(c3 a9) + 3B(e6 97 a5) + 4B(f0 9f 98 80) + 3B(e3 80 82 "。") + 2B(c3 bc)
221b = new uchar 32 0
222put b 0 0xc3
223put b 1 0xa9
224put b 2 0xe6
225put b 3 0x97
226put b 4 0xa5
227put b 5 0xf0
228put b 6 0x9f
229put b 7 0x98
230put b 8 0x80
231put b 9 0xe3
232put b 10 0x80
233put b 11 0x82
234put b 12 0xc3
235put b 13 0xbc
236if ne tr_chunk_utf8 b 14 5 TR_OK
237ret false
238end
239cnt = get tr_g_chk_cnt 0
240if lt cnt 3
241ret false
242end
243note Verify every chunk is <= 5 bytes, contiguous, covers all 14 bytes, and is strictly valid UTF-8!
244tot = ulong 0
245k = ulong 0
246while lt k cnt bound 64
247off = get tr_g_chk_off k
248clen = get tr_g_chk_len k
249if ne off tot
250ret false
251end
252if or eq clen 0 gt clen 5
253ret false
254end
255if not tr_utf8_valid part b off clen clen
256ret false
257end
258tot = add tot clen
259k = add k 1
260end
261if ne tot 14
262ret false
263end
264note Negative control: max_chunk < 4 must be refused
265if ne tr_chunk_utf8 b 14 3 TR_ERR_CHUNK
266ret false
267end
268ret true
269end
270
271note Check 5: Paragraph & sentence boundary preference in chunker
272fn test_chunker_para_sent to bool
273note "First para.\n\nSecond para!" (25 bytes), max_chunk=16 -> splits cleanly after \n\n at byte 13
274b = new uchar 32 0
275put b 0 70
276put b 1 105
277put b 2 114
278put b 3 115
279put b 4 116
280put b 5 32
281put b 6 112
282put b 7 97
283put b 8 114
284put b 9 97
285put b 10 46
286put b 11 10
287put b 12 10
288put b 13 83
289put b 14 101
290put b 15 99
291put b 16 111
292put b 17 110
293put b 18 100
294put b 19 32
295put b 20 112
296put b 21 97
297put b 22 114
298put b 23 97
299put b 24 33
300if ne tr_chunk_utf8 b 25 16 TR_OK
301ret false
302end
303if ne get tr_g_chk_cnt 0 2
304ret false
305end
306if ne get tr_g_chk_off 0 0
307ret false
308end
309if ne get tr_g_chk_len 0 13
310ret false
311end
312if ne get tr_g_chk_off 1 13
313ret false
314end
315if ne get tr_g_chk_len 1 12
316ret false
317end
318ret true
319end
320
321note Check 6: Oversize payload (> 16384 bytes and > 3000 chars) strictly refused
322fn test_oversize_refusal to bool
323bigb = new uchar 16385 65
324if ne tr_validate_text bigb 16385 TR_ERR_OVERSIZE
325ret false
326end
327if ne tr_validate_text bigb 3001 TR_ERR_OVERSIZE
328ret false
329end
330if ne tr_validate_text bigb 3000 TR_OK
331ret false
332end
333ret true
334end
335
336note Check 7: Deterministic SHA-256 LRU cache hit, miss, and bounded eviction
337fn test_cache_lru to bool
338tr_cache_init 3
339note Put A, B, C (fills cap=3, 0 evictions)
340tt_put_tmp tt_c_va
341tr_cache_put 0 3 tt_c_a len tt_c_a
342tt_put_tmp tt_c_vb
343tr_cache_put 0 3 tt_c_b len tt_c_b
344tt_put_tmp tt_c_vc
345tr_cache_put 0 3 tt_c_c len tt_c_c
346if ne get tr_g_cache_evicts 0 0
347ret false
348end
349note Touch A so B becomes least-recently-used!
350if ne tr_cache_get 0 3 tt_c_a len tt_c_a 1
351ret false
352end
353if not tt_tmp_eq tt_c_va
354ret false
355end
356note Put D -> must evict B (LRU), keeping A, C, D!
357tt_put_tmp tt_c_vd
358tr_cache_put 0 3 tt_c_d len tt_c_d
359if ne get tr_g_cache_evicts 0 1
360ret false
361end
362note B must now miss; A, C, D must hit!
363if ne tr_cache_get 0 3 tt_c_b len tt_c_b 0
364ret false
365end
366if ne tr_cache_get 0 3 tt_c_a len tt_c_a 1
367ret false
368end
369if ne tr_cache_get 0 3 tt_c_c len tt_c_c 1
370ret false
371end
372if not tt_tmp_eq tt_c_vc
373ret false
374end
375if ne tr_cache_get 0 3 tt_c_d len tt_c_d 1
376ret false
377end
378if not tt_tmp_eq tt_c_vd
379ret false
380end
381note Different target lang (dst=6 French instead of dst=3 Japanese) must miss for A!
382if ne tr_cache_get 0 6 tt_c_a len tt_c_a 0
383ret false
384end
385ret true
386end
387
388note Check 8: Guard checks (keep-list, URL, backticks, placeholders, inline tags, runaway)
389fn test_guard to bool
390if ne tr_guard_check tt_src_keep len tt_src_keep tt_trn_ok len tt_trn_ok TR_OK
391ret false
392end
393note Negative control: dropped keep word "Aien" must be refused
394if ne tr_guard_check tt_src_keep len tt_src_keep tt_trn_lost_keep len tt_trn_lost_keep TR_ERR_GUARD
395ret false
396end
397note Negative control: dropped URL must be refused
398if ne tr_guard_check tt_src_keep len tt_src_keep tt_trn_lost_url len tt_trn_lost_url TR_ERR_GUARD
399ret false
400end
401note Negative control: dropped <g1> inline tag must be refused
402if ne tr_guard_check tt_src_keep len tt_src_keep tt_trn_lost_tag len tt_trn_lost_tag TR_ERR_GUARD
403ret false
404end
405note Subword check: AIEN inside AIENOS is NOT falsely protected as separate word
406if ne tr_guard_check tt_src_subword len tt_src_subword tt_trn_subword len tt_trn_subword TR_OK
407ret false
408end
409note Runaway length check: 100 chars for 2-char source ("hi") > 2*4 + 80 = 88 -> refused
410hi = new uchar 2 104
411runaway = new uchar 100 97
412if ne tr_guard_check hi 2 runaway 100 TR_ERR_GUARD
413ret false
414end
415ret true
416end
417
418note Check 9: Tidy & split_ws & list_closed
419fn test_helpers to bool
420w = tr_tidy tt_tidy_src len tt_tidy_src tt_tidy_trn len tt_tidy_trn
421if ne w len tt_tidy_exp
422ret false
423end
424if not tt_tmp_eq tt_tidy_exp
425ret false
426end
427tr_split_ws tt_sw_link len tt_sw_link
428note " Read the docs at https: " -> lead=2, core_end=18 ("Read the docs at")
429if ne get tr_g_sw_lead 0 2
430ret false
431end
432if ne get tr_g_sw_core_end 0 18
433ret false
434end
435if not tr_list_closed tt_lc_ok len tt_lc_ok
436ret false
437end
438if tr_list_closed tt_lc_open len tt_lc_open
439ret false
440end
441ret true
442end
443
444note Check 10: End-to-end JSONL request handling (cache miss then cache hit + negative refusals)
445fn test_jsonl_pipeline to bool
446tr_cache_init 16
447note First request: 3 chunks ("Hello world.", " Second ", "sentence!"), all 3 miss cache
448rc1 = tr_handle_request tt_req_ok len tt_req_ok
449if ne rc1 TR_OK
450ret false
451end
452if ne get tr_g_chk_cnt 0 3
453ret false
454end
455if ne get tr_g_cache_misses 0 3
456ret false
457end
458if ne get tr_g_cache_hits 0 0
459ret false
460end
461note Second identical request: all 3 chunks HIT cache!
462rc2 = tr_handle_request tt_req_ok len tt_req_ok
463if ne rc2 TR_OK
464ret false
465end
466if ne get tr_g_cache_hits 0 3
467ret false
468end
469note Negative control: unsupported language code
470if ne tr_handle_request tt_req_bad_lang len tt_req_bad_lang TR_ERR_LANG
471ret false
472end
473note Negative control: empty/whitespace text
474if ne tr_handle_request tt_req_empty len tt_req_empty TR_ERR_EMPTY
475ret false
476end
477note Negative control: malformed JSON
478if ne tr_handle_request tt_req_bad_json len tt_req_bad_json TR_ERR_JSON
479ret false
480end
481ret true
482end
483
484fn main to int
485if not test_langs
486ret tt_fail 1
487end
488if not test_utf8_valid_multi
489ret tt_fail 2
490end
491if not test_utf8_refusals
492ret tt_fail 3
493end
494if not test_utf8_chunker_boundaries
495ret tt_fail 4
496end
497if not test_chunker_para_sent
498ret tt_fail 5
499end
500if not test_oversize_refusal
501ret tt_fail 6
502end
503if not test_cache_lru
504ret tt_fail 7
505end
506if not test_guard
507ret tt_fail 8
508end
509if not test_helpers
510ret tt_fail 9
511end
512if not test_jsonl_pipeline
513ret tt_fail 10
514end
515tt_write_text tt_pass_all
516ret 0
517end