ruby-changes:13834
From: tenderlove <ko1@a...>
Date: Wed, 4 Nov 2009 02:13:06 +0900 (JST)
Subject: [ruby-changes:13834] Ruby:r25634 (trunk): * ext/dl/cptr.c (rb_dlptr_eql, rb_dlptr_cmp): DL::CPtr#== and DL::CPtr#<=>
tenderlove 2009-11-04 02:12:46 +0900 (Wed, 04 Nov 2009) New Revision: 25634 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=25634 Log: * ext/dl/cptr.c (rb_dlptr_eql, rb_dlptr_cmp): DL::CPtr#== and DL::CPtr#<=> should not raise an exception when compared to a different object. Modified files: trunk/ChangeLog trunk/ext/dl/cptr.c trunk/test/dl/test_cptr.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 25633) +++ ChangeLog (revision 25634) @@ -1,3 +1,8 @@ +Wed Nov 4 02:08:14 2009 Aaron Patterson <tenderlove@r...> + + * ext/dl/cptr.c (rb_dlptr_eql, rb_dlptr_cmp): DL::CPtr#== and DL::CPtr#<=> + should not raise an exception when compared to a different object. + Wed Nov 4 00:05:36 2009 NARUSE, Yui <naruse@r...> * string.c (rb_str_upto): make next object before yield its block. Index: ext/dl/cptr.c =================================================================== --- ext/dl/cptr.c (revision 25633) +++ ext/dl/cptr.c (revision 25634) @@ -333,21 +333,42 @@ return rb_str_new2(str); } +/* + * call-seq: + * ptr == other => true or false + * ptr.eql?(other) => true or false + * + * Returns true if +other+ wraps the same pointer, otherwise returns + * false. + */ VALUE rb_dlptr_eql(VALUE self, VALUE other) { void *ptr1, *ptr2; + + if(!rb_obj_is_kind_of(other, rb_cDLCPtr)) return Qfalse; + ptr1 = rb_dlptr2cptr(self); ptr2 = rb_dlptr2cptr(other); return ptr1 == ptr2 ? Qtrue : Qfalse; } -VALUE +/* + * call-seq: + * ptr <=> other => -1, 0, 1, or nil + * + * Returns -1 if less than, 0 if equal to, 1 if greater than +other+. Returns + * nil if +ptr+ cannot be compared to +other+. + */ +static VALUE rb_dlptr_cmp(VALUE self, VALUE other) { void *ptr1, *ptr2; SIGNED_VALUE diff; + + if(!rb_obj_is_kind_of(other, rb_cDLCPtr)) return Qnil; + ptr1 = rb_dlptr2cptr(self); ptr2 = rb_dlptr2cptr(other); diff = (SIGNED_VALUE)ptr1 - (SIGNED_VALUE)ptr2; Index: test/dl/test_cptr.rb =================================================================== --- test/dl/test_cptr.rb (revision 25633) +++ test/dl/test_cptr.rb (revision 25634) @@ -3,6 +3,22 @@ module DL class TestCPtr < TestBase + def test_equals + ptr = CPtr.new 0 + ptr2 = CPtr.new 0 + assert_equal ptr2, ptr + end + + def test_not_equals + ptr = CPtr.new 0 + assert_not_equal 10, ptr, '10 should not equal the pointer' + end + + def test_cmp + ptr = CPtr.new 0 + assert_nil(ptr <=> 10, '10 should not be comparable') + end + def test_ref_ptr ary = [0,1,2,4,5] addr = CPtr.new(dlwrap(ary)) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/