forked from lune-org/lune
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add and organize ffi tests (lune-org#243)
- Loading branch information
Showing
13 changed files
with
97 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)> " | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 )> ") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} ` | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |