mirror of
https://github.com/ruby/ruby.git
synced 2025-09-18 01:54:00 +02:00
* ext/fiddle/fiddle.c: adding alignment constants for compatibility
with DL. * ext/fiddle/fiddle.h: ditto * ext/fiddle/lib/fiddle/cparser.rb: importing the C parser for DL backwards compatibility. * ext/fiddle/lib/fiddle/import.rb: importing the import DSL for DL backwards compatibility. * ext/fiddle/lib/fiddle/pack.rb: importing structure pack for DL backwards compatibility. * ext/fiddle/lib/fiddle/value.rb: ditto * ext/fiddle/lib/fiddle/struct.rb: importing struct DSL for DL backwards compatibility. * test/dl/test_c_struct_entry.rb: importing tests * test/dl/test_c_union_entity.rb: ditto * test/dl/test_cparser.rb: ditto * test/dl/test_import.rb: ditto * test/fiddle/test_c_struct_entry.rb: ditto * test/fiddle/test_c_union_entity.rb: ditto * test/fiddle/test_cparser.rb: ditto * test/fiddle/test_import.rb: ditto git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37914 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
118d9f43c8
commit
0713f89944
16 changed files with 1316 additions and 61 deletions
|
@ -2,52 +2,53 @@ require_relative 'test_base'
|
|||
|
||||
require 'dl/struct'
|
||||
|
||||
class DL::TestCStructEntity < DL::TestBase
|
||||
def test_class_size
|
||||
types = [DL::TYPE_DOUBLE, DL::TYPE_CHAR]
|
||||
module DL
|
||||
class TestCStructEntity < TestBase
|
||||
def test_class_size
|
||||
types = [TYPE_DOUBLE, TYPE_CHAR]
|
||||
|
||||
size = DL::CStructEntity.size types
|
||||
size = CStructEntity.size types
|
||||
|
||||
alignments = types.map { |type| DL::PackInfo::ALIGN_MAP[type] }
|
||||
alignments = types.map { |type| PackInfo::ALIGN_MAP[type] }
|
||||
|
||||
expected = DL::PackInfo.align 0, alignments[0]
|
||||
expected += DL::PackInfo::SIZE_MAP[DL::TYPE_DOUBLE]
|
||||
expected = PackInfo.align 0, alignments[0]
|
||||
expected += PackInfo::SIZE_MAP[TYPE_DOUBLE]
|
||||
|
||||
expected = DL::PackInfo.align expected, alignments[1]
|
||||
expected += DL::PackInfo::SIZE_MAP[DL::TYPE_CHAR]
|
||||
expected = PackInfo.align expected, alignments[1]
|
||||
expected += PackInfo::SIZE_MAP[TYPE_CHAR]
|
||||
|
||||
expected = DL::PackInfo.align expected, alignments.max
|
||||
expected = PackInfo.align expected, alignments.max
|
||||
|
||||
assert_equal expected, size
|
||||
end
|
||||
assert_equal expected, size
|
||||
end
|
||||
|
||||
def test_class_size_with_count
|
||||
size = DL::CStructEntity.size([[DL::TYPE_DOUBLE, 2], [DL::TYPE_CHAR, 20]])
|
||||
def test_class_size_with_count
|
||||
size = CStructEntity.size([[TYPE_DOUBLE, 2], [TYPE_CHAR, 20]])
|
||||
|
||||
types = [DL::TYPE_DOUBLE, DL::TYPE_CHAR]
|
||||
alignments = types.map { |type| DL::PackInfo::ALIGN_MAP[type] }
|
||||
types = [TYPE_DOUBLE, TYPE_CHAR]
|
||||
alignments = types.map { |type| PackInfo::ALIGN_MAP[type] }
|
||||
|
||||
expected = DL::PackInfo.align 0, alignments[0]
|
||||
expected += DL::PackInfo::SIZE_MAP[DL::TYPE_DOUBLE] * 2
|
||||
expected = PackInfo.align 0, alignments[0]
|
||||
expected += PackInfo::SIZE_MAP[TYPE_DOUBLE] * 2
|
||||
|
||||
expected = DL::PackInfo.align expected, alignments[1]
|
||||
expected += DL::PackInfo::SIZE_MAP[DL::TYPE_CHAR] * 20
|
||||
expected = PackInfo.align expected, alignments[1]
|
||||
expected += PackInfo::SIZE_MAP[TYPE_CHAR] * 20
|
||||
|
||||
expected = DL::PackInfo.align expected, alignments.max
|
||||
expected = PackInfo.align expected, alignments.max
|
||||
|
||||
assert_equal expected, size
|
||||
end
|
||||
assert_equal expected, size
|
||||
end
|
||||
|
||||
def test_set_ctypes
|
||||
union = DL::CStructEntity.malloc [DL::TYPE_INT, DL::TYPE_LONG]
|
||||
union.assign_names %w[int long]
|
||||
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
|
||||
# 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']
|
||||
assert_equal 1, union['long']
|
||||
assert_equal 2, union['int']
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -2,29 +2,30 @@ require_relative 'test_base'
|
|||
|
||||
require 'dl/struct'
|
||||
|
||||
class DL::TestCUnionEntity < DL::TestBase
|
||||
def test_class_size
|
||||
size = DL::CUnionEntity.size([DL::TYPE_DOUBLE, DL::TYPE_CHAR])
|
||||
module DL
|
||||
class TestCUnionEntity < TestBase
|
||||
def test_class_size
|
||||
size = CUnionEntity.size([TYPE_DOUBLE, TYPE_CHAR])
|
||||
|
||||
assert_equal DL::SIZEOF_DOUBLE, size
|
||||
end
|
||||
assert_equal SIZEOF_DOUBLE, size
|
||||
end
|
||||
|
||||
def test_class_size_with_count
|
||||
size = DL::CUnionEntity.size([[DL::TYPE_DOUBLE, 2], [DL::TYPE_CHAR, 20]])
|
||||
def test_class_size_with_count
|
||||
size = CUnionEntity.size([[TYPE_DOUBLE, 2], [TYPE_CHAR, 20]])
|
||||
|
||||
assert_equal DL::SIZEOF_CHAR * 20, size
|
||||
end
|
||||
assert_equal SIZEOF_CHAR * 20, size
|
||||
end
|
||||
|
||||
def test_set_ctypes
|
||||
union = DL::CUnionEntity.malloc [DL::TYPE_INT, DL::TYPE_LONG]
|
||||
union.assign_names %w[int long]
|
||||
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']
|
||||
# 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']
|
||||
union['int'] = 1
|
||||
assert_equal 1, union['int']
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -4,30 +4,30 @@ require 'dl/cparser'
|
|||
|
||||
module DL
|
||||
class TestCParser < TestBase
|
||||
include DL::CParser
|
||||
include CParser
|
||||
|
||||
def test_uint_ctype
|
||||
assert_equal(-DL::TYPE_INT, parse_ctype('uint'))
|
||||
assert_equal(-TYPE_INT, parse_ctype('uint'))
|
||||
end
|
||||
|
||||
def test_size_t_ctype
|
||||
assert_equal(DL::TYPE_SIZE_T, parse_ctype("size_t"))
|
||||
assert_equal(TYPE_SIZE_T, parse_ctype("size_t"))
|
||||
end
|
||||
|
||||
def test_ssize_t_ctype
|
||||
assert_equal(DL::TYPE_SSIZE_T, parse_ctype("ssize_t"))
|
||||
assert_equal(TYPE_SSIZE_T, parse_ctype("ssize_t"))
|
||||
end
|
||||
|
||||
def test_ptrdiff_t_ctype
|
||||
assert_equal(DL::TYPE_PTRDIFF_T, parse_ctype("ptrdiff_t"))
|
||||
assert_equal(TYPE_PTRDIFF_T, parse_ctype("ptrdiff_t"))
|
||||
end
|
||||
|
||||
def test_intptr_t_ctype
|
||||
assert_equal(DL::TYPE_INTPTR_T, parse_ctype("intptr_t"))
|
||||
assert_equal(TYPE_INTPTR_T, parse_ctype("intptr_t"))
|
||||
end
|
||||
|
||||
def test_uintptr_t_ctype
|
||||
assert_equal(DL::TYPE_UINTPTR_T, parse_ctype("uintptr_t"))
|
||||
assert_equal(TYPE_UINTPTR_T, parse_ctype("uintptr_t"))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -35,7 +35,7 @@ module DL
|
|||
]
|
||||
|
||||
CallCallback = bind("void call_callback(void*, void*)"){|ptr1, ptr2|
|
||||
f = Function.new(CFunc.new(ptr1.to_i, DL::TYPE_VOID, "<anonymous>"), [TYPE_VOIDP])
|
||||
f = Function.new(CFunc.new(ptr1.to_i, TYPE_VOID, "<anonymous>"), [TYPE_VOIDP])
|
||||
f.call(ptr2)
|
||||
}
|
||||
CarriedFunction = bind("void callback_function(void*)", :carried, 0)
|
||||
|
@ -45,7 +45,7 @@ module DL
|
|||
def test_ensure_call_dlload
|
||||
err = assert_raises(RuntimeError) do
|
||||
Class.new do
|
||||
extend DL::Importer
|
||||
extend Importer
|
||||
extern "void *strcpy(char*, char*)"
|
||||
end
|
||||
end
|
||||
|
@ -59,7 +59,7 @@ module DL
|
|||
end
|
||||
|
||||
def test_sizeof()
|
||||
assert_equal(DL::SIZEOF_VOIDP, LIBC.sizeof("FILE*"))
|
||||
assert_equal(SIZEOF_VOIDP, LIBC.sizeof("FILE*"))
|
||||
assert_equal(LIBC::MyStruct.size(), LIBC.sizeof(LIBC::MyStruct))
|
||||
end
|
||||
|
||||
|
@ -71,7 +71,7 @@ module DL
|
|||
end
|
||||
|
||||
def test_io()
|
||||
if( RUBY_PLATFORM != DL::BUILD_RUBY_PLATFORM )
|
||||
if( RUBY_PLATFORM != BUILD_RUBY_PLATFORM )
|
||||
return
|
||||
end
|
||||
io_in,io_out = IO.pipe()
|
||||
|
|
54
test/fiddle/test_c_struct_entry.rb
Normal file
54
test/fiddle/test_c_struct_entry.rb
Normal file
|
@ -0,0 +1,54 @@
|
|||
require_relative 'helper'
|
||||
|
||||
require 'fiddle/struct'
|
||||
|
||||
module Fiddle
|
||||
class TestCStructEntity < TestCase
|
||||
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
|
31
test/fiddle/test_c_union_entity.rb
Normal file
31
test/fiddle/test_c_union_entity.rb
Normal file
|
@ -0,0 +1,31 @@
|
|||
require_relative 'helper'
|
||||
|
||||
require 'fiddle/struct'
|
||||
|
||||
module Fiddle
|
||||
class TestCUnionEntity < TestCase
|
||||
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
|
33
test/fiddle/test_cparser.rb
Normal file
33
test/fiddle/test_cparser.rb
Normal file
|
@ -0,0 +1,33 @@
|
|||
require_relative 'helper'
|
||||
|
||||
require 'fiddle/cparser'
|
||||
|
||||
module Fiddle
|
||||
class TestCParser < TestCase
|
||||
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
|
136
test/fiddle/test_import.rb
Normal file
136
test/fiddle/test_import.rb
Normal file
|
@ -0,0 +1,136 @@
|
|||
# coding: US-ASCII
|
||||
require_relative 'helper'
|
||||
require 'fiddle/import'
|
||||
|
||||
module Fiddle
|
||||
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
|
||||
|
||||
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(ptr1.to_i, [TYPE_VOIDP], TYPE_VOID)
|
||||
f.call(ptr2)
|
||||
}
|
||||
end
|
||||
|
||||
class TestImport < TestCase
|
||||
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()
|
||||
refute_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_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_operator(r1, :>, 0)
|
||||
assert_operator(r2, :>, 0)
|
||||
assert_equal(0, rr)
|
||||
end
|
||||
|
||||
def test_atof
|
||||
r = LIBC.atof("12.34")
|
||||
assert_includes(12.00..13.00, r)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue