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

ruby-changes:4350

From: ko1@a...
Date: Wed, 26 Mar 2008 00:31:25 +0900 (JST)
Subject: [ruby-changes:4350] mame - Ruby:r15840 (trunk): * test/ruby/test_rational.rb: add tests to achieve over 90% test

mame	2008-03-26 00:30:56 +0900 (Wed, 26 Mar 2008)

  New Revision: 15840

  Modified files:
    trunk/ChangeLog
    trunk/test/ruby/test_complex.rb
    trunk/test/ruby/test_rational.rb
    trunk/version.h

  Log:
    * test/ruby/test_rational.rb: add tests to achieve over 90% test
      coverage of rational.c.
    
    * test/ruby/test_complex.rb: ditto for complex.c.


  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/version.h?r1=15840&r2=15839&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/test/ruby/test_complex.rb?r1=15840&r2=15839&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/ChangeLog?r1=15840&r2=15839&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/test/ruby/test_rational.rb?r1=15840&r2=15839&diff_format=u

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 15839)
+++ ChangeLog	(revision 15840)
@@ -1,3 +1,10 @@
+Wed Mar 26 00:28:55 2008  Yusuke Endoh  <mame@t...>
+
+	* test/ruby/test_rational.rb: add tests to achieve over 90% test
+	  coverage of rational.c.
+
+	* test/ruby/test_complex.rb: ditto for complex.c.
+
 Tue Mar 25 19:34:05 2008  Yusuke Endoh  <mame@t...>
 
 	* bootstraptest/test_knownbug.rb: add tests.  [ruby-dev:34128]
Index: version.h
===================================================================
--- version.h	(revision 15839)
+++ version.h	(revision 15840)
@@ -1,7 +1,7 @@
 #define RUBY_VERSION "1.9.0"
-#define RUBY_RELEASE_DATE "2008-03-25"
+#define RUBY_RELEASE_DATE "2008-03-26"
 #define RUBY_VERSION_CODE 190
-#define RUBY_RELEASE_CODE 20080325
+#define RUBY_RELEASE_CODE 20080326
 #define RUBY_PATCHLEVEL 0
 
 #define RUBY_VERSION_MAJOR 1
@@ -9,7 +9,7 @@
 #define RUBY_VERSION_TEENY 0
 #define RUBY_RELEASE_YEAR 2008
 #define RUBY_RELEASE_MONTH 3
-#define RUBY_RELEASE_DAY 25
+#define RUBY_RELEASE_DAY 26
 
 #ifdef RUBY_EXTERN
 RUBY_EXTERN const char ruby_version[];
Index: test/ruby/test_complex.rb
===================================================================
--- test/ruby/test_complex.rb	(revision 15839)
+++ test/ruby/test_complex.rb	(revision 15840)
@@ -314,7 +314,7 @@
     end
   end
 
-  def test_sub
+  def test_sub2
     c = Complex(1,2)
     c2 = Complex(2,3)
 
@@ -723,6 +723,7 @@
       assert_equal(Complex(Rational(-1,5),Rational(-3,2)), '-1/5-(3/2)i'.to_c)
     end
 
+    assert_equal(Complex(5, 3), Complex('5', '3'))
   end
 
   def test_respond
@@ -1004,6 +1005,130 @@
 
   end
 
+  def test_canonicalize
+    f = defined?(Complex::Unify)
+    Complex.const_set(:Unify, true) unless f
+
+    assert_same(1, Complex.instance_eval { new(1, 0) })
+    assert_not_same(1.0, Complex.instance_eval { new(1.0, 0) })
+    assert_equal(Complex(1.0, 0), Complex.instance_eval { new(1.0, 0) })
+
+    Complex.instance_eval { remove_const(:Unify) } unless f
+  end
+
+  def test_polar
+    c = Complex.polar(2, 2)
+    assert_in_delta(2*Math.cos(2), c.real , 0.001)
+    assert_in_delta(2*Math.sin(2), c.image, 0.001)
+
+    c = Complex.polar(1, Complex(0, 1))
+    assert_in_delta(1/Math::E, c.real , 0.001)
+    assert_in_delta(        0, c.image, 0.001)
+  end
+
+  def test_generic?
+    assert_equal(true, Complex.generic?(1))
+    assert_equal(true, Complex.generic?(2**100))
+    assert_equal(true, Complex.generic?(Rational(1, 2)))
+    assert_equal(true, Complex.generic?(1.0))
+    assert_equal(false, Complex.generic?(Complex(1, 1)))
+  end
+
+  def test_new_bang2
+    o = Object.new
+    def o.to_i; 1; end
+    assert_equal(Complex(1, 1), Complex.instance_eval { new!(o, o) })
+  end
+
+  def test_denominator
+    f = defined?(Complex::Unify)
+    unify_val = f && Complex::Unify
+    Complex.instance_eval { remove_const(:Unify) } if f
+
+    dummy_rational = Class.new(Rational)
+    o1 = dummy_rational.instance_eval { new(1, 1) }
+    o2 = dummy_rational.instance_eval { new(1, 1) }
+    d1 = d2 = nil
+    class << o1; self; end.instance_eval { define_method(:denominator) { d1 } rescue nil }
+    class << o2; self; end.instance_eval { define_method(:denominator) { d2 } rescue nil }
+    # o1.denominator returns d1 and o1.denominator returns d2
+
+    c = Complex(o1, o2)
+
+    d1 = d2 = 0
+    assert_equal(0, c.denominator)
+
+    d1 = d2 = -1
+    assert_equal(1, c.denominator)
+
+    d1 = d2 = 256
+    assert_equal(256, c.denominator)
+
+    d1, d2 = 512, 256
+    assert_equal(512, c.denominator)
+
+    d1, d2 = 256, 512
+    assert_equal(512, c.denominator)
+
+    d1, d2 = -(2**100), -(3**100)
+    assert_equal(6**100, c.denominator)
+
+    d1, d2 = 1, 2**100
+    assert_equal(2**100, c.denominator)
+
+    Complex.const_set(:Unify, unify_val) if f
+  end
+
+  def test_abs
+    b = 2**100
+    def b.*(x); self; end rescue nil
+    def b.+(x); -1; end rescue nil
+    assert_equal(Complex(0, 1), Complex(b, 1).abs)
+
+    def b.+(x); Complex(0, 1); end rescue nil
+    c = Complex(b, 1).abs
+    assert_in_delta(1/Math.sqrt(2), c.real , 0.001)
+    assert_in_delta(1/Math.sqrt(2), c.image, 0.001)
+
+    def b.+(x); Complex(0, -1); end rescue nil
+    c = Complex(b, 1).abs
+    assert_in_delta( 1/Math.sqrt(2), c.real , 0.001)
+    assert_in_delta(-1/Math.sqrt(2), c.image, 0.001)
+
+    inf = 1.0/0.0
+    nan = inf/inf
+    assert_raise(Errno::EDOM, Errno::ERANGE) { Complex(1, nan).abs }
+  end
+
+  def test_coerce
+    c = Complex(6, 3)
+    assert_equal(Complex(42, 0), c.coerce(42).first)
+    assert_raise(TypeError) { c.coerce(Object.new) }
+
+    o = Object.new
+    def o.coerce(x); [x.real, x.image]; end
+    assert_equal(9, c + o)
+    assert_equal(3, c - o)
+    assert_equal(18, c * o)
+    assert_equal(2, c / o)
+    assert_equal(216, c ** o)
+  end
+
+  def test_add2
+    assert_equal(Complex(2**100, 1), Complex(0, 1) + 2**100)
+  end
+
+  def test_mul2
+    assert_equal(Complex(0.0, 0.0), Complex(1.0, 1.0) * 0)
+    assert_equal(Complex(0, 0), Complex(0, 0) * (2**100))
+  end
+
+  def test_expt2
+    assert_equal(Complex(1, 0), Complex(2, 2) ** 0)
+    assert_equal(Complex(0, -1), Complex(0, 1) ** (2**100-1))
+    assert_equal(Complex(1, 0), Complex(1, 0) ** Rational(1, 2**100))
+  end
+
   def test_fixed_bug
     if defined?(Rational) && !Rational.instance_variable_get('@RCS_ID')
       assert_equal(Complex(1), 1 ** Complex(1))
Index: test/ruby/test_rational.rb
===================================================================
--- test/ruby/test_rational.rb	(revision 15839)
+++ test/ruby/test_rational.rb	(revision 15840)
@@ -686,6 +686,8 @@
     assert_equal(true, Rational(2,1) != Rational(1))
     assert_equal(false, Rational(1) == nil)
     assert_equal(false, Rational(1) == '')
+
+    assert_equal(false, Rational(1,2**100) == 1)
   end
 
   def test_unify
@@ -955,6 +957,108 @@
     assert_equal(0.25, Rational(1,2).fdiv(2))
   end
 
+  def test_zero_div
+    assert_raise(ZeroDivisionError) { Rational(1, 0) }
+    assert_raise(ZeroDivisionError) { Rational(1, 1) / 0 }
+  end
+
+  def test_gcd
+    assert_equal(0, Rational(0, 2**100))
+  end
+
+  def test_unify
+    f = defined?(Rational::Unify)
+    Rational.const_set(:Unify, true) unless f
+
+    assert_same(42, Rational(84, 2))
+    assert_same(1, Rational(1, 2) + Rational(1, 2))
+
+    Rational.instance_eval { remove_const(:Unify) } unless f
+  end
+
+  def test_coerce
+    r = Rational(7, 3)
+    assert_equal(Rational(42, 1), r.coerce(42).first)
+    assert_raise(TypeError) { r.coerce(Object.new) }
+
+    o = Object.new
+    def o.coerce(x); [x.numerator, x.denominator]; end
+    assert_equal(10, r + o)
+    assert_equal(4, r - o)
+    assert_equal(21, r * o)
+    assert_equal(2, r / o)
+    assert_equal(343, r ** o)
+    assert_equal(1, r <=> o)
+
+    b = 2**100
+    def b.<=>(x); 0; end rescue nil
+    assert_equal(1, r ** b)
+    b = 2**100
+    def b.**(x); -1; end rescue nil
+    assert_equal(-1, Rational(1, b)**3)
+  end
+
+  def test_modulo_remainder
+    assert_equal(Rational(1, 2), Rational(5, 2).modulo(1))
+    assert_equal(Rational(1, 2), Rational(5, 2).modulo(2))
+    assert_equal(Rational(5, 2), Rational(5, 2).modulo(3))
+    assert_equal(Rational(5, 6), Rational(5, 2).modulo(Rational(5, 3)))
+    assert_equal(Rational(1, 2), Rational(-5, 2).modulo(1))
+    assert_equal(Rational(-1, 2), Rational(5, 2).modulo(-1))
+    assert_equal(Rational(-1, 2), Rational(-5, 2).modulo(-1))
+
+    assert_equal(Rational(1, 2), Rational(5, 2).remainder(1))
+    assert_equal(Rational(1, 2), Rational(5, 2).remainder(2))
+    assert_equal(Rational(5, 2), Rational(5, 2).remainder(3))
+    assert_equal(Rational(5, 6), Rational(5, 2).remainder(Rational(5, 3)))
+    assert_equal(Rational(-1, 2), Rational(-5, 2).remainder(1))
+    assert_equal(Rational(1, 2), Rational(5, 2).remainder(-1))
+    assert_equal(Rational(-1, 2), Rational(-5, 2).remainder(-1))
+  end
+
+  def test_abs
+    assert_equal(Rational(1, 2), Rational(1, 2).abs)
+    assert_equal(Rational(1, 2), Rational(-1, 2).abs)
+  end
+
+  def test_floor_ceil_truncate_round
+    assert_equal( 2, Rational( 5, 2).floor)
+    assert_equal(-3, Rational(-5, 2).floor)
+    assert_equal( 3, Rational( 5, 2).ceil)
+    assert_equal(-2, Rational(-5, 2).ceil)
+    assert_equal( 2, Rational( 5, 2).truncate)
+    assert_equal(-2, Rational(-5, 2).truncate)
+    assert_equal( 3, Rational( 5, 2).round)
+    assert_equal(-3, Rational(-5, 2).round)
+    assert_equal( 1, Rational( 4, 3).round)
+    assert_equal(-1, Rational(-4, 3).round)
+    assert_equal( 2, Rational( 5, 3).round)
+    assert_equal(-2, Rational(-5, 3).round)
+  end
+
+  def test_convert
+    assert_equal(Rational(1, 2), Rational(Complex(1, 0), 2))
+    assert_raise(RangeError) { Rational(Complex(1, 1), 1) }
+    assert_equal(Rational(1, 2), Rational(1, Complex(2, 0)))
+    assert_raise(RangeError) { Rational(1, Complex(2, 1)) }
+    assert_equal(Rational(1, 2), Rational(0.25, 0.5))
+    assert_equal(Rational(1, 2), Rational('1', '2'))
+  end
+
+  def test_add2
+    assert_equal(Rational(2**100, 3), Rational(0, 1) + Rational(2**100, 3))
+    assert_equal(Rational(2, 3**100), Rational(0, 1) + Rational(2, 3**100))
+  end
+
+  def test_div2
+    assert_raise(ZeroDivisionError) { Rational(1, 1) / Rational(0, 1) }
+  end
+
+  def test_to_f2
+    assert_equal(1, Rational(2**5000,3).to_f.infinite?)
+    assert_equal(0, Rational(1, 2**5000).to_f)
+  end
+
   def test_fixed_bug
     if defined?(Rational::Unify)
       assert_instance_of(Fixnum, Rational(1,2) ** 0) # mathn's bug

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

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