summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-03-03 06:03:23 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-03-03 06:03:23 +0000
commitf9ca2119e403653cdf529646d3a3790c5c473ac2 (patch)
treebc0355516dd1ea4e6d90f100726c337502b05dce /test
parent078928bbdd66586ee18fce8fa4485ac31d960df3 (diff)
* test/dl: moved from ext/dl/test.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@22717 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test')
-rw-r--r--test/dl/test_base.rb70
-rw-r--r--test/dl/test_dl2.rb111
-rw-r--r--test/dl/test_func.rb62
-rw-r--r--test/dl/test_import.rb154
-rw-r--r--test/dl/test_win32.rb54
5 files changed, 451 insertions, 0 deletions
diff --git a/test/dl/test_base.rb b/test/dl/test_base.rb
new file mode 100644
index 00000000000..11243dfebdb
--- /dev/null
+++ b/test/dl/test_base.rb
@@ -0,0 +1,70 @@
+require 'test/unit'
+require 'dl'
+
+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/, /mswin32/
+ LIBC_SO = "msvcrt.dll"
+ LIBM_SO = "msvcrt.dll"
+when /darwin/
+ LIBC_SO = "/usr/lib/libc.dylib"
+ LIBM_SO = "/usr/lib/libm.dylib"
+when /bsd/
+ LIBC_SO = "/usr/lib/libc.so"
+ LIBM_SO = "/usr/lib/libm.so"
+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
diff --git a/test/dl/test_dl2.rb b/test/dl/test_dl2.rb
new file mode 100644
index 00000000000..3c854aea8f4
--- /dev/null
+++ b/test/dl/test_dl2.rb
@@ -0,0 +1,111 @@
+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("ppL!").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("l!p").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("pL!L!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
+
+ def test_cptr()
+ 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"
+ ptr[0] = "c".ord
+ check.call(str, ptr)
+ str[0,2] = "aa"
+ ptr[0,2] = "aa"
+ check.call(str, ptr)
+ end
+end
+end # module DL
diff --git a/test/dl/test_func.rb b/test/dl/test_func.rb
new file mode 100644
index 00000000000..be937719dfa
--- /dev/null
+++ b/test/dl/test_func.rb
@@ -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.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 = "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
diff --git a/test/dl/test_import.rb b/test/dl/test_import.rb
new file mode 100644
index 00000000000..8514f8298ec
--- /dev/null
+++ b/test/dl/test_import.rb
@@ -0,0 +1,154 @@
+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 "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 *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, DL::TYPE_VOID, "<anonymous>"), [TYPE_VOIDP])
+ f.call(ptr2)
+ }
+ CarriedFunction = bind("void callback_function(void*)", :carried, 0)
+ end
+
+ class TestImport < TestBase
+ 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(DL::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 != DL::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
new file mode 100644
index 00000000000..b40e3690d8b
--- /dev/null
+++ b/test/dl/test_win32.rb
@@ -0,0 +1,54 @@
+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
+rescue DL::DLError
+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 if defined?(Win32API::OSVERSIONINFO)