2017-09-12 07:52:23 -04:00
|
|
|
# frozen_string_literal: true
|
2013-05-25 10:29:02 -04:00
|
|
|
begin
|
|
|
|
require_relative 'helper'
|
|
|
|
rescue LoadError
|
|
|
|
end
|
2012-11-27 14:54:50 -05:00
|
|
|
|
|
|
|
module Fiddle
|
|
|
|
class TestFunc < TestCase
|
|
|
|
def test_random
|
|
|
|
f = Function.new(@libc['srand'], [-TYPE_LONG], TYPE_VOID)
|
|
|
|
assert_nil f.call(10)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_sinf
|
|
|
|
begin
|
|
|
|
f = Function.new(@libm['sinf'], [TYPE_FLOAT], TYPE_FLOAT)
|
|
|
|
rescue Fiddle::DLError
|
2021-09-04 15:54:25 -04:00
|
|
|
omit "libm may not have sinf()"
|
2012-11-27 14:54:50 -05:00
|
|
|
end
|
|
|
|
assert_in_delta 1.0, f.call(90 * Math::PI / 180), 0.0001
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_sin
|
|
|
|
f = Function.new(@libm['sin'], [TYPE_DOUBLE], TYPE_DOUBLE)
|
|
|
|
assert_in_delta 1.0, f.call(90 * Math::PI / 180), 0.0001
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_string
|
2021-09-04 15:54:25 -04:00
|
|
|
under_gc_stress do
|
|
|
|
f = Function.new(@libc['strcpy'], [TYPE_VOIDP, TYPE_VOIDP], TYPE_VOIDP)
|
|
|
|
buff = +"000"
|
|
|
|
str = f.call(buff, "123")
|
|
|
|
assert_equal("123", buff)
|
|
|
|
assert_equal("123", str.to_s)
|
|
|
|
end
|
2012-11-27 14:54:50 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_isdigit
|
|
|
|
f = Function.new(@libc['isdigit'], [TYPE_INT], TYPE_INT)
|
|
|
|
r1 = f.call(?1.ord)
|
|
|
|
r2 = f.call(?2.ord)
|
|
|
|
rr = f.call(?r.ord)
|
|
|
|
assert_operator r1, :>, 0
|
|
|
|
assert_operator r2, :>, 0
|
|
|
|
assert_equal 0, rr
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_atof
|
|
|
|
f = Function.new(@libc['atof'], [TYPE_VOIDP], TYPE_DOUBLE)
|
|
|
|
r = f.call("12.34")
|
|
|
|
assert_includes(12.00..13.00, r)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_strtod
|
|
|
|
f = Function.new(@libc['strtod'], [TYPE_VOIDP, TYPE_VOIDP], TYPE_DOUBLE)
|
|
|
|
buff1 = Pointer["12.34"]
|
|
|
|
buff2 = buff1 + 4
|
|
|
|
r = f.call(buff1, - buff2)
|
|
|
|
assert_in_delta(12.34, r, 0.001)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_qsort1
|
2022-09-14 17:38:52 -04:00
|
|
|
closure_class = Class.new(Closure) do
|
2012-11-27 14:54:50 -05:00
|
|
|
def call(x, y)
|
|
|
|
Pointer.new(x)[0] <=> Pointer.new(y)[0]
|
|
|
|
end
|
2022-09-14 17:38:52 -04:00
|
|
|
end
|
2012-11-27 14:54:50 -05:00
|
|
|
|
2022-09-14 17:38:52 -04:00
|
|
|
closure_class.create(TYPE_INT, [TYPE_VOIDP, TYPE_VOIDP]) do |callback|
|
|
|
|
qsort = Function.new(@libc['qsort'],
|
|
|
|
[TYPE_VOIDP, TYPE_SIZE_T, TYPE_SIZE_T, TYPE_VOIDP],
|
|
|
|
TYPE_VOID)
|
|
|
|
buff = "9341"
|
|
|
|
qsort.call(buff, buff.size, 1, callback)
|
|
|
|
assert_equal("1349", buff)
|
2012-11-27 14:54:50 -05:00
|
|
|
|
2022-09-14 17:38:52 -04:00
|
|
|
bug4929 = '[ruby-core:37395]'
|
|
|
|
buff = "9341"
|
|
|
|
under_gc_stress do
|
|
|
|
qsort.call(buff, buff.size, 1, callback)
|
|
|
|
end
|
|
|
|
assert_equal("1349", buff, bug4929)
|
2021-09-04 15:54:25 -04:00
|
|
|
end
|
2022-09-09 10:59:41 -04:00
|
|
|
ensure
|
|
|
|
# Ensure freeing all closures.
|
|
|
|
# See https://github.com/ruby/fiddle/issues/102#issuecomment-1241763091 .
|
2022-09-14 17:38:52 -04:00
|
|
|
not_freed_closures = []
|
|
|
|
ObjectSpace.each_object(Fiddle::Closure) do |closure|
|
|
|
|
not_freed_closures << closure unless closure.freed?
|
|
|
|
end
|
|
|
|
assert_equal([], not_freed_closures)
|
2012-11-27 14:54:50 -05:00
|
|
|
end
|
2020-06-26 18:25:47 -04:00
|
|
|
|
|
|
|
def test_snprintf
|
2020-06-27 21:34:01 -04:00
|
|
|
unless Fiddle.const_defined?("TYPE_VARIADIC")
|
2021-09-04 15:54:25 -04:00
|
|
|
omit "libffi doesn't support variadic arguments"
|
2020-06-27 21:34:01 -04:00
|
|
|
end
|
2020-06-26 19:31:59 -04:00
|
|
|
if Fiddle::WINDOWS
|
|
|
|
snprintf_name = "_snprintf"
|
|
|
|
else
|
|
|
|
snprintf_name = "snprintf"
|
|
|
|
end
|
2020-06-26 22:01:22 -04:00
|
|
|
begin
|
|
|
|
snprintf_pointer = @libc[snprintf_name]
|
|
|
|
rescue Fiddle::DLError
|
2021-09-04 15:54:25 -04:00
|
|
|
omit "Can't find #{snprintf_name}: #{$!.message}"
|
2020-06-26 22:01:22 -04:00
|
|
|
end
|
|
|
|
snprintf = Function.new(snprintf_pointer,
|
2020-06-26 18:25:47 -04:00
|
|
|
[
|
2020-07-09 08:39:51 -04:00
|
|
|
:voidp,
|
|
|
|
:size_t,
|
|
|
|
:const_string,
|
|
|
|
:variadic,
|
2020-06-26 18:25:47 -04:00
|
|
|
],
|
2020-07-09 08:39:51 -04:00
|
|
|
:int)
|
2020-06-26 18:25:47 -04:00
|
|
|
output_buffer = " " * 1024
|
|
|
|
output = Pointer[output_buffer]
|
|
|
|
|
|
|
|
written = snprintf.call(output,
|
|
|
|
output.size,
|
2020-07-09 07:19:20 -04:00
|
|
|
"int: %d, string: %.*s, const string: %s\n",
|
2020-07-09 08:39:51 -04:00
|
|
|
:int, -29,
|
|
|
|
:int, 4,
|
|
|
|
:voidp, "Hello",
|
|
|
|
:const_string, "World")
|
2020-07-09 07:19:20 -04:00
|
|
|
assert_equal("int: -29, string: Hell, const string: World\n",
|
2020-06-26 18:25:47 -04:00
|
|
|
output_buffer[0, written])
|
|
|
|
|
2020-07-09 07:19:20 -04:00
|
|
|
string_like_class = Class.new do
|
|
|
|
def initialize(string)
|
|
|
|
@string = string
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_str
|
|
|
|
@string
|
|
|
|
end
|
|
|
|
end
|
2020-06-26 18:25:47 -04:00
|
|
|
written = snprintf.call(output,
|
|
|
|
output.size,
|
2020-07-09 07:19:20 -04:00
|
|
|
"string: %.*s, const string: %s, uint: %u\n",
|
2020-07-09 08:39:51 -04:00
|
|
|
:int, 2,
|
|
|
|
:voidp, "Hello",
|
|
|
|
:const_string, string_like_class.new("World"),
|
|
|
|
:int, 29)
|
2020-07-09 07:19:20 -04:00
|
|
|
assert_equal("string: He, const string: World, uint: 29\n",
|
2020-06-26 18:25:47 -04:00
|
|
|
output_buffer[0, written])
|
|
|
|
end
|
2012-11-27 14:54:50 -05:00
|
|
|
end
|
2013-05-25 10:29:02 -04:00
|
|
|
end if defined?(Fiddle)
|