mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
a28006f959
* test/ruby/test_sprintf.rb (test_nan, test_inf): add tests. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6495 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
141 lines
4.9 KiB
Ruby
141 lines
4.9 KiB
Ruby
require 'test/unit'
|
|
|
|
class TestSprintf < Test::Unit::TestCase
|
|
def test_binary
|
|
assert_equal("0", sprintf("%b", 0))
|
|
assert_equal("1", sprintf("%b", 1))
|
|
assert_equal("10", sprintf("%b", 2))
|
|
assert_equal("..1", sprintf("%b", -1))
|
|
|
|
assert_equal(" 0", sprintf("%4b", 0))
|
|
assert_equal(" 1", sprintf("%4b", 1))
|
|
assert_equal(" 10", sprintf("%4b", 2))
|
|
assert_equal(" ..1", sprintf("%4b", -1))
|
|
|
|
assert_equal("0000", sprintf("%04b", 0))
|
|
assert_equal("0001", sprintf("%04b", 1))
|
|
assert_equal("0010", sprintf("%04b", 2))
|
|
assert_equal("1111", sprintf("%04b", -1))
|
|
|
|
assert_equal("0000", sprintf("%.4b", 0))
|
|
assert_equal("0001", sprintf("%.4b", 1))
|
|
assert_equal("0010", sprintf("%.4b", 2))
|
|
assert_equal("1111", sprintf("%.4b", -1))
|
|
|
|
assert_equal(" 0000", sprintf("%6.4b", 0))
|
|
assert_equal(" 0001", sprintf("%6.4b", 1))
|
|
assert_equal(" 0010", sprintf("%6.4b", 2))
|
|
assert_equal(" 1111", sprintf("%6.4b", -1))
|
|
|
|
assert_equal(" 0b0", sprintf("%#4b", 0))
|
|
assert_equal(" 0b1", sprintf("%#4b", 1))
|
|
assert_equal("0b10", sprintf("%#4b", 2))
|
|
assert_equal("0b..1", sprintf("%#4b", -1))
|
|
|
|
assert_equal("0b00", sprintf("%#04b", 0))
|
|
assert_equal("0b01", sprintf("%#04b", 1))
|
|
assert_equal("0b10", sprintf("%#04b", 2))
|
|
assert_equal("0b11", sprintf("%#04b", -1))
|
|
|
|
assert_equal("0b0000", sprintf("%#.4b", 0))
|
|
assert_equal("0b0001", sprintf("%#.4b", 1))
|
|
assert_equal("0b0010", sprintf("%#.4b", 2))
|
|
assert_equal("0b1111", sprintf("%#.4b", -1))
|
|
|
|
assert_equal("0b0000", sprintf("%#6.4b", 0))
|
|
assert_equal("0b0001", sprintf("%#6.4b", 1))
|
|
assert_equal("0b0010", sprintf("%#6.4b", 2))
|
|
assert_equal("0b1111", sprintf("%#6.4b", -1))
|
|
|
|
assert_equal("+0", sprintf("%+b", 0))
|
|
assert_equal("+1", sprintf("%+b", 1))
|
|
assert_equal("+10", sprintf("%+b", 2))
|
|
assert_equal("-1", sprintf("%+b", -1))
|
|
|
|
assert_equal(" +0", sprintf("%+4b", 0))
|
|
assert_equal(" +1", sprintf("%+4b", 1))
|
|
assert_equal(" +10", sprintf("%+4b", 2))
|
|
assert_equal(" -1", sprintf("%+4b", -1))
|
|
|
|
assert_equal("+000", sprintf("%+04b", 0))
|
|
assert_equal("+001", sprintf("%+04b", 1))
|
|
assert_equal("+010", sprintf("%+04b", 2))
|
|
assert_equal("-001", sprintf("%+04b", -1))
|
|
|
|
assert_equal("+0000", sprintf("%+.4b", 0))
|
|
assert_equal("+0001", sprintf("%+.4b", 1))
|
|
assert_equal("+0010", sprintf("%+.4b", 2))
|
|
assert_equal("-0001", sprintf("%+.4b", -1))
|
|
|
|
assert_equal(" +0000", sprintf("%+6.4b", 0))
|
|
assert_equal(" +0001", sprintf("%+6.4b", 1))
|
|
assert_equal(" +0010", sprintf("%+6.4b", 2))
|
|
assert_equal(" -0001", sprintf("%+6.4b", -1))
|
|
end
|
|
|
|
def test_nan
|
|
nan = 0.0 / 0.0
|
|
assert_equal("NaN", sprintf("%f", nan))
|
|
assert_equal("NaN", sprintf("%-f", nan))
|
|
assert_equal("+NaN", sprintf("%+f", nan))
|
|
|
|
assert_equal(" NaN", sprintf("%8f", nan))
|
|
assert_equal("NaN ", sprintf("%-8f", nan))
|
|
assert_equal(" +NaN", sprintf("%+8f", nan))
|
|
|
|
assert_equal("00000NaN", sprintf("%08f", nan))
|
|
assert_equal("NaN ", sprintf("%-08f", nan))
|
|
assert_equal("+0000NaN", sprintf("%+08f", nan))
|
|
|
|
assert_equal(" NaN", sprintf("% 8f", nan))
|
|
assert_equal(" NaN ", sprintf("%- 8f", nan))
|
|
assert_equal(" +NaN", sprintf("%+ 8f", nan))
|
|
|
|
assert_equal(" 0000NaN", sprintf("% 08f", nan))
|
|
assert_equal(" NaN ", sprintf("%- 08f", nan))
|
|
assert_equal("+0000NaN", sprintf("%+ 08f", nan))
|
|
end
|
|
|
|
def test_inf
|
|
inf = 1.0 / 0.0
|
|
assert_equal("Inf", sprintf("%f", inf))
|
|
assert_equal("Inf", sprintf("%-f", inf))
|
|
assert_equal("+Inf", sprintf("%+f", inf))
|
|
|
|
assert_equal(" Inf", sprintf("%8f", inf))
|
|
assert_equal("Inf ", sprintf("%-8f", inf))
|
|
assert_equal(" +Inf", sprintf("%+8f", inf))
|
|
|
|
assert_equal("00000Inf", sprintf("%08f", inf))
|
|
assert_equal("Inf ", sprintf("%-08f", inf))
|
|
assert_equal("+0000Inf", sprintf("%+08f", inf))
|
|
|
|
assert_equal(" Inf", sprintf("% 8f", inf))
|
|
assert_equal(" Inf ", sprintf("%- 8f", inf))
|
|
assert_equal(" +Inf", sprintf("%+ 8f", inf))
|
|
|
|
assert_equal(" 0000Inf", sprintf("% 08f", inf))
|
|
assert_equal(" Inf ", sprintf("%- 08f", inf))
|
|
assert_equal("+0000Inf", sprintf("%+ 08f", inf))
|
|
|
|
assert_equal("-Inf", sprintf("%f", -inf))
|
|
assert_equal("-Inf", sprintf("%-f", -inf))
|
|
assert_equal("-Inf", sprintf("%+f", -inf))
|
|
|
|
assert_equal(" -Inf", sprintf("%8f", -inf))
|
|
assert_equal("-Inf ", sprintf("%-8f", -inf))
|
|
assert_equal(" -Inf", sprintf("%+8f", -inf))
|
|
|
|
assert_equal("-0000Inf", sprintf("%08f", -inf))
|
|
assert_equal("-Inf ", sprintf("%-08f", -inf))
|
|
assert_equal("-0000Inf", sprintf("%+08f", -inf))
|
|
|
|
assert_equal(" -Inf", sprintf("% 8f", -inf))
|
|
assert_equal("-Inf ", sprintf("%- 8f", -inf))
|
|
assert_equal(" -Inf", sprintf("%+ 8f", -inf))
|
|
|
|
assert_equal("-0000Inf", sprintf("% 08f", -inf))
|
|
assert_equal("-Inf ", sprintf("%- 08f", -inf))
|
|
assert_equal("-0000Inf", sprintf("%+ 08f", -inf))
|
|
end
|
|
end
|