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

61 lines · 2657 bytes Raw Blame History
1// Go caller for the omega-bridge native call ABI v1 test library.
2// Run with Go 1.27.3 by tests/native/run.sh. Every Omega declaration comes from the omeganative
3// package (omega_native.go, generated by tools/native); this file has no cgo of its own.
4package main
5
6import (
7 "fmt"
8 "os"
9 "unsafe"
10
11 on "omegacaller/omeganative"
12)
13
14var fails int
15
16func check(name string, ok bool) {
17 if ok {
18 fmt.Println("PASS", name)
19 } else {
20 fmt.Println("FAIL", name)
21 fails++
22 }
23}
24
25func main() {
26 const sentinel = uint64(6148914691236517205)
27 var r uint64
28 check("madd 3*5+7 = 22", on.Omega_madd(&r, 3, 5, 7) == 0 && r == 22)
29 check("madd wraps = 3", on.Omega_madd(&r, ^uint64(0), 2, 5) == 0 && r == 3)
30 check("madd large", on.Omega_madd(&r, 1311768467463790320, 18364758544493064720, 1085102592571150095) == 0 && r == 3637949782307626511)
31 m3 := int64(-3)
32 check("madd signed bits = -13", on.Omega_madd(&r, uint64(m3), 5, 2) == 0 && int64(r) == -13)
33 r = sentinel
34 check("smadd overflow traps 1, result unchanged", on.Omega_smadd(&r, 1<<62, 4, 0) == 1 && r == sentinel)
35 var buf [4]uint64 // Go memory holding no Go pointers may be handed to C (cgo pointer rules)
36 check("fill k=1 traps 3, result unchanged", on.Omega_fill(&r, unsafe.Pointer(&buf[0]), 4, 1) == 3 && r == sentinel)
37 check("writes before the trap stay visible", buf[0] == 11 && buf[1] == 22 && buf[2] == 0)
38 check("fill k=0 = 3", on.Omega_fill(&r, unsafe.Pointer(&buf[0]), 4, 0) == 0 && r == 3 && buf[2] == 33)
39 check("null result refused 32", on.Omega_madd(nil, 3, 5, 7) == 32)
40 // mix: six parameters (ulong, long, view uchar 3, edit uchar 2, view ulong 2, ulong), no result.
41 // 5 + 20 + 7 = 32 (calc:08302bc4); 5 + 20 + 300 is 70 over 255 (calc:a6a8bb76), so the conversion traps 5.
42 cb := [3]byte{1, 2, 3}
43 var db [2]byte
44 eb := [2]uint64{10, 20}
45 r = sentinel
46 check("mix six parameters, void result writes 0",
47 on.Omega_mix(&r, 5, 1, unsafe.Pointer(&cb[0]), 3, unsafe.Pointer(&db[0]), 2, unsafe.Pointer(&eb[0]), 2, 7) == 0 && r == 0 && db == [2]byte{3, 32})
48 db = [2]byte{}
49 r = sentinel
50 m1 := int64(-1)
51 check("mix negative long traps 9 after both writes, result unchanged",
52 on.Omega_mix(&r, 5, uint64(m1), unsafe.Pointer(&cb[0]), 3, unsafe.Pointer(&db[0]), 2, unsafe.Pointer(&eb[0]), 2, 7) == 9 && r == sentinel && db == [2]byte{3, 32})
53 db = [2]byte{}
54 check("mix uchar conversion 325 traps 5, first write visible",
55 on.Omega_mix(&r, 5, 1, unsafe.Pointer(&cb[0]), 3, unsafe.Pointer(&db[0]), 2, unsafe.Pointer(&eb[0]), 2, 300) == 5 && r == sentinel && db == [2]byte{3, 0})
56 if fails > 0 {
57 fmt.Println("FAIL", fails, "failure(s)")
58 os.Exit(1)
59 }
60 fmt.Println("PASS 0 failure(s)")
61}