ruby-changes:13959
From: marcandre <ko1@a...>
Date: Sat, 14 Nov 2009 09:18:53 +0900 (JST)
Subject: [ruby-changes:13959] Ruby:r25765 (trunk): * ext/bigdecimal/bigdecimal.c (BigDecimalCmp): Fix comparisons
marcandre 2009-11-14 09:17:07 +0900 (Sat, 14 Nov 2009) New Revision: 25765 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=25765 Log: * ext/bigdecimal/bigdecimal.c (BigDecimalCmp): Fix comparisons [ruby-core:26646] * test/bigdecimal/test_bigdecimal.rb (class): Fix and improve tests. Modified files: trunk/ChangeLog trunk/ext/bigdecimal/bigdecimal.c trunk/test/bigdecimal/test_bigdecimal.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 25764) +++ ChangeLog (revision 25765) @@ -1,3 +1,10 @@ +Sat Nov 14 09:16:54 2009 Marc-Andre Lafortune <ruby-core@m...> + + * ext/bigdecimal/bigdecimal.c (BigDecimalCmp): Fix comparisons + [ruby-core:26646] + + * test/bigdecimal/test_bigdecimal.rb (class): Fix and improve tests. + Sat Nov 14 04:07:06 2009 Hidetoshi NAGAI <nagai@a...> * ext/tk/lib/tk/variable.rb (TkVariable::coerce): fix bug on a Index: ext/bigdecimal/bigdecimal.c =================================================================== --- ext/bigdecimal/bigdecimal.c (revision 25764) +++ ext/bigdecimal/bigdecimal.c (revision 25765) @@ -724,23 +724,21 @@ switch(op) { - case '*': f = rb_intern("<=>");break; - case '=': f = rb_intern("=="); break; - case '!': f = rb_intern("!="); break; + case '*': return rb_num_coerce_cmp(self,r,rb_intern("<=>")); + case '=': return RTEST(rb_num_coerce_cmp(self,r,rb_intern("=="))) ? Qtrue : Qfalse; case 'G': f = rb_intern(">="); break; case 'L': f = rb_intern("<="); break; case '>': case '<': f = (ID)op; break; } - return rb_num_coerce_cmp(self,r,f); + return rb_num_coerce_relop(self,r,f); } SAVE(b); e = VpComp(a, b); - if(e==999) return Qnil; + if(e==999) return (op == '*') ? Qnil : Qfalse; switch(op) { case '*': return INT2FIX(e); /* any op */ case '=': if(e==0) return Qtrue ; return Qfalse; - case '!': if(e!=0) return Qtrue ; return Qfalse; case 'G': if(e>=0) return Qtrue ; return Qfalse; case '>': if(e> 0) return Qtrue ; return Qfalse; case 'L': if(e<=0) return Qtrue ; return Qfalse; Index: test/bigdecimal/test_bigdecimal.rb =================================================================== --- test/bigdecimal/test_bigdecimal.rb (revision 25764) +++ test/bigdecimal/test_bigdecimal.rb (revision 25765) @@ -61,7 +61,6 @@ x = BigDecimal.new("0.1") 100.times do x *= x - break if x == false end end end @@ -71,7 +70,6 @@ x = BigDecimal.new("10") 100.times do x *= x - break if x == false end end end @@ -219,8 +217,21 @@ assert_operator(1, :<, inf) end - def test_cmp_corece + def test_cmp_nan n1 = BigDecimal.new("1") + BigDecimal.mode(BigDecimal::EXCEPTION_NaN, false) + assert_equal(nil, BigDecimal.new("NaN") <=> n1) + assert_equal(false, BigDecimal.new("NaN") > n1) + end + + def test_cmp_failing_coercion + n1 = BigDecimal.new("1") + assert_equal(nil, n1 <=> nil) + assert_raise(ArgumentError){n1 > nil} + end + + def test_cmp_coerce + n1 = BigDecimal.new("1") n2 = BigDecimal.new("2") o1 = Object.new; def o1.coerce(x); [x, BigDecimal.new("1")]; end o2 = Object.new; def o2.coerce(x); [x, BigDecimal.new("2")]; end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/