Skip to content

Commit

Permalink
Add and organize ffi tests (lune-org#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
qwreey committed Nov 9, 2024
1 parent b191218 commit 902f5f9
Show file tree
Hide file tree
Showing 13 changed files with 97 additions and 57 deletions.
10 changes: 9 additions & 1 deletion crates/lune/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,18 @@ create_tests! {
ffi_external_pointer_pointer_write: "ffi/external_pointer/pointerWrite",
ffi_external_print_hello_world: "ffi/external_print/helloWorld",
ffi_external_struct_ab: "ffi/external_struct/ab",
ffi_pretty_print_arr: "ffi/pretty_print/arr",
ffi_pretty_print_box: "ffi/pretty_print/box",
ffi_pretty_print_fn: "ffi/pretty_print/fn",
ffi_pretty_print_ptr: "ffi/pretty_print/ptr",
ffi_pretty_print_struct: "ffi/pretty_print/struct",
ffi_pretty_print_type: "ffi/pretty_print/type",
ffi_types_arr: "ffi/types/arr",
ffi_types_ptr: "ffi/types/ptr",
ffi_types_struct: "ffi/types/struct",
ffi_cast: "ffi/cast",
ffi_free: "ffi/free",
ffi_is_integer: "ffi/isInteger",
ffi_pretty_print: "ffi/prettyPrint",
ffi_read_boundary: "ffi/readBoundary",
ffi_write_boundary: "ffi/writeBoundary",
}
Expand Down
17 changes: 6 additions & 11 deletions tests/ffi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,15 @@ gcc for library compiling (for external-\*)

**Luau-side**

- [x] [isInteger](./isInteger)
- [x] [isInteger](./isInteger.luau)
- [x] [cast](./cast.luau)
- [x] [write_boundary](./write_boundary.luau)
- [x] [read_boundary](./read_boundary.luau)
- [x] [free](./free.luau)

- [ ] [pretty_print](./pretty_print)

> need box, ref test
- [ ] [write_boundary](./write_boundary.luau)
**Pretty Print**

> Failed, need fix
- [ ] [read_boundary](./read_boundary.luau)

> Failed, need fix
- [ ] [pretty_print](./pretty_print)

## Benchmark Results

Expand Down
32 changes: 0 additions & 32 deletions tests/ffi/prettyPrint.luau

This file was deleted.

6 changes: 6 additions & 0 deletions tests/ffi/pretty_print/arr.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
local ffi = require("@lune/ffi")
local c = ffi.c

assert(typeof(c.int:arr(5)) :: string == "CArrInfo")
assert(tostring(c.int:arr(5)) == " int, length = 5 ")
assert(tostring(c.int:ptr():arr(5)) == " <CPtrInfo(int)>, length = 5 ")
4 changes: 4 additions & 0 deletions tests/ffi/pretty_print/box.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
local ffi = require("@lune/ffi")
local half_back = 0b_0000_0000_0000_0000_0000_0000_1111_1111

print(ffi.i32:box(half_back))
13 changes: 13 additions & 0 deletions tests/ffi/pretty_print/fn.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
local ffi = require("@lune/ffi")
local c = ffi.c

assert(typeof(c.fn({ c.int }, c.int)) :: string == "CFnInfo")
assert(tostring(c.fn({ c.int }, c.int)) == " (int) -> int ")
assert(tostring(c.fn({ c.int, ffi.f32 }, c.int)) == " (int, f32) -> int ")
assert(tostring(c.fn({ c.int:ptr() }, c.int)) == " (<CPtrInfo(int)>) -> int ")
assert(tostring(c.fn({ c.int }, c.int:ptr())) == " (int) -> <CPtrInfo(int)> ")
assert(tostring(c.fn({ c.int:ptr() }, c.int:ptr())) == " (<CPtrInfo(int)>) -> <CPtrInfo(int)> ")
assert(
tostring(c.fn({ c.int:ptr(), c.int:ptr() }, c.int:ptr()))
== " (<CPtrInfo(int)>, <CPtrInfo(int)>) -> <CPtrInfo(int)> "
)
6 changes: 6 additions & 0 deletions tests/ffi/pretty_print/ptr.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
local ffi = require("@lune/ffi")
local c = ffi.c

assert(typeof(c.int:ptr()) :: string == "CPtrInfo")
assert(tostring(c.int:ptr()) == "int")
assert(tostring(c.int:arr(5):ptr()) == " <CArrInfo( int, length = 5 )> ")
8 changes: 8 additions & 0 deletions tests/ffi/pretty_print/struct.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
local ffi = require("@lune/ffi")
local c = ffi.c

assert(typeof(c.struct({ c.int, c.char })) :: string == "CStructInfo")
assert(
tostring(c.struct({ c.int, c.char:ptr() }))
== ` int, <CPtrInfo(char)>, size = {c.struct({ c.int, c.char:ptr() }).size} `
)
6 changes: 6 additions & 0 deletions tests/ffi/pretty_print/type.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
local ffi = require("@lune/ffi")
local c = ffi.c

assert(typeof(c.int) :: string == "CTypeInfo")
assert(tostring(c.int) == "int")
assert(tostring(ffi.i32) == "i32")
19 changes: 19 additions & 0 deletions tests/ffi/types/arr.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
local ffi = require("@lune/ffi")
local ok

local arr = ffi.i32:arr(4)

assert(rawequal(arr.length, 4), "length assersion failed, arr.length should be 4")
assert(rawequal(arr.inner, ffi.i32), "inner assersion failed, arr.inner should be ffi.i32")

-- offset(2) success
ok = pcall(function()
arr:offset(2)
end)
assert(ok, "assersion failed, arr:offset(2) should success")

-- offset(4) success
ok = pcall(function()
arr:offset(4)
end)
assert(not ok, "assersion failed, arr:offset(4) should fail")
Empty file removed tests/ffi/types/fn.luau
Empty file.
17 changes: 8 additions & 9 deletions tests/ffi/types/ptr.luau
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
--!nocheck
--!nolint

local ffi = require("@lune/ffi")

-- ptr size test
assert(
ffi.i32:ptr().size == ffi.i64:ptr().size,
"All of Ptr.size must be same.\n" .. "ffi.i32:ptr().size == ffi.i64:ptr().size failed"
"size assersion failed, size of pointer should be same with each other (ffi.i32:ptr().size == ffi.i64:ptr().size)"
)

-- inner test
local i32ptr = ffi.i32:ptr()
assert(
rawequal(ffi.i32, i32ptr.inner),
"Ptr.inner must be same with their parent\n" .. "raweq ffi.i32 == ffi.i32:ptr().inner failed"
"inner assersion failed, inner field must be same with their parent"
.. " (ffi.i32 == ffi.i32:ptr().inner)"
)
assert(
rawequal(i32ptr, i32ptr:ptr().inner),
"Ptr.inner must be same with their parent\n" .. "raweq i32ptr == i32ptr:ptr().inner failed"
"inner assersion failed, inner field must be same with their parent"
.. " (i32ptr == i32ptr:ptr().inner)"
)
assert(
rawequal(i32ptr, i32ptr:ptr().inner:ptr().inner:ptr().inner),
"Ptr.inner must be same with their parent\n"
.. "raweq i32ptr == i32ptr:ptr().inner:ptr().inner:ptr().inner failed"
"inner assersion failed, inner field must be same with their parent"
.. " (i32ptr == i32ptr:ptr().inner:ptr().inner:ptr().inner)"
)

-- deep ptr test
local ok, err = pcall(function()
i32ptr:ptr():ptr():ptr():ptr():ptr():ptr():ptr()
end)
assert(ok, `Deep ptr test failed.\n{err}`)
assert(ok, "deep ptr assersion failed\n" .. (err or ""))
16 changes: 12 additions & 4 deletions tests/ffi/types/struct.luau
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
--!nocheck
--!nolint

local ffi = require("@lune/ffi")
local ok

local i32ptr = ffi.i32:ptr()
local struct = ffi.struct({ i32ptr, ffi.i32 })
local struct = ffi.c.struct({ i32ptr, ffi.i32 })

assert(rawequal(struct:field(0), i32ptr), "Struct get field failed")
assert(rawequal(struct:field(1), ffi.i32), "Struct get field failed")

-- offset(2) should fail
ok = pcall(function()
struct:offset(2)
end)
assert(not ok, "assersion failed, struct:offset(2) should fail")

-- field(2) should fail
ok = pcall(function()
struct:field(2)
end)
assert(not ok, "assersion failed, struct:field(2) should fail")

0 comments on commit 902f5f9

Please sign in to comment.