2017-09-12 14:52:23 +03:00
|
|
|
# frozen_string_literal: true
|
2011-01-23 16:20:58 +03:00
|
|
|
begin
|
|
|
|
require_relative 'helper'
|
|
|
|
rescue LoadError
|
|
|
|
end
|
2010-05-06 10:59:24 +04:00
|
|
|
|
|
|
|
module Fiddle
|
|
|
|
class TestClosure < Fiddle::TestCase
|
2022-09-09 17:59:41 +03:00
|
|
|
def teardown
|
|
|
|
super
|
|
|
|
# Ensure freeing all closures.
|
|
|
|
# See https://github.com/ruby/fiddle/issues/102#issuecomment-1241763091 .
|
2022-09-15 00:38:52 +03:00
|
|
|
not_freed_closures = []
|
|
|
|
ObjectSpace.each_object(Fiddle::Closure) do |closure|
|
|
|
|
not_freed_closures << closure unless closure.freed?
|
|
|
|
end
|
|
|
|
assert_equal([], not_freed_closures)
|
2022-09-09 17:59:41 +03:00
|
|
|
end
|
|
|
|
|
2010-05-06 10:59:24 +04:00
|
|
|
def test_argument_errors
|
2017-07-13 12:46:16 +03:00
|
|
|
assert_raise(TypeError) do
|
2010-05-06 10:59:24 +04:00
|
|
|
Closure.new(TYPE_INT, TYPE_INT)
|
|
|
|
end
|
|
|
|
|
2017-07-13 12:46:16 +03:00
|
|
|
assert_raise(TypeError) do
|
2010-05-06 10:59:24 +04:00
|
|
|
Closure.new('foo', [TYPE_INT])
|
|
|
|
end
|
|
|
|
|
2017-07-13 12:46:16 +03:00
|
|
|
assert_raise(TypeError) do
|
2010-05-06 10:59:24 +04:00
|
|
|
Closure.new(TYPE_INT, ['meow!'])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-12-25 00:01:12 +03:00
|
|
|
def test_type_symbol
|
2022-09-14 06:59:13 +03:00
|
|
|
Closure.create(:int, [:void]) do |closure|
|
|
|
|
assert_equal([
|
|
|
|
TYPE_INT,
|
|
|
|
[TYPE_VOID],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
closure.instance_variable_get(:@ctype),
|
|
|
|
closure.instance_variable_get(:@args),
|
|
|
|
])
|
|
|
|
end
|
2020-12-25 00:01:12 +03:00
|
|
|
end
|
|
|
|
|
2010-05-06 10:59:24 +04:00
|
|
|
def test_call
|
2022-09-14 06:59:13 +03:00
|
|
|
closure_class = Class.new(Closure) do
|
2010-05-06 10:59:24 +04:00
|
|
|
def call
|
|
|
|
10
|
|
|
|
end
|
2022-09-14 06:59:13 +03:00
|
|
|
end
|
|
|
|
closure_class.create(TYPE_INT, []) do |closure|
|
|
|
|
func = Function.new(closure, [], TYPE_INT)
|
|
|
|
assert_equal 10, func.call
|
|
|
|
end
|
2010-05-06 10:59:24 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_returner
|
2022-09-14 06:59:13 +03:00
|
|
|
closure_class = Class.new(Closure) do
|
2010-05-06 10:59:24 +04:00
|
|
|
def call thing
|
|
|
|
thing
|
|
|
|
end
|
2022-09-14 06:59:13 +03:00
|
|
|
end
|
|
|
|
closure_class.create(TYPE_INT, [TYPE_INT]) do |closure|
|
|
|
|
func = Function.new(closure, [TYPE_INT], TYPE_INT)
|
|
|
|
assert_equal 10, func.call(10)
|
|
|
|
end
|
2010-05-06 10:59:24 +04:00
|
|
|
end
|
|
|
|
|
2020-12-25 00:02:19 +03:00
|
|
|
def test_const_string
|
|
|
|
closure_class = Class.new(Closure) do
|
|
|
|
def call(string)
|
|
|
|
@return_string = "Hello! #{string}"
|
|
|
|
@return_string
|
|
|
|
end
|
|
|
|
end
|
2022-09-14 06:59:13 +03:00
|
|
|
closure_class.create(:const_string, [:const_string]) do |closure|
|
|
|
|
func = Function.new(closure, [:const_string], :const_string)
|
|
|
|
assert_equal("Hello! World!", func.call("World!"))
|
|
|
|
end
|
|
|
|
end
|
2020-12-25 00:02:19 +03:00
|
|
|
|
2022-09-14 06:59:13 +03:00
|
|
|
def test_free
|
|
|
|
Closure.create(:int, [:void]) do |closure|
|
2022-09-15 23:54:47 +03:00
|
|
|
assert(!closure.freed?)
|
2022-09-14 06:59:13 +03:00
|
|
|
closure.free
|
2022-09-15 23:54:47 +03:00
|
|
|
assert(closure.freed?)
|
2022-09-14 06:59:13 +03:00
|
|
|
closure.free
|
|
|
|
end
|
2020-12-25 00:02:19 +03:00
|
|
|
end
|
|
|
|
|
2010-05-06 10:59:24 +04:00
|
|
|
def test_block_caller
|
|
|
|
cb = Closure::BlockCaller.new(TYPE_INT, [TYPE_INT]) do |one|
|
|
|
|
one
|
|
|
|
end
|
2022-09-14 06:59:13 +03:00
|
|
|
begin
|
|
|
|
func = Function.new(cb, [TYPE_INT], TYPE_INT)
|
|
|
|
assert_equal 11, func.call(11)
|
|
|
|
ensure
|
|
|
|
cb.free
|
|
|
|
end
|
2010-05-06 10:59:24 +04:00
|
|
|
end
|
2010-10-30 04:09:42 +04:00
|
|
|
|
2022-09-15 01:07:59 +03:00
|
|
|
def test_memsize_ruby_dev_42480
|
2010-10-30 04:09:42 +04:00
|
|
|
require 'objspace'
|
|
|
|
n = 10000
|
2022-09-15 01:07:59 +03:00
|
|
|
n.times do
|
|
|
|
Closure.create(:int, [:void]) do |closure|
|
|
|
|
ObjectSpace.memsize_of(closure)
|
|
|
|
end
|
|
|
|
end
|
2010-10-30 04:09:42 +04:00
|
|
|
end
|
2012-02-15 14:52:31 +04:00
|
|
|
|
|
|
|
%w[INT SHORT CHAR LONG LONG_LONG].each do |name|
|
2012-11-27 23:54:50 +04:00
|
|
|
type = Fiddle.const_get("TYPE_#{name}") rescue next
|
|
|
|
size = Fiddle.const_get("SIZEOF_#{name}")
|
2012-02-15 14:52:31 +04:00
|
|
|
[[type, size-1, name], [-type, size, "unsigned_"+name]].each do |t, s, n|
|
|
|
|
define_method("test_conversion_#{n.downcase}") do
|
|
|
|
arg = nil
|
|
|
|
|
2022-09-14 06:59:13 +03:00
|
|
|
closure_class = Class.new(Closure) do
|
2012-02-15 14:52:31 +04:00
|
|
|
define_method(:call) {|x| arg = x}
|
2022-09-14 06:59:13 +03:00
|
|
|
end
|
|
|
|
closure_class.create(t, [t]) do |closure|
|
|
|
|
v = ~(~0 << (8*s))
|
|
|
|
|
|
|
|
arg = nil
|
|
|
|
assert_equal(v, closure.call(v))
|
|
|
|
assert_equal(arg, v, n)
|
|
|
|
|
|
|
|
arg = nil
|
|
|
|
func = Function.new(closure, [t], t)
|
|
|
|
assert_equal(v, func.call(v))
|
|
|
|
assert_equal(arg, v, n)
|
|
|
|
end
|
2012-02-15 14:52:31 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-05-06 10:59:24 +04:00
|
|
|
end
|
2011-01-23 16:20:58 +03:00
|
|
|
end if defined?(Fiddle)
|