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.
Restruct tests/ffi and add std-ffi tests (lune-org#243)
- Loading branch information
Showing
35 changed files
with
350 additions
and
302 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 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
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,35 +1,27 @@ | ||
local ffi = require("@lune/ffi") | ||
local lib = require("../../utility/compile")("./tests/ffi/benchmark/external_call/lib.c") | ||
local process = require("@lune/process") | ||
local c = ffi.c | ||
local BENCH_SCALE: number = tonumber(process.env.BENCH_SCALE) or 1000000 | ||
|
||
local proc_clock = require("../../utility/proc_clock") | ||
local before, after = proc_clock.newBox() | ||
local get_clock = proc_clock.getClock | ||
-- Get clock provider | ||
local procClock = require("../../utility/proc_clock") | ||
local before, after = procClock.newBox() | ||
local getClock = procClock.getClock | ||
|
||
local testdir = "./tests/ffi/benchmark/external_call" | ||
local compile = require("../../utility/compile") | ||
compile(`{testdir}/lib.c`, `{testdir}/lib.so`) | ||
local lib = ffi.open(`{testdir}/lib.so`) | ||
local add = c.fn({ c.int, c.int }, c.int):callable(lib:find("add")) | ||
|
||
local function bench_add(bench_scale: number) | ||
local add_info = c.fn({ c.int, c.int }, c.int) | ||
local a = c.int:box(0) | ||
local delta = c.int:box(1) | ||
local a_ref = a:ref() | ||
local delta_ref = delta:ref() | ||
|
||
local add_callable = add_info:callable(lib:find("add")) | ||
|
||
local a = c.int:box(0) | ||
local delta = c.int:box(1) | ||
|
||
local a_ref = a:ref() | ||
local delta_ref = delta:ref() | ||
|
||
get_clock(before) | ||
for i = 1, bench_scale do | ||
add_callable(a, a_ref, delta_ref) | ||
end | ||
get_clock(after) | ||
print(proc_clock.getOffset(before, after)) | ||
|
||
local result = c.int:readData(a) | ||
assert(result == bench_scale, `bench_add failed. result expected {bench_scale}, got {result}`) | ||
getClock(before) | ||
for i = 1, BENCH_SCALE do | ||
add(a, a_ref, delta_ref) | ||
end | ||
getClock(after) | ||
|
||
bench_add(1000000) | ||
print(procClock.getOffset(before, after)) | ||
local result = c.int:readData(a) | ||
assert(result == BENCH_SCALE, `bench_add failed. result expected {BENCH_SCALE}, got {result}`) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
local callableWrapper = require("../utils/callableWrapper") | ||
local ffi = require("@lune/ffi") | ||
local lib = require("../utils/compile")("./tests/ffi/external_closure/lib.c") | ||
local c = ffi.c | ||
|
||
-- Create closure | ||
local closureInfo = c.fn({ c.int, c.int }, c.int) | ||
local closure = closureInfo:closure(function(ret, a, b) | ||
c.int:writeData(ret, c.int:readData(a) + c.int:readData(b)) | ||
end) | ||
|
||
local callClosure = callableWrapper(lib:find("call_closure"), { closureInfo }, c.int) | ||
local result = callClosure(closure:ref()) | ||
assert(result == 72, `callClosure failed. result expected 20000, got {result}`) |
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,15 @@ | ||
local callableWrapper = require("../utils/callableWrapper") | ||
local ffi = require("@lune/ffi") | ||
local lib = require("../utils/compile")("./tests/ffi/external_closure/lib.c") | ||
local c = ffi.c | ||
|
||
-- Create closure | ||
local closureWithPointerInfo = c.fn({ c.int, c.int:ptr() }, c.int) | ||
local closureWithPointer = closureWithPointerInfo:closure(function(returnRef, aRef, bRef) | ||
c.int:writeData(returnRef, c.int:readData(aRef) + c.int:readData(bRef:deref())) | ||
end) | ||
|
||
local callClosureWithPointer = | ||
callableWrapper(lib:find("call_closure_with_pointer"), { closureWithPointerInfo }, c.int) | ||
local result = callClosureWithPointer(closureWithPointer:ref()) | ||
assert(result == 72, `closureWithPointer failed. result expected 20000, got {result}`) |
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,12 @@ | ||
local ffi = require("@lune/ffi") | ||
local lib = require("../utils/compile")("./tests/ffi/external_closure/lib.c") | ||
local c = ffi.c | ||
|
||
-- Create closure | ||
local helloWorldInfo = c.fn({}, c.void) | ||
local helloWorld = helloWorldInfo:closure(function() | ||
print("Hello world in lua closure!") | ||
end) | ||
|
||
local callHelloWorld = c.fn({ helloWorldInfo }, c.void):callable(lib:find("call_hello_world")) | ||
callHelloWorld(nil, helloWorld:ref()) |
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,9 @@ | ||
local callableWrapper = require("../utils/callableWrapper") | ||
local ffi = require("@lune/ffi") | ||
local lib = require("../utils/compile")("./tests/ffi/external_math/lib.c") | ||
local c = ffi.c | ||
|
||
local addInt = callableWrapper(lib:find("add_int"), { c.int, c.int }, c.int) | ||
local result = addInt(100, 200) | ||
|
||
assert(result == 300, `test_addInt failed. result expected 300, got {result}`) |
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,8 @@ | ||
local callableWrapper = require("../utils/callableWrapper") | ||
local ffi = require("@lune/ffi") | ||
local lib = require("../utils/compile")("./tests/ffi/external_math/lib.c") | ||
local c = ffi.c | ||
|
||
local mulInt = callableWrapper(lib:find("mul_int"), { c.int, c.int }, c.int) | ||
local result = mulInt(100, 200) | ||
assert(result == 20000, `test_mulInt failed. result expected 20000, got {result}`) |
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 |
---|---|---|
@@ -1,3 +1,7 @@ | ||
void pointer_write(int *a) { | ||
*a = 123; | ||
} | ||
|
||
int pointer_read(int *a) { | ||
return *a; | ||
} |
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 callableWrapper = require("../utils/callableWrapper") | ||
local ffi = require("@lune/ffi") | ||
local lib = require("../utils/compile")("./tests/ffi/external_pointer/lib.c") | ||
local c = ffi.c | ||
|
||
local pointerRead = callableWrapper(lib:find("pointer_read"), { c.int:ptr() }, c.int) | ||
local result = pointerRead(c.int:box(123):ref():ref()) | ||
assert(result == 123, `pointerRead failed. result expected 123, got {result}`) |
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,9 @@ | ||
local ffi = require("@lune/ffi") | ||
local lib = require("../utils/compile")("./tests/ffi/external_pointer/lib.c") | ||
local c = ffi.c | ||
|
||
local pointerWrite = c.fn({ c.int:ptr() }, c.void):callable(lib:find("pointer_write")) | ||
local aBox = ffi.box(c.int.size) | ||
pointerWrite(nil, aBox:ref():ref()) | ||
local result = c.int:readData(aBox) | ||
assert(result == 123, `pointerWrite failed. result expected 123, got {result}`) |
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,5 @@ | ||
local ffi = require("@lune/ffi") | ||
local lib = require("../utils/compile")("./tests/ffi/external_print/lib.c") | ||
local c = ffi.c | ||
|
||
c.fn({}, c.void):callable(lib:find("hello_world"))(nil) |
Oops, something went wrong.