diff options
| author | tenderlove <tenderlove@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2009-11-06 18:02:30 +0000 |
|---|---|---|
| committer | tenderlove <tenderlove@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2009-11-06 18:02:30 +0000 |
| commit | 0601d00f81d0b40ec227552128edb586a1a8aa2b (patch) | |
| tree | 2fcd356477cdc2d12af29f099024d643673082d3 | |
| parent | 542e9707eff07c2cb3809f4e3090b46bcfc293a3 (diff) | |
| download | ruby-0601d00f81d0b40ec227552128edb586a1a8aa2b.tar.gz ruby-0601d00f81d0b40ec227552128edb586a1a8aa2b.tar.xz ruby-0601d00f81d0b40ec227552128edb586a1a8aa2b.zip | |
* 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
git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@25675 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
| -rw-r--r-- | ext/dl/cptr.c | 24 | ||||
| -rw-r--r-- | test/dl/test_cptr.rb | 22 |
2 files changed, 43 insertions, 3 deletions
diff --git a/ext/dl/cptr.c b/ext/dl/cptr.c index cdcc23c72..393cc1165 100644 --- a/ext/dl/cptr.c +++ b/ext/dl/cptr.c @@ -331,7 +331,13 @@ rb_dlptr_to_str(int argc, VALUE argv[], VALUE self) 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 @@ rb_dlptr_cmp(VALUE self, VALUE other) 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 @@ rb_dlptr_plus(VALUE self, VALUE other) 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; diff --git a/test/dl/test_cptr.rb b/test/dl/test_cptr.rb index 3c4b5e686..43605f84c 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_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] |
