Skip to content

Commit

Permalink
Merge pull request #695 from eregon/more-specs
Browse files Browse the repository at this point in the history
Add specs for missing cases
  • Loading branch information
larskanis committed May 17, 2019
2 parents 5530048 + 5b81727 commit 1bda3ef
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
8 changes: 8 additions & 0 deletions spec/ffi/buffer_spec.rb
Expand Up @@ -138,6 +138,14 @@
end

describe "Reading/Writing binary strings" do
it "Buffer#write_bytes and read_bytes" do
str = "hello\0world"
buf = FFI::Buffer.new 11
buf.write_bytes(str)
s2 = buf.read_bytes(11)
expect(s2).to eq(str)
end

it "Buffer#put_bytes" do
str = "hello\0world"
buf = FFI::Buffer.new 1024
Expand Down
35 changes: 23 additions & 12 deletions spec/ffi/callback_spec.rb
Expand Up @@ -75,6 +75,7 @@ class S8F32S32 < FFI::Struct
attach_function :testCallbackVrS64, :testClosureVrLL, [ :cbVrS64 ], :long_long
attach_function :testCallbackVrU64, :testClosureVrLL, [ :cbVrU64 ], :ulong_long
attach_function :testCallbackVrP, :testClosureVrP, [ :cbVrP ], :pointer
attach_function :testCallbackReturningFunction, :testClosureVrP, [ :cbVrP ], :cbVrP
attach_function :testCallbackVrY, :testClosureVrP, [ :cbVrY ], S8F32S32.ptr
attach_function :testCallbackVrT, :testClosureVrT, [ :cbVrT ], S8F32S32.by_value
attach_function :testCallbackTrV, :testClosureTrV, [ :cbTrV, S8F32S32.ptr ], :void
Expand Down Expand Up @@ -266,6 +267,12 @@ class S8F32S32 < FFI::Struct
expect(LibTest.testCallbackVrP { p }).to eq(p)
end

it "returning a callback function" do
ret = LibTest.testCallbackReturningFunction { FFI::Pointer.new(42) }
expect(ret).to be_kind_of(FFI::Function)
expect(ret.address).to eq(42)
end

it "returning struct by value" do
skip "Segfault on 32 bit MINGW" if RUBY_PLATFORM == 'i386-mingw32'
s = LibTest::S8F32S32.new
Expand Down Expand Up @@ -500,28 +507,32 @@ class S8F32S32 < FFI::Struct
expect(v).to eq(-1)
end

def testCallbackU8rV(value)
v1 = 0xdeadbeef
LibTest.testCallbackU8rV(value) { |i| v1 = i }
expect(v1).to eq(value)

# Using a FFI::Function (v2) should be consistent with the direct callback (v1)
v2 = 0xdeadbeef
fun = FFI::Function.new(:void, [:uchar]) { |i| v2 = i }
LibTest.testCallbackU8rV(fun, value)
expect(v2).to eq(value)
end

it ":uchar (0) argument" do
v = 0xdeadbeef
LibTest.testCallbackU8rV(0) { |i| v = i }
expect(v).to eq(0)
testCallbackU8rV(0)
end

it ":uchar (127) argument" do
v = 0xdeadbeef
LibTest.testCallbackU8rV(127) { |i| v = i }
expect(v).to eq(127)
testCallbackU8rV(127)
end

it ":uchar (128) argument" do
v = 0xdeadbeef
LibTest.testCallbackU8rV(128) { |i| v = i }
expect(v).to eq(128)
testCallbackU8rV(128)
end

it ":uchar (255) argument" do
v = 0xdeadbeef
LibTest.testCallbackU8rV(255) { |i| v = i }
expect(v).to eq(255)
testCallbackU8rV(255)
end

it ":short (0) argument" do
Expand Down
7 changes: 7 additions & 0 deletions spec/ffi/string_spec.rb
Expand Up @@ -11,6 +11,7 @@ module StrLibTest
ffi_lib TestLibrary::PATH
attach_function :ptr_ret_pointer, [ :pointer, :int], :string
attach_function :string_equals, [ :string, :string ], :int
attach_function :pointer_string_equals, :string_equals, [ :pointer, :string ], :int
attach_function :string_dummy, [ :string ], :void
attach_function :string_null, [ ], :string
end
Expand All @@ -32,6 +33,12 @@ module StrLibTest
expect(str).to be_tainted
end

it "A String can be passed to a :pointer argument" do
str = "string buffer"
expect(StrLibTest.pointer_string_equals(str, str)).to eq(1)
expect(StrLibTest.pointer_string_equals(str + "a", str)).to eq(0)
end

it "Poison null byte raises error" do
s = "123\0abc"
expect { StrLibTest.string_equals(s, s) }.to raise_error(ArgumentError)
Expand Down

0 comments on commit 1bda3ef

Please sign in to comment.