- tests/ffi.cat: load failure, missing symbol, invalid signature and return type (all -> nil), pow/floor/sqrt/abs/strlen conversions, void return -> nil, pointer spelling, callable library - 16 new assertions wired into run_tests.sh (69 total, all green) - (note: '!=' is not catscript syntax; the not-equal operator is '~=')
51 lines
1.5 KiB
Plaintext
51 lines
1.5 KiB
Plaintext
-- tests/ffi.cat -- edge cases for the `ffi` library.
|
|
--
|
|
-- Deterministic output (no getpid): loads, symbol binding, signatures,
|
|
-- return conversions, and the nil-on-error contract.
|
|
|
|
local m = ffi.load("libm.so.6")
|
|
local c = ffi.load("libc.so.6")
|
|
|
|
-- load failure / bad library -> nil
|
|
local bad = ffi.load("no_such_library_xyz.so.999")
|
|
print("bad load nil:", bad == nil)
|
|
print("bad load type:", type(bad))
|
|
|
|
-- symbol binding
|
|
local pow = m:func("pow", "double(double,double)")
|
|
print("pow bound:", pow ~= nil)
|
|
print("missing sym nil:", m:func("no_such_symbol_xyz", "int()") == nil)
|
|
|
|
-- invalid signature -> nil
|
|
print("bad sig nil:", m:func("pow", "not a signature") == nil)
|
|
print("bad rettype nil:", m:func("pow", "nope(double)") == nil)
|
|
|
|
-- return conversions
|
|
print("pow 2,10:", pow(2, 10))
|
|
print("pow 3,4:", pow(3, 4))
|
|
local floor = m:func("floor", "double(double)")
|
|
print("floor 3.7:", floor(3.7))
|
|
local sqrt = m:func("sqrt", "double(double)")
|
|
print("sqrt 144:", sqrt(144))
|
|
|
|
-- int return
|
|
local abs = c:func("abs", "int(int)")
|
|
print("abs -42:", abs(-42))
|
|
|
|
-- long + string arg
|
|
local strlen = c:func("strlen", "long(string)")
|
|
print("strlen abc:", strlen("abc"))
|
|
print("strlen empty:", strlen(""))
|
|
|
|
-- void return -> nil
|
|
local srand = c:func("srand", "void(uint32)")
|
|
print("void ret nil:", srand(1) == nil)
|
|
|
|
-- pointer-ish spelling accepted
|
|
local strchr = c:func("strchr", "string(string,int)")
|
|
print("strchr bound:", strchr ~= nil)
|
|
|
|
-- library is callable like :func
|
|
local cosf = m("cos", "double(double)")
|
|
print("cos 0:", cosf(0))
|