tests: dedicated ffi edge-case battery
- 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 '~=')
This commit is contained in:
1 parent
c319a0986d
commit
05f31aed68
2 files changed
+70
No files matched your search
@@ -0,0 +1,50 @@
|
||||
-- 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))
|
||||
@@ -115,6 +115,26 @@ check "ffi-sqrt" "sqrt(144) = 12" "`echo "$out" | sed -n 2p`"
|
||||
check "ffi-getpid" "getpid > 0: true" "`echo "$out" | sed -n 3p`"
|
||||
check "ffi-strlen" "strlen: 9" "`echo "$out" | sed -n 4p`"
|
||||
check "ffi-abs" "abs(-42) = 42" "`echo "$out" | sed -n 5p`"
|
||||
|
||||
# ---- ffi edge cases (deterministic) ---------------------------------
|
||||
|
||||
out=`$BIN tests/ffi.cat`
|
||||
check "ffi-badload-nil" "bad load nil: true" "`echo "$out" | sed -n 1p`"
|
||||
check "ffi-badload-type" "bad load type: nil" "`echo "$out" | sed -n 2p`"
|
||||
check "ffi-bound" "pow bound: true" "`echo "$out" | sed -n 3p`"
|
||||
check "ffi-missing-sym" "missing sym nil: true" "`echo "$out" | sed -n 4p`"
|
||||
check "ffi-badsig" "bad sig nil: true" "`echo "$out" | sed -n 5p`"
|
||||
check "ffi-badret" "bad rettype nil: true" "`echo "$out" | sed -n 6p`"
|
||||
check "ffi-pow-2-10" "pow 2,10: 1024" "`echo "$out" | sed -n 7p`"
|
||||
check "ffi-pow-3-4" "pow 3,4: 81" "`echo "$out" | sed -n 8p`"
|
||||
check "ffi-floor" "floor 3.7: 3" "`echo "$out" | sed -n 9p`"
|
||||
check "ffi-sqrt-144" "sqrt 144: 12" "`echo "$out" | sed -n 10p`"
|
||||
check "ffi-abs-neg" "abs -42: 42" "`echo "$out" | sed -n 11p`"
|
||||
check "ffi-strlen-abc" "strlen abc: 3" "`echo "$out" | sed -n 12p`"
|
||||
check "ffi-strlen-empty" "strlen empty: 0" "`echo "$out" | sed -n 13p`"
|
||||
check "ffi-void-nil" "void ret nil: true" "`echo "$out" | sed -n 14p`"
|
||||
check "ffi-strchr-bound" "strchr bound: true" "`echo "$out" | sed -n 15p`"
|
||||
check "ffi-lib-callable" "cos 0: 1" "`echo "$out" | sed -n 16p`"
|
||||
# ---- bytecode dump ---------------------------------------------------
|
||||
|
||||
out=`$BIN --dump examples/hello.cat`
|
||||
|
||||
Reference in new issue
Block a user