From 07308c4d30b8c5260e5366c8eed2abf054d86fe7 Mon Sep 17 00:00:00 2001 From: tenderlove Date: Fri, 31 Oct 2014 21:13:09 +0000 Subject: * ext/dl/*: remove DL as it is replaced by Fiddle. [Feature #5458] Thanks to Jonan Scheffler for this patch * test/dl/*: ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@48217 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/dl/test_base.rb | 146 -------------------------- test/dl/test_c_struct_entry.rb | 55 ---------- test/dl/test_c_union_entity.rb | 31 ------ test/dl/test_callback.rb | 72 ------------- test/dl/test_cfunc.rb | 80 --------------- test/dl/test_cparser.rb | 33 ------ test/dl/test_cptr.rb | 226 ----------------------------------------- test/dl/test_dl2.rb | 140 ------------------------- test/dl/test_func.rb | 190 ---------------------------------- test/dl/test_handle.rb | 191 ---------------------------------- test/dl/test_import.rb | 165 ------------------------------ test/dl/test_win32.rb | 54 ---------- test/ruby/memory_status.rb | 31 +----- 13 files changed, 4 insertions(+), 1410 deletions(-) delete mode 100644 test/dl/test_base.rb delete mode 100644 test/dl/test_c_struct_entry.rb delete mode 100644 test/dl/test_c_union_entity.rb delete mode 100644 test/dl/test_callback.rb delete mode 100644 test/dl/test_cfunc.rb delete mode 100644 test/dl/test_cparser.rb delete mode 100644 test/dl/test_cptr.rb delete mode 100644 test/dl/test_dl2.rb delete mode 100644 test/dl/test_func.rb delete mode 100644 test/dl/test_handle.rb delete mode 100644 test/dl/test_import.rb delete mode 100644 test/dl/test_win32.rb (limited to 'test') diff --git a/test/dl/test_base.rb b/test/dl/test_base.rb deleted file mode 100644 index dafba6e44d..0000000000 --- a/test/dl/test_base.rb +++ /dev/null @@ -1,146 +0,0 @@ -# -*- coding: us-ascii -*- -require 'test/unit' -require_relative '../ruby/envutil' -EnvUtil.suppress_warning {require 'dl'} - -libc_so = libm_so = nil - -case RUBY_PLATFORM -when /cygwin/ - libc_so = "cygwin1.dll" - libm_so = "cygwin1.dll" -when /x86_64-linux/ - libc_so = "/lib64/libc.so.6" - libm_so = "/lib64/libm.so.6" -when /linux/ - libdir = '/lib' - case [0].pack('L!').size - when 4 - # 32-bit ruby - libdir = '/lib32' if File.directory? '/lib32' - when 8 - # 64-bit ruby - libdir = '/lib64' if File.directory? '/lib64' - end - libc_so = File.join(libdir, "libc.so.6") - libm_so = File.join(libdir, "libm.so.6") -when /mingw/, /mswin/ - require "rbconfig" - libc_so = libm_so = RbConfig::CONFIG["RUBY_SO_NAME"].split(/-/).find{|e| /^msvc/ =~ e} + ".dll" -when /darwin/ - libc_so = "/usr/lib/libc.dylib" - libm_so = "/usr/lib/libm.dylib" -when /kfreebsd/ - libc_so = "/lib/libc.so.0.1" - libm_so = "/lib/libm.so.1" -when /gnu/ #GNU/Hurd - libc_so = "/lib/libc.so.0.3" - libm_so = "/lib/libm.so.6" -when /mirbsd/ - libc_so = "/usr/lib/libc.so.41.10" - libm_so = "/usr/lib/libm.so.7.0" -when /freebsd/ - libc_so = "/lib/libc.so.7" - libm_so = "/lib/libm.so.5" -when /bsd|dragonfly/ - libc_so = "/usr/lib/libc.so" - libm_so = "/usr/lib/libm.so" -when /solaris/ - libdir = '/lib' - case [0].pack('L!').size - when 4 - # 32-bit ruby - libdir = '/lib' if File.directory? '/lib' - when 8 - # 64-bit ruby - libdir = '/lib/64' if File.directory? '/lib/64' - end - libc_so = File.join(libdir, "libc.so") - libm_so = File.join(libdir, "libm.so") -when /aix/ - pwd=Dir.pwd - libc_so = libm_so = "#{pwd}/libaixdltest.so" - unless File.exist? libc_so - cobjs=%w!strcpy.o! - mobjs=%w!floats.o sin.o! - funcs=%w!sin sinf strcpy strncpy! - expfile='dltest.exp' - require 'tmpdir' - Dir.mktmpdir do |dir| - begin - Dir.chdir dir - %x!/usr/bin/ar x /usr/lib/libc.a #{cobjs.join(' ')}! - %x!/usr/bin/ar x /usr/lib/libm.a #{mobjs.join(' ')}! - %x!echo "#{funcs.join("\n")}\n" > #{expfile}! - require 'rbconfig' - if RbConfig::CONFIG["GCC"] = 'yes' - lflag='-Wl,' - else - lflag='' - end - flags="#{lflag}-bE:#{expfile} #{lflag}-bnoentry -lm" - %x!#{RbConfig::CONFIG["LDSHARED"]} -o #{libc_so} #{(cobjs+mobjs).join(' ')} #{flags}! - ensure - Dir.chdir pwd - end - end - end -else - libc_so = ARGV[0] if ARGV[0] && ARGV[0][0] == ?/ - libm_so = ARGV[1] if ARGV[1] && ARGV[1][0] == ?/ - if( !(libc_so && libm_so) ) - $stderr.puts("libc and libm not found: #{$0} ") - end -end - -libc_so = nil if !libc_so || (libc_so[0] == ?/ && !File.file?(libc_so)) -libm_so = nil if !libm_so || (libm_so[0] == ?/ && !File.file?(libm_so)) - -if !libc_so || !libm_so - ruby = EnvUtil.rubybin - ldd = `ldd #{ruby}` - #puts ldd - libc_so = $& if !libc_so && %r{/\S*/libc\.so\S*} =~ ldd - libm_so = $& if !libm_so && %r{/\S*/libm\.so\S*} =~ ldd - #p [libc_so, libm_so] -end - -DL::LIBC_SO = libc_so -DL::LIBM_SO = libm_so - -module DL - class TestBase < Test::Unit::TestCase - include Math - include DL - - def setup - @libc = dlopen(LIBC_SO) - @libm = dlopen(LIBM_SO) - end - - def assert_match(expected, actual, message="") - assert_operator(expected, :===, actual, message) - end - - def assert_positive(actual) - assert_operator(actual, :>, 0) - end - - def assert_zero(actual) - assert_equal(0, actual) - end - - def assert_negative(actual) - assert_operator(actual, :<, 0) - end - - def test_empty() - end - - def teardown - if /linux/ =~ RUBY_PLATFORM - GC.start - end - end - end -end diff --git a/test/dl/test_c_struct_entry.rb b/test/dl/test_c_struct_entry.rb deleted file mode 100644 index b2f1f94419..0000000000 --- a/test/dl/test_c_struct_entry.rb +++ /dev/null @@ -1,55 +0,0 @@ -# -*- coding: us-ascii -*- -require_relative 'test_base' - -EnvUtil.suppress_warning {require 'dl/struct'} - -module DL - class TestCStructEntity < TestBase - def test_class_size - types = [TYPE_DOUBLE, TYPE_CHAR] - - size = CStructEntity.size types - - alignments = types.map { |type| PackInfo::ALIGN_MAP[type] } - - expected = PackInfo.align 0, alignments[0] - expected += PackInfo::SIZE_MAP[TYPE_DOUBLE] - - expected = PackInfo.align expected, alignments[1] - expected += PackInfo::SIZE_MAP[TYPE_CHAR] - - expected = PackInfo.align expected, alignments.max - - assert_equal expected, size - end - - def test_class_size_with_count - size = CStructEntity.size([[TYPE_DOUBLE, 2], [TYPE_CHAR, 20]]) - - types = [TYPE_DOUBLE, TYPE_CHAR] - alignments = types.map { |type| PackInfo::ALIGN_MAP[type] } - - expected = PackInfo.align 0, alignments[0] - expected += PackInfo::SIZE_MAP[TYPE_DOUBLE] * 2 - - expected = PackInfo.align expected, alignments[1] - expected += PackInfo::SIZE_MAP[TYPE_CHAR] * 20 - - expected = PackInfo.align expected, alignments.max - - assert_equal expected, size - end - - def test_set_ctypes - union = CStructEntity.malloc [TYPE_INT, TYPE_LONG] - union.assign_names %w[int long] - - # this test is roundabout because the stored ctypes are not accessible - union['long'] = 1 - union['int'] = 2 - - assert_equal 1, union['long'] - assert_equal 2, union['int'] - end - end -end diff --git a/test/dl/test_c_union_entity.rb b/test/dl/test_c_union_entity.rb deleted file mode 100644 index 09f7c60e4c..0000000000 --- a/test/dl/test_c_union_entity.rb +++ /dev/null @@ -1,31 +0,0 @@ -require_relative 'test_base' - -require 'dl/struct' - -module DL - class TestCUnionEntity < TestBase - def test_class_size - size = CUnionEntity.size([TYPE_DOUBLE, TYPE_CHAR]) - - assert_equal SIZEOF_DOUBLE, size - end - - def test_class_size_with_count - size = CUnionEntity.size([[TYPE_DOUBLE, 2], [TYPE_CHAR, 20]]) - - assert_equal SIZEOF_CHAR * 20, size - end - - def test_set_ctypes - union = CUnionEntity.malloc [TYPE_INT, TYPE_LONG] - union.assign_names %w[int long] - - # this test is roundabout because the stored ctypes are not accessible - union['long'] = 1 - assert_equal 1, union['long'] - - union['int'] = 1 - assert_equal 1, union['int'] - end - end -end diff --git a/test/dl/test_callback.rb b/test/dl/test_callback.rb deleted file mode 100644 index 8ae652b95a..0000000000 --- a/test/dl/test_callback.rb +++ /dev/null @@ -1,72 +0,0 @@ -require_relative 'test_base' -require_relative '../ruby/envutil' -require 'dl/callback' -require 'dl/func' - -module DL - class TestCallback < TestBase - include DL - - def test_remove_callback_failed - assert_equal(false, remove_callback(0, TYPE_VOIDP)) - end - - def test_remove_callback - addr = set_callback(TYPE_VOIDP, 1) do |str| - str - end - assert remove_callback(addr, TYPE_VOIDP), 'callback removed' - end - - def test_callback_return_value - addr = set_callback(TYPE_VOIDP, 1) do |str| - str - end - func = CFunc.new(addr, TYPE_VOIDP, 'test') - f = Function.new(func, [TYPE_VOIDP]) - ptr = CPtr['blah'] - assert_equal ptr.to_i, f.call(ptr).to_i - end - - def test_callback_return_arbitrary - foo = 'foo' - addr = set_callback(TYPE_VOIDP, 1) do |ptr| - CPtr[foo].to_i - end - func = CFunc.new(addr, TYPE_VOIDP, 'test') - f = Function.new(func, [TYPE_VOIDP]) - - ptr = CPtr['foo'] - assert_equal 'foo', f.call(ptr).to_s - end - - def test_callback_with_string - called_with = nil - addr = set_callback(TYPE_VOID, 1) do |str| - called_with = dlunwrap(str) - end - func = CFunc.new(addr, TYPE_VOID, 'test') - f = Function.new(func, [TYPE_VOIDP]) - - # Don't remove local variable arg. - # This necessary to protect objects from GC. - arg = 'foo' - f.call(dlwrap(arg)) - assert_equal arg, called_with - end - - def test_call_callback - called = false - - addr = set_callback(TYPE_VOID, 1) do |foo| - called = true - end - - func = CFunc.new(addr, TYPE_VOID, 'test') - f = Function.new(func, [TYPE_VOIDP]) - f.call(nil) - - assert called, 'function should be called' - end - end -end diff --git a/test/dl/test_cfunc.rb b/test/dl/test_cfunc.rb deleted file mode 100644 index 39fa8133a2..0000000000 --- a/test/dl/test_cfunc.rb +++ /dev/null @@ -1,80 +0,0 @@ -require_relative 'test_base' -require 'dl/func' - -module DL - class TestCFunc < TestBase - def setup - super - @name = 'strcpy' - @cf = CFunc.new(@libc[@name], TYPE_VOIDP, @name) - end - - def test_ptr= - @cf.ptr = @libc['malloc'] - assert_equal @cf.ptr, @libc['malloc'] - end - - def test_ptr - assert_equal @cf.ptr, @libc[@name] - end - - def test_set_calltype - @cf.calltype = :foo - assert_equal :foo, @cf.calltype - end - - def test_new_ptr_type_name - assert_equal @name, @cf.name - assert @cf.name.tainted?, 'name should be tainted' - assert_equal :cdecl, @cf.calltype - assert_equal TYPE_VOIDP, @cf.ctype - end - - def test_new_ptr - cf = CFunc.new(@libc['strcpy']) - assert_nil cf.name - assert_equal :cdecl, cf.calltype - assert_equal TYPE_VOID, cf.ctype - end - - def test_name_should_be_duped - assert_equal @name, @cf.name - assert @cf.name.tainted?, 'name should be tainted' - - name = @name.dup - @name << 'foo' - - assert_equal name, @cf.name - end - - def test_to_s - s = @cf.to_s - assert s.tainted?, 'to_s should be tainted' - assert_match(/ptr=#{sprintf("0x0*%x", @cf.ptr)}/, s) - assert_match(/name='#{@cf.name}'/, s) - assert_match(/type=#{@cf.ctype}/, s) - end - - def test_inspect - assert_equal @cf.inspect, @cf.to_s - end - - def test_inspect_is_tainted - assert @cf.inspect.tainted?, 'inspect is tainted' - end - - def test_to_i - assert_equal @cf.to_i, @cf.ptr - assert_equal @libc[@name], @cf.to_i - end - - def test_last_error - Thread.new do - f = Function.new(@cf, [TYPE_VOIDP, TYPE_VOIDP]) - assert_nil CFunc.last_error - f.call("000", "123") - assert_not_nil CFunc.last_error - end.join - end - end -end diff --git a/test/dl/test_cparser.rb b/test/dl/test_cparser.rb deleted file mode 100644 index a4deb6733e..0000000000 --- a/test/dl/test_cparser.rb +++ /dev/null @@ -1,33 +0,0 @@ -require_relative 'test_base' - -require 'dl/cparser' - -module DL - class TestCParser < TestBase - include CParser - - def test_uint_ctype - assert_equal(-TYPE_INT, parse_ctype('uint')) - end - - def test_size_t_ctype - assert_equal(TYPE_SIZE_T, parse_ctype("size_t")) - end - - def test_ssize_t_ctype - assert_equal(TYPE_SSIZE_T, parse_ctype("ssize_t")) - end - - def test_ptrdiff_t_ctype - assert_equal(TYPE_PTRDIFF_T, parse_ctype("ptrdiff_t")) - end - - def test_intptr_t_ctype - assert_equal(TYPE_INTPTR_T, parse_ctype("intptr_t")) - end - - def test_uintptr_t_ctype - assert_equal(TYPE_UINTPTR_T, parse_ctype("uintptr_t")) - end - end -end diff --git a/test/dl/test_cptr.rb b/test/dl/test_cptr.rb deleted file mode 100644 index aa74a73fe5..0000000000 --- a/test/dl/test_cptr.rb +++ /dev/null @@ -1,226 +0,0 @@ -require_relative 'test_base' -require_relative '../ruby/envutil' - -module DL - class TestCPtr < TestBase - def test_cptr_to_int - null = DL::NULL - assert_equal(null.to_i, null.to_int) - end - - def test_malloc_free_func_int - free = CFunc.new(DL::RUBY_FREE, TYPE_VOID, 'free') - - ptr = CPtr.malloc(10, free.to_i) - assert_equal 10, ptr.size - assert_equal free.to_i, ptr.free.to_i - end - - def test_malloc_free_func - free = CFunc.new(DL::RUBY_FREE, TYPE_VOID, 'free') - - ptr = CPtr.malloc(10, free) - assert_equal 10, ptr.size - assert_equal free.to_i, ptr.free.to_i - end - - def test_to_str - str = "hello world" - ptr = CPtr[str] - - assert_equal 3, ptr.to_str(3).length - assert_equal str, ptr.to_str - - ptr[5] = 0 - assert_equal "hello\0world", ptr.to_str - end - - def test_to_s - str = "hello world" - ptr = CPtr[str] - - assert_equal 3, ptr.to_s(3).length - assert_equal str, ptr.to_s - - ptr[5] = 0 - assert_equal 'hello', ptr.to_s - end - - def test_minus - str = "hello world" - ptr = CPtr[str] - assert_equal ptr.to_s, (ptr + 3 - 3).to_s - end - - # TODO: what if the pointer size is 0? raise an exception? do we care? - def test_plus - str = "hello world" - ptr = CPtr[str] - new_str = ptr + 3 - assert_equal 'lo world', new_str.to_s - end - - def test_inspect - ptr = CPtr.new(0) - inspect = ptr.inspect - assert_match(/size=#{ptr.size}/, inspect) - assert_match(/free=#{sprintf("%#x", ptr.free.to_i)}/, inspect) - assert_match(/ptr=#{sprintf("%#x", ptr.to_i)}/, inspect) - end - - def test_to_ptr_string - str = "hello world" - ptr = CPtr[str] - assert ptr.tainted?, 'pointer should be tainted' - assert_equal str.length, ptr.size - assert_equal 'hello', ptr[0,5] - end - - def test_to_ptr_io - buf = CPtr.malloc(10) - File.open(__FILE__, 'r') do |f| - ptr = CPtr.to_ptr f - fread = CFunc.new(@libc['fread'], TYPE_VOID, 'fread') - fread.call([buf.to_i, DL::SIZEOF_CHAR, buf.size - 1, ptr.to_i]) - end - - File.open(__FILE__, 'r') do |f| - assert_equal f.read(9), buf.to_s - end - end - - def test_to_ptr_with_ptr - ptr = CPtr.new 0 - ptr2 = CPtr.to_ptr Struct.new(:to_ptr).new(ptr) - assert_equal ptr, ptr2 - - assert_raises(DL::DLError) do - CPtr.to_ptr Struct.new(:to_ptr).new(nil) - end - end - - def test_to_ptr_with_num - ptr = CPtr.new 0 - assert_equal ptr, CPtr[0] - end - - def test_equals - ptr = CPtr.new 0 - ptr2 = CPtr.new 0 - assert_equal ptr2, ptr - end - - def test_not_equals - ptr = CPtr.new 0 - assert_not_equal 10, ptr, '10 should not equal the pointer' - end - - def test_cmp - ptr = CPtr.new 0 - assert_nil(ptr <=> 10, '10 should not be comparable') - end - - def test_ref_ptr - ary = [0,1,2,4,5] - addr = CPtr.new(dlwrap(ary)) - assert_equal addr.to_i, addr.ref.ptr.to_i - - assert_equal addr.to_i, (+ (- addr)).to_i - end - - def test_to_value - ary = [0,1,2,4,5] - addr = CPtr.new(dlwrap(ary)) - assert_equal ary, addr.to_value - end - - def test_free - ptr = CPtr.malloc(4) - assert_nil ptr.free - end - - def test_free= - assert_normal_exit(<<-"End", '[ruby-dev:39269]') - require 'dl' - DL::LIBC_SO = #{DL::LIBC_SO.dump} - DL::LIBM_SO = #{DL::LIBM_SO.dump} - include DL - @libc = dlopen(LIBC_SO) - @libm = dlopen(LIBM_SO) - free = CFunc.new(DL::RUBY_FREE, TYPE_VOID, 'free') - ptr = CPtr.malloc(4) - ptr.free = free - free.ptr - ptr.free.ptr - End - - free = CFunc.new(DL::RUBY_FREE, TYPE_VOID, 'free') - ptr = CPtr.malloc(4) - ptr.free = free - - assert_equal free.ptr, ptr.free.ptr - end - - def test_null? - ptr = CPtr.new(0) - assert ptr.null? - end - - def test_size - ptr = CPtr.malloc(4) - assert_equal 4, ptr.size - DL.free ptr.to_i - end - - def test_size= - ptr = CPtr.malloc(4) - ptr.size = 10 - assert_equal 10, ptr.size - DL.free ptr.to_i - end - - def test_aref_aset - check = Proc.new{|str,ptr| - assert_equal(str.size(), ptr.size()) - assert_equal(str, ptr.to_s()) - assert_equal(str[0,2], ptr.to_s(2)) - assert_equal(str[0,2], ptr[0,2]) - assert_equal(str[1,2], ptr[1,2]) - assert_equal(str[1,0], ptr[1,0]) - assert_equal(str[0].ord, ptr[0]) - assert_equal(str[1].ord, ptr[1]) - } - str = 'abc' - ptr = CPtr[str] - check.call(str, ptr) - - str[0] = "c" - assert_equal 'c'.ord, ptr[0] = "c".ord - check.call(str, ptr) - - str[0,2] = "aa" - assert_equal 'aa', ptr[0,2] = "aa" - check.call(str, ptr) - - ptr2 = CPtr['cdeeee'] - str[0,2] = "cd" - assert_equal ptr2, ptr[0,2] = ptr2 - check.call(str, ptr) - - ptr3 = CPtr['vvvv'] - str[0,2] = "vv" - assert_equal ptr3.to_i, ptr[0,2] = ptr3.to_i - check.call(str, ptr) - end - - def test_null_pointer - nullpo = CPtr.new(0) - assert_raise(DLError) {nullpo[0]} - assert_raise(DLError) {nullpo[0] = 1} - end - end - - def test_no_memory_leak - assert_no_memory_leak(%w[-W0 -rdl.so], '', '100_000.times {DL::CPtr.allocate}', rss: true) - end -end diff --git a/test/dl/test_dl2.rb b/test/dl/test_dl2.rb deleted file mode 100644 index 955ac1d855..0000000000 --- a/test/dl/test_dl2.rb +++ /dev/null @@ -1,140 +0,0 @@ -require_relative 'test_base.rb' -require 'dl/callback' -require 'dl/func' -require 'dl/pack' - -module DL -class TestDL < TestBase - def ptr2num(*list) - list.pack("p*").unpack(PackInfo::PACK_MAP[TYPE_VOIDP] + "*") - end - - # TODO: refactor test repetition - - def test_realloc - str = "abc" - ptr_id = DL.realloc(0, 4) - ptr = CPtr.new(ptr_id, 4) - - assert_equal ptr_id, ptr.to_i - - cfunc = CFunc.new(@libc['strcpy'], TYPE_VOIDP, 'strcpy') - cfunc.call([ptr_id,str].pack("l!p").unpack("l!*")) - assert_equal("abc\0", ptr[0,4]) - DL.free ptr_id - end - - def test_malloc - str = "abc" - - ptr_id = DL.malloc(4) - ptr = CPtr.new(ptr_id, 4) - - assert_equal ptr_id, ptr.to_i - - cfunc = CFunc.new(@libc['strcpy'], TYPE_VOIDP, 'strcpy') - cfunc.call([ptr_id,str].pack("l!p").unpack("l!*")) - assert_equal("abc\0", ptr[0,4]) - DL.free ptr_id - end - - def test_call_int() - cfunc = CFunc.new(@libc['atoi'], TYPE_INT, 'atoi') - x = cfunc.call(["100"].pack("p").unpack("l!*")) - assert_equal(100, x) - - cfunc = CFunc.new(@libc['atoi'], TYPE_INT, 'atoi') - x = cfunc.call(["-100"].pack("p").unpack("l!*")) - assert_equal(-100, x) - end - - def test_call_long() - cfunc = CFunc.new(@libc['atol'], TYPE_LONG, 'atol') - x = cfunc.call(["100"].pack("p").unpack("l!*")) - assert_equal(100, x) - cfunc = CFunc.new(@libc['atol'], TYPE_LONG, 'atol') - x = cfunc.call(["-100"].pack("p").unpack("l!*")) - assert_equal(-100, x) - end - - def test_call_double() - cfunc = CFunc.new(@libc['atof'], TYPE_DOUBLE, 'atof') - x = cfunc.call(["0.1"].pack("p").unpack("l!*")) - assert_in_delta(0.1, x) - - cfunc = CFunc.new(@libc['atof'], TYPE_DOUBLE, 'atof') - x = cfunc.call(["-0.1"].pack("p").unpack("l!*")) - assert_in_delta(-0.1, x) - end - - def test_sin - return if /x86_64/ =~ RUBY_PLATFORM - pi_2 = Math::PI/2 - cfunc = Function.new(CFunc.new(@libm['sin'], TYPE_DOUBLE, 'sin'), - [TYPE_DOUBLE]) - x = cfunc.call(pi_2) - assert_equal(Math.sin(pi_2), x) - - cfunc = Function.new(CFunc.new(@libm['sin'], TYPE_DOUBLE, 'sin'), - [TYPE_DOUBLE]) - x = cfunc.call(-pi_2) - assert_equal(Math.sin(-pi_2), x) - end - - def test_strlen() - cfunc = CFunc.new(@libc['strlen'], TYPE_INT, 'strlen') - x = cfunc.call(["abc"].pack("p").unpack("l!*")) - assert_equal("abc".size, x) - end - - def test_strcpy() - buff = "xxxx" - str = "abc" - cfunc = CFunc.new(@libc['strcpy'], TYPE_VOIDP, 'strcpy') - x = cfunc.call(ptr2num(buff,str)) - assert_equal("abc\0", buff) - assert_equal("abc\0", CPtr.new(x).to_s(4)) - - buff = "xxxx" - str = "abc" - cfunc = CFunc.new(@libc['strncpy'], TYPE_VOIDP, 'strncpy') - x = cfunc.call(ptr2num(buff,str) + [3]) - assert_equal("abcx", buff) - assert_equal("abcx", CPtr.new(x).to_s(4)) - - ptr = CPtr.malloc(4) - str = "abc" - cfunc = CFunc.new(@libc['strcpy'], TYPE_VOIDP, 'strcpy') - x = cfunc.call([ptr.to_i, *ptr2num(str)]) - assert_equal("abc\0", ptr[0,4]) - assert_equal("abc\0", CPtr.new(x).to_s(4)) - end - - def test_callback() - buff = "foobarbaz" - cb = set_callback(TYPE_INT,2){|x,y| CPtr.new(x)[0] <=> CPtr.new(y)[0]} - cfunc = CFunc.new(@libc['qsort'], TYPE_VOID, 'qsort') - cfunc.call(ptr2num(buff) + [buff.size, 1, cb]) - assert_equal('aabbfoorz', buff) - end - - def test_dlwrap() - ary = [0,1,2,4,5] - addr = dlwrap(ary) - ary2 = dlunwrap(addr) - assert_equal(ary, ary2) - end - - def test_type_size_t - assert_equal(DL::TYPE_SSIZE_T, DL::TYPE_SIZE_T.abs) - end - - def test_type_uintptr_t - assert_equal(-DL::TYPE_INTPTR_T, DL::TYPE_UINTPTR_T) - end - - def test_sizeof_uintptr_t - assert_equal(DL::SIZEOF_VOIDP, DL::SIZEOF_INTPTR_T) - end -end -end # module DL diff --git a/test/dl/test_func.rb b/test/dl/test_func.rb deleted file mode 100644 index 8cc1eac4a0..0000000000 --- a/test/dl/test_func.rb +++ /dev/null @@ -1,190 +0,0 @@ -require_relative 'test_base' -require 'dl/func' - -module DL - class TestFunc < TestBase - def test_name - f = Function.new(CFunc.new(@libc['strcpy'], TYPE_VOIDP, 'strcpy'), - [TYPE_VOIDP, TYPE_VOIDP]) - assert_equal 'strcpy', f.name - end - - 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_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]) - assert_equal cfunc.to_i, f.to_i - end - - def test_random - f = Function.new(CFunc.new(@libc['srand'], TYPE_VOID, 'srand'), - [-TYPE_LONG]) - assert_nil f.call(10) - end - - def test_sinf - return if /x86_64/ =~ RUBY_PLATFORM - begin - f = Function.new(CFunc.new(@libm['sinf'], TYPE_FLOAT, 'sinf'), - [TYPE_FLOAT]) - rescue DL::DLError - skip "libm may not have sinf()" - end - assert_in_delta 1.0, f.call(90 * Math::PI / 180), 0.0001 - end - - def test_sin - return if /x86_64/ =~ RUBY_PLATFORM - f = Function.new(CFunc.new(@libm['sin'], TYPE_DOUBLE, 'sin'), - [TYPE_DOUBLE]) - assert_in_delta 1.0, f.call(90 * Math::PI / 180), 0.0001 - end - - def test_strcpy() - f = Function.new(CFunc.new(@libc['strcpy'], TYPE_VOIDP, 'strcpy'), - [TYPE_VOIDP, TYPE_VOIDP]) - buff = "000" - str = f.call(buff, "123") - assert_equal("123", buff) - assert_equal("123", str.to_s) - end - - def test_string() - stress, GC.stress = GC.stress, true - f = Function.new(CFunc.new(@libc['strcpy'], TYPE_VOIDP, 'strcpy'), - [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 - end - - def test_isdigit() - f = Function.new(CFunc.new(@libc['isdigit'], TYPE_INT, 'isdigit'), - [TYPE_INT]) - r1 = f.call(?1.ord) - r2 = f.call(?2.ord) - rr = f.call(?r.ord) - assert_positive(r1) - assert_positive(r2) - assert_zero(rr) - end - - def test_atof() - f = Function.new(CFunc.new(@libc['atof'], TYPE_DOUBLE, 'atof'), - [TYPE_VOIDP]) - r = f.call("12.34") - assert_match(12.00..13.00, r) - end - - def test_strtod() - f = Function.new(CFunc.new(@libc['strtod'], TYPE_DOUBLE, 'strtod'), - [TYPE_VOIDP, TYPE_VOIDP]) - buff1 = CPtr["12.34"] - buff2 = buff1 + 4 - r = f.call(buff1, - buff2) - assert_in_delta(12.34, r, 0.001) - end - - def test_qsort1() - assert_separately(%W[--disable=gems -r#{__dir__}/test_base.rb -rdl/func], __FILE__, __LINE__, <<-"end;") - include DL - @libc = dlopen(LIBC_SO) - @libm = dlopen(LIBM_SO) - 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_SIZE_T, TYPE_SIZE_T, 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) - ensure - cb.unbind if cb # max number of callbacks is limited to MAX_CALLBACK - end - end; - end - - def test_qsort2() - cb = TempFunction.new(CFunc.new(0, TYPE_INT, 'qsort'), - [TYPE_VOIDP, TYPE_VOIDP]) - qsort = Function.new(CFunc.new(@libc['qsort'], TYPE_VOID, 'qsort'), - [TYPE_VOIDP, TYPE_SIZE_T, TYPE_SIZE_T, TYPE_VOIDP]) - buff = "9341" - qsort.call(buff, buff.size, 1, cb){|x,y| CPtr.new(x)[0] <=> CPtr.new(y)[0]} - assert_equal("1349", buff) - end - end -end diff --git a/test/dl/test_handle.rb b/test/dl/test_handle.rb deleted file mode 100644 index 6a8964e9a0..0000000000 --- a/test/dl/test_handle.rb +++ /dev/null @@ -1,191 +0,0 @@ -require_relative 'test_base' -require_relative '../ruby/envutil' - -module DL - class TestHandle < TestBase - def test_to_i - handle = DL::Handle.new(LIBC_SO) - assert_kind_of Integer, handle.to_i - end - - def test_static_sym_secure - assert_raises(SecurityError) do - Thread.new do - $SAFE = 2 - DL::Handle.sym('calloc') - end.join - end - end - - def test_static_sym_unknown - assert_raises(DL::DLError) { DL::Handle.sym('fooo') } - assert_raises(DL::DLError) { DL::Handle['fooo'] } - end - - def test_static_sym - skip "DL::Handle.sym is not supported" if /mswin|mingw/ =~ RUBY_PLATFORM - begin - # Linux / Darwin / FreeBSD - assert_not_nil DL::Handle.sym('dlopen') - assert_equal DL::Handle.sym('dlopen'), DL::Handle['dlopen'] - rescue - # NetBSD - require 'objspace' - assert_not_nil DL::Handle.sym('Init_objspace') - assert_equal DL::Handle.sym('Init_objspace'), DL::Handle['Init_objspace'] - end - end - - def test_sym_closed_handle - handle = DL::Handle.new(LIBC_SO) - handle.close - assert_raises(DL::DLError) { handle.sym("calloc") } - assert_raises(DL::DLError) { handle["calloc"] } - end - - def test_sym_unknown - handle = DL::Handle.new(LIBC_SO) - assert_raises(DL::DLError) { handle.sym('fooo') } - assert_raises(DL::DLError) { handle['fooo'] } - end - - def test_sym_with_bad_args - handle = DL::Handle.new(LIBC_SO) - assert_raises(TypeError) { handle.sym(nil) } - assert_raises(TypeError) { handle[nil] } - end - - def test_sym_secure - assert_raises(SecurityError) do - Thread.new do - $SAFE = 2 - handle = DL::Handle.new(LIBC_SO) - handle.sym('calloc') - end.join - end - end - - def test_sym - handle = DL::Handle.new(LIBC_SO) - assert_not_nil handle.sym('calloc') - assert_not_nil handle['calloc'] - end - - def test_handle_close - handle = DL::Handle.new(LIBC_SO) - assert_equal 0, handle.close - end - - def test_handle_close_twice - handle = DL::Handle.new(LIBC_SO) - handle.close - assert_raises(DL::DLError) do - handle.close - end - end - - def test_dlopen_returns_handle - assert_instance_of DL::Handle, dlopen(LIBC_SO) - end - - def test_dlopen_safe - assert_raises(SecurityError) do - Thread.new do - $SAFE = 2 - dlopen(LIBC_SO) - end.join - end - end - - def test_initialize_safe - assert_raises(SecurityError) do - Thread.new do - $SAFE = 2 - DL::Handle.new(LIBC_SO) - end.join - end - end - - def test_initialize_noargs - handle = DL::Handle.new - assert_not_nil handle['rb_str_new'] - end - - def test_initialize_flags - handle = DL::Handle.new(LIBC_SO, DL::RTLD_LAZY | DL::RTLD_GLOBAL) - assert_not_nil handle['calloc'] - end - - def test_enable_close - handle = DL::Handle.new(LIBC_SO) - assert !handle.close_enabled?, 'close is enabled' - - handle.enable_close - assert handle.close_enabled?, 'close is not enabled' - end - - def test_disable_close - handle = DL::Handle.new(LIBC_SO) - - handle.enable_close - assert handle.close_enabled?, 'close is enabled' - handle.disable_close - assert !handle.close_enabled?, 'close is enabled' - end - - def test_NEXT - begin - # Linux / Darwin - # - # There are two special pseudo-handles, RTLD_DEFAULT and RTLD_NEXT. The former will find - # the first occurrence of the desired symbol using the default library search order. The - # latter will find the next occurrence of a function in the search order after the current - # library. This allows one to provide a wrapper around a function in another shared - # library. - # --- Ubuntu Linux 8.04 dlsym(3) - handle = DL::Handle::NEXT - assert_not_nil handle['malloc'] - rescue - # BSD - # - # If dlsym() is called with the special handle RTLD_NEXT, then the search - # for the symbol is limited to the shared objects which were loaded after - # the one issuing the call to dlsym(). Thus, if the function is called - # from the main program, all the shared libraries are searched. If it is - # called from a shared library, all subsequent shared libraries are - # searched. RTLD_NEXT is useful for implementing wrappers around library - # functions. For example, a wrapper function getpid() could access the - # "real" getpid() with dlsym(RTLD_NEXT, "getpid"). (Actually, the dlfunc() - # interface, below, should be used, since getpid() is a function and not a - # data object.) - # --- FreeBSD 8.0 dlsym(3) - assert_in_out_err(['RUBYOPT' => '-W0'], <<-INPUT, /\A#\z/) - require 'dl' - require 'objspace' - print DL::Handle::NEXT.inspect - INPUT - end - end unless /mswin|mingw/ =~ RUBY_PLATFORM - - def test_DEFAULT - skip "DL::Handle::DEFAULT is not supported" if /mswin|mingw/ =~ RUBY_PLATFORM - handle = DL::Handle::DEFAULT - assert_not_nil handle['malloc'] - end unless /mswin|mingw/ =~ RUBY_PLATFORM - - def test_dlerror - # FreeBSD (at least 7.2 to 7.2) calls nsdispatch(3) when it calls - # getaddrinfo(3). And nsdispatch(3) doesn't call dlerror(3) even if - # it calls _nss_cache_cycle_prevention_function with dlsym(3). - # So our DL::Handle#sym must call dlerror(3) before call dlsym. - # In general uses of dlerror(3) should call it before use it. - require 'socket' - Socket.gethostbyname("localhost") - DL.dlopen("/lib/libc.so.7").sym('strcpy') - end if /freebsd/=~ RUBY_PLATFORM - end - - def test_no_memory_leak - assert_no_memory_leak(%w[-W0 -rdl.so], '', '100_000.times {DL::Handle.allocate}; GC.start', rss: true) - end -end diff --git a/test/dl/test_import.rb b/test/dl/test_import.rb deleted file mode 100644 index 8b3f39b851..0000000000 --- a/test/dl/test_import.rb +++ /dev/null @@ -1,165 +0,0 @@ -# coding: US-ASCII -require_relative 'test_base' -require 'dl/import' - -module DL - module LIBC - extend Importer - dlload LIBC_SO, LIBM_SO - - typealias 'string', 'char*' - typealias 'FILE*', 'void*' - - extern "void *strcpy(char*, char*)" - extern "int isdigit(int)" - extern "double atof(string)" - extern "unsigned long strtoul(char*, char **, int)" - extern "int qsort(void*, unsigned long, unsigned long, void*)" - extern "int fprintf(FILE*, char*)" - extern "int gettimeofday(timeval*, timezone*)" rescue nil - - QsortCallback = bind("void *qsort_callback(void*, void*)", :temp) - BoundQsortCallback = bind("void *bound_qsort_callback(void*, void*)"){|ptr1,ptr2| ptr1[0] <=> ptr2[0]} - Timeval = struct [ - "long tv_sec", - "long tv_usec", - ] - Timezone = struct [ - "int tz_minuteswest", - "int tz_dsttime", - ] - MyStruct = struct [ - "short num[5]", - "char c", - "unsigned char buff[7]", - ] - - CallCallback = bind("void call_callback(void*, void*)"){|ptr1, ptr2| - f = Function.new(CFunc.new(ptr1.to_i, TYPE_VOID, ""), [TYPE_VOIDP]) - f.call(ptr2) - } - CarriedFunction = bind("void callback_function(void*)", :carried, 0) - end - - class TestImport < TestBase - def test_ensure_call_dlload - err = assert_raises(RuntimeError) do - Class.new do - extend Importer - extern "void *strcpy(char*, char*)" - end - end - assert_match(/call dlload before/, err.message) - end - - def test_malloc() - s1 = LIBC::Timeval.malloc() - s2 = LIBC::Timeval.malloc() - assert_not_equal(s1.to_ptr.to_i, s2.to_ptr.to_i) - end - - def test_sizeof() - assert_equal(SIZEOF_VOIDP, LIBC.sizeof("FILE*")) - assert_equal(LIBC::MyStruct.size(), LIBC.sizeof(LIBC::MyStruct)) - end - - def test_unsigned_result() - d = (2 ** 31) + 1 - - r = LIBC.strtoul(d.to_s, 0, 0) - assert_equal(d, r) - end - - def test_io() - if( RUBY_PLATFORM != BUILD_RUBY_PLATFORM ) - return - end - io_in,io_out = IO.pipe() - LIBC.fprintf(io_out, "hello") - io_out.flush() - io_out.close() - str = io_in.read() - io_in.close() - assert_equal("hello", str) - end - - def test_value() - i = LIBC.value('int', 2) - assert_equal(2, i.value) - - d = LIBC.value('double', 2.0) - assert_equal(2.0, d.value) - - ary = LIBC.value('int[3]', [0,1,2]) - assert_equal([0,1,2], ary.value) - end - - def test_carried_function() - data1 = "data" - data2 = nil - LIBC.call_callback(LIBC::CarriedFunction, LIBC::CarriedFunction.create_carrier(data1)){|d| - data2 = d - } - assert_equal(data1, data2) - end - - def test_struct() - s = LIBC::MyStruct.malloc() - s.num = [0,1,2,3,4] - s.c = ?a.ord - s.buff = "012345\377" - assert_equal([0,1,2,3,4], s.num) - assert_equal(?a.ord, s.c) - assert_equal([?0.ord,?1.ord,?2.ord,?3.ord,?4.ord,?5.ord,?\377.ord], s.buff) - end - - def test_gettimeofday() - if( defined?(LIBC.gettimeofday) ) - timeval = LIBC::Timeval.malloc() - timezone = LIBC::Timezone.malloc() - LIBC.gettimeofday(timeval, timezone) - cur = Time.now() - assert(cur.to_i - 2 <= timeval.tv_sec && timeval.tv_sec <= cur.to_i) - end - end - - def test_strcpy() - buff = "000" - str = LIBC.strcpy(buff, "123") - assert_equal("123", buff) - assert_equal("123", str.to_s) - end - - def test_isdigit() - r1 = LIBC.isdigit(?1.ord) - r2 = LIBC.isdigit(?2.ord) - rr = LIBC.isdigit(?r.ord) - assert_positive(r1) - assert_positive(r2) - assert_zero(rr) - end - - def test_atof() - r = LIBC.atof("12.34") - assert_match(12.00..13.00, r) - end - - def test_strtod() - f = Function.new(CFunc.new(@libc['strtod'], TYPE_DOUBLE, 'strtod'), - [TYPE_VOIDP, TYPE_VOIDP]) - buff1 = "12.34" - buff2 = " " - r = f.call(buff1, buff2) - assert_match(12.00..13.00, r) - end - - def test_qsort() - buff = "9341" - LIBC.qsort(buff, buff.size, 1, LIBC::QsortCallback){|ptr1,ptr2| ptr1[0] <=> ptr2[0]} - assert_equal("1349", buff) - buff = "9341" - LIBC.qsort(buff, buff.size, 1, LIBC::BoundQsortCallback) - assert_equal("1349", buff) - end - end -end diff --git a/test/dl/test_win32.rb b/test/dl/test_win32.rb deleted file mode 100644 index d0c4571bf8..0000000000 --- a/test/dl/test_win32.rb +++ /dev/null @@ -1,54 +0,0 @@ -require_relative 'test_base' -require 'dl/import' -require 'dl/types' - -class DL::TestWin32 < DL::TestBase -module Win32API - extend DL::Importer - - dlload "kernel32.dll" - - include DL::Win32Types - - OSVERSIONINFO = struct [ - "DWORD dwOSVersionInfoSize", - "DWORD dwMajorVersion", - "DWORD dwMinorVersion", - "DWORD dwBuildNumber", - "DWORD dwPlatformId", - "UCHAR szCSDVersion[128]", - ] - - typealias "POSVERSIONINFO", "OSVERSIONINFO*" - - extern "BOOL GetVersionEx(POSVERSIONINFO)", :stdcall - - def get_version_ex() - ptr = OSVERSIONINFO.malloc() - ptr.dwOSVersionInfoSize = OSVERSIONINFO.size - ret = GetVersionEx(ptr) - if( ret ) - ptr - else - nil - end - end - module_function :get_version_ex -rescue DL::DLError -end - -if defined?(Win32API::OSVERSIONINFO) - def test_version() - platform = Win32API.get_version_ex().dwPlatformId - case ENV['OS'] - when 'Windows_NT' - expect = 2 - when /Windows.+/ - expect = 1 - else - expect = 0 - end - assert_equal(expect, platform) - end -end -end diff --git a/test/ruby/memory_status.rb b/test/ruby/memory_status.rb index 5347e34cb6..6c6b3f8934 100644 --- a/test/ruby/memory_status.rb +++ b/test/ruby/memory_status.rb @@ -15,36 +15,13 @@ module Memory read_status {|k, v| keys << k; vals << v} when /mswin|mingw/ =~ RUBY_PLATFORM - begin - require 'fiddle/import' - rescue LoadError - $LOAD_PATH.unshift File.join(File.join(__dir__, '..'), 'lib') - require_relative 'envutil' - EnvUtil.suppress_warning do - require 'dl/import' - end - end - begin - require 'fiddle/types' - rescue LoadError - require_relative 'envutil' - EnvUtil.suppress_warning do - require 'dl/types' - end - end + require 'fiddle/import' + require 'fiddle/types' module Win32 - begin - extend Fiddle::Importer - rescue NameError - extend DL::Importer - end + extend Fiddle::Importer dlload "kernel32.dll", "psapi.dll" - begin - include Fiddle::Win32Types - rescue NameError - include DL::Win32Types - end + include Fiddle::Win32Types typealias "SIZE_T", "size_t" PROCESS_MEMORY_COUNTERS = struct [ -- cgit v1.2.3