[前][次][番号順一覧][スレッド一覧]

ruby-changes:13836

From: tenderlove <ko1@a...>
Date: Wed, 4 Nov 2009 05:28:30 +0900 (JST)
Subject: [ruby-changes:13836] Ruby:r25636 (trunk): * test/dl/test_cptr.rb (test_to_ptr*): testing DL::CPtr#to_ptr

tenderlove	2009-11-04 05:28:20 +0900 (Wed, 04 Nov 2009)

  New Revision: 25636

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=25636

  Log:
    * 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

  Modified files:
    trunk/ext/dl/cptr.c
    trunk/test/dl/test_cptr.rb

Index: ext/dl/cptr.c
===================================================================
--- ext/dl/cptr.c	(revision 25635)
+++ ext/dl/cptr.c	(revision 25636)
@@ -254,7 +254,12 @@
     return data->ptr ? Qfalse : Qtrue;
 }
 
-VALUE
+/*
+ * call-seq: free=(function)
+ *
+ * Set the free function for this pointer to the DL::CFunc in +function+.
+ */
+static VALUE
 rb_dlptr_free_set(VALUE self, VALUE val)
 {
     struct ptr_data *data;
@@ -265,7 +270,12 @@
     return Qnil;
 }
 
-VALUE
+/*
+ * call-seq: free
+ *
+ * Get the free function for this pointer.  Returns  DL::CFunc or nil.
+ */
+static VALUE
 rb_dlptr_free_get(VALUE self)
 {
     struct ptr_data *pdata;
@@ -499,21 +509,29 @@
     return LONG2NUM(RPTR_DATA(self)->size);
 }
 
-VALUE
+/*
+ * call-seq:
+ *    DL::CPtr.to_ptr(val)  => cptr
+ *    DL::CPtr[val]         => cptr
+ *
+ * Get the underlying pointer for ruby object +val+ and return it as a
+ * DL::CPtr object.
+ */
+static VALUE
 rb_dlptr_s_to_ptr(VALUE self, VALUE val)
 {
     VALUE ptr;
 
-    if (rb_obj_is_kind_of(val, rb_cIO) == Qtrue){
+    if (RTEST(rb_obj_is_kind_of(val, rb_cIO))){
 	rb_io_t *fptr;
 	FILE *fp;
 	GetOpenFile(val, fptr);
 	fp = rb_io_stdio_file(fptr);
 	ptr = rb_dlptr_new(fp, 0, NULL);
     }
-    else if (rb_obj_is_kind_of(val, rb_cString) == Qtrue){
-        char *str = StringValuePtr(val);
-        ptr = rb_dlptr_new(str, RSTRING_LEN(val), NULL); 
+    else if (RTEST(rb_obj_is_kind_of(val, rb_cString))){
+	char *str = StringValuePtr(val);
+	ptr = rb_dlptr_new(str, RSTRING_LEN(val), NULL);
     }
     else if (rb_respond_to(val, id_to_ptr)){
 	VALUE vptr = rb_funcall(val, id_to_ptr, 0);
Index: test/dl/test_cptr.rb
===================================================================
--- test/dl/test_cptr.rb	(revision 25635)
+++ test/dl/test_cptr.rb	(revision 25636)
@@ -3,6 +3,42 @@
 
 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

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]