mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
added new files.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@7883 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
4ae9132605
commit
bda37095ca
17 changed files with 2555 additions and 0 deletions
11
ext/dl/test/test_all.rb
Normal file
11
ext/dl/test/test_all.rb
Normal file
|
@ -0,0 +1,11 @@
|
|||
require 'test_base'
|
||||
require 'dl/import'
|
||||
|
||||
require 'test_dl2'
|
||||
require 'test_func'
|
||||
require 'test_import'
|
||||
|
||||
case RUBY_PLATFORM
|
||||
when /cygwin/, /mingw32/, /mswin32/
|
||||
require 'test_win32'
|
||||
end
|
52
ext/dl/test/test_base.rb
Normal file
52
ext/dl/test/test_base.rb
Normal file
|
@ -0,0 +1,52 @@
|
|||
require 'test/unit'
|
||||
require 'dl'
|
||||
|
||||
case RUBY_PLATFORM
|
||||
when /cygwin/
|
||||
LIBC_SO = "cygwin1.dll"
|
||||
LIBM_SO = "cygwin1.dll"
|
||||
when /linux/
|
||||
LIBC_SO = "/lib/libc.so.6"
|
||||
LIBM_SO = "/lib/libm.so.6"
|
||||
when /mingw/, /msvcrt/
|
||||
LIBC_SO = "C:\\WINDOWS\\system32\\msvcrt.dll"
|
||||
LIBM_SO = "C:\\WINDOWS\\system32\\msvcrt.dll"
|
||||
else
|
||||
LIBC_SO = ARGV[0]
|
||||
LIBM_SO = ARGV[1]
|
||||
if( !(LIBC_SO && LIBM_SO) )
|
||||
$stderr.puts("#{$0} <libc> <libm>")
|
||||
exit
|
||||
end
|
||||
end
|
||||
|
||||
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(expected === actual, message)
|
||||
end
|
||||
|
||||
def assert_positive(actual)
|
||||
assert(actual > 0)
|
||||
end
|
||||
|
||||
def assert_zero(actual)
|
||||
assert(actual == 0)
|
||||
end
|
||||
|
||||
def assert_negative(actual)
|
||||
assert(actual < 0)
|
||||
end
|
||||
|
||||
def test_empty()
|
||||
end
|
||||
end
|
||||
end
|
89
ext/dl/test/test_dl2.rb
Normal file
89
ext/dl/test/test_dl2.rb
Normal file
|
@ -0,0 +1,89 @@
|
|||
require 'test_base.rb'
|
||||
require 'dl/callback'
|
||||
|
||||
module DL
|
||||
class TestDL < TestBase
|
||||
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_match(0.09..0.11, x)
|
||||
|
||||
cfunc = CFunc.new(@libc['atof'], TYPE_DOUBLE, 'atof')
|
||||
x = cfunc.call(["-0.1"].pack("p").unpack("l!*"))
|
||||
assert_match(-0.11 .. -0.09, x)
|
||||
end
|
||||
|
||||
def test_sin()
|
||||
cfunc = CFunc.new(@libm['sin'], TYPE_DOUBLE, 'sin')
|
||||
x = cfunc.call([3.14/2].pack("d").unpack("l!*"))
|
||||
assert_equal(x, Math.sin(3.14/2))
|
||||
|
||||
cfunc = CFunc.new(@libm['sin'], TYPE_DOUBLE, 'sin')
|
||||
x = cfunc.call([-3.14/2].pack("d").unpack("l!*"))
|
||||
assert_equal(Math.sin(-3.14/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([buff,str].pack("pp").unpack("l!*"))
|
||||
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([buff,str,3].pack("ppi").unpack("l!*"))
|
||||
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,str].pack("lp").unpack("l!*"))
|
||||
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([buff, buff.size, 1, cb].pack("pI!I!L!").unpack("l!*"))
|
||||
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
|
||||
end
|
||||
end # module DL
|
62
ext/dl/test/test_func.rb
Normal file
62
ext/dl/test/test_func.rb
Normal file
|
@ -0,0 +1,62 @@
|
|||
require 'test_base'
|
||||
require 'dl/func'
|
||||
|
||||
module DL
|
||||
class TestFunc < TestBase
|
||||
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_isdigit()
|
||||
f = Function.new(CFunc.new(@libc['isdigit'], TYPE_INT, 'isdigit'),
|
||||
[TYPE_INT])
|
||||
r1 = f.call(?1)
|
||||
r2 = f.call(?2)
|
||||
rr = f.call(?r)
|
||||
assert_positive(r1)
|
||||
assert_positive(r2)
|
||||
assert_zero(rr)
|
||||
end
|
||||
|
||||
def test_atof()
|
||||
f = Function.new(CFunc.new(@libc['atof'], TYPE_FLOAT, '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 = "12.34"
|
||||
buff2 = " "
|
||||
r = f.call(buff1, buff2)
|
||||
assert_match(12.00..13.00, r)
|
||||
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)
|
||||
end
|
||||
|
||||
def test_qsort2()
|
||||
cb = TempFunction.new(CFunc.new(0, TYPE_INT, '<callback>qsort'),
|
||||
[TYPE_VOIDP, TYPE_VOIDP])
|
||||
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){|x,y| CPtr.new(x)[0] <=> CPtr.new(y)[0]}
|
||||
assert_equal("1349", buff)
|
||||
end
|
||||
end
|
||||
end
|
137
ext/dl/test/test_import.rb
Normal file
137
ext/dl/test/test_import.rb
Normal file
|
@ -0,0 +1,137 @@
|
|||
require '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 "float atof(string)"
|
||||
extern "unsigned long strtoul(char*, char **, int)"
|
||||
extern "int qsort(void*, int, int, void*)"
|
||||
extern "void fprintf(FILE*, char*)"
|
||||
extern "int gettimeofday(timeval*, timezone*)" rescue nil
|
||||
|
||||
QsortCallback = bind("void *qsort_callback(void*, void*)", :temp)
|
||||
BoundQsortCallback = bind("void *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 [
|
||||
"int num[10]",
|
||||
"unsigned char buff[8]",
|
||||
]
|
||||
|
||||
CallCallback = bind("void call_callback(void*, void*)"){|ptr1, ptr2|
|
||||
f = Function.new(CFunc.new(ptr1.to_i, DL::TYPE_VOID, "<anonymous>"), [TYPE_VOIDP])
|
||||
f.call(ptr2)
|
||||
}
|
||||
CarriedFunction = bind("void callback_function(void*)", :carried, 0)
|
||||
end
|
||||
|
||||
class TestImport < TestBase
|
||||
def test_unsigned_result()
|
||||
d = (2 ** 31) + 1
|
||||
|
||||
r = LIBC.strtoul(d.to_s, 0, 0)
|
||||
assert_equal(d, r)
|
||||
end
|
||||
|
||||
def test_io()
|
||||
io_in,io_out = IO.pipe()
|
||||
LIBC.fprintf(DL::CPtr[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,5,6,7,8,9]
|
||||
s.buff = "0123456\377"
|
||||
assert_equal([0,1,2,3,4,5,6,7,8,9], s.num)
|
||||
assert_equal([?0,?1,?2,?3,?4,?5,?6,?\377], 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)
|
||||
r2 = LIBC.isdigit(?2)
|
||||
rr = LIBC.isdigit(?r)
|
||||
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
|
53
ext/dl/test/test_win32.rb
Normal file
53
ext/dl/test/test_win32.rb
Normal file
|
@ -0,0 +1,53 @@
|
|||
require 'test_base'
|
||||
require 'dl/import'
|
||||
require 'dl/types'
|
||||
|
||||
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
|
||||
end
|
||||
|
||||
module DL
|
||||
class TestWin32 < TestBase
|
||||
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
|
Loading…
Add table
Add a link
Reference in a new issue