Skip to content

Commit

Permalink
Modernise some FFI tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
corsix committed Apr 23, 2016
1 parent 9c27a59 commit a82c499
Show file tree
Hide file tree
Showing 13 changed files with 169 additions and 162 deletions.
48 changes: 29 additions & 19 deletions test/lib/ffi/ffi_bit64.lua → test/lib/ffi/bit64.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,37 @@ typedef enum { ZZI = -1 } ienum_t;
typedef enum { ZZU } uenum_t;
]]

assert(tobit(0xfedcba9876543210ll) == 0x76543210)
assert(tobit(0xfedcba9876543210ull) == 0x76543210)
do --- smoke tobit
assert(tobit(0xfedcba9876543210ll) == 0x76543210)
assert(tobit(0xfedcba9876543210ull) == 0x76543210)
end

assert(tostring(band(1ll, 1, 1ll, -1)) == "1LL")
assert(tostring(band(1ll, 1, 1ull, -1)) == "1ULL")
do --- smoke band
assert(tostring(band(1ll, 1, 1ll, -1)) == "1LL")
assert(tostring(band(1ll, 1, 1ull, -1)) == "1ULL")
end

assert(shl(10ll, 2) == 40)
assert(shl(10, 2ll) == 40)
assert(shl(10ll, 2ll) == 40)
do --- smoke shl
assert(shl(10ll, 2) == 40)
assert(shl(10, 2ll) == 40)
assert(shl(10ll, 2ll) == 40)
end

assert(bit.tohex(0x123456789abcdef0LL) == "123456789abcdef0")
do --- smoke tohex
assert(bit.tohex(0x123456789abcdef0LL) == "123456789abcdef0")
end

for _,tp in ipairs{ "int", "ienum_t", "uenum_t", "int64_t", "uint64_t"} do
local x = ffi.new(tp, 10)
local y = tobit(x)
local z = band(x)
assert(type(y) == "number" and y == 10)
assert(type(z) == "cdata" and z == 10)
do --- tobit/band assorted C types
for _,tp in ipairs{"int", "ienum_t", "uenum_t", "int64_t", "uint64_t"} do
local x = ffi.new(tp, 10)
local y = tobit(x)
local z = band(x)
assert(type(y) == "number" and y == 10)
assert(type(z) == "cdata" and z == 10)
end
end

do
do --- tobit/band negative unsigned enum
local x = ffi.new("uenum_t", -10)
local y = tobit(x)
local z = band(x)
Expand All @@ -41,7 +51,7 @@ do
assert(z == 2^32-10)
end

do
do --- jit band/bor/bxor
local a = 0x123456789abcdef0LL
local y1, y2, y3, y4, y5, y6
for i=1,100 do
Expand Down Expand Up @@ -88,7 +98,7 @@ do
assert(y6 == 0x123456786543210fLL)
end

do
do --- jit shift/xor
local a, b = 0x123456789abcdef0LL, 0x31415926535898LL
for i=1,200 do
a = bxor(a, b); b = sar(b, 14) + shl(b, 50)
Expand All @@ -98,7 +108,7 @@ do
assert(b == -7993764627526027113LL)
end

do
do --- jit rotate/xor
local a, b = 0x123456789abcdef0LL, 0x31415926535898LL
for i=1,200 do
a = bxor(a, b); b = rol(b, 14)
Expand All @@ -108,7 +118,7 @@ do
assert(b == -6199148037344061526LL)
end

do
do --- jit all ops
local a, b = 0x123456789abcdef0LL, 0x31415926535898LL
for i=1,200 do
a = bxor(a, b); b = rol(b, a)
Expand Down
6 changes: 3 additions & 3 deletions test/lib/ffi/ffi_copy_fill.lua → test/lib/ffi/copy_fill.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local ffi = require("ffi")

do
do --- misc
local arr = ffi.typeof("char[11]")
local a = arr()
local b = arr()
Expand Down Expand Up @@ -43,14 +43,14 @@ do
assert(ffi.string(c, 5) == "AAAAA")
end

do
do --- jit char[10]
local a = ffi.new("char[10]", 64)
local x
for i=1,100 do a[0] = i; x = ffi.string(a, 10) end
assert(x == "d@@@@@@@@@")
end

do
do --- jit char[1]
local a = ffi.new("char[1]")
local x, y
for i=1,100 do
Expand Down
22 changes: 0 additions & 22 deletions test/lib/ffi/ffi_redir.lua

This file was deleted.

7 changes: 7 additions & 0 deletions test/lib/ffi/index
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
bit64.lua +bit
copy_fill.lua
jit_array.lua
jit_struct.lua
meta_tostring.lua
redir.lua
type_punning.lua
58 changes: 29 additions & 29 deletions test/lib/ffi/ffi_jit_array.lua → test/lib/ffi/jit_array.lua
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
local ffi = require("ffi")

local types = {
"int8_t", "uint8_t",
"int16_t", "uint16_t",
"int32_t", "uint32_t",
"int64_t", "uint64_t",
"float", "double",
}

for j,tp in ipairs(types) do
local t = ffi.new(tp.."[?]", 301)
for i=1,300 do t[i] = 1 end
for i=1,300 do assert(t[i] == 1) end
for i=1,300 do t[i] = t[i-1] end -- reassoc across PHIs, a[i-1] forwarding
for i=1,300 do assert(t[i] == 0) end
for i=1,300 do t[i] = i end
local x = 0
for i=1,300 do x = x + t[i] end
if tp == "int8_t" then assert(x == 862)
elseif tp == "uint8_t" then assert(x == 33630)
else assert(x == 45150) end
do --- smoke
local types = {
"int8_t", "uint8_t",
"int16_t", "uint16_t",
"int32_t", "uint32_t",
"int64_t", "uint64_t",
"float", "double",
}
for j,tp in ipairs(types) do
local t = ffi.new(tp.."[?]", 301)
for i=1,300 do t[i] = 1 end
for i=1,300 do assert(t[i] == 1) end
for i=1,300 do t[i] = t[i-1] end -- reassoc across PHIs, a[i-1] forwarding
for i=1,300 do assert(t[i] == 0) end
for i=1,300 do t[i] = i end
local x = 0
for i=1,300 do x = x + t[i] end
if tp == "int8_t" then assert(x == 862)
elseif tp == "uint8_t" then assert(x == 33630)
else assert(x == 45150) end
end
end

do
do --- int array pointer arithmetic
local a = ffi.new("int[?]", 101)
local p = a+1;
for i=1,100 do
Expand All @@ -35,7 +36,7 @@ do
for i=1,100 do assert((i+a)[0] == i) end -- pointer arithmetic
end

do
do --- double array pointer arithmetic
local a = ffi.new("double[?]", 101)
local p = a+1;
for i=1,100 do
Expand All @@ -46,7 +47,7 @@ do
for i=1,100 do assert((a+i)[0] == i) end -- pointer arithmetic
end

do
do --- double array pointer comparisons +bit
local a = ffi.new("double[?]", 201)
local p = a+3
for i=1,200 do local j = bit.band(i, 7); assert((a+j == p) == (j == 3)) end
Expand All @@ -55,36 +56,36 @@ do
for i=1,200 do assert((a+i <= p) == (i <= 100)) end
end

do
do --- constant offset in double array index
local a = ffi.new("double[?]", 100)
for i=1,100 do a[i-1LL] = i end
for i=1,100 do assert(a[100LL-i] == 101-i) end
end

do
do --- fixed index of minus one
local a = ffi.new("int[10]")
local p = a+1
local k = ffi.new("int", -1)
a[0] = 42
for i=1,100 do assert(p[-1] == 42); assert(p[k] == 42) end
end

do
do --- uint8_t array element comparisons
local a = ffi.new("uint8_t[?]", 256)
for i=0,255 do a[i] = i end
for i=1,255 do assert(a[i] >= 1) end
for i=0,254 do assert(a[i] <= 254) end
end

do
do --- int32_t array bit/bswap tricks +bit
local a = ffi.new("int32_t[?]", 256)
local tobit, bswap, shl = bit.tobit, bit.bswap, bit.lshift
for i=0,255 do a[i] = bswap(i+0x12345600) end
for i=0,255 do assert(a[i] == tobit(shl(i, 24)+0x00563412)) end
for i=0,255 do assert(bswap(a[i]) == tobit(i+0x12345600)) end
end

do
do --- int32_t shift/rotate/and +bit
local a = ffi.new("int32_t[?]", 256)
local shl, shr, rol, band = bit.lshift, bit.rshift, bit.rol, bit.band
for i=0,255 do a[i] = i + shl(i, 8) + shl(i, 16) end
Expand All @@ -101,4 +102,3 @@ do
for i=0,255 do assert(shl(band(a[i], 0x000000ff), 24) == shl(i, 24)) end
for i=0,255 do assert(shr(band(a[i], 0xffff0000), 16) == i) end
end

Loading

0 comments on commit a82c499

Please sign in to comment.