From 74bca55da9e509592bd04bc785b6eb86082f8cfe Mon Sep 17 00:00:00 2001 From: usa Date: Thu, 20 Dec 2012 09:52:46 +0000 Subject: merge revision(s) 38202,38238,38324,38326: [Backport #7527] * test/dl/test_func.rb (test_name_with_block, test_bind, test_qsort1): call unbind to release the callback closure because maximum number of callbacks is limited to DL::MAX_CALLBACK (== 5) with pure DL without Fiddle. * ext/dl/lib/dl/func.rb (DL::Function#unbind, #bound?): suppress NoMethodError when Fiddle is available. [ruby-core:50756] [Bug #7543] * test/dl/test_func.rb (test_bound*, test_unbind*): tests for the above. * ext/dl/lib/dl/func.rb (DL::Function#initialize, DL::Function#bind): ABI should be set by using CFunc#calltype even when Fiddle is used. When Fiddle is used and a block is given, name shoud not be ignored. [ruby-core:50562] [Bug #7514] * ext/dl/lib/dl/import.rb (DL::Importer#bind_function): should respect abi and name when Fiddle is used. * test/dl/test_func.rb (test_name_with_block): test for "name" method with giving a block. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_9_3@38506 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/dl/test_func.rb | 91 +++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 76 insertions(+), 15 deletions(-) (limited to 'test') diff --git a/test/dl/test_func.rb b/test/dl/test_func.rb index 819e3eb952..4e69dea10d 100644 --- a/test/dl/test_func.rb +++ b/test/dl/test_func.rb @@ -9,12 +9,69 @@ module DL assert_equal 'strcpy', f.name end - def test_bind + def test_name_with_block + begin + cb = Function.new(CFunc.new(0, TYPE_INT, 'qsort'), + [TYPE_VOIDP, TYPE_VOIDP]){|x,y| CPtr.new(x)[0] <=> CPtr.new(y)[0]} + assert_equal('qsort', cb.name) + ensure + cb.unbind if cb # max number of callbacks is limited to MAX_CALLBACK + end + end + + def test_bound f = Function.new(CFunc.new(0, TYPE_INT, 'test'), [TYPE_INT, TYPE_INT]) - assert_nothing_raised { + assert_equal false, f.bound? + begin + f.bind { |x,y| x + y } + assert_equal true, f.bound? + ensure + f.unbind # max number of callbacks is limited to MAX_CALLBACK + end + end + + def test_bound_for_callback_closure + begin + f = Function.new(CFunc.new(0, TYPE_INT, 'test'), + [TYPE_INT, TYPE_INT]) { |x,y| x + y } + assert_equal true, f.bound? + ensure + f.unbind if f # max number of callbacks is limited to MAX_CALLBACK + end + end + + def test_unbind + f = Function.new(CFunc.new(0, TYPE_INT, 'test'), [TYPE_INT, TYPE_INT]) + begin f.bind { |x, y| x + y } - } - assert_equal 579, f.call(123, 456) + assert_nothing_raised { f.unbind } + assert_equal false, f.bound? + # unbind() after unbind() should not raise error + assert_nothing_raised { f.unbind } + ensure + f.unbind # max number of callbacks is limited to MAX_CALLBACK + end + end + + def test_unbind_normal_function + f = Function.new(CFunc.new(@libc['strcpy'], TYPE_VOIDP, 'strcpy'), + [TYPE_VOIDP, TYPE_VOIDP]) + assert_nothing_raised { f.unbind } + assert_equal false, f.bound? + # unbind() after unbind() should not raise error + assert_nothing_raised { f.unbind } + end + + def test_bind + f = Function.new(CFunc.new(0, TYPE_INT, 'test'), [TYPE_INT, TYPE_INT]) + begin + assert_nothing_raised { + f.bind { |x, y| x + y } + } + assert_equal 579, f.call(123, 456) + ensure + f.unbind # max number of callbacks is limited to MAX_CALLBACK + end end def test_to_i @@ -96,18 +153,22 @@ module DL end def test_qsort1() - cb = Function.new(CFunc.new(0, TYPE_INT, 'qsort'), - [TYPE_VOIDP, TYPE_VOIDP]){|x,y| CPtr.new(x)[0] <=> CPtr.new(y)[0]} - qsort = Function.new(CFunc.new(@libc['qsort'], TYPE_VOID, 'qsort'), - [TYPE_VOIDP, TYPE_INT, TYPE_INT, TYPE_VOIDP]) - buff = "9341" - qsort.call(buff, buff.size, 1, cb) - assert_equal("1349", buff) + begin + cb = Function.new(CFunc.new(0, TYPE_INT, 'qsort'), + [TYPE_VOIDP, TYPE_VOIDP]){|x,y| CPtr.new(x)[0] <=> CPtr.new(y)[0]} + qsort = Function.new(CFunc.new(@libc['qsort'], TYPE_VOID, 'qsort'), + [TYPE_VOIDP, TYPE_INT, TYPE_INT, TYPE_VOIDP]) + buff = "9341" + qsort.call(buff, buff.size, 1, cb) + 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" + EnvUtil.under_gc_stress {qsort.call(buff, buff.size, 1, cb)} + assert_equal("1349", buff, bug4929) + ensure + cb.unbind if cb # max number of callbacks is limited to MAX_CALLBACK + end end def test_qsort2() -- cgit v1.2.3