Created
November 28, 2010 15:17
-
-
Save matthewd/719010 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From f67f1689b35c10e2a5677b8341b63b4cb4615988 Mon Sep 17 00:00:00 2001 | |
From: Matthew Draper <matthew@trebex.net> | |
Date: Mon, 29 Nov 2010 01:34:16 +1030 | |
Subject: [PATCH 2/2] Some sprintf specs. | |
Horribly ungrouped, and not in a state to be inflicted upon the world. | |
But they do provide some extra exercise for sprintf implementations. | |
--- | |
spec/ruby/core/kernel/sprintf_spec.rb | 214 +++++++++++++++++++++++++++++++++ | |
spec/ruby/core/string/modulo_spec.rb | 20 +-- | |
2 files changed, 220 insertions(+), 14 deletions(-) | |
diff --git a/spec/ruby/core/kernel/sprintf_spec.rb b/spec/ruby/core/kernel/sprintf_spec.rb | |
index e15c881..c780472 100644 | |
--- a/spec/ruby/core/kernel/sprintf_spec.rb | |
+++ b/spec/ruby/core/kernel/sprintf_spec.rb | |
@@ -15,6 +15,220 @@ describe "Kernel#sprintf" do | |
sprintf("%d%d%s%s", nil, 4, 'a', 'b').should == '04ab' | |
end | |
end | |
+ | |
+ it "passes some tests for positive %x" do | |
+ sprintf("%x", 123).should == "7b" | |
+ sprintf("%0x", 123).should == "7b" | |
+ sprintf("% x", 123).should == " 7b" | |
+ sprintf("%+x", 123).should == "+7b" | |
+ sprintf("%+0x", 123).should == "+7b" | |
+ sprintf("%+ x", 123).should == "+7b" | |
+ sprintf("% 0x", 123).should == " 7b" | |
+ | |
+ sprintf("%#x", 123).should == "0x7b" | |
+ sprintf("%#0x", 123).should == "0x7b" | |
+ sprintf("%# x", 123).should == " 0x7b" | |
+ sprintf("%#+x", 123).should == "+0x7b" | |
+ sprintf("%#+0x", 123).should == "+0x7b" | |
+ sprintf("%#+ x", 123).should == "+0x7b" | |
+ sprintf("%# 0x", 123).should == " 0x7b" | |
+ | |
+ sprintf("%8x", 123).should == " 7b" | |
+ sprintf("%08x", 123).should == "0000007b" | |
+ sprintf("% 8x", 123).should == " 7b" | |
+ sprintf("%+8x", 123).should == " +7b" | |
+ sprintf("%+08x", 123).should == "+000007b" | |
+ sprintf("%+ 8x", 123).should == " +7b" | |
+ sprintf("% 08x", 123).should == " 000007b" | |
+ | |
+ sprintf("%#8x", 123).should == " 0x7b" | |
+ sprintf("%#08x", 123).should == "0x00007b" | |
+ sprintf("%# 8x", 123).should == " 0x7b" | |
+ sprintf("%#+8x", 123).should == " +0x7b" | |
+ sprintf("%#+08x", 123).should == "+0x0007b" | |
+ sprintf("%#+ 8x", 123).should == " +0x7b" | |
+ sprintf("%# 08x", 123).should == " 0x0007b" | |
+ | |
+ sprintf("%8.10x", 123).should == "000000007b" | |
+ sprintf("%08.10x", 123).should == "000000007b" | |
+ sprintf("% 8.10x", 123).should == " 000000007b" | |
+ sprintf("%+8.10x", 123).should == "+000000007b" | |
+ sprintf("%+08.10x", 123).should == "+000000007b" | |
+ sprintf("%+ 8.10x", 123).should == "+000000007b" | |
+ sprintf("% 08.10x", 123).should == " 000000007b" | |
+ | |
+ sprintf("%10.8x", 123).should == " 0000007b" | |
+ sprintf("%010.8x", 123).should == " 0000007b" | |
+ sprintf("% 10.8x", 123).should == " 0000007b" | |
+ sprintf("%+10.8x", 123).should == " +0000007b" | |
+ sprintf("%+010.8x", 123).should == " +0000007b" | |
+ sprintf("%+ 10.8x", 123).should == " +0000007b" | |
+ sprintf("% 010.8x", 123).should == " 0000007b" | |
+ end | |
+ | |
+ it "passes some tests for negative %x" do | |
+ sprintf("%x", -123).should == "..f85" | |
+ sprintf("%0x", -123).should == "f85" | |
+ sprintf("% x", -123).should == "-7b" | |
+ sprintf("%+x", -123).should == "-7b" | |
+ sprintf("%+0x", -123).should == "-7b" | |
+ sprintf("%+ x", -123).should == "-7b" | |
+ sprintf("% 0x", -123).should == "-7b" | |
+ | |
+ sprintf("%#x", -123).should == "0x..f85" | |
+ sprintf("%#0x", -123).should == "0xf85" | |
+ sprintf("%# x", -123).should == "-0x7b" | |
+ sprintf("%#+x", -123).should == "-0x7b" | |
+ sprintf("%#+0x", -123).should == "-0x7b" | |
+ sprintf("%#+ x", -123).should == "-0x7b" | |
+ sprintf("%# 0x", -123).should == "-0x7b" | |
+ | |
+ sprintf("%8x", -123).should == " ..f85" | |
+ sprintf("%08x", -123).should == "ffffff85" | |
+ sprintf("% 8x", -123).should == " -7b" | |
+ sprintf("%+8x", -123).should == " -7b" | |
+ sprintf("%+08x", -123).should == "-000007b" | |
+ sprintf("%+ 8x", -123).should == " -7b" | |
+ sprintf("% 08x", -123).should == "-000007b" | |
+ | |
+ sprintf("%#8x", -123).should == " 0x..f85" | |
+ sprintf("%#08x", -123).should == "0xffff85" | |
+ sprintf("%# 8x", -123).should == " -0x7b" | |
+ sprintf("%#+8x", -123).should == " -0x7b" | |
+ sprintf("%#+08x", -123).should == "-0x0007b" | |
+ sprintf("%#+ 8x", -123).should == " -0x7b" | |
+ sprintf("%# 08x", -123).should == "-0x0007b" | |
+ | |
+ sprintf("%8.10x", -123).should == "ffffffff85" | |
+ sprintf("%08.10x", -123).should == "ffffffff85" | |
+ sprintf("% 8.10x", -123).should == "-000000007b" | |
+ sprintf("%+8.10x", -123).should == "-000000007b" | |
+ sprintf("%+08.10x", -123).should == "-000000007b" | |
+ sprintf("%+ 8.10x", -123).should == "-000000007b" | |
+ sprintf("% 08.10x", -123).should == "-000000007b" | |
+ | |
+ sprintf("%10.8x", -123).should == " ffffff85" | |
+ sprintf("%010.8x", -123).should == " ffffff85" | |
+ sprintf("% 10.8x", -123).should == " -0000007b" | |
+ sprintf("%+10.8x", -123).should == " -0000007b" | |
+ sprintf("%+010.8x", -123).should == " -0000007b" | |
+ sprintf("%+ 10.8x", -123).should == " -0000007b" | |
+ sprintf("% 010.8x", -123).should == " -0000007b" | |
+ end | |
+ | |
+ it "passes some tests for negative %u" do | |
+ # This needs to be 64-bit guarded | |
+ | |
+ sprintf("%u", -123).should == "..18446744073709551493" | |
+ sprintf("%0u", -123).should == "18446744073709551493" | |
+ sprintf("% u", -123).should == "-123" | |
+ sprintf("%+u", -123).should == "-123" | |
+ sprintf("%+0u", -123).should == "-123" | |
+ sprintf("%+ u", -123).should == "-123" | |
+ sprintf("% 0u", -123).should == "-123" | |
+ | |
+ sprintf("%#u", -123).should == "..18446744073709551493" | |
+ sprintf("%#0u", -123).should == "18446744073709551493" | |
+ sprintf("%# u", -123).should == "-123" | |
+ sprintf("%#+u", -123).should == "-123" | |
+ sprintf("%#+0u", -123).should == "-123" | |
+ sprintf("%#+ u", -123).should == "-123" | |
+ sprintf("%# 0u", -123).should == "-123" | |
+ | |
+ sprintf("%8u", -123).should == "..18446744073709551493" | |
+ sprintf("%08u", -123).should == "18446744073709551493" | |
+ sprintf("% 8u", -123).should == " -123" | |
+ sprintf("%+8u", -123).should == " -123" | |
+ sprintf("%+08u", -123).should == "-0000123" | |
+ sprintf("%+ 8u", -123).should == " -123" | |
+ sprintf("% 08u", -123).should == "-0000123" | |
+ | |
+ sprintf("%#8u", -123).should == "..18446744073709551493" | |
+ sprintf("%#08u", -123).should == "18446744073709551493" | |
+ sprintf("%# 8u", -123).should == " -123" | |
+ sprintf("%#+8u", -123).should == " -123" | |
+ sprintf("%#+08u", -123).should == "-0000123" | |
+ sprintf("%#+ 8u", -123).should == " -123" | |
+ sprintf("%# 08u", -123).should == "-0000123" | |
+ | |
+ | |
+ sprintf("%30u", -123).should == " ..18446744073709551493" | |
+ sprintf("%030u", -123).should == "..........18446744073709551493" | |
+ | |
+ sprintf("%#30u", -123).should == " ..18446744073709551493" | |
+ sprintf("%#030u", -123).should == "..........18446744073709551493" | |
+ | |
+ | |
+ sprintf("%24.30u", -123).should == "..........18446744073709551493" | |
+ sprintf("%024.30u", -123).should == "..........18446744073709551493" | |
+ | |
+ sprintf("%#24.30u", -123).should == "..........18446744073709551493" | |
+ sprintf("%#024.30u", -123).should == "..........18446744073709551493" | |
+ | |
+ | |
+ sprintf("%30.24u", -123).should == " ....18446744073709551493" | |
+ sprintf("%030.24u", -123).should == " ....18446744073709551493" | |
+ | |
+ sprintf("%#30.24u", -123).should == " ....18446744073709551493" | |
+ sprintf("%#030.24u", -123).should == " ....18446744073709551493" | |
+ end | |
+ | |
+ it "passes some tests for positive %u" do | |
+ sprintf("%30u", 123).should == " 123" | |
+ sprintf("%030u", 123).should == "000000000000000000000000000123" | |
+ | |
+ sprintf("%#30u", 123).should == " 123" | |
+ sprintf("%#030u", 123).should == "000000000000000000000000000123" | |
+ | |
+ | |
+ sprintf("%24.30u", 123).should == "000000000000000000000000000123" | |
+ sprintf("%024.30u", 123).should == "000000000000000000000000000123" | |
+ | |
+ sprintf("%#24.30u", 123).should == "000000000000000000000000000123" | |
+ sprintf("%#024.30u", 123).should == "000000000000000000000000000123" | |
+ | |
+ | |
+ sprintf("%30.24u", 123).should == " 000000000000000000000123" | |
+ sprintf("%030.24u", 123).should == " 000000000000000000000123" | |
+ | |
+ sprintf("%#30.24u", 123).should == " 000000000000000000000123" | |
+ sprintf("%#030.24u", 123).should == " 000000000000000000000123" | |
+ end | |
+ | |
+ it "passes kstephens's tests" do | |
+ sprintf("%*1$.*2$3$d", 10, 5, 1).should == " 00001" | |
+ sprintf("%.7b", -5).should == "1111011" | |
+ sprintf("%.7B", -5).should == "1111011" | |
+ sprintf("%b", 0).should == "0" | |
+ sprintf("%B", 0).should == "0" | |
+ sprintf("%b", -5).should == "..1011" | |
+ sprintf("%B", -5).should == "..1011" | |
+ sprintf("%0b", -5).should == "1011" | |
+ sprintf("%0x", -125).should == "f83" | |
+ sprintf("%+b", -5).should == "-101" | |
+ sprintf("%+b", 10).should == "+1010" | |
+ sprintf("%+b", 0).should == "+0" | |
+ sprintf("%+o", -5).should == "-5" | |
+ sprintf("%+o", 10).should == "+12" | |
+ sprintf("%+o", 0).should == "+0" | |
+ sprintf("%+d", -5).should == "-5" | |
+ sprintf("%+d", 10).should == "+10" | |
+ sprintf("%+d", 0).should == "+0" | |
+ sprintf("%+x", -15).should == "-f" | |
+ sprintf("%+x", 100).should == "+64" | |
+ sprintf("%+x", 0).should == "+0" | |
+ sprintf("%+X", -15).should == "-F" | |
+ sprintf("%+X", 100).should == "+64" | |
+ sprintf("%+X", 0).should == "+0" | |
+ sprintf("=%02X", 1).should == "=01" | |
+ sprintf("%+03d", 0).should == "+00" | |
+ sprintf("%+03d", 5).should == "+05" | |
+ sprintf("%+03d", -5).should == "-05" | |
+ sprintf("%+03d", 12).should == "+12" | |
+ sprintf("%+03d", -12).should == "-12" | |
+ sprintf("%+03d", 123).should == "+123" | |
+ sprintf("%+03d", -123).should == "-123" | |
+ end | |
end | |
describe "Kernel.sprintf" do | |
diff --git a/spec/ruby/core/string/modulo_spec.rb b/spec/ruby/core/string/modulo_spec.rb | |
index d636ba7..0f37c00 100644 | |
--- a/spec/ruby/core/string/modulo_spec.rb | |
+++ b/spec/ruby/core/string/modulo_spec.rb | |
@@ -30,7 +30,7 @@ describe "String#%" do | |
lambda { ("foo%quux" % []) }.should raise_error(ArgumentError) | |
end | |
- it "raises an error if NULL or \n appear anywhere else in the format string" do | |
+ it "raises an error if NULL or \\n appear anywhere else in the format string" do | |
begin | |
old_debug, $DEBUG = $DEBUG, false | |
@@ -664,19 +664,11 @@ describe "String#%" do | |
end | |
ruby_version_is ""..."1.9" do | |
- not_compliant_on :rubinius do | |
- # This is the proper, compliant behavior of both JRuby, and | |
- # MRI 1.8.6 with patchlevel greater than 114. | |
- ruby_bug "http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/8418", "1.8.6.114" do | |
- it "supports negative bignums by prefixing the value with dots" do | |
- ("%u" % -(2 ** 64 + 5)).should == "..79228162495817593519834398715" | |
- end | |
- end | |
- end | |
- | |
- deviates_on :rubinius do | |
- it "does not support negative bignums" do | |
- lambda { ("%u" % -(2 ** 64 + 5)) }.should raise_error(ArgumentError) | |
+ # This is the proper, compliant behavior of both JRuby, and | |
+ # MRI 1.8.6 with patchlevel greater than 114. | |
+ ruby_bug "http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/8418", "1.8.6.114" do | |
+ it "supports negative bignums by prefixing the value with dots" do | |
+ ("%u" % -(2 ** 64 + 5)).should == "..79228162495817593519834398715" | |
end | |
end | |
end | |
-- | |
1.7.2.3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment