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.cpp

50 lines · 2634 bytes Raw Blame History
1// C++ caller for the omega-bridge native call ABI v1 test library.
2#include <cstdint>
3#include <cstdio>
4#include <vector>
5
6#include "omega_native.h" // every Omega declaration: generated by tools/native, nothing typed by hand
7
8static int fails;
9static void check(const char *name, bool ok)
10{
11 std::printf("%s %s\n", ok ? "PASS" : "FAIL", name);
12 fails += !ok;
13}
14
15int main()
16{
17 const std::uint64_t sentinel = 6148914691236517205ull;
18 std::uint64_t r = 0;
19 check("madd 3*5+7 = 22", omega_madd(&r, 3, 5, 7) == 0 && r == 22);
20 check("madd wraps = 3", omega_madd(&r, UINT64_MAX, 2, 5) == 0 && r == 3);
21 check("madd large", omega_madd(&r, 1311768467463790320ull, 18364758544493064720ull, 1085102592571150095ull) == 0 &&
22 r == 3637949782307626511ull);
23 check("madd signed bits = -13", omega_madd(&r, static_cast<std::uint64_t>(std::int64_t{-3}), 5, 2) == 0 &&
24 static_cast<std::int64_t>(r) == -13);
25 r = sentinel;
26 check("smadd overflow traps 1, result unchanged", omega_smadd(&r, 4611686018427387904ull, 4, 0) == 1 && r == sentinel);
27 std::vector<std::uint64_t> buf(4, 0);
28 check("fill k=1 traps 3, result unchanged", omega_fill(&r, buf.data(), buf.size(), 1) == 3 && r == sentinel);
29 check("writes before the trap stay visible", buf[0] == 11 && buf[1] == 22 && buf[2] == 0);
30 check("fill k=0 = 3", omega_fill(&r, buf.data(), buf.size(), 0) == 0 && r == 3 && buf[2] == 33);
31 check("wrong length refused 32", omega_fill(&r, buf.data(), 3, 0) == 32);
32 // mix: six parameters (ulong, long, view uchar 3, edit uchar 2, view ulong 2, ulong), no result.
33 // 5 + 20 + 7 = 32 (calc:08302bc4); 5 + 20 + 300 is 70 over 255 (calc:a6a8bb76), so the conversion traps 5.
34 const unsigned char cb[3] = {1, 2, 3};
35 unsigned char db[2] = {0, 0};
36 const std::uint64_t eb[2] = {10, 20};
37 r = sentinel;
38 check("mix six parameters, void result writes 0",
39 omega_mix(&r, 5, 1, cb, 3, db, 2, eb, 2, 7) == 0 && r == 0 && db[0] == 3 && db[1] == 32);
40 db[0] = db[1] = 0;
41 r = sentinel;
42 check("mix negative long traps 9 after both writes, result unchanged",
43 omega_mix(&r, 5, static_cast<std::uint64_t>(std::int64_t{-1}), cb, 3, db, 2, eb, 2, 7) == 9 && r == sentinel &&
44 db[0] == 3 && db[1] == 32);
45 db[0] = db[1] = 0;
46 check("mix uchar conversion 325 traps 5, first write visible",
47 omega_mix(&r, 5, 1, cb, 3, db, 2, eb, 2, 300) == 5 && r == sentinel && db[0] == 3 && db[1] == 0);
48 std::printf("%s %d failure(s)\n", fails ? "FAIL" : "PASS", fails);
49 return fails != 0;
50}