aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortenderlove <tenderlove@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-11-06 18:29:31 +0000
committertenderlove <tenderlove@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-11-06 18:29:31 +0000
commit5296367cf4d22222aad08acaa65eb369de65241b (patch)
treee2fd2610cf0abf8648d6926c93e1994a6845749a
parentc970682f022f3933162116808af114494eeedc0c (diff)
downloadruby-5296367cf4d22222aad08acaa65eb369de65241b.tar.gz
* ext/dl/cptr.c (rb_dlptr_to_str, rb_dlptr_to_s) adding documentation
* test/dl/test_cptr.rb (test_to_str, test_to_s) testing the stringification of DL::Ptr git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25677 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ext/dl/cptr.c33
-rw-r--r--test/dl/test_cptr.rb22
2 files changed, 51 insertions, 4 deletions
diff --git a/ext/dl/cptr.c b/ext/dl/cptr.c
index 393cc11650..a5b07edc73 100644
--- a/ext/dl/cptr.c
+++ b/ext/dl/cptr.c
@@ -188,7 +188,12 @@ rb_dlptr_s_malloc(int argc, VALUE argv[], VALUE klass)
return obj;
}
-VALUE
+/*
+ * call-seq: to_i
+ *
+ * Returns the integer memory location of this DL::CPtr.
+ */
+static VALUE
rb_dlptr_to_i(VALUE self)
{
struct ptr_data *data;
@@ -202,7 +207,7 @@ rb_dlptr_to_i(VALUE self)
*
* Cast this CPtr to a ruby object.
*/
-VALUE
+static VALUE
rb_dlptr_to_value(VALUE self)
{
struct ptr_data *data;
@@ -285,7 +290,17 @@ rb_dlptr_free_get(VALUE self)
return rb_dlcfunc_new(pdata->free, DLTYPE_VOID, "free<anonymous>", CFUNC_CDECL);
}
-VALUE
+/*
+ * call-seq:
+ *
+ * ptr.to_s => string
+ * ptr.to_s(len) => string
+ *
+ * Returns the pointer contents as a string. When called with no arguments,
+ * this method will return the contents until the first NULL byte. When
+ * called with +len+, a string of +len+ bytes will be returned.
+ */
+static VALUE
rb_dlptr_to_s(int argc, VALUE argv[], VALUE self)
{
struct ptr_data *data;
@@ -308,7 +323,17 @@ rb_dlptr_to_s(int argc, VALUE argv[], VALUE self)
return val;
}
-VALUE
+/*
+ * call-seq:
+ *
+ * ptr.to_str => string
+ * ptr.to_str(len) => string
+ *
+ * Returns the pointer contents as a string. When called with no arguments,
+ * this method will return the contents with the length of this pointer's
+ * +size+. When called with +len+, a string of +len+ bytes will be returned.
+ */
+static VALUE
rb_dlptr_to_str(int argc, VALUE argv[], VALUE self)
{
struct ptr_data *data;
diff --git a/test/dl/test_cptr.rb b/test/dl/test_cptr.rb
index 43605f84ce..979c5f50bc 100644
--- a/test/dl/test_cptr.rb
+++ b/test/dl/test_cptr.rb
@@ -3,6 +3,28 @@ require_relative '../ruby/envutil'
module DL
class TestCPtr < TestBase
+ def test_to_str
+ str = "hello world"
+ ptr = CPtr[str]
+
+ assert_equal 3, ptr.to_str(3).length
+ assert_equal str, ptr.to_str
+
+ ptr[5] = 0
+ assert_equal "hello\0world", ptr.to_str
+ end
+
+ def test_to_s
+ str = "hello world"
+ ptr = CPtr[str]
+
+ assert_equal 3, ptr.to_s(3).length
+ assert_equal str, ptr.to_s
+
+ ptr[5] = 0
+ assert_equal 'hello', ptr.to_s
+ end
+
def test_minus
str = "hello world"
ptr = CPtr[str]