interplane.Get Omega
Read in your language

omega-bridge

Omega-first language interoperability: checked conversion backends and native language bindings

Brought by drakestapleton

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).
2import ctypes, os, sys
3sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) # python3 -I drops this folder; the binding is beside us
4import omega_native as om # every Omega declaration: generated by tools/native, nothing typed by hand
5u64 = ctypes.c_uint64
6SENTINEL = 6148914691236517205
7fails = 0
8def check(name, ok):
9 global fails
10 print(("PASS " if ok else "FAIL ") + name)
11 fails += not ok
12def bits(v): # signed Python int -> canonical 64-bit two's-complement bits
13 return v & (2**64 - 1)
14def signed(b):
15 return b - 2**64 if b >= 2**63 else b
16r = u64(0)
17check("madd 3*5+7 = 22", om.omega_madd(ctypes.byref(r), 3, 5, 7) == 0 and r.value == 22)
18check("madd wraps = 3", om.omega_madd(ctypes.byref(r), 2**64 - 1, 2, 5) == 0 and r.value == 3)
19check("madd large", om.omega_madd(ctypes.byref(r), 1311768467463790320, 18364758544493064720, 1085102592571150095) == 0
20 and r.value == 3637949782307626511)
21check("madd signed bits = -13", om.omega_madd(ctypes.byref(r), bits(-3), 5, 2) == 0 and signed(r.value) == -13)
22r = u64(SENTINEL)
23check("smadd overflow traps 1, result unchanged", om.omega_smadd(ctypes.byref(r), 2**62, 4, 0) == 1 and r.value == SENTINEL)
24buf = (u64 * 4)()
25check("fill k=1 traps 3, result unchanged", om.omega_fill(ctypes.byref(r), buf, 4, 1) == 3 and r.value == SENTINEL)
26check("writes before the trap stay visible", list(buf) == [11, 22, 0, 0])
27check("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])
28check("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.
31cb = bytes([1, 2, 3]) # view uchar: bytes
32db = (ctypes.c_ubyte * 2)() # edit uchar: a writable ctypes array
33eb = (u64 * 2)(10, 20) # view ulong: a ctypes array
34r = u64(SENTINEL)
35check("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])
37db[0] = db[1] = 0
38r = u64(SENTINEL)
39check("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])
41db[0] = db[1] = 0
42check("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.
45def refused(f):
46 try:
47 f()
48 except ctypes.ArgumentError:
49 return True
50 return False
51check("scalar 2**64+3 refused, not truncated", refused(lambda: om.omega_madd(ctypes.byref(r), 2**64 + 3, 5, 7)))
52check("scalar -1 refused (pass its bits)", refused(lambda: om.omega_madd(ctypes.byref(r), -1, 5, 7)))
53check("scalar 2.0 refused", refused(lambda: om.omega_madd(ctypes.byref(r), 2.0, 5, 7)))
54check("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)))
55check("bytes for an edit ulong array refused", refused(lambda: om.omega_fill(ctypes.byref(r), bytes(32), 4, 0)))
56check("call after refusals still works", om.omega_madd(ctypes.byref(r), 3, 5, 7) == 0 and r.value == 22)
57print(("FAIL " if fails else "PASS ") + "%d failure(s)" % fails)
58sys.exit(fails != 0)