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

ruby-changes:13876

From: tenderlove <ko1@a...>
Date: Sat, 7 Nov 2009 03:03:25 +0900 (JST)
Subject: [ruby-changes:13876] Ruby:r25675 (trunk): * ext/dl/cptr.c (rb_dlptr_inspect, rb_dlptr_plus, rb_dlptr_minus)

tenderlove	2009-11-07 03:02:30 +0900 (Sat, 07 Nov 2009)

  New Revision: 25675

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

  Log:
    * ext/dl/cptr.c (rb_dlptr_inspect, rb_dlptr_plus, rb_dlptr_minus)
      documenting +, -, inspect
    * text/dl/test_cptr.rb (test_minus, test_plus, test_inspect) testing
      minus, plus, and inspect

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

Index: ext/dl/cptr.c
===================================================================
--- ext/dl/cptr.c	(revision 25674)
+++ ext/dl/cptr.c	(revision 25675)
@@ -331,7 +331,13 @@
     return val;
 }
 
-VALUE
+/*
+ * call-seq: inspect
+ *
+ * Returns a string formatted with an easily readable representation of the
+ * internal state of the DL::CPtr
+ */
+static VALUE
 rb_dlptr_inspect(VALUE self)
 {
     struct ptr_data *data;
@@ -386,7 +392,13 @@
     return diff > 0 ? INT2NUM(1) : INT2NUM(-1);
 }
 
-VALUE
+/*
+ * call-seq:
+ *    ptr + n   => new cptr
+ *
+ * Returns a new DL::CPtr that has been advanced +n+ bytes.
+ */
+static VALUE
 rb_dlptr_plus(VALUE self, VALUE other)
 {
     void *ptr;
@@ -398,7 +410,13 @@
     return rb_dlptr_new((char *)ptr + num, size - num, 0);
 }
 
-VALUE
+/*
+ * call-seq:
+ *    ptr - n   => new cptr
+ *
+ * Returns a new DL::CPtr that has been moved back +n+ bytes.
+ */
+static VALUE
 rb_dlptr_minus(VALUE self, VALUE other)
 {
     void *ptr;
Index: test/dl/test_cptr.rb
===================================================================
--- test/dl/test_cptr.rb	(revision 25674)
+++ test/dl/test_cptr.rb	(revision 25675)
@@ -3,6 +3,28 @@
 
 module DL
   class TestCPtr < TestBase
+    def test_minus
+      str = "hello world"
+      ptr = CPtr[str]
+      assert_equal ptr.to_s, (ptr + 3 - 3).to_s
+    end
+
+    # TODO: what if the pointer size is 0?  raise an exception? do we care?
+    def test_plus
+      str = "hello world"
+      ptr = CPtr[str]
+      new_str = ptr + 3
+      assert_equal 'lo world', new_str.to_s
+    end
+
+    def test_inspect
+      ptr = CPtr.new(0)
+      inspect = ptr.inspect
+      assert_match(/size=#{ptr.size}/, inspect)
+      assert_match(/free=#{ptr.free}/, inspect)
+      assert_match(/ptr=#{ptr.to_i}/, inspect)
+    end
+
     def test_to_ptr_string
       str = "hello world"
       ptr = CPtr[str]

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

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