From 7897e7fff3f04e65838c316ee9caf27f78e9f76b Mon Sep 17 00:00:00 2001 From: Peter Cawley Date: Tue, 11 Oct 2016 20:51:35 -0700 Subject: [PATCH] Add tests for allocation of variable-sized cdata --- test/lib/ffi/cdata_var.lua | 41 ++++++++++++++++++++++++++++++++++++++ test/lib/ffi/index | 1 + 2 files changed, 42 insertions(+) create mode 100644 test/lib/ffi/cdata_var.lua diff --git a/test/lib/ffi/cdata_var.lua b/test/lib/ffi/cdata_var.lua new file mode 100644 index 0000000000..d388c00233 --- /dev/null +++ b/test/lib/ffi/cdata_var.lua @@ -0,0 +1,41 @@ +local ffi = require("ffi") + +do --- byte array allocations + local typ = ffi.typeof"uint8_t[?]" + for i = 4, 20 do + for d = -3, 3 do + local sz = 2^i + d + assert(ffi.sizeof(typ, sz) == sz) + local mem = ffi.new(typ, sz) + assert(ffi.sizeof(mem) == sz) + mem[0] = 0x21 + mem[1] = 0x32 + mem[2] = 0x43 + mem[sz-3] = 0x54 + mem[sz-2] = 0x65 + mem[sz-1] = 0x76 + assert(mem[0] == 0x21) + assert(mem[1] == 0x32) + assert(mem[2] == 0x43) + assert(mem[sz-3] == 0x54) + assert(mem[sz-2] == 0x65) + assert(mem[sz-1] == 0x76) + end + end +end + +do --- int array allocations + local typ = ffi.typeof"int32_t[?]" + for i = 2, 17 do + for d = -2, 2 do + local sz = 2^i + d + assert(ffi.sizeof(typ, sz) == sz*4) + local mem = ffi.new(typ, sz) + assert(ffi.sizeof(mem) == sz*4) + mem[0] = -3 + mem[sz-1] = -4 + assert(mem[0] == -3) + assert(mem[sz-1] == -4) + end + end +end diff --git a/test/lib/ffi/index b/test/lib/ffi/index index bda633000d..59e36dd8b0 100644 --- a/test/lib/ffi/index +++ b/test/lib/ffi/index @@ -1,4 +1,5 @@ bit64.lua +luajit>=2.1 +cdata_var.lua copy_fill.lua err.lua istype.lua