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

ruby-changes:2518

From: ko1@a...
Date: 25 Nov 2007 01:32:30 +0900
Subject: [ruby-changes:2518] akr - Ruby:r14009 (trunk): add test for printf format %x, %o and %b.

akr	2007-11-25 01:32:10 +0900 (Sun, 25 Nov 2007)

  New Revision: 14009

  Modified files:
    trunk/test/ruby/test_integer.rb

  Log:
    add test for printf format %x, %o and %b.


  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/test/ruby/test_integer.rb?r1=14009&r2=14008

Index: test/ruby/test_integer.rb
===================================================================
--- test/ruby/test_integer.rb	(revision 14008)
+++ test/ruby/test_integer.rb	(revision 14009)
@@ -490,6 +490,85 @@
     }
   end
 
+  def test_to_s
+    2.upto(36) {|radix|
+      VS.each {|a|
+        s = a.to_s(radix)
+        b = s.to_i(radix)
+        assert_equal(a, b)
+      }
+    }
+  end
+
+  def test_printf_x
+    VS.reverse_each {|a|
+      s = sprintf("%x", a)
+      if /\A\.\./ =~ s
+        b = -($'.tr('0123456789abcdef', 'fedcba9876543210').to_i(16) + 1)
+      else
+        b = s.to_i(16)
+      end
+      assert_equal(a, b, "sprintf('%x', #{a}) = #{s.inspect}")
+    }
+  end
+
+  def test_printf_x_sign
+    VS.reverse_each {|a|
+      s = sprintf("%+x", a)
+      b = s.to_i(16)
+      assert_equal(a, b, "sprintf('%+x', #{a}) = #{s.inspect}")
+      s = sprintf("% x", a)
+      b = s.to_i(16)
+      assert_equal(a, b, "sprintf('% x', #{a}) = #{s.inspect}")
+    }
+  end
+
+  def test_printf_o
+    VS.reverse_each {|a|
+      s = sprintf("%o", a)
+      if /\A\.\./ =~ s
+        b = -($'.tr('01234567', '76543210').to_i(8) + 1)
+      else
+        b = s.to_i(8)
+      end
+      assert_equal(a, b, "sprintf('%o', #{a}) = #{s.inspect}")
+    }
+  end
+
+  def test_printf_o_sign
+    VS.reverse_each {|a|
+      s = sprintf("%+o", a)
+      b = s.to_i(8)
+      assert_equal(a, b, "sprintf('%+o', #{a}) = #{s.inspect}")
+      s = sprintf("% o", a)
+      b = s.to_i(8)
+      assert_equal(a, b, "sprintf('% o', #{a}) = #{s.inspect}")
+    }
+  end
+
+  def test_printf_b
+    VS.reverse_each {|a|
+      s = sprintf("%b", a)
+      if /\A\.\./ =~ s
+        b = -($'.tr('01', '10').to_i(2) + 1)
+      else
+        b = s.to_i(2)
+      end
+      assert_equal(a, b, "sprintf('%b', #{a}) = #{s.inspect}")
+    }
+  end
+
+  def test_printf_b_sign
+    VS.reverse_each {|a|
+      s = sprintf("%+b", a)
+      b = s.to_i(2)
+      assert_equal(a, b, "sprintf('%+b', #{a}) = #{s.inspect}")
+      s = sprintf("% b", a)
+      b = s.to_i(2)
+      assert_equal(a, b, "sprintf('% b', #{a}) = #{s.inspect}")
+    }
+  end
+
   def test_Integer
     assert_raise(ArgumentError) {Integer("0x-1")}
     assert_raise(ArgumentError) {Integer("-0x-1")}

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

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