omega-bridge
Omega-first language interoperability: checked conversion backends and native language bindings
Brought by drakestapleton
omega-bridge / examples / deepinfer / callers / caller.py
| 1 | """Python caller for examples/deepinfer/flops_pairs.om: python3 -I caller.py BUILD_DIR. Every Omega |
| 2 | declaration comes from the generated omega_native.py; nothing is typed by hand here. Expected values: |
| 3 | evidence/deepinfer-flops-pairs/wolfram.md.""" |
| 4 | import ctypes |
| 5 | import sys |
| 6 | |
| 7 | sys.path.insert(0, sys.argv[1]) |
| 8 | import omega_native as om # noqa: E402 |
| 9 | |
| 10 | FAILS = 0 |
| 11 | |
| 12 | |
| 13 | def check(name, ok): |
| 14 | global FAILS |
| 15 | print("PASS" if ok else "FAIL", name) |
| 16 | FAILS += 0 if ok else 1 |
| 17 | |
| 18 | |
| 19 | VS = [ |
| 20 | ("floor_prefix", (9, 2), 0, 20), |
| 21 | ("capped_prefix", (9, 2, 3), 0, 18), |
| 22 | ("pairs", (3, 9, 2, 3), 0, 16), |
| 23 | ("pairs", (3, 9, 2, 0), 0, 18), |
| 24 | ("pairs", (0, 4096, 1, 512), 0, 1966336), |
| 25 | ("floor_prefix", (3037000499, 1), 0, 4611686016981624750), |
| 26 | ("capped_prefix", (4611686018427387904, 1, 2), 0, 9223372036854775807), |
| 27 | ("floor_prefix", (3037000500, 1), 1, 0), |
| 28 | ("capped_prefix", (4611686018427387905, 1, 2), 1, 0), |
| 29 | ("capped_prefix", (0, 4294967296, 2147483648), 1, 0), |
| 30 | ("floor_prefix", (-1, 1), 9, 0), |
| 31 | ("floor_prefix", (5, 0), 9, 0), |
| 32 | ("pairs", (5, 3, 1, 0), 9, 0), |
| 33 | ("pairs", (0, 3, 1, -1), 9, 0), |
| 34 | ] |
| 35 | SENTINEL = 0x5A5A5A5A5A5A5A5A |
| 36 | |
| 37 | check("ident: library == binding", om.omega2_ident_flops_pairs() == om.OMEGA2_IDENT_flops_pairs) |
| 38 | for fn, args, status, want in VS: |
| 39 | r = ctypes.c_uint64(SENTINEL) |
| 40 | st = getattr(om, "omega2_" + fn)(ctypes.byref(r), *(a & 0xFFFFFFFFFFFFFFFF for a in args)) |
| 41 | got = r.value - (1 << 64) if r.value >> 63 else r.value |
| 42 | check("%s %s -> status %d" % (fn, " ".join(map(str, args)), st), |
| 43 | st == status and (got == want if st == om.OMEGA_OK else r.value == SENTINEL)) |
| 44 | check("null result refused 32", om.omega2_pairs(None, 0, 3, 1, 0) == om.OMEGA_BAD_ARG) |
| 45 | try: |
| 46 | om.omega2_pairs(ctypes.byref(ctypes.c_uint64()), -1, 3, 1, 0) |
| 47 | check("negative int refused by the binding", False) |
| 48 | except ctypes.ArgumentError: |
| 49 | check("negative int refused by the binding (a long is passed as its two's-complement bits)", True) |
| 50 | sys.exit(1 if FAILS else 0) |