ruby/test/fiddle/test_fiddle.rb
Sutou Kouhei a47f153d9d
Import JRuby implementation (#147)
Fix GH-104

lib/fiddle/jruby.rb is based on

https://github.com/jruby/jruby/blob/master/lib/ruby/stdlib/fiddle/jruby.rb
.

Here are changes for it:

* Move `Fiddle::TYPE_*` to `Fiddle::Types::*`
* Add `Fiddle::Types::VARIADIC`
* Add `Fiddle::Types::CONST_STRING`
* Add `Fiddle::Types::BOOL`
* Add `Fiddle::Types::INT8_T`
* Add `Fiddle::Types::UINT8_T`
* Add `Fiddle::Types::INT16_T`
* Add `Fiddle::Types::UINT16_T`
* Add `Fiddle::Types::INT32_T`
* Add `Fiddle::Types::UINT32_T`
* Add `Fiddle::Types::INT64_T`
* Add `Fiddle::Types::UINT64_T`
* Add more `Fiddle::ALIGN_*` for the above new `Fiddle::Types::*`
* Add more `Fiddle::SIZEOF_*` for the above new `Fiddle::Types::*`
* Add support for specifying type as not only `Fiddle::Types::*` but
also `Symbol` like `:int`
* Add support for variable size arguments in `Fiddle::Function`
* Add `Fiddle::Closure#free`
* Add `Fiddle::Closure#freed?`
* Add `Fiddle::Error` as base the error class
* Add `Fiddle::Pointer#call_free` and stop using `FFI::AutoPointer` in
`Fiddle::Pointer`
* Add support for `Fiddle::Pointer.malloc {}` `Fiddle::Pointer`
* Add support for `Fiddle::Pointer#free=`
* Add `Fiddle::Pointer#freed?`
* Use binary string not C string for `Fiddle::Pointer#[]`
* Add `Fiddle::Handle.sym_defined?`
* Add `Fiddle::Handle#sym_defined?`
* Add `Fiddle::Handle::DEFAULT`
* Add `Fiddle::ClearedReferenceError`
* Add no-op `Fiddle::Pinned`

Some features are still "not implemented". So there are some "omit"s for
JRuby in tests.
2024-10-10 10:05:52 +09:00

62 lines
2 KiB
Ruby

# frozen_string_literal: true
begin
require_relative 'helper'
rescue LoadError
end
class TestFiddle < Fiddle::TestCase
def test_nil_true_etc
if RUBY_ENGINE == "jruby"
omit("Fiddle::Q* aren't supported with JRuby")
end
assert_equal Fiddle::Qtrue, Fiddle.dlwrap(true)
assert_equal Fiddle::Qfalse, Fiddle.dlwrap(false)
assert_equal Fiddle::Qnil, Fiddle.dlwrap(nil)
assert Fiddle::Qundef
end
def test_windows_constant
require 'rbconfig'
if RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
assert Fiddle::WINDOWS, "Fiddle::WINDOWS should be 'true' on Windows platforms"
else
refute Fiddle::WINDOWS, "Fiddle::WINDOWS should be 'false' on non-Windows platforms"
end
end
def test_dlopen_linker_script_input_linux
omit("This is only for Linux") unless RUBY_PLATFORM.match?("linux")
if Dir.glob("/usr/lib/*/libncurses.so").empty?
omit("libncurses.so is needed")
end
# libncurses.so uses INPUT() on Debian GNU/Linux
# $ cat /usr/lib/x86_64-linux-gnu/libncurses.so
# INPUT(libncurses.so.6 -ltinfo)
handle = Fiddle.dlopen("libncurses.so")
begin
assert_equal("libncurses.so",
File.basename(handle.file_name, ".*"))
ensure
handle.close
end
end
def test_dlopen_linker_script_group_linux
omit("This is only for Linux") unless RUBY_PLATFORM.match?("linux")
# libc.so uses GROUP() on Debian GNU/Linux
# $ cat /usr/lib/x86_64-linux-gnu/libc.so
# /* GNU ld script
# Use the shared library, but some functions are only in
# the static library, so try that secondarily. */
# OUTPUT_FORMAT(elf64-x86-64)
# GROUP ( /lib/x86_64-linux-gnu/libc.so.6 /usr/lib/x86_64-linux-gnu/libc_nonshared.a AS_NEEDED ( /lib64/ld-linux-x86-64.so.2 ) )
handle = Fiddle.dlopen("libc.so")
begin
assert_equal("libc.so",
File.basename(handle.file_name, ".*"))
ensure
handle.close
end
end
end if defined?(Fiddle)