summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Kanis <lars@greiz-reinsdorf.de>2021-02-28 18:31:13 +0100
committerLars Kanis <lars@greiz-reinsdorf.de>2021-02-28 18:31:13 +0100
commit6d14c0a9107c0d5febb3bf92a60d2581e768fa2f (patch)
treea3dbe1929c79d4b0ecc9a5ec74bd62b06693c07e
parent57bcc6074ad45a52e4d93d3ef56af0105b0dcb26 (diff)
downloadffi-6d14c0a9107c0d5febb3bf92a60d2581e768fa2f.tar.gz
Separate double callback spec and variadic call with function pointer
This adds missing float and double specs for callbacks and moves variadic call with a function pointer to variadic specs. Also add another variadic spec with multiple function pointers.
-rw-r--r--spec/ffi/callback_spec.rb11
-rw-r--r--spec/ffi/fixtures/ClosureTest.c13
-rw-r--r--spec/ffi/variadic_spec.rb16
3 files changed, 37 insertions, 3 deletions
diff --git a/spec/ffi/callback_spec.rb b/spec/ffi/callback_spec.rb
index 972119d..7b05d7c 100644
--- a/spec/ffi/callback_spec.rb
+++ b/spec/ffi/callback_spec.rb
@@ -49,6 +49,7 @@ module CallbackSpecs
callback :cbVrU32, [ ], :uint
callback :cbVrL, [ ], :long
callback :cbVrUL, [ ], :ulong
+ callback :cbVrF, [ ], :float
callback :cbVrD, [ ], :double
callback :cbVrS64, [ ], :long_long
callback :cbVrU64, [ ], :ulong_long
@@ -71,6 +72,8 @@ module CallbackSpecs
attach_function :testCallbackVrU16, :testClosureVrS, [ :cbVrU16 ], :ushort
attach_function :testCallbackVrS32, :testClosureVrI, [ :cbVrS32 ], :int
attach_function :testCallbackVrU32, :testClosureVrI, [ :cbVrU32 ], :uint
+ attach_function :testCallbackVrF, :testClosureVrF, [ :cbVrF ], :float
+ attach_function :testCallbackVrD, :testClosureVrD, [ :cbVrD ], :double
attach_function :testCallbackVrL, :testClosureVrL, [ :cbVrL ], :long
attach_function :testCallbackVrZ, :testClosureVrZ, [ :cbVrZ ], :bool
attach_function :testCallbackVrUL, :testClosureVrL, [ :cbVrUL ], :ulong
@@ -87,7 +90,6 @@ module CallbackSpecs
attach_variable :pVrS8, :gvar_pointer, :pointer
attach_function :testGVarCallbackVrS8, :testClosureVrB, [ :pointer ], :char
attach_function :testOptionalCallbackCrV, :testOptionalClosureBrV, [ :cbCrV, :char ], :void
- attach_function :testCallbackVrDva, :testClosureVrDva, [ :double, :varargs ], :double
end
it "returning :char (0)" do
@@ -262,9 +264,12 @@ module CallbackSpecs
expect(LibTest.testCallbackVrZ { true }).to be true
end
+ it "returning float" do
+ expect(LibTest.testCallbackVrF { 1.234567890123456789 }).to be_within(1E-7).of(1.234567890123456789)
+ end
+
it "returning double" do
- pr = proc { 42.0 }
- expect(LibTest.testCallbackVrDva(3.0, :cbVrD, pr)).to be_within(0.0000001).of(45.0)
+ expect(LibTest.testCallbackVrD { 1.234567890123456789 }).to be_within(1E-15).of(1.234567890123456789)
end
it "returning :pointer (nil)" do
diff --git a/spec/ffi/fixtures/ClosureTest.c b/spec/ffi/fixtures/ClosureTest.c
index daabe0e..16f72c4 100644
--- a/spec/ffi/fixtures/ClosureTest.c
+++ b/spec/ffi/fixtures/ClosureTest.c
@@ -25,6 +25,19 @@ double testClosureVrDva(double d, ...) {
return d + closure();
}
+long testClosureVrILva(int i, long l, ...) {
+ va_list args;
+ int (*cl1)(int);
+ long (*cl2)(long);
+
+ va_start(args, l);
+ cl1 = va_arg(args, int (*)(int));
+ cl2 = va_arg(args, long (*)(long));
+ va_end(args);
+
+ return cl1(i) + cl2(l);
+}
+
#define R(T, rtype) rtype testClosureVr##T(rtype (*closure)(void)) { \
return closure != NULL ? (*closure)() : (rtype) 0; \
}
diff --git a/spec/ffi/variadic_spec.rb b/spec/ffi/variadic_spec.rb
index 0f1e077..f379ed4 100644
--- a/spec/ffi/variadic_spec.rb
+++ b/spec/ffi/variadic_spec.rb
@@ -13,11 +13,16 @@ describe "Function with variadic arguments" do
enum :enum_type2, [:c3, 42, :c4]
attach_function :pack_varargs, [ :buffer_out, :string, :varargs ], :void
attach_function :pack_varargs2, [ :buffer_out, :enum_type1, :string, :varargs ], :enum_type1
+ callback :cbVrD, [ ], :double
+ callback :cbVrI, [:int], :int
+ callback :cbVrL, [:long], :long
attach_function :testBlockingOpen, [ ], :pointer
attach_function :testBlockingRWva, [ :pointer, :char, :varargs ], :char, :blocking => true
attach_function :testBlockingWRva, [ :pointer, :char, :varargs ], :char, :blocking => true
attach_function :testBlockingClose, [ :pointer ], :void
+ attach_function :testCallbackVrDva, :testClosureVrDva, [ :double, :varargs ], :double
+ attach_function :testCallbackVrILva, :testClosureVrILva, [ :int, :long, :varargs ], :long
end
it "takes enum arguments" do
@@ -77,6 +82,17 @@ describe "Function with variadic arguments" do
end
end
+ it "call variadic with callback argument" do
+ pr = proc { 42.0 }
+ expect(LibTest.testCallbackVrDva(3.0, :cbVrD, pr)).to be_within(0.0000001).of(45.0)
+ end
+
+ it "call variadic with several callback arguments" do
+ pr1 = proc { |i| i + 1 }
+ pr2 = proc { |l| l + 2 }
+ expect(LibTest.testCallbackVrILva(5, 6, :cbVrI, pr1, :cbVrL, pr2)).to eq(14)
+ end
+
module Varargs
PACK_VALUES = {
'c' => [ 0x12 ],