################################# setup
require 'rbconfig'
ruby = RbConfig::CONFIG['ruby_install_name']
unless File.exists?('./ext/ffi_c/ffi_c.so')
  system("cd ext/ffi_c && #{ruby} extconf.rb && make") or raise("Failed building extension")
end
unless File.exists?('./build/libtest.so')
  system("make -f libtest/GNUmakefile") or raise("Failed building test library")
end

$LOAD_PATH << 'lib'
$LOAD_PATH << 'ext/ffi_c'

################################# actual test code
require 'ffi'

module LibTest
  extend FFI::Library
  ffi_lib './build/libtest.so'
  class S8S32 < FFI::Struct
    layout :s8, :char, :s32, :int
  end
  attach_function :struct_return_s8s32, [ ], S8S32.by_value
end

$status = 0
def check(got, expected)
  puts "Expected #{expected}, got #{got}"
  $status += 1 if expected != got
end

s = LibTest.struct_return_s8s32
check s[:s8], 0x7f
check s[:s32], 0x12345678

exit($status)
