diff options
Diffstat (limited to 'test/fiddle/test_func.rb')
-rw-r--r-- | test/fiddle/test_func.rb | 74 |
1 files changed, 51 insertions, 23 deletions
diff --git a/test/fiddle/test_func.rb b/test/fiddle/test_func.rb index d3604c79c3..df79539e76 100644 --- a/test/fiddle/test_func.rb +++ b/test/fiddle/test_func.rb @@ -15,7 +15,7 @@ module Fiddle begin f = Function.new(@libm['sinf'], [TYPE_FLOAT], TYPE_FLOAT) rescue Fiddle::DLError - skip "libm may not have sinf()" + omit "libm may not have sinf()" end assert_in_delta 1.0, f.call(90 * Math::PI / 180), 0.0001 end @@ -26,14 +26,13 @@ module Fiddle end def test_string - stress, GC.stress = GC.stress, true - f = Function.new(@libc['strcpy'], [TYPE_VOIDP, TYPE_VOIDP], TYPE_VOIDP) - buff = +"000" - str = f.call(buff, "123") - assert_equal("123", buff) - assert_equal("123", str.to_s) - ensure - GC.stress = stress + under_gc_stress do + f = Function.new(@libc['strcpy'], [TYPE_VOIDP, TYPE_VOIDP], TYPE_VOIDP) + buff = +"000" + str = f.call(buff, "123") + assert_equal("123", buff) + assert_equal("123", str.to_s) + end end def test_isdigit @@ -61,28 +60,40 @@ module Fiddle end def test_qsort1 - cb = Class.new(Closure) { + closure_class = Class.new(Closure) do def call(x, y) Pointer.new(x)[0] <=> Pointer.new(y)[0] end - }.new(TYPE_INT, [TYPE_VOIDP, TYPE_VOIDP]) + end - qsort = Function.new(@libc['qsort'], - [TYPE_VOIDP, TYPE_SIZE_T, TYPE_SIZE_T, TYPE_VOIDP], - TYPE_VOID) - buff = "9341" - qsort.call(buff, buff.size, 1, cb) - assert_equal("1349", buff) + closure_class.create(TYPE_INT, [TYPE_VOIDP, TYPE_VOIDP]) do |callback| + qsort = Function.new(@libc['qsort'], + [TYPE_VOIDP, TYPE_SIZE_T, TYPE_SIZE_T, TYPE_VOIDP], + TYPE_VOID) + buff = "9341" + qsort.call(buff, buff.size, 1, callback) + assert_equal("1349", buff) - bug4929 = '[ruby-core:37395]' - buff = "9341" - EnvUtil.under_gc_stress {qsort.call(buff, buff.size, 1, cb)} - assert_equal("1349", buff, bug4929) + bug4929 = '[ruby-core:37395]' + buff = "9341" + under_gc_stress do + qsort.call(buff, buff.size, 1, callback) + end + assert_equal("1349", buff, bug4929) + end + ensure + # Ensure freeing all closures. + # See https://github.com/ruby/fiddle/issues/102#issuecomment-1241763091 . + not_freed_closures = [] + ObjectSpace.each_object(Fiddle::Closure) do |closure| + not_freed_closures << closure unless closure.freed? + end + assert_equal([], not_freed_closures) end def test_snprintf unless Fiddle.const_defined?("TYPE_VARIADIC") - skip "libffi doesn't support variadic arguments" + omit "libffi doesn't support variadic arguments" end if Fiddle::WINDOWS snprintf_name = "_snprintf" @@ -92,7 +103,7 @@ module Fiddle begin snprintf_pointer = @libc[snprintf_name] rescue Fiddle::DLError - skip "Can't find #{snprintf_name}: #{$!.message}" + omit "Can't find #{snprintf_name}: #{$!.message}" end snprintf = Function.new(snprintf_pointer, [ @@ -134,5 +145,22 @@ module Fiddle assert_equal("string: He, const string: World, uint: 29\n", output_buffer[0, written]) end + + def test_rb_memory_view_available_p + omit "MemoryView is unavailable" unless defined? Fiddle::MemoryView + libruby = Fiddle.dlopen(nil) + case Fiddle::SIZEOF_VOIDP + when Fiddle::SIZEOF_LONG_LONG + value_type = -Fiddle::TYPE_LONG_LONG + else + value_type = -Fiddle::TYPE_LONG + end + rb_memory_view_available_p = + Function.new(libruby["rb_memory_view_available_p"], + [value_type], + :bool, + need_gvl: true) + assert_equal(false, rb_memory_view_available_p.call(Fiddle::Qnil)) + end end end if defined?(Fiddle) |