2004-02-18 04:09:21 -05:00
|
|
|
require 'test/unit'
|
|
|
|
|
|
|
|
class TestSprintf < Test::Unit::TestCase
|
2007-08-02 00:46:41 -04:00
|
|
|
def test_positional
|
|
|
|
assert_equal(" 00001", sprintf("%*1$.*2$3$d", 10, 5, 1))
|
|
|
|
end
|
|
|
|
|
2004-02-18 04:09:21 -05:00
|
|
|
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))
|
2008-01-24 05:29:22 -05:00
|
|
|
assert_equal("..11", sprintf("%04b", -1))
|
2004-02-18 04:09:21 -05:00
|
|
|
|
|
|
|
assert_equal("0000", sprintf("%.4b", 0))
|
|
|
|
assert_equal("0001", sprintf("%.4b", 1))
|
|
|
|
assert_equal("0010", sprintf("%.4b", 2))
|
2010-10-15 04:31:58 -04:00
|
|
|
assert_equal("..11", sprintf("%.4b", -1))
|
2004-02-18 04:09:21 -05:00
|
|
|
|
|
|
|
assert_equal(" 0000", sprintf("%6.4b", 0))
|
|
|
|
assert_equal(" 0001", sprintf("%6.4b", 1))
|
|
|
|
assert_equal(" 0010", sprintf("%6.4b", 2))
|
2010-10-15 04:31:58 -04:00
|
|
|
assert_equal(" ..11", sprintf("%6.4b", -1))
|
2004-02-18 04:09:21 -05:00
|
|
|
|
2008-02-29 20:22:45 -05:00
|
|
|
assert_equal(" 0", sprintf("%#4b", 0))
|
2004-02-18 04:09:21 -05:00
|
|
|
assert_equal(" 0b1", sprintf("%#4b", 1))
|
|
|
|
assert_equal("0b10", sprintf("%#4b", 2))
|
|
|
|
assert_equal("0b..1", sprintf("%#4b", -1))
|
|
|
|
|
2008-02-29 20:22:45 -05:00
|
|
|
assert_equal("0000", sprintf("%#04b", 0))
|
2004-02-18 04:09:21 -05:00
|
|
|
assert_equal("0b01", sprintf("%#04b", 1))
|
|
|
|
assert_equal("0b10", sprintf("%#04b", 2))
|
2008-01-24 05:29:22 -05:00
|
|
|
assert_equal("0b..1", sprintf("%#04b", -1))
|
2004-02-18 04:09:21 -05:00
|
|
|
|
2008-02-29 20:22:45 -05:00
|
|
|
assert_equal("0000", sprintf("%#.4b", 0))
|
2004-02-18 04:09:21 -05:00
|
|
|
assert_equal("0b0001", sprintf("%#.4b", 1))
|
|
|
|
assert_equal("0b0010", sprintf("%#.4b", 2))
|
2010-10-15 04:31:58 -04:00
|
|
|
assert_equal("0b..11", sprintf("%#.4b", -1))
|
2004-02-18 04:09:21 -05:00
|
|
|
|
2008-02-29 20:22:45 -05:00
|
|
|
assert_equal(" 0000", sprintf("%#6.4b", 0))
|
2004-02-18 04:09:21 -05:00
|
|
|
assert_equal("0b0001", sprintf("%#6.4b", 1))
|
|
|
|
assert_equal("0b0010", sprintf("%#6.4b", 2))
|
2010-10-15 04:31:58 -04:00
|
|
|
assert_equal("0b..11", sprintf("%#6.4b", -1))
|
2004-02-18 04:09:21 -05:00
|
|
|
|
|
|
|
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
|
2004-06-22 10:38:21 -04:00
|
|
|
|
|
|
|
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))
|
|
|
|
|
2008-03-06 01:11:43 -05:00
|
|
|
assert_equal(" NaN", sprintf("%08f", nan))
|
2004-06-22 10:38:21 -04:00
|
|
|
assert_equal("NaN ", sprintf("%-08f", nan))
|
2008-03-06 01:11:43 -05:00
|
|
|
assert_equal(" +NaN", sprintf("%+08f", nan))
|
2004-06-22 10:38:21 -04:00
|
|
|
|
|
|
|
assert_equal(" NaN", sprintf("% 8f", nan))
|
|
|
|
assert_equal(" NaN ", sprintf("%- 8f", nan))
|
|
|
|
assert_equal(" +NaN", sprintf("%+ 8f", nan))
|
|
|
|
|
2008-03-06 01:11:43 -05:00
|
|
|
assert_equal(" NaN", sprintf("% 08f", nan))
|
2004-06-22 10:38:21 -04:00
|
|
|
assert_equal(" NaN ", sprintf("%- 08f", nan))
|
2008-03-06 01:11:43 -05:00
|
|
|
assert_equal(" +NaN", sprintf("%+ 08f", nan))
|
2004-06-22 10:38:21 -04:00
|
|
|
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))
|
|
|
|
|
2008-03-06 01:11:43 -05:00
|
|
|
assert_equal(" Inf", sprintf("%08f", inf))
|
2004-06-22 10:38:21 -04:00
|
|
|
assert_equal("Inf ", sprintf("%-08f", inf))
|
2008-03-06 01:11:43 -05:00
|
|
|
assert_equal(" +Inf", sprintf("%+08f", inf))
|
2004-06-22 10:38:21 -04:00
|
|
|
|
|
|
|
assert_equal(" Inf", sprintf("% 8f", inf))
|
|
|
|
assert_equal(" Inf ", sprintf("%- 8f", inf))
|
|
|
|
assert_equal(" +Inf", sprintf("%+ 8f", inf))
|
|
|
|
|
2008-03-06 01:11:43 -05:00
|
|
|
assert_equal(" Inf", sprintf("% 08f", inf))
|
2004-06-22 10:38:21 -04:00
|
|
|
assert_equal(" Inf ", sprintf("%- 08f", inf))
|
2008-03-06 01:11:43 -05:00
|
|
|
assert_equal(" +Inf", sprintf("%+ 08f", inf))
|
2004-06-22 10:38:21 -04:00
|
|
|
|
|
|
|
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))
|
|
|
|
|
2008-03-06 01:11:43 -05:00
|
|
|
assert_equal(" -Inf", sprintf("%08f", -inf))
|
2004-06-22 10:38:21 -04:00
|
|
|
assert_equal("-Inf ", sprintf("%-08f", -inf))
|
2008-03-06 01:11:43 -05:00
|
|
|
assert_equal(" -Inf", sprintf("%+08f", -inf))
|
2004-06-22 10:38:21 -04:00
|
|
|
|
|
|
|
assert_equal(" -Inf", sprintf("% 8f", -inf))
|
|
|
|
assert_equal("-Inf ", sprintf("%- 8f", -inf))
|
|
|
|
assert_equal(" -Inf", sprintf("%+ 8f", -inf))
|
|
|
|
|
2008-03-06 01:11:43 -05:00
|
|
|
assert_equal(" -Inf", sprintf("% 08f", -inf))
|
2004-06-22 10:38:21 -04:00
|
|
|
assert_equal("-Inf ", sprintf("%- 08f", -inf))
|
2008-03-06 01:11:43 -05:00
|
|
|
assert_equal(" -Inf", sprintf("%+ 08f", -inf))
|
2007-12-01 00:43:06 -05:00
|
|
|
assert_equal('..f00000000',
|
|
|
|
sprintf("%x", -2**32), '[ruby-dev:32351]')
|
|
|
|
assert_equal("..101111111111111111111111111111111",
|
|
|
|
sprintf("%b", -2147483649), '[ruby-dev:32365]')
|
2008-03-06 01:21:07 -05:00
|
|
|
assert_equal(" Inf", sprintf("% e", inf), '[ruby-dev:34002]')
|
2004-06-22 10:38:21 -04:00
|
|
|
end
|
2007-07-15 16:45:55 -04:00
|
|
|
|
2014-08-18 04:06:48 -04:00
|
|
|
def test_rational
|
|
|
|
assert_match(/\A0\.10+\z/, sprintf("%.60f", 0.1r))
|
2014-09-14 19:13:36 -04:00
|
|
|
assert_match(/\A0\.010+\z/, sprintf("%.60f", 0.01r))
|
|
|
|
assert_match(/\A0\.0010+\z/, sprintf("%.60f", 0.001r))
|
2014-08-18 04:06:48 -04:00
|
|
|
assert_match(/\A0\.3+\z/, sprintf("%.60f", 1/3r))
|
2014-08-19 09:28:32 -04:00
|
|
|
assert_match(/\A1\.20+\z/, sprintf("%.60f", 1.2r))
|
2014-09-14 19:13:36 -04:00
|
|
|
|
|
|
|
0.upto(9) do |len|
|
|
|
|
-1.upto(9) do |prec|
|
|
|
|
['', '+', '-', ' ', '0', '+0', '-0', ' 0', '+ ', '- ', '+ 0', '- 0'].each do |flags|
|
|
|
|
fmt = "%#{flags}#{len > 0 ? len : ''}#{prec >= 0 ? ".#{prec}" : ''}f"
|
|
|
|
[0, 0.1, 0.01, 0.001, 1.001, 100.0, 100.001, 10000000000.0, 0.00000000001, 1/3r, 2/3r, 1.2r, 10r].each do |num|
|
|
|
|
assert_equal(sprintf(fmt, num.to_f), sprintf(fmt, num.to_r), "sprintf(#{fmt.inspect}, #{num.inspect}.to_r)")
|
|
|
|
assert_equal(sprintf(fmt, -num.to_f), sprintf(fmt, -num.to_r), "sprintf(#{fmt.inspect}, #{(-num).inspect}.to_r)") if num > 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-08-18 04:06:48 -04:00
|
|
|
end
|
|
|
|
|
2013-12-28 18:47:27 -05:00
|
|
|
def test_hash
|
|
|
|
options = {:capture=>/\d+/}
|
|
|
|
assert_equal("with options {:capture=>/\\d+/}", sprintf("with options %p" % options))
|
|
|
|
end
|
|
|
|
|
2007-07-15 16:45:55 -04:00
|
|
|
def test_invalid
|
|
|
|
# Star precision before star width:
|
2007-11-18 02:18:56 -05:00
|
|
|
assert_raise(ArgumentError, "[ruby-core:11569]") {sprintf("%.**d", 5, 10, 1)}
|
2007-07-15 16:45:55 -04:00
|
|
|
|
|
|
|
# Precision before flags and width:
|
2007-11-18 02:18:56 -05:00
|
|
|
assert_raise(ArgumentError, "[ruby-core:11569]") {sprintf("%.5+05d", 5)}
|
|
|
|
assert_raise(ArgumentError, "[ruby-core:11569]") {sprintf("%.5 5d", 5)}
|
2007-07-15 16:45:55 -04:00
|
|
|
|
|
|
|
# Overriding a star width with a numeric one:
|
2007-11-18 02:18:56 -05:00
|
|
|
assert_raise(ArgumentError, "[ruby-core:11569]") {sprintf("%*1s", 5, 1)}
|
2007-07-15 16:45:55 -04:00
|
|
|
|
|
|
|
# Width before flags:
|
2007-11-18 02:18:56 -05:00
|
|
|
assert_raise(ArgumentError, "[ruby-core:11569]") {sprintf("%5+0d", 1)}
|
|
|
|
assert_raise(ArgumentError, "[ruby-core:11569]") {sprintf("%5 0d", 1)}
|
2007-07-15 16:45:55 -04:00
|
|
|
|
|
|
|
# Specifying width multiple times:
|
2007-11-18 02:18:56 -05:00
|
|
|
assert_raise(ArgumentError, "[ruby-core:11569]") {sprintf("%50+30+20+10+5d", 5)}
|
|
|
|
assert_raise(ArgumentError, "[ruby-core:11569]") {sprintf("%50 30 20 10 5d", 5)}
|
2007-07-15 16:45:55 -04:00
|
|
|
|
|
|
|
# Specifying the precision multiple times with negative star arguments:
|
2007-11-18 02:18:56 -05:00
|
|
|
assert_raise(ArgumentError, "[ruby-core:11570]") {sprintf("%.*.*.*.*f", -1, -1, -1, 5, 1)}
|
2007-07-15 16:45:55 -04:00
|
|
|
|
|
|
|
# Null bytes after percent signs are removed:
|
2007-11-18 02:18:56 -05:00
|
|
|
assert_equal("%\0x hello", sprintf("%\0x hello"), "[ruby-core:11571]")
|
2007-07-15 16:45:55 -04:00
|
|
|
|
2007-11-18 02:18:56 -05:00
|
|
|
assert_raise(ArgumentError, "[ruby-core:11573]") {sprintf("%.25555555555555555555555555555555555555s", "hello")}
|
2008-01-23 08:49:06 -05:00
|
|
|
|
|
|
|
assert_raise(ArgumentError) { sprintf("%\1", 1) }
|
|
|
|
assert_raise(ArgumentError) { sprintf("%!", 1) }
|
|
|
|
assert_raise(ArgumentError) { sprintf("%1$1$d", 1) }
|
|
|
|
assert_raise(ArgumentError) { sprintf("%0%") }
|
2014-06-26 14:41:57 -04:00
|
|
|
|
|
|
|
assert_raise_with_message(ArgumentError, /unnumbered\(1\) mixed with numbered/) { sprintf("%1$*d", 3) }
|
|
|
|
assert_raise_with_message(ArgumentError, /unnumbered\(1\) mixed with numbered/) { sprintf("%1$.*d", 3) }
|
|
|
|
|
2008-12-28 04:51:18 -05:00
|
|
|
verbose, $VERBOSE = $VERBOSE, nil
|
2008-01-23 08:49:06 -05:00
|
|
|
assert_nothing_raised { sprintf("", 1) }
|
2008-12-28 04:51:18 -05:00
|
|
|
ensure
|
|
|
|
$VERBOSE = verbose
|
2007-07-15 16:45:55 -04:00
|
|
|
end
|
2007-12-22 00:19:38 -05:00
|
|
|
|
|
|
|
def test_float
|
|
|
|
assert_equal("36893488147419111424",
|
|
|
|
sprintf("%20.0f", 36893488147419107329.0))
|
2008-05-19 01:20:21 -04:00
|
|
|
assert_equal(" Inf", sprintf("% 0e", 1.0/0.0), "moved from btest/knownbug")
|
2010-11-08 01:44:39 -05:00
|
|
|
assert_equal(" -0.", sprintf("%#10.0f", -0.5), "[ruby-dev:42552]")
|
2010-11-22 11:06:38 -05:00
|
|
|
assert_equal("0x1p+2", sprintf('%.0a', Float('0x1.fp+1')), "[ruby-dev:42551]")
|
2010-11-08 23:22:25 -05:00
|
|
|
assert_equal("-0x1.0p+2", sprintf('%.1a', Float('-0x1.ffp+1')), "[ruby-dev:42551]")
|
2007-12-22 00:19:38 -05:00
|
|
|
end
|
2008-01-23 08:49:06 -05:00
|
|
|
|
2010-04-01 00:32:57 -04:00
|
|
|
def test_float_hex
|
|
|
|
assert_equal("-0x0p+0", sprintf("%a", -0.0))
|
|
|
|
assert_equal("0x0p+0", sprintf("%a", 0.0))
|
|
|
|
assert_equal("0x1p-1", sprintf("%a", 0.5))
|
|
|
|
assert_equal("0x1p+0", sprintf("%a", 1.0))
|
|
|
|
assert_equal("0x1p+1", sprintf("%a", 2.0))
|
|
|
|
assert_equal("0x1p+10", sprintf("%a", 1024))
|
|
|
|
assert_equal("0x1.23456p+789", sprintf("%a", 3.704450999893983e+237))
|
|
|
|
assert_equal("0x1p-1074", sprintf("%a", 4.9e-324))
|
2010-04-01 02:44:30 -04:00
|
|
|
assert_equal("Inf", sprintf("%e", Float::INFINITY))
|
|
|
|
assert_equal("Inf", sprintf("%E", Float::INFINITY))
|
|
|
|
assert_equal("NaN", sprintf("%e", Float::NAN))
|
|
|
|
assert_equal("NaN", sprintf("%E", Float::NAN))
|
2010-10-17 20:59:31 -04:00
|
|
|
|
|
|
|
assert_equal(" -0x1p+0", sprintf("%10a", -1))
|
|
|
|
assert_equal(" -0x1.8p+0", sprintf("%10a", -1.5))
|
|
|
|
assert_equal(" -0x1.4p+0", sprintf("%10a", -1.25))
|
|
|
|
assert_equal(" -0x1.2p+0", sprintf("%10a", -1.125))
|
|
|
|
assert_equal(" -0x1.1p+0", sprintf("%10a", -1.0625))
|
2010-10-17 21:53:41 -04:00
|
|
|
assert_equal("-0x1.08p+0", sprintf("%10a", -1.03125))
|
2010-10-18 14:08:58 -04:00
|
|
|
|
|
|
|
bug3962 = '[ruby-core:32841]'
|
|
|
|
assert_equal("-0x0001p+0", sprintf("%010a", -1), bug3962)
|
|
|
|
assert_equal("-0x01.8p+0", sprintf("%010a", -1.5), bug3962)
|
|
|
|
assert_equal("-0x01.4p+0", sprintf("%010a", -1.25), bug3962)
|
|
|
|
assert_equal("-0x01.2p+0", sprintf("%010a", -1.125), bug3962)
|
|
|
|
assert_equal("-0x01.1p+0", sprintf("%010a", -1.0625), bug3962)
|
|
|
|
assert_equal("-0x1.08p+0", sprintf("%010a", -1.03125), bug3962)
|
2010-10-18 23:26:35 -04:00
|
|
|
|
|
|
|
bug3964 = '[ruby-core:32848]'
|
2010-10-19 21:03:18 -04:00
|
|
|
assert_equal("0x000000000000000p+0", sprintf("%020a", 0), bug3964)
|
2010-10-18 23:26:35 -04:00
|
|
|
assert_equal("0x000000000000001p+0", sprintf("%020a", 1), bug3964)
|
|
|
|
assert_equal("-0x00000000000001p+0", sprintf("%020a", -1), bug3964)
|
2010-10-19 21:03:18 -04:00
|
|
|
assert_equal("0x00000000000000.p+0", sprintf("%#020a", 0), bug3964)
|
|
|
|
|
|
|
|
bug3965 = '[ruby-dev:42431]'
|
|
|
|
assert_equal("0x1.p+0", sprintf("%#.0a", 1), bug3965)
|
|
|
|
assert_equal("0x00000000000000.p+0", sprintf("%#020a", 0), bug3965)
|
|
|
|
assert_equal("0x0000.0000000000p+0", sprintf("%#020.10a", 0), bug3965)
|
2010-10-24 17:21:40 -04:00
|
|
|
|
|
|
|
bug3979 = '[ruby-dev:42453]'
|
|
|
|
assert_equal(" 0x0.000p+0", sprintf("%20.3a", 0), bug3979)
|
|
|
|
assert_equal(" 0x1.000p+0", sprintf("%20.3a", 1), bug3979)
|
2010-04-01 00:32:57 -04:00
|
|
|
end
|
|
|
|
|
2008-01-23 08:49:06 -05:00
|
|
|
BSIZ = 120
|
|
|
|
|
|
|
|
def test_skip
|
|
|
|
assert_equal(" " * BSIZ + "1", sprintf(" " * BSIZ + "%d", 1))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_char
|
|
|
|
assert_equal("a", sprintf("%c", 97))
|
|
|
|
assert_equal("a", sprintf("%c", ?a))
|
|
|
|
assert_raise(ArgumentError) { sprintf("%c", sprintf("%c%c", ?a, ?a)) }
|
|
|
|
assert_equal(" " * (BSIZ - 1) + "a", sprintf(" " * (BSIZ - 1) + "%c", ?a))
|
|
|
|
assert_equal(" " * (BSIZ - 1) + "a", sprintf(" " * (BSIZ - 1) + "%-1c", ?a))
|
|
|
|
assert_equal(" " * BSIZ + "a", sprintf("%#{ BSIZ + 1 }c", ?a))
|
|
|
|
assert_equal("a" + " " * BSIZ, sprintf("%-#{ BSIZ + 1 }c", ?a))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_string
|
|
|
|
assert_equal("foo", sprintf("%s", "foo"))
|
|
|
|
assert_equal("fo", sprintf("%.2s", "foo"))
|
|
|
|
assert_equal(" " * BSIZ, sprintf("%s", " " * BSIZ))
|
|
|
|
assert_equal(" " * (BSIZ - 1) + "foo", sprintf("%#{ BSIZ - 1 + 3 }s", "foo"))
|
|
|
|
assert_equal(" " * BSIZ + "foo", sprintf("%#{ BSIZ + 3 }s", "foo"))
|
|
|
|
assert_equal("foo" + " " * BSIZ, sprintf("%-#{ BSIZ + 3 }s", "foo"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_integer
|
|
|
|
assert_equal("01", sprintf("%#o", 1))
|
|
|
|
assert_equal("0x1", sprintf("%#x", 1))
|
|
|
|
assert_equal("0X1", sprintf("%#X", 1))
|
|
|
|
assert_equal("0b1", sprintf("%#b", 1))
|
|
|
|
assert_equal("0B1", sprintf("%#B", 1))
|
|
|
|
assert_equal("1", sprintf("%d", 1.0))
|
|
|
|
assert_equal("4294967296", sprintf("%d", (2**32).to_f))
|
2008-03-06 01:48:15 -05:00
|
|
|
assert_equal("-2147483648", sprintf("%d", -(2**31).to_f))
|
|
|
|
assert_equal("18446744073709551616", sprintf("%d", (2**64).to_f))
|
|
|
|
assert_equal("-9223372036854775808", sprintf("%d", -(2**63).to_f))
|
2008-01-23 08:49:06 -05:00
|
|
|
assert_equal("1", sprintf("%d", "1"))
|
|
|
|
o = Object.new; def o.to_int; 1; end
|
|
|
|
assert_equal("1", sprintf("%d", o))
|
|
|
|
assert_equal("+1", sprintf("%+d", 1))
|
|
|
|
assert_equal(" 1", sprintf("% d", 1))
|
|
|
|
assert_equal("..f", sprintf("%x", -1))
|
|
|
|
assert_equal("..7", sprintf("%o", -1))
|
|
|
|
one = (2**32).coerce(1).first
|
|
|
|
mone = (2**32).coerce(-1).first
|
|
|
|
assert_equal("+1", sprintf("%+d", one))
|
|
|
|
assert_equal(" 1", sprintf("% d", one))
|
|
|
|
assert_equal("..f", sprintf("%x", mone))
|
|
|
|
assert_equal("..7", sprintf("%o", mone))
|
|
|
|
assert_equal(" " * BSIZ + "1", sprintf("%#{ BSIZ + 1 }d", one))
|
|
|
|
assert_equal(" " * (BSIZ - 1) + "1", sprintf(" " * (BSIZ - 1) + "%d", 1))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_float2
|
|
|
|
inf = 1.0 / 0.0
|
|
|
|
assert_equal(" " * BSIZ + "Inf", sprintf("%#{ BSIZ + 3 }.1f", inf))
|
|
|
|
assert_equal("+Inf", sprintf("%+-f", inf))
|
|
|
|
assert_equal(" " * BSIZ + "1.0", sprintf("%#{ BSIZ + 3 }.1f", 1.0))
|
|
|
|
end
|
|
|
|
|
|
|
|
class T012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_star
|
|
|
|
assert_equal("-1 ", sprintf("%*d", -3, -1))
|
2014-06-26 09:30:08 -04:00
|
|
|
assert_raise_with_message(ArgumentError, /width too big/) {
|
|
|
|
sprintf("%*999999999999999999999999999999999999999999999999999999999999$d", 1)
|
|
|
|
}
|
|
|
|
assert_raise_with_message(ArgumentError, /prec too big/) {
|
|
|
|
sprintf("%.*999999999999999999999999999999999999999999999999999999999999$d", 1)
|
|
|
|
}
|
2008-01-23 08:49:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_escape
|
|
|
|
assert_equal("%" * BSIZ, sprintf("%%" * BSIZ))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_rb_sprintf
|
2010-04-29 01:46:04 -04:00
|
|
|
assert_match(/^#<TestSprintf::T012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789:0x[0-9a-f]+>$/,
|
|
|
|
T012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789.new.inspect)
|
2008-01-23 08:49:06 -05:00
|
|
|
end
|
2008-01-24 01:59:03 -05:00
|
|
|
|
|
|
|
def test_negative_hex
|
|
|
|
s1 = sprintf("%0x", -0x40000000)
|
|
|
|
s2 = sprintf("%0x", -0x40000001)
|
|
|
|
b1 = (/\.\./ =~ s1) != nil
|
|
|
|
b2 = (/\.\./ =~ s2) != nil
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_equal(b1, b2, "[ruby-dev:33224]")
|
2008-01-24 01:59:03 -05:00
|
|
|
end
|
2009-03-27 19:49:52 -04:00
|
|
|
|
2012-04-11 09:01:14 -04:00
|
|
|
def test_named_untyped
|
2009-03-27 19:49:52 -04:00
|
|
|
assert_equal("value", sprintf("%<key>s", :key => "value"))
|
2013-10-09 04:43:12 -04:00
|
|
|
assert_raise_with_message(ArgumentError, "named<key2> after numbered") {sprintf("%1$<key2>s", :key => "value")}
|
|
|
|
assert_raise_with_message(ArgumentError, "named<key2> after unnumbered(2)") {sprintf("%s%s%<key2>s", "foo", "bar", :key => "value")}
|
|
|
|
assert_raise_with_message(ArgumentError, "named<key2> after <key>") {sprintf("%<key><key2>s", :key => "value")}
|
|
|
|
assert_raise_with_message(KeyError, "key<key> not found") {sprintf("%<key>s", {})}
|
2012-04-11 09:01:14 -04:00
|
|
|
end
|
|
|
|
|
2012-04-11 09:01:16 -04:00
|
|
|
def test_named_untyped_enc
|
|
|
|
key = "\u{3012}"
|
|
|
|
[Encoding::UTF_8, Encoding::EUC_JP].each do |enc|
|
|
|
|
k = key.encode(enc)
|
2013-10-09 04:43:12 -04:00
|
|
|
e = assert_raise_with_message(ArgumentError, "named<#{k}> after numbered") {sprintf("%1$<#{k}>s", key: "value")}
|
2012-04-11 09:01:16 -04:00
|
|
|
assert_equal(enc, e.message.encoding)
|
2013-10-09 04:43:12 -04:00
|
|
|
e = assert_raise_with_message(ArgumentError, "named<#{k}> after unnumbered(2)") {sprintf("%s%s%<#{k}>s", "foo", "bar", key: "value")}
|
2012-04-11 09:01:16 -04:00
|
|
|
assert_equal(enc, e.message.encoding)
|
2013-10-09 04:43:12 -04:00
|
|
|
e = assert_raise_with_message(ArgumentError, "named<#{k}> after <key>") {sprintf("%<key><#{k}>s", key: "value")}
|
2012-04-11 09:01:16 -04:00
|
|
|
assert_equal(enc, e.message.encoding)
|
2013-10-09 04:43:12 -04:00
|
|
|
e = assert_raise_with_message(ArgumentError, "named<key> after <#{k}>") {sprintf("%<#{k}><key>s", k.to_sym => "value")}
|
2012-04-11 09:01:16 -04:00
|
|
|
assert_equal(enc, e.message.encoding)
|
2013-10-09 04:43:12 -04:00
|
|
|
e = assert_raise_with_message(KeyError, "key<#{k}> not found") {sprintf("%<#{k}>s", {})}
|
2012-04-11 09:31:23 -04:00
|
|
|
assert_equal(enc, e.message.encoding)
|
2012-04-11 09:01:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-04-11 09:01:14 -04:00
|
|
|
def test_named_typed
|
2010-07-21 02:16:07 -04:00
|
|
|
assert_equal("value", sprintf("%{key}", :key => "value"))
|
2013-10-09 04:43:12 -04:00
|
|
|
assert_raise_with_message(ArgumentError, "named{key2} after numbered") {sprintf("%1${key2}", :key => "value")}
|
|
|
|
assert_raise_with_message(ArgumentError, "named{key2} after unnumbered(2)") {sprintf("%s%s%{key2}", "foo", "bar", :key => "value")}
|
|
|
|
assert_raise_with_message(ArgumentError, "named{key2} after <key>") {sprintf("%<key>{key2}", :key => "value")}
|
2010-07-21 02:16:07 -04:00
|
|
|
assert_equal("value{key2}", sprintf("%{key}{key2}", :key => "value"))
|
2013-10-09 04:43:12 -04:00
|
|
|
assert_raise_with_message(KeyError, "key{key} not found") {sprintf("%{key}", {})}
|
2009-03-27 19:49:52 -04:00
|
|
|
end
|
2012-04-11 09:01:16 -04:00
|
|
|
|
|
|
|
def test_named_typed_enc
|
|
|
|
key = "\u{3012}"
|
|
|
|
[Encoding::UTF_8, Encoding::EUC_JP].each do |enc|
|
|
|
|
k = key.encode(enc)
|
2013-10-09 04:43:12 -04:00
|
|
|
e = assert_raise_with_message(ArgumentError, "named{#{k}} after numbered") {sprintf("%1${#{k}}s", key: "value")}
|
2012-04-11 09:01:16 -04:00
|
|
|
assert_equal(enc, e.message.encoding)
|
2013-10-09 04:43:12 -04:00
|
|
|
e = assert_raise_with_message(ArgumentError, "named{#{k}} after unnumbered(2)") {sprintf("%s%s%{#{k}}s", "foo", "bar", key: "value")}
|
2012-04-11 09:01:16 -04:00
|
|
|
assert_equal(enc, e.message.encoding)
|
2013-10-09 04:43:12 -04:00
|
|
|
e = assert_raise_with_message(ArgumentError, "named{#{k}} after <key>") {sprintf("%<key>{#{k}}s", key: "value")}
|
2012-04-11 09:01:16 -04:00
|
|
|
assert_equal(enc, e.message.encoding)
|
2013-10-09 04:43:12 -04:00
|
|
|
e = assert_raise_with_message(ArgumentError, "named{key} after <#{k}>") {sprintf("%<#{k}>{key}s", k.to_sym => "value")}
|
2012-04-11 09:01:16 -04:00
|
|
|
assert_equal(enc, e.message.encoding)
|
2013-10-09 04:43:12 -04:00
|
|
|
e = assert_raise_with_message(KeyError, "key{#{k}} not found") {sprintf("%{#{k}}", {})}
|
2012-04-11 09:31:23 -04:00
|
|
|
assert_equal(enc, e.message.encoding)
|
2012-04-11 09:01:16 -04:00
|
|
|
end
|
|
|
|
end
|
2004-02-18 04:09:21 -05:00
|
|
|
end
|