omega-bridge
Omega-first language interoperability: checked conversion backends and native language bindings
Brought by drakestapleton
omega-bridge / tests / native / caller.zig
| 1 | // Zig caller for the omega-bridge native call ABI v1 test library. Written against Zig 0.13. |
| 2 | // Run with Zig 0.13.0 by tests/native/run.sh. Every Omega declaration comes from omega_native.zig, |
| 3 | // generated by tools/native beside this file; nothing is typed by hand here. |
| 4 | const std = @import("std"); |
| 5 | const om = @import("omega_native.zig"); |
| 6 | |
| 7 | var fails: u32 = 0; |
| 8 | fn check(name: []const u8, ok: bool) void { |
| 9 | std.debug.print("{s} {s}\n", .{ if (ok) "PASS" else "FAIL", name }); |
| 10 | if (!ok) fails += 1; |
| 11 | } |
| 12 | |
| 13 | pub fn main() void { |
| 14 | const sentinel: u64 = 6148914691236517205; |
| 15 | var r: u64 = 0; |
| 16 | check("madd 3*5+7 = 22", om.omega_madd(&r, 3, 5, 7) == 0 and r == 22); |
| 17 | check("madd wraps = 3", om.omega_madd(&r, std.math.maxInt(u64), 2, 5) == 0 and r == 3); |
| 18 | check("madd large", om.omega_madd(&r, 1311768467463790320, 18364758544493064720, 1085102592571150095) == 0 and r == 3637949782307626511); |
| 19 | check("madd signed bits = -13", om.omega_madd(&r, @bitCast(@as(i64, -3)), 5, 2) == 0 and @as(i64, @bitCast(r)) == -13); |
| 20 | r = sentinel; |
| 21 | check("smadd overflow traps 1, result unchanged", om.omega_smadd(&r, 1 << 62, 4, 0) == 1 and r == sentinel); |
| 22 | var buf = [_]u64{ 0, 0, 0, 0 }; |
| 23 | check("fill k=1 traps 3, result unchanged", om.omega_fill(&r, &buf, 4, 1) == 3 and r == sentinel); |
| 24 | check("writes before the trap stay visible", buf[0] == 11 and buf[1] == 22 and buf[2] == 0); |
| 25 | check("fill k=0 = 3", om.omega_fill(&r, &buf, 4, 0) == 0 and r == 3 and buf[2] == 33); |
| 26 | check("null result refused 32", om.omega_madd(null, 3, 5, 7) == 32); |
| 27 | // mix: six parameters (ulong, long, view uchar 3, edit uchar 2, view ulong 2, ulong), no result. |
| 28 | // 5 + 20 + 7 = 32 (calc:08302bc4); 5 + 20 + 300 is 70 over 255 (calc:a6a8bb76), so the conversion traps 5. |
| 29 | const cb = [_]u8{ 1, 2, 3 }; |
| 30 | var db = [_]u8{ 0, 0 }; |
| 31 | const eb = [_]u64{ 10, 20 }; |
| 32 | r = sentinel; |
| 33 | check("mix six parameters, void result writes 0", om.omega_mix(&r, 5, 1, &cb, 3, &db, 2, &eb, 2, 7) == 0 and r == 0 and db[0] == 3 and db[1] == 32); |
| 34 | db = .{ 0, 0 }; |
| 35 | r = sentinel; |
| 36 | check("mix negative long traps 9 after both writes, result unchanged", om.omega_mix(&r, 5, @bitCast(@as(i64, -1)), &cb, 3, &db, 2, &eb, 2, 7) == 9 and r == sentinel and db[0] == 3 and db[1] == 32); |
| 37 | db = .{ 0, 0 }; |
| 38 | check("mix uchar conversion 325 traps 5, first write visible", om.omega_mix(&r, 5, 1, &cb, 3, &db, 2, &eb, 2, 300) == 5 and r == sentinel and db[0] == 3 and db[1] == 0); |
| 39 | std.debug.print("{s} {d} failure(s)\n", .{ if (fails > 0) "FAIL" else "PASS", fails }); |
| 40 | std.process.exit(if (fails > 0) 1 else 0); |
| 41 | } |