From 02d55ac989bf29d6a87d63e93dc49941438f495b Mon Sep 17 00:00:00 2001 From: tenderlove Date: Tue, 3 Nov 2009 20:28:20 +0000 Subject: * test/dl/test_cptr.rb (test_to_ptr*): testing DL::CPtr#to_ptr * ext/dl/cptr.c (rb_dlptr_free_set, rb_dlptr_free_get, rb_dlptr_s_to_ptr): adding documentation, fixing indentation git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25636 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/dl/test_cptr.rb | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'test/dl') diff --git a/test/dl/test_cptr.rb b/test/dl/test_cptr.rb index c03abbe73e..3c4b5e6867 100644 --- a/test/dl/test_cptr.rb +++ b/test/dl/test_cptr.rb @@ -3,6 +3,42 @@ require_relative '../ruby/envutil' module DL class TestCPtr < TestBase + def test_to_ptr_string + str = "hello world" + ptr = CPtr[str] + assert ptr.tainted?, 'pointer should be tainted' + assert_equal str.length, ptr.size + assert_equal 'hello', ptr[0,5] + end + + def test_to_ptr_io + buf = CPtr.malloc(10) + File.open(__FILE__, 'r') do |f| + ptr = CPtr.to_ptr f + fread = CFunc.new(@libc['fread'], TYPE_VOID, 'fread') + fread.call([buf.to_i, DL::SIZEOF_CHAR, buf.size - 1, ptr.to_i]) + end + + File.open(__FILE__, 'r') do |f| + assert_equal f.read(9), buf.to_s + end + end + + def test_to_ptr_with_ptr + ptr = CPtr.new 0 + ptr2 = CPtr.to_ptr Struct.new(:to_ptr).new(ptr) + assert_equal ptr, ptr2 + + assert_raises(DL::DLError) do + CPtr.to_ptr Struct.new(:to_ptr).new(nil) + end + end + + def test_to_ptr_with_num + ptr = CPtr.new 0 + assert_equal ptr, CPtr[0] + end + def test_equals ptr = CPtr.new 0 ptr2 = CPtr.new 0 -- cgit v1.2.3