diff options
Diffstat (limited to 'test/dl')
-rw-r--r-- | test/dl/test_func.rb | 91 |
1 files changed, 80 insertions, 11 deletions
diff --git a/test/dl/test_func.rb b/test/dl/test_func.rb index 761ae43..4e69dea 100644 --- a/test/dl/test_func.rb +++ b/test/dl/test_func.rb @@ -9,6 +9,71 @@ module DL assert_equal 'strcpy', f.name end + def test_name_with_block + begin + cb = Function.new(CFunc.new(0, TYPE_INT, '<callback>qsort'), + [TYPE_VOIDP, TYPE_VOIDP]){|x,y| CPtr.new(x)[0] <=> CPtr.new(y)[0]} + assert_equal('<callback>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_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_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 cfunc = CFunc.new(@libc['strcpy'], TYPE_VOIDP, 'strcpy') f = Function.new(cfunc, [TYPE_VOIDP, TYPE_VOIDP]) @@ -88,18 +153,22 @@ module DL end def test_qsort1() - cb = Function.new(CFunc.new(0, TYPE_INT, '<callback>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, '<callback>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() |