Skip to content

Commit

Permalink
Modernise more ffi tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
corsix committed Apr 24, 2016
1 parent a82c499 commit d7985d1
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 53 deletions.
15 changes: 6 additions & 9 deletions test/lib/ffi/ffi_err.lua → test/lib/ffi/err.lua
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
local ffi = require("ffi")

-- error in FFI metamethod: don't print metamethod frame.
do
do --- error in FFI metamethod: don't print metamethod frame.
local ok, err = xpcall(function()
local x = (1ll).foo
end, debug.traceback)
assert(ok == false)
assert(not string.find(err, "__index"))
end

-- tailcall in regular metamethod: keep metamethod frame.
do
do --- tailcall in regular metamethod: keep metamethod frame.
local ok, err = xpcall(function()
local t = setmetatable({}, {__index = function() return rawget("x") end })
local y = t[1]
Expand All @@ -19,13 +17,12 @@ do
assert(string.find(err, "__index"))
end

-- error in FFI metamethod: set correct PC.
do
do --- error in FFI metamethod: set correct PC.
ffi.cdef[[
typedef struct { int x; int y; } point;
point strchr(point* op1, point* op2);
typedef struct { int x; int y; } ffi_err_point;
ffi_err_point ffi_err_strchr(ffi_err_point* op1, ffi_err_point* op2) asm("strchr");
]]
local point = ffi.metatype("point", { __add = ffi.C.strchr })
local point = ffi.metatype("ffi_err_point", { __add = ffi.C.ffi_err_strchr })
local function foo()
local p = point{ 3, 4 }
local r = p + p
Expand Down
4 changes: 4 additions & 0 deletions test/lib/ffi/index
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
bit64.lua +bit
copy_fill.lua
err.lua
istype.lua
jit_array.lua
jit_complex.lua
jit_misc.lua
jit_struct.lua
meta_tostring.lua
redir.lua
Expand Down
33 changes: 16 additions & 17 deletions test/lib/ffi/ffi_istype.lua → test/lib/ffi/istype.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
local ffi = require("ffi")

ffi.cdef[[
typedef int arr_t[10];
typedef const arr_t carr_t;
typedef struct { int x; } struct_t;
]]

do
do --- 1
local void_t = ffi.typeof("void")
assert(ffi.istype(void_t, void_t))
assert(ffi.istype("const void", void_t))
Expand All @@ -15,7 +9,7 @@ do
assert(ffi.istype("double", 1.5) == false) -- 2nd arg is a number.
end

do
do --- 2
local i8_t = ffi.typeof("int8_t")
local u8_t = ffi.typeof("uint8_t")
local i32_t = ffi.typeof("int32_t")
Expand All @@ -32,7 +26,7 @@ do
assert(ffi.istype("long long", ffi.typeof("int64_t")))
end

do
do --- 3
local ptr_t = ffi.typeof("int *")
local p = ptr_t()
assert(ffi.istype(ptr_t, ptr_t) == true)
Expand All @@ -46,9 +40,15 @@ do
assert(ffi.istype("void *", ptr_t) == false)
end

do
local arr_t = ffi.typeof("arr_t")
local carr_t = ffi.typeof("carr_t")
do --- 4
ffi.cdef[[
typedef int istype_arr_t[10];
typedef const istype_arr_t istype_carr_t;
typedef struct { int x; } istype_struct_t;
]]

local arr_t = ffi.typeof("istype_arr_t")
local carr_t = ffi.typeof("istype_carr_t")
assert(ffi.istype(arr_t, arr_t) == true)
assert(ffi.istype("int[10]", arr_t) == true)

Expand All @@ -60,16 +60,16 @@ do
assert(ffi.istype("volatile int[10]", arr_t) == true)
assert(ffi.istype(carr_t, arr_t) == true)

local struct_t = ffi.typeof("struct_t")
local structp_t = ffi.typeof("struct_t *")
local struct_t = ffi.typeof("istype_struct_t")
local structp_t = ffi.typeof("istype_struct_t *")
assert(ffi.istype(struct_t, struct_t) == true)
assert(ffi.istype("const struct_t", struct_t) == true)
assert(ffi.istype("const istype_struct_t", struct_t) == true)
assert(ffi.istype("struct { int x; }", struct_t) == false)
assert(ffi.istype(struct_t, structp_t) == true) -- struct ptr is ok for struct.
assert(ffi.istype(structp_t, struct_t) == false)
end

do
do --- 5
local int_t = ffi.typeof("int")
local t = {}
for i=1,200 do t[i] = int_t() end
Expand All @@ -86,4 +86,3 @@ do
for i=1,200 do if not ffi.istype(t[i], int_t) then x = x + i end end
assert(x == 100)
end

28 changes: 13 additions & 15 deletions test/lib/ffi/ffi_jit_complex.lua → test/lib/ffi/jit_complex.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ local cx = ffi.typeof("complex")
local cxf = ffi.typeof("complex float")

ffi.cdef[[
typedef struct cc_t {
struct cc_t *next;
typedef struct jit_complex_chain_t {
struct jit_complex_chain_t *next;
complex c;
} cc_t;
} jit_complex_chain_t;
]]

do
do --- field access
local c = cx(1, 2)
local x
for i=1,100 do
Expand All @@ -19,8 +19,8 @@ do
assert(x == 3)
end

do
local cp = ffi.new("cc_t")
do --- one element circular chain, named indexing
local cp = ffi.new("jit_complex_chain_t")
local p = cp
p.next = p
p.c = cx(1, 2)
Expand All @@ -34,8 +34,8 @@ do
assert(y == 200)
end

do
local cp = ffi.new("cc_t")
do --- one element circular chain, array indexing
local cp = ffi.new("jit_complex_chain_t")
local p = cp
p.next = p
p.c = cx(1, 2)
Expand All @@ -49,7 +49,7 @@ do
assert(y == 200)
end

do
do --- one-arg initialiser
local ca = ffi.new("complex[?]", 101)
for i=1,100 do
ca[i] = cx(i) -- handled as init single
Expand All @@ -63,7 +63,7 @@ do
assert(y == 0)
end

do
do --- two-arg initialiser
local ca = ffi.new("complex[?]", 101)
for i=1,100 do
ca[i] = cx(i, -i)
Expand All @@ -77,7 +77,7 @@ do
assert(y == -5050)
end

do
do --- float<>double conversions
local ca = ffi.new("complex[?]", 101)
local caf = ffi.new("complex float[?]", 101)
for i=1,100 do
Expand All @@ -93,7 +93,7 @@ do
assert(y == -2*5050)
end

do
do --- Complex struct field
local s = ffi.new("struct { complex x;}")
for i=1,100 do
s.x = 12.5i
Expand All @@ -102,10 +102,8 @@ do
assert(s.x.im == 12.5)
end

-- Index overflow for complex is ignored
do
do --- Index overflow for complex is ignored
local c = cx(1, 2)
local x
for i=1e7,1e7+100 do x = c[i] end
end

23 changes: 11 additions & 12 deletions test/lib/ffi/ffi_jit_misc.lua → test/lib/ffi/jit_misc.lua
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
local ffi = require("ffi")

do
do --- errno
ffi.errno(42)
local x = 0
for i=1,100 do x = x + ffi.errno() end
assert(x == 4200)
ffi.errno(0)
end

do
do --- string
local a = ffi.new("uint8_t[?]", 101)
for i=0,99 do a[i] = i end
local s
Expand All @@ -18,7 +18,7 @@ do
assert(s == "Z[\\]^_`abc")
end

do
do --- fill
local a = ffi.new("uint8_t[?]", 100)
local x = 0
for i=0,90 do x = x + a[i]; ffi.fill(a+i, 10, i); x = x + a[i] end
Expand All @@ -31,7 +31,7 @@ do
assert(b[103] == (ffi.abi("le") and 0x343434 or 0x34343400))
end

do
do --- copy array elements
local a = ffi.new("uint8_t[?]", 100)
local b = ffi.new("uint8_t[?]", 100)
for i=0,99 do b[i] = i end
Expand All @@ -43,15 +43,15 @@ do
assert(x == 4095)
end

do
do --- copy from string
local a = ffi.new("uint8_t[?]", 100, 42)
for i=0,90 do ffi.copy(a+i, "abc") end
local x = 0
for i=0,99 do x = x + a[i] end
assert(x == 9276)
end

do
do --- copy structures
local tp = ffi.typeof("struct { int x, y; }")
local a = tp(1, 2)
local b = tp(3, 4)
Expand All @@ -63,7 +63,7 @@ do
assert(x == 5050)
end

do
do --- init struct from first field, complex
local tp = ffi.typeof("struct { complex x, y; }")
local cx = ffi.typeof("complex")
local a = tp(cx(1, 2), cx(3, 4))
Expand All @@ -72,15 +72,15 @@ do
assert(x == 5050)
end

do
do --- int array as parameterised type
local tp = ffi.typeof("int[10]")
local a = tp(42)
local b = ffi.new(ffi.typeof("struct { $ x; }", tp))
for i=1,100 do b.x = a end
assert(b.x[0] == 42 and b.x[9] == 42)
end

do
do --- double array as parameterised type
local tp = ffi.typeof("double[5]")
local a = tp(42)
local b = ffi.new(ffi.typeof("struct { $ x; }", tp))
Expand All @@ -91,7 +91,7 @@ do
assert(b.x[0] == 42 and b.x[4] == 42)
end

do
do --- abi
local x, y
for i=1,100 do x = ffi.abi("32bit"); y = ffi.abi("64bit") end
assert(x == ffi.abi("32bit"))
Expand All @@ -102,9 +102,8 @@ do
end
end

do
do --- typeof constructed typeof
local ct = ffi.typeof("struct { int x; }")
local cd = ct()
for i=1,100 do assert(ffi.typeof(cd) == ct) end
end

0 comments on commit d7985d1

Please sign in to comment.