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-bridge/blob/main/tests/native/caller.py # omega-bridge Omega-first language interoperability: checked conversion backends and native language bindings Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-bridge / tests / native / caller.py` 58 lines ยท 3640 bytes Raw Blame History 1 # Python caller for the omega-bridge native call ABI v1 test library (ctypes, standard library only). 2 import ctypes, os, sys 3 sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) # python3 -I drops this folder; the binding is beside us 4 import omega_native as om # every Omega declaration: generated by tools/native, nothing typed by hand 5 u64 = ctypes.c_uint64 6 SENTINEL = `6148914691236517205` 7 fails = 0 8 def check(name, ok): 9 global fails 10 print(("PASS " if ok else "FAIL ") + name) 11 fails += not ok 12 def bits(v): # signed Python int -> canonical 64-bit two's-complement bits 13 return v & (2**64 - 1) 14 def signed(b): 15 return b - 2**64 if b >= 2**63 else b 16 r = u64(0) 17 check("madd 3*5+7 = 22", om.omega_madd(ctypes.byref(r), 3, 5, 7) == 0 and r.value == 22) 18 check("madd wraps = 3", om.omega_madd(ctypes.byref(r), 2**64 - 1, 2, 5) == 0 and r.value == 3) 19 check("madd large", om.omega_madd(ctypes.byref(r), `1311768467463790320`, `18364758544493064720`, `1085102592571150095`) == 0 20 and r.value == `3637949782307626511`) 21 check("madd signed bits = -13", om.omega_madd(ctypes.byref(r), bits(-3), 5, 2) == 0 and signed(r.value) == -13) 22 r = u64(SENTINEL) 23 check("smadd overflow traps 1, result unchanged", om.omega_smadd(ctypes.byref(r), 2**62, 4, 0) == 1 and r.value == SENTINEL) 24 buf = (u64 * 4)() 25 check("fill k=1 traps 3, result unchanged", om.omega_fill(ctypes.byref(r), buf, 4, 1) == 3 and r.value == SENTINEL) 26 check("writes before the trap stay visible", list(buf) == [11, 22, 0, 0]) 27 check("fill k=0 = 3", om.omega_fill(ctypes.byref(r), buf, 4, 0) == 0 and r.value == 3 and list(buf) == [11, 22, 33, 0]) 28 check("null result refused 32", om.omega_madd(None, 3, 5, 7) == 32) 29 # mix: six parameters (ulong, long, view uchar 3, edit uchar 2, view ulong 2, ulong), no result. 30 # 5 + 20 + 7 = 32 (calc:`08302bc4`); 5 + 20 + 300 is 70 over 255 (calc:`a6a8bb76`), so the conversion traps 5. 31 cb = bytes([1, 2, 3]) # view uchar: bytes 32 db = (ctypes.c_ubyte * 2)() # edit uchar: a writable ctypes array 33 eb = (u64 * 2)(10, 20) # view ulong: a ctypes array 34 r = u64(SENTINEL) 35 check("mix six parameters, void result writes 0", 36 om.omega_mix(ctypes.byref(r), 5, 1, cb, 3, db, 2, eb, 2, 7) == 0 and r.value == 0 and list(db) == [3, 32]) 37 db[0] = db[1] = 0 38 r = u64(SENTINEL) 39 check("mix negative long traps 9 after both writes, result unchanged", 40 om.omega_mix(ctypes.byref(r), 5, bits(-1), cb, 3, db, 2, eb, 2, 7) == 9 and r.value == SENTINEL and list(db) == [3, 32]) 41 db[0] = db[1] = 0 42 check("mix uchar conversion 325 traps 5, first write visible", 43 om.omega_mix(ctypes.byref(r), 5, 1, cb, 3, db, 2, eb, 2, 300) == 5 and r.value == SENTINEL and list(db) == [3, 0]) 44 # The binding refuses what the ABI cannot carry instead of truncating or writing into immutable bytes. 45 def refused(f): 46 try: 47 f() 48 except ctypes.ArgumentError: 49 return True 50 return False 51 check("scalar 2**64+3 refused, not truncated", refused(lambda: om.omega_madd(ctypes.byref(r), 2**64 + 3, 5, 7))) 52 check("scalar -1 refused (pass its bits)", refused(lambda: om.omega_madd(ctypes.byref(r), -1, 5, 7))) 53 check("scalar 2.0 refused", refused(lambda: om.omega_madd(ctypes.byref(r), 2.0, 5, 7))) 54 check("bytes for an edit uchar array refused", refused(lambda: om.omega_mix(ctypes.byref(r), 5, 1, cb, 3, bytes(2), 2, eb, 2, 7))) 55 check("bytes for an edit ulong array refused", refused(lambda: om.omega_fill(ctypes.byref(r), bytes(32), 4, 0))) 56 check("call after refusals still works", om.omega_madd(ctypes.byref(r), 3, 5, 7) == 0 and r.value == 22) 57 print(("FAIL " if fails else "PASS ") + "%d failure(s)" % fails) 58 sys.exit(fails != 0)