mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
[ruby/fiddle] Use test-unit gem (https://github.com/ruby/fiddle/pull/69)
https://github.com/ruby/fiddle/commit/e08c4c635e Co-authored-by: Sutou Kouhei <kou@clear-code.com>
This commit is contained in:
parent
afa33da72b
commit
8f752c95d2
Notes:
git
2021-09-05 19:26:21 +09:00
8 changed files with 79 additions and 37 deletions
|
@ -58,9 +58,5 @@ Gem::Specification.new do |spec|
|
||||||
|
|
||||||
spec.required_ruby_version = ">= 2.5.0"
|
spec.required_ruby_version = ">= 2.5.0"
|
||||||
|
|
||||||
spec.add_development_dependency "bundler"
|
|
||||||
spec.add_development_dependency "rake"
|
|
||||||
spec.add_development_dependency "rake-compiler"
|
|
||||||
|
|
||||||
spec.metadata["msys2_mingw_dependencies"] = "libffi"
|
spec.metadata["msys2_mingw_dependencies"] = "libffi"
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require 'rbconfig/sizeof'
|
||||||
require 'test/unit'
|
require 'test/unit'
|
||||||
require 'fiddle'
|
require 'fiddle'
|
||||||
|
|
||||||
|
@ -165,5 +167,12 @@ module Fiddle
|
||||||
GC.start
|
GC.start
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def under_gc_stress
|
||||||
|
stress, GC.stress = GC.stress, true
|
||||||
|
yield
|
||||||
|
ensure
|
||||||
|
GC.stress = stress
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -326,7 +326,7 @@ module Fiddle
|
||||||
|
|
||||||
def test_signature_variadic_arguments
|
def test_signature_variadic_arguments
|
||||||
unless Fiddle.const_defined?("TYPE_VARIADIC")
|
unless Fiddle.const_defined?("TYPE_VARIADIC")
|
||||||
skip "libffi doesn't support variadic arguments"
|
omit "libffi doesn't support variadic arguments"
|
||||||
end
|
end
|
||||||
assert_equal([
|
assert_equal([
|
||||||
"printf",
|
"printf",
|
||||||
|
|
|
@ -15,7 +15,7 @@ module Fiddle
|
||||||
begin
|
begin
|
||||||
f = Function.new(@libm['sinf'], [TYPE_FLOAT], TYPE_FLOAT)
|
f = Function.new(@libm['sinf'], [TYPE_FLOAT], TYPE_FLOAT)
|
||||||
rescue Fiddle::DLError
|
rescue Fiddle::DLError
|
||||||
skip "libm may not have sinf()"
|
omit "libm may not have sinf()"
|
||||||
end
|
end
|
||||||
assert_in_delta 1.0, f.call(90 * Math::PI / 180), 0.0001
|
assert_in_delta 1.0, f.call(90 * Math::PI / 180), 0.0001
|
||||||
end
|
end
|
||||||
|
@ -26,14 +26,13 @@ module Fiddle
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_string
|
def test_string
|
||||||
stress, GC.stress = GC.stress, true
|
under_gc_stress do
|
||||||
f = Function.new(@libc['strcpy'], [TYPE_VOIDP, TYPE_VOIDP], TYPE_VOIDP)
|
f = Function.new(@libc['strcpy'], [TYPE_VOIDP, TYPE_VOIDP], TYPE_VOIDP)
|
||||||
buff = +"000"
|
buff = +"000"
|
||||||
str = f.call(buff, "123")
|
str = f.call(buff, "123")
|
||||||
assert_equal("123", buff)
|
assert_equal("123", buff)
|
||||||
assert_equal("123", str.to_s)
|
assert_equal("123", str.to_s)
|
||||||
ensure
|
end
|
||||||
GC.stress = stress
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_isdigit
|
def test_isdigit
|
||||||
|
@ -76,13 +75,15 @@ module Fiddle
|
||||||
|
|
||||||
bug4929 = '[ruby-core:37395]'
|
bug4929 = '[ruby-core:37395]'
|
||||||
buff = "9341"
|
buff = "9341"
|
||||||
EnvUtil.under_gc_stress {qsort.call(buff, buff.size, 1, cb)}
|
under_gc_stress do
|
||||||
|
qsort.call(buff, buff.size, 1, cb)
|
||||||
|
end
|
||||||
assert_equal("1349", buff, bug4929)
|
assert_equal("1349", buff, bug4929)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_snprintf
|
def test_snprintf
|
||||||
unless Fiddle.const_defined?("TYPE_VARIADIC")
|
unless Fiddle.const_defined?("TYPE_VARIADIC")
|
||||||
skip "libffi doesn't support variadic arguments"
|
omit "libffi doesn't support variadic arguments"
|
||||||
end
|
end
|
||||||
if Fiddle::WINDOWS
|
if Fiddle::WINDOWS
|
||||||
snprintf_name = "_snprintf"
|
snprintf_name = "_snprintf"
|
||||||
|
@ -92,7 +93,7 @@ module Fiddle
|
||||||
begin
|
begin
|
||||||
snprintf_pointer = @libc[snprintf_name]
|
snprintf_pointer = @libc[snprintf_name]
|
||||||
rescue Fiddle::DLError
|
rescue Fiddle::DLError
|
||||||
skip "Can't find #{snprintf_name}: #{$!.message}"
|
omit "Can't find #{snprintf_name}: #{$!.message}"
|
||||||
end
|
end
|
||||||
snprintf = Function.new(snprintf_pointer,
|
snprintf = Function.new(snprintf_pointer,
|
||||||
[
|
[
|
||||||
|
|
|
@ -164,7 +164,7 @@ module Fiddle
|
||||||
begin
|
begin
|
||||||
poll = @libc['poll']
|
poll = @libc['poll']
|
||||||
rescue Fiddle::DLError
|
rescue Fiddle::DLError
|
||||||
skip 'poll(2) not available'
|
omit 'poll(2) not available'
|
||||||
end
|
end
|
||||||
f = Function.new(poll, [TYPE_VOIDP, TYPE_INT, TYPE_INT], TYPE_INT)
|
f = Function.new(poll, [TYPE_VOIDP, TYPE_INT, TYPE_INT], TYPE_INT)
|
||||||
|
|
||||||
|
@ -180,9 +180,37 @@ module Fiddle
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_no_memory_leak
|
def test_no_memory_leak
|
||||||
prep = 'r = Fiddle::Function.new(Fiddle.dlopen(nil)["rb_obj_frozen_p"], [Fiddle::TYPE_UINTPTR_T], Fiddle::TYPE_UINTPTR_T); a = "a"'
|
if respond_to?(:assert_nothing_leaked_memory)
|
||||||
code = 'begin r.call(a); rescue TypeError; end'
|
rb_obj_frozen_p_symbol = Fiddle.dlopen(nil)["rb_obj_frozen_p"]
|
||||||
assert_no_memory_leak(%w[-W0 -rfiddle], "#{prep}\n1000.times{#{code}}", "10_000.times {#{code}}", limit: 1.2)
|
rb_obj_frozen_p = Fiddle::Function.new(rb_obj_frozen_p_symbol,
|
||||||
|
[Fiddle::TYPE_UINTPTR_T],
|
||||||
|
Fiddle::TYPE_UINTPTR_T)
|
||||||
|
a = "a"
|
||||||
|
n_tries = 100_000
|
||||||
|
n_tries.times do
|
||||||
|
begin
|
||||||
|
a + 1
|
||||||
|
rescue TypeError
|
||||||
|
end
|
||||||
|
end
|
||||||
|
n_arguments = 1
|
||||||
|
sizeof_fiddle_generic = Fiddle::SIZEOF_VOIDP # Rough
|
||||||
|
size_per_try =
|
||||||
|
(sizeof_fiddle_generic * n_arguments) +
|
||||||
|
(Fiddle::SIZEOF_VOIDP * (n_arguments + 1))
|
||||||
|
assert_nothing_leaked_memory(size_per_try * n_tries) do
|
||||||
|
n_tries.times do
|
||||||
|
begin
|
||||||
|
rb_obj_frozen_p.call(a)
|
||||||
|
rescue TypeError
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
prep = 'r = Fiddle::Function.new(Fiddle.dlopen(nil)["rb_obj_frozen_p"], [Fiddle::TYPE_UINTPTR_T], Fiddle::TYPE_UINTPTR_T); a = "a"'
|
||||||
|
code = 'begin r.call(a); rescue TypeError; end'
|
||||||
|
assert_no_memory_leak(%w[-W0 -rfiddle], "#{prep}\n1000.times{#{code}}", "10_000.times {#{code}}", limit: 1.2)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
|
@ -185,7 +185,16 @@ module Fiddle
|
||||||
end if /freebsd/=~ RUBY_PLATFORM
|
end if /freebsd/=~ RUBY_PLATFORM
|
||||||
|
|
||||||
def test_no_memory_leak
|
def test_no_memory_leak
|
||||||
assert_no_memory_leak(%w[-W0 -rfiddle.so], '', '100_000.times {Fiddle::Handle.allocate}; GC.start', rss: true)
|
if respond_to?(:assert_nothing_leaked_memory)
|
||||||
|
n_tries = 100_000
|
||||||
|
assert_nothing_leaked_memory(SIZEOF_VOIDP * (n_tries / 100)) do
|
||||||
|
n_tries.times do
|
||||||
|
Fiddle::Handle.allocate
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
assert_no_memory_leak(%w[-W0 -rfiddle.so], '', '100_000.times {Fiddle::Handle.allocate}; GC.start', rss: true)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if /cygwin|mingw|mswin/ =~ RUBY_PLATFORM
|
if /cygwin|mingw|mswin/ =~ RUBY_PLATFORM
|
||||||
|
|
|
@ -14,7 +14,7 @@ end
|
||||||
module Fiddle
|
module Fiddle
|
||||||
class TestMemoryView < TestCase
|
class TestMemoryView < TestCase
|
||||||
def setup
|
def setup
|
||||||
skip "MemoryView is unavailable" unless defined? Fiddle::MemoryView
|
omit "MemoryView is unavailable" unless defined? Fiddle::MemoryView
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_null_ptr
|
def test_null_ptr
|
||||||
|
@ -49,7 +49,7 @@ module Fiddle
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_memory_view_multi_dimensional
|
def test_memory_view_multi_dimensional
|
||||||
skip "MemoryViewTestUtils is unavailable" unless defined? MemoryViewTestUtils
|
omit "MemoryViewTestUtils is unavailable" unless defined? MemoryViewTestUtils
|
||||||
|
|
||||||
buf = [ 1, 2, 3, 4,
|
buf = [ 1, 2, 3, 4,
|
||||||
5, 6, 7, 8,
|
5, 6, 7, 8,
|
||||||
|
@ -71,7 +71,7 @@ module Fiddle
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_memory_view_multi_dimensional_with_strides
|
def test_memory_view_multi_dimensional_with_strides
|
||||||
skip "MemoryViewTestUtils is unavailable" unless defined? MemoryViewTestUtils
|
omit "MemoryViewTestUtils is unavailable" unless defined? MemoryViewTestUtils
|
||||||
|
|
||||||
buf = [ 1, 2, 3, 4, 5, 6, 7, 8,
|
buf = [ 1, 2, 3, 4, 5, 6, 7, 8,
|
||||||
9, 10, 11, 12, 13, 14, 15, 16 ].pack("l!*")
|
9, 10, 11, 12, 13, 14, 15, 16 ].pack("l!*")
|
||||||
|
@ -93,7 +93,7 @@ module Fiddle
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_memory_view_multi_dimensional_with_multiple_members
|
def test_memory_view_multi_dimensional_with_multiple_members
|
||||||
skip "MemoryViewTestUtils is unavailable" unless defined? MemoryViewTestUtils
|
omit "MemoryViewTestUtils is unavailable" unless defined? MemoryViewTestUtils
|
||||||
|
|
||||||
buf = [ 1, 2, 3, 4, 5, 6, 7, 8,
|
buf = [ 1, 2, 3, 4, 5, 6, 7, 8,
|
||||||
-1, -2, -3, -4, -5, -6, -7, -8].pack("s*")
|
-1, -2, -3, -4, -5, -6, -7, -8].pack("s*")
|
||||||
|
|
|
@ -179,16 +179,6 @@ module Fiddle
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_free=
|
def test_free=
|
||||||
assert_normal_exit(<<-"End", '[ruby-dev:39269]')
|
|
||||||
require 'fiddle'
|
|
||||||
include Fiddle
|
|
||||||
free = Fiddle::Function.new(Fiddle::RUBY_FREE, [TYPE_VOIDP], TYPE_VOID)
|
|
||||||
ptr = Fiddle::Pointer.malloc(4)
|
|
||||||
ptr.free = free
|
|
||||||
free.ptr
|
|
||||||
ptr.free.ptr
|
|
||||||
End
|
|
||||||
|
|
||||||
free = Function.new(Fiddle::RUBY_FREE, [TYPE_VOIDP], TYPE_VOID)
|
free = Function.new(Fiddle::RUBY_FREE, [TYPE_VOIDP], TYPE_VOID)
|
||||||
ptr = Pointer.malloc(4)
|
ptr = Pointer.malloc(4)
|
||||||
ptr.free = free
|
ptr.free = free
|
||||||
|
@ -282,7 +272,16 @@ module Fiddle
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_no_memory_leak
|
def test_no_memory_leak
|
||||||
assert_no_memory_leak(%w[-W0 -rfiddle.so], '', '100_000.times {Fiddle::Pointer.allocate}', rss: true)
|
if respond_to?(:assert_nothing_leaked_memory)
|
||||||
|
n_tries = 100_000
|
||||||
|
assert_nothing_leaked_memory(SIZEOF_VOIDP * (n_tries / 100)) do
|
||||||
|
n_tries.times do
|
||||||
|
Fiddle::Pointer.allocate
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
assert_no_memory_leak(%w[-W0 -rfiddle.so], '', '100_000.times {Fiddle::Pointer.allocate}', rss: true)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end if defined?(Fiddle)
|
end if defined?(Fiddle)
|
||||||
|
|
Loading…
Add table
Reference in a new issue