diff options
Diffstat (limited to 'test/fiddle')
-rw-r--r-- | test/fiddle/test_c_struct_entry.rb | 8 | ||||
-rw-r--r-- | test/fiddle/test_closure.rb | 12 | ||||
-rw-r--r-- | test/fiddle/test_cparser.rb | 40 | ||||
-rw-r--r-- | test/fiddle/test_func.rb | 17 | ||||
-rw-r--r-- | test/fiddle/test_handle.rb | 3 | ||||
-rw-r--r-- | test/fiddle/test_pointer.rb | 16 |
6 files changed, 95 insertions, 1 deletions
diff --git a/test/fiddle/test_c_struct_entry.rb b/test/fiddle/test_c_struct_entry.rb index 9fd16d7101..45de2efe21 100644 --- a/test/fiddle/test_c_struct_entry.rb +++ b/test/fiddle/test_c_struct_entry.rb @@ -8,7 +8,7 @@ end module Fiddle class TestCStructEntity < TestCase def test_class_size - types = [TYPE_DOUBLE, TYPE_CHAR] + types = [TYPE_DOUBLE, TYPE_CHAR, TYPE_DOUBLE, TYPE_BOOL] size = CStructEntity.size types @@ -20,6 +20,12 @@ module Fiddle expected = PackInfo.align expected, alignments[1] expected += PackInfo::SIZE_MAP[TYPE_CHAR] + expected = PackInfo.align expected, alignments[2] + expected += PackInfo::SIZE_MAP[TYPE_DOUBLE] + + expected = PackInfo.align expected, alignments[3] + expected += PackInfo::SIZE_MAP[TYPE_BOOL] + expected = PackInfo.align expected, alignments.max assert_equal expected, size diff --git a/test/fiddle/test_closure.rb b/test/fiddle/test_closure.rb index 825ea9651d..abb6bdbd32 100644 --- a/test/fiddle/test_closure.rb +++ b/test/fiddle/test_closure.rb @@ -81,6 +81,18 @@ module Fiddle end end + def test_bool + closure_class = Class.new(Closure) do + def call(bool) + not bool + end + end + closure_class.create(:bool, [:bool]) do |closure| + func = Function.new(closure, [:bool], :bool) + assert_equal(false, func.call(true)) + end + end + def test_free Closure.create(:int, [:void]) do |closure| assert(!closure.freed?) diff --git a/test/fiddle/test_cparser.rb b/test/fiddle/test_cparser.rb index ae319197a4..f1b67476ba 100644 --- a/test/fiddle/test_cparser.rb +++ b/test/fiddle/test_cparser.rb @@ -24,14 +24,32 @@ module Fiddle assert_equal(TYPE_SHORT, parse_ctype('const short')) assert_equal(TYPE_SHORT, parse_ctype('short int')) assert_equal(TYPE_SHORT, parse_ctype('const short int')) + assert_equal(TYPE_SHORT, parse_ctype('int short')) + assert_equal(TYPE_SHORT, parse_ctype('const int short')) assert_equal(TYPE_SHORT, parse_ctype('signed short')) assert_equal(TYPE_SHORT, parse_ctype('const signed short')) + assert_equal(TYPE_SHORT, parse_ctype('short signed')) + assert_equal(TYPE_SHORT, parse_ctype('const short signed')) assert_equal(TYPE_SHORT, parse_ctype('signed short int')) assert_equal(TYPE_SHORT, parse_ctype('const signed short int')) + assert_equal(TYPE_SHORT, parse_ctype('signed int short')) + assert_equal(TYPE_SHORT, parse_ctype('const signed int short')) + assert_equal(TYPE_SHORT, parse_ctype('int signed short')) + assert_equal(TYPE_SHORT, parse_ctype('const int signed short')) + assert_equal(TYPE_SHORT, parse_ctype('int short signed')) + assert_equal(TYPE_SHORT, parse_ctype('const int short signed')) assert_equal(-TYPE_SHORT, parse_ctype('unsigned short')) assert_equal(-TYPE_SHORT, parse_ctype('const unsigned short')) assert_equal(-TYPE_SHORT, parse_ctype('unsigned short int')) assert_equal(-TYPE_SHORT, parse_ctype('const unsigned short int')) + assert_equal(-TYPE_SHORT, parse_ctype('unsigned int short')) + assert_equal(-TYPE_SHORT, parse_ctype('const unsigned int short')) + assert_equal(-TYPE_SHORT, parse_ctype('short int unsigned')) + assert_equal(-TYPE_SHORT, parse_ctype('const short int unsigned')) + assert_equal(-TYPE_SHORT, parse_ctype('int unsigned short')) + assert_equal(-TYPE_SHORT, parse_ctype('const int unsigned short')) + assert_equal(-TYPE_SHORT, parse_ctype('int short unsigned')) + assert_equal(-TYPE_SHORT, parse_ctype('const int short unsigned')) end def test_int_ctype @@ -50,14 +68,32 @@ module Fiddle assert_equal(TYPE_LONG, parse_ctype('const long')) assert_equal(TYPE_LONG, parse_ctype('long int')) assert_equal(TYPE_LONG, parse_ctype('const long int')) + assert_equal(TYPE_LONG, parse_ctype('int long')) + assert_equal(TYPE_LONG, parse_ctype('const int long')) assert_equal(TYPE_LONG, parse_ctype('signed long')) assert_equal(TYPE_LONG, parse_ctype('const signed long')) assert_equal(TYPE_LONG, parse_ctype('signed long int')) assert_equal(TYPE_LONG, parse_ctype('const signed long int')) + assert_equal(TYPE_LONG, parse_ctype('signed int long')) + assert_equal(TYPE_LONG, parse_ctype('const signed int long')) + assert_equal(TYPE_LONG, parse_ctype('long signed')) + assert_equal(TYPE_LONG, parse_ctype('const long signed')) + assert_equal(TYPE_LONG, parse_ctype('long int signed')) + assert_equal(TYPE_LONG, parse_ctype('const long int signed')) + assert_equal(TYPE_LONG, parse_ctype('int long signed')) + assert_equal(TYPE_LONG, parse_ctype('const int long signed')) assert_equal(-TYPE_LONG, parse_ctype('unsigned long')) assert_equal(-TYPE_LONG, parse_ctype('const unsigned long')) assert_equal(-TYPE_LONG, parse_ctype('unsigned long int')) assert_equal(-TYPE_LONG, parse_ctype('const unsigned long int')) + assert_equal(-TYPE_LONG, parse_ctype('long int unsigned')) + assert_equal(-TYPE_LONG, parse_ctype('const long int unsigned')) + assert_equal(-TYPE_LONG, parse_ctype('unsigned int long')) + assert_equal(-TYPE_LONG, parse_ctype('const unsigned int long')) + assert_equal(-TYPE_LONG, parse_ctype('int unsigned long')) + assert_equal(-TYPE_LONG, parse_ctype('const int unsigned long')) + assert_equal(-TYPE_LONG, parse_ctype('int long unsigned')) + assert_equal(-TYPE_LONG, parse_ctype('const int long unsigned')) end def test_size_t_ctype @@ -85,6 +121,10 @@ module Fiddle assert_equal(TYPE_UINTPTR_T, parse_ctype("const uintptr_t")) end + def test_bool_ctype + assert_equal(TYPE_BOOL, parse_ctype('bool')) + end + def test_undefined_ctype assert_raise(DLError) { parse_ctype('DWORD') } end diff --git a/test/fiddle/test_func.rb b/test/fiddle/test_func.rb index ff52f727d0..df79539e76 100644 --- a/test/fiddle/test_func.rb +++ b/test/fiddle/test_func.rb @@ -145,5 +145,22 @@ module Fiddle assert_equal("string: He, const string: World, uint: 29\n", output_buffer[0, written]) end + + def test_rb_memory_view_available_p + omit "MemoryView is unavailable" unless defined? Fiddle::MemoryView + libruby = Fiddle.dlopen(nil) + case Fiddle::SIZEOF_VOIDP + when Fiddle::SIZEOF_LONG_LONG + value_type = -Fiddle::TYPE_LONG_LONG + else + value_type = -Fiddle::TYPE_LONG + end + rb_memory_view_available_p = + Function.new(libruby["rb_memory_view_available_p"], + [value_type], + :bool, + need_gvl: true) + assert_equal(false, rb_memory_view_available_p.call(Fiddle::Qnil)) + end end end if defined?(Fiddle) diff --git a/test/fiddle/test_handle.rb b/test/fiddle/test_handle.rb index c19bcc6623..412c10e09d 100644 --- a/test/fiddle/test_handle.rb +++ b/test/fiddle/test_handle.rb @@ -183,9 +183,12 @@ module Fiddle # it calls _nss_cache_cycle_prevention_function with dlsym(3). # So our Fiddle::Handle#sym must call dlerror(3) before call dlsym. # In general uses of dlerror(3) should call it before use it. + verbose, $VERBOSE = $VERBOSE, nil require 'socket' Socket.gethostbyname("localhost") Fiddle.dlopen("/lib/libc.so.7").sym('strcpy') + ensure + $VERBOSE = verbose end if /freebsd/=~ RUBY_PLATFORM def test_no_memory_leak diff --git a/test/fiddle/test_pointer.rb b/test/fiddle/test_pointer.rb index d6cba04bbe..f2c1d285ad 100644 --- a/test/fiddle/test_pointer.rb +++ b/test/fiddle/test_pointer.rb @@ -10,6 +10,22 @@ module Fiddle Fiddle.dlwrap arg end + def test_can_read_write_memory + # Allocate some memory + address = Fiddle.malloc(Fiddle::SIZEOF_VOIDP) + + bytes_to_write = Fiddle::SIZEOF_VOIDP.times.to_a.pack("C*") + + # Write to the memory + Fiddle::Pointer.write(address, bytes_to_write) + + # Read the bytes out again + bytes = Fiddle::Pointer.read(address, Fiddle::SIZEOF_VOIDP) + assert_equal bytes_to_write, bytes + ensure + Fiddle.free address + end + def test_cptr_to_int null = Fiddle::NULL assert_equal(null.to_i, null.to_int) |