Sha256: 9fd587e22195b183b26a6d07bde40a53d24f35e245983f81a0fd7d7afdddb0dc

Contents?: true

Size: 945 Bytes

Versions: 2

Compression:

Stored size: 945 Bytes

Contents

require 'ffi'

describe "Callback" do
  module LibC
    extend FFI::Library
    callback :qsort_cmp, [ :pointer, :pointer ], :int
    attach_function :qsort, [ :pointer, :int, :int, :qsort_cmp ], :int
  end
  it "arguments get passed correctly" do
    p = MemoryPointer.new(:int, 2)
    p.put_array_of_int32(0, [ 1 , 2 ])
    args = []
    cmp = proc do |p1, p2| args.push(p1.get_int(0)); args.push(p2.get_int(0)); 0; end
    # this is a bit dodgey, as it relies on qsort passing the args in order
    LibC.qsort(p, 2, 4, cmp)
    args.should == [ 1, 2 ]
  end

  it "Block can be substituted for Callback as last argument" do
    p = MemoryPointer.new(:int, 2)
    p.put_array_of_int32(0, [ 1 , 2 ])
    args = []
    # this is a bit dodgey, as it relies on qsort passing the args in order
    LibC.qsort(p, 2, 4) do |p1, p2| 
      args.push(p1.get_int(0))
      args.push(p2.get_int(0))
      0
    end
    args.should == [ 1, 2 ]
  end  
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ffi-0.1.0 specs/callback_spec.rb
ffi-0.1.1 specs/callback_spec.rb