From 902f5f960b99f85061e9ecbc4647996501a667fc Mon Sep 17 00:00:00 2001 From: qwreey Date: Sat, 9 Nov 2024 12:46:20 +0000 Subject: [PATCH] Add and organize ffi tests (#243) --- crates/lune/src/tests.rs | 10 +++++++++- tests/ffi/README.md | 17 ++++++---------- tests/ffi/prettyPrint.luau | 32 ------------------------------ tests/ffi/pretty_print/arr.luau | 6 ++++++ tests/ffi/pretty_print/box.luau | 4 ++++ tests/ffi/pretty_print/fn.luau | 13 ++++++++++++ tests/ffi/pretty_print/ptr.luau | 6 ++++++ tests/ffi/pretty_print/struct.luau | 8 ++++++++ tests/ffi/pretty_print/type.luau | 6 ++++++ tests/ffi/types/arr.luau | 19 ++++++++++++++++++ tests/ffi/types/fn.luau | 0 tests/ffi/types/ptr.luau | 17 ++++++++-------- tests/ffi/types/struct.luau | 16 +++++++++++---- 13 files changed, 97 insertions(+), 57 deletions(-) delete mode 100644 tests/ffi/prettyPrint.luau create mode 100644 tests/ffi/pretty_print/arr.luau create mode 100644 tests/ffi/pretty_print/box.luau create mode 100644 tests/ffi/pretty_print/fn.luau create mode 100644 tests/ffi/pretty_print/ptr.luau create mode 100644 tests/ffi/pretty_print/struct.luau create mode 100644 tests/ffi/pretty_print/type.luau delete mode 100644 tests/ffi/types/fn.luau diff --git a/crates/lune/src/tests.rs b/crates/lune/src/tests.rs index 0599f4a3..4d06e1b1 100644 --- a/crates/lune/src/tests.rs +++ b/crates/lune/src/tests.rs @@ -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", } diff --git a/tests/ffi/README.md b/tests/ffi/README.md index de366fad..77ed2693 100644 --- a/tests/ffi/README.md +++ b/tests/ffi/README.md @@ -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 diff --git a/tests/ffi/prettyPrint.luau b/tests/ffi/prettyPrint.luau deleted file mode 100644 index 6019089e..00000000 --- a/tests/ffi/prettyPrint.luau +++ /dev/null @@ -1,32 +0,0 @@ -local ffi = require("@lune/ffi") -local c = ffi.c - -assert(typeof(c.int) :: string == "CTypeInfo") -assert(tostring(c.int) == "int") - -assert(typeof(c.int:ptr()) :: string == "CPtrInfo") -assert(tostring(c.int:ptr()) == "int") -assert(tostring(c.int:arr(5):ptr()) == " ") - -assert(typeof(c.int:arr(5)) :: string == "CArrInfo") -assert(tostring(c.int:arr(5)) == " int, length = 5 ") -assert(tostring(c.int:ptr():arr(5)) == " , length = 5 ") - -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)) == " () -> int ") -assert(tostring(c.fn({ c.int }, c.int:ptr())) == " (int) -> ") -assert(tostring(c.fn({ c.int:ptr() }, c.int:ptr())) == " () -> ") -assert( - tostring(c.fn({ c.int:ptr(), c.int:ptr() }, c.int:ptr())) - == " (, ) -> " -) - -assert(typeof(c.struct({ c.int, c.char })) :: string == "CStructInfo") -assert( - tostring(c.struct({ c.int, c.char:ptr() })) - == ` int, , size = {c.struct({ c.int, c.char:ptr() }).size} ` -) - --- FIXME: add box, ref pretty-print test diff --git a/tests/ffi/pretty_print/arr.luau b/tests/ffi/pretty_print/arr.luau new file mode 100644 index 00000000..4b78d5e8 --- /dev/null +++ b/tests/ffi/pretty_print/arr.luau @@ -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)) == " , length = 5 ") diff --git a/tests/ffi/pretty_print/box.luau b/tests/ffi/pretty_print/box.luau new file mode 100644 index 00000000..e9714343 --- /dev/null +++ b/tests/ffi/pretty_print/box.luau @@ -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)) diff --git a/tests/ffi/pretty_print/fn.luau b/tests/ffi/pretty_print/fn.luau new file mode 100644 index 00000000..d20326d7 --- /dev/null +++ b/tests/ffi/pretty_print/fn.luau @@ -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)) == " () -> int ") +assert(tostring(c.fn({ c.int }, c.int:ptr())) == " (int) -> ") +assert(tostring(c.fn({ c.int:ptr() }, c.int:ptr())) == " () -> ") +assert( + tostring(c.fn({ c.int:ptr(), c.int:ptr() }, c.int:ptr())) + == " (, ) -> " +) diff --git a/tests/ffi/pretty_print/ptr.luau b/tests/ffi/pretty_print/ptr.luau new file mode 100644 index 00000000..fd3da1d0 --- /dev/null +++ b/tests/ffi/pretty_print/ptr.luau @@ -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()) == " ") diff --git a/tests/ffi/pretty_print/struct.luau b/tests/ffi/pretty_print/struct.luau new file mode 100644 index 00000000..124d4127 --- /dev/null +++ b/tests/ffi/pretty_print/struct.luau @@ -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, , size = {c.struct({ c.int, c.char:ptr() }).size} ` +) diff --git a/tests/ffi/pretty_print/type.luau b/tests/ffi/pretty_print/type.luau new file mode 100644 index 00000000..af86dcc8 --- /dev/null +++ b/tests/ffi/pretty_print/type.luau @@ -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") diff --git a/tests/ffi/types/arr.luau b/tests/ffi/types/arr.luau index e69de29b..448fff34 100644 --- a/tests/ffi/types/arr.luau +++ b/tests/ffi/types/arr.luau @@ -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") diff --git a/tests/ffi/types/fn.luau b/tests/ffi/types/fn.luau deleted file mode 100644 index e69de29b..00000000 diff --git a/tests/ffi/types/ptr.luau b/tests/ffi/types/ptr.luau index cdbc2fe7..05cdf136 100644 --- a/tests/ffi/types/ptr.luau +++ b/tests/ffi/types/ptr.luau @@ -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 "")) diff --git a/tests/ffi/types/struct.luau b/tests/ffi/types/struct.luau index a955e718..10edd4a3 100644 --- a/tests/ffi/types/struct.luau +++ b/tests/ffi/types/struct.luau @@ -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")