Skip to content

Commit 597dc7b

Browse files
committed
Merge pull request #15377 from thepulkitagarwal/printf.jl
Add tests for printf.jl
2 parents 43c597b + aff3a10 commit 597dc7b

File tree

2 files changed

+139
-10
lines changed

2 files changed

+139
-10
lines changed

base/printf.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1142,7 +1142,7 @@ function _printf(macroname, io, fmt, args)
11421142
# Immediately check for corresponding arguments if there is no splatting
11431143
#
11441144
if !has_splatting && length(sym_args) != length(args)
1145-
error("$macroname: wrong number of arguments ($(length(args))) should be ($(length(sym_args)))")
1145+
throw(ArgumentError("$macroname: wrong number of arguments ($(length(args))) should be ($(length(sym_args)))"))
11461146
end
11471147

11481148
for i = length(sym_args):-1:1

test/printf.jl

Lines changed: 138 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,37 @@
66
@test (@sprintf "%a" typemax(Int64)) == "0x7.fffffffffffffffp+60"
77
@test (@sprintf "%A" typemax(Int64)) == "0X7.FFFFFFFFFFFFFFFP+60"
88

9+
#printing an int value
910
for (fmt, val) in (("%i", "42"),
1011
("%u", "42"),
1112
("Test: %i", "Test: 42"),
1213
("%#x", "0x2a"),
1314
("%#o", "052"),
14-
("%X", "2A"),
15+
("%x", "2a"),
1516
("%X", "2A"),
1617
("% i", " 42"),
1718
("%+i", "+42"),
1819
("%4i", " 42"),
1920
("%-4i", "42 "),
20-
("%a","0x2.ap+4"),
21+
("%a", "0x2.ap+4"),
22+
("%A", "0X2.AP+4"),
2123
("%20a"," 0x2.ap+4"),
22-
("%-20a","0x2.ap+4 "))
23-
@test( @eval(@sprintf($fmt, 42) == $val))
24+
("%-20a","0x2.ap+4 "),
25+
("%f", "42.000000"),
26+
("%g", "42")),
27+
num in (UInt16(42), UInt32(42), UInt64(42), UInt128(42),
28+
Int16(42), Int32(42), Int64(42), Int128(42))
29+
#big"42" causes stack overflow on %a ; gh #14409
30+
@test @eval(@sprintf($fmt, $num) == $val)
31+
end
32+
33+
# pointers
34+
if WORD_SIZE == 64
35+
@test (@sprintf "%20p" 0) == " 0x0000000000000000"
36+
@test (@sprintf "%-20p" 0) == "0x0000000000000000 "
37+
elseif WORD_SIZE == 32
38+
@test (@sprintf "%20p" 0) == " 0x00000000"
39+
@test (@sprintf "%-20p" 0) == "0x00000000 "
2440
end
2541

2642
# float / BigFloat
@@ -29,9 +45,66 @@ for (fmt, val) in (("%7.2f", " 1.23"),
2945
("%07.2f", "0001.23"),
3046
("%.0f", "1"),
3147
("%#.0f", "1."),
32-
("%.4e", "1.2345e+00")),
48+
("%.4e", "1.2345e+00"),
49+
("%.4E", "1.2345E+00"),
50+
("%.2a", "0x1.3cp+0"),
51+
("%.2A", "0X1.3CP+0")),
3352
num in (1.2345, big"1.2345")
34-
@test( @eval(@sprintf($fmt, $num) == $val))
53+
@test @eval(@sprintf($fmt, $num) == $val)
54+
end
55+
56+
# numeric spacing and various flag tests
57+
function _test_flags(val, vflag::AbstractString, fmt::AbstractString, res::AbstractString, prefix::AbstractString)
58+
vflag = string("%", vflag)
59+
space_fmt = string(length(res) + length(prefix) + 3, fmt)
60+
fsign = string((val < 0 ? "-" : "+"), prefix)
61+
nsign = string((val < 0 ? "-" : " "), prefix)
62+
osign = val < 0 ? string("-", prefix) : string(prefix, "0")
63+
esign = string(val < 0 ? "-" : "", prefix)
64+
esignend = val < 0 ? "" : " "
65+
66+
for (flag::AbstractString, ans::AbstractString) in (
67+
("", string(" ", nsign, res)),
68+
("+", string(" ", fsign, res)),
69+
(" ", string(" ", nsign, res)),
70+
("0", string(osign, "00", res)),
71+
("-", string(esign, res, " ", esignend)),
72+
("0+", string(fsign, "00", res)),
73+
("0 ", string(nsign, "00", res)),
74+
("-+", string(fsign, res, " ")),
75+
("- ", string(nsign, res, " ")),
76+
)
77+
fmt_string = string(vflag, flag, space_fmt)
78+
@test @eval(@sprintf($fmt_string, $val) == $ans)
79+
end
80+
end
81+
82+
for i in (
83+
(42, "", "i", "42", ""),
84+
(42, "", "d", "42", ""),
85+
86+
(42, "", "u", "42", ""),
87+
(42, "", "x", "2a", ""),
88+
(42, "", "X", "2A", ""),
89+
(42, "", "o", "52", ""),
90+
91+
(42, "#", "x", "2a", "0x"),
92+
(42, "#", "X", "2A", "0X"),
93+
(42, "#", "o", "052", ""),
94+
95+
(1.2345, "", ".2f", "1.23", ""),
96+
(1.2345, "", ".2e", "1.23e+00", ""),
97+
(1.2345, "", ".2E", "1.23E+00", ""),
98+
99+
(1.2345, "#", ".0f", "1.", ""),
100+
(1.2345, "#", ".0e", "1.e+00", ""),
101+
(1.2345, "#", ".0E", "1.E+00", ""),
102+
103+
(1.2345, "", ".2a", "1.3cp+0", "0x"),
104+
(1.2345, "", ".2A", "1.3CP+0", "0X"),
105+
)
106+
_test_flags(i...)
107+
_test_flags(-i[1], i[2:5]...)
35108
end
36109

37110
# Inf / NaN handling
@@ -46,19 +119,30 @@ end
46119
@test (@sprintf "%.0e" big"3e142") == "3e+142"
47120
@test (@sprintf "%#.0e" big"3e142") == "3.e+142"
48121

122+
@test (@sprintf "%.0e" big"3e1042") == "3e+1042"
123+
124+
@test (@sprintf "%e" 3e42) == "3.000000e+42"
125+
@test (@sprintf "%E" 3e42) == "3.000000E+42"
126+
@test (@sprintf "%e" 3e-42) == "3.000000e-42"
127+
@test (@sprintf "%E" 3e-42) == "3.000000E-42"
128+
@test (@sprintf "%a" 3e4) == "0x1.d4cp+14"
129+
@test (@sprintf "%A" 3e4) == "0X1.D4CP+14"
130+
@test (@sprintf "%.4a" 3e-4) == "0x1.3a93p-12"
131+
@test (@sprintf "%.4A" 3e-4) == "0X1.3A93P-12"
132+
49133
# %g
50134
for (val, res) in ((12345678., "1.23457e+07"),
51135
(1234567.8, "1.23457e+06"),
52136
(123456.78, "123457"),
53137
(12345.678, "12345.7"),
54138
(12340000.0, "1.234e+07"))
55-
@test( @sprintf("%.6g", val) == res)
139+
@test (@sprintf("%.6g", val) == res)
56140
end
57141
for (val, res) in ((big"12345678.", "1.23457e+07"),
58142
(big"1234567.8", "1.23457e+06"),
59143
(big"123456.78", "123457"),
60144
(big"12345.678", "12345.7"))
61-
@test( @sprintf("%.6g", val) == res)
145+
@test (@sprintf("%.6g", val) == res)
62146
end
63147
for (fmt, val) in (("%10.5g", " 123.4"),
64148
("%+10.5g", " +123.4"),
@@ -68,7 +152,7 @@ for (fmt, val) in (("%10.5g", " 123.4"),
68152
("%-+10.5g", "+123.4 "),
69153
("%010.5g", "00000123.4")),
70154
num in (123.4, big"123.4")
71-
@test( @eval(@sprintf($fmt, $num) == $val))
155+
@test @eval(@sprintf($fmt, $num) == $val)
72156
end
73157
@test( @sprintf( "%10.5g", -123.4 ) == " -123.4")
74158
@test( @sprintf( "%010.5g", -123.4 ) == "-0000123.4")
@@ -94,19 +178,61 @@ end
94178
# chars
95179
@test (@sprintf "%c" 65) == "A"
96180
@test (@sprintf "%c" 'A') == "A"
181+
@test (@sprintf "%3c" 'A') == " A"
182+
@test (@sprintf "%-3c" 'A') == "A "
97183
@test (@sprintf "%c" 248) == "ø"
98184
@test (@sprintf "%c" 'ø') == "ø"
99185

186+
# escape %
187+
@test (@sprintf "%%") == "%"
188+
@test (@sprintf "%%s") == "%s"
189+
@test_throws ArgumentError eval(:(@sprintf "%"))
190+
191+
# argument count
192+
@test_throws ArgumentError eval(:(@sprintf "%s"))
193+
@test_throws ArgumentError eval(:(@sprintf "%s" "1" "2"))
194+
195+
# no interpolation
196+
@test_throws ArgumentError eval(:(@sprintf "$n"))
197+
198+
# type width specifier parsing (ignored)
199+
@test (@sprintf "%llf" 1.2) == "1.200000"
200+
@test (@sprintf "%Lf" 1.2) == "1.200000"
201+
@test (@sprintf "%hhu" 1) == "1"
202+
@test (@sprintf "%hu" 1) == "1"
203+
@test (@sprintf "%lu" 1) == "1"
204+
@test (@sprintf "%llu" 1) == "1"
205+
@test (@sprintf "%Lu" 1) == "1"
206+
@test (@sprintf "%zu" 1) == "1"
207+
@test (@sprintf "%ju" 1) == "1"
208+
@test (@sprintf "%tu" 1) == "1"
209+
100210
# strings
101211
@test (@sprintf "%s" "test") == "test"
102212
@test (@sprintf "%s" "tést") == "tést"
103213

214+
@test (@sprintf "%8s" "test") == " test"
215+
@test (@sprintf "%-8s" "test") == "test "
216+
217+
@test (@sprintf "%s" "tést") == "tést"
218+
219+
@test (@sprintf "%s" :test) == "test"
220+
@test (@sprintf "%#s" :test) == ":test"
221+
@test (@sprintf "%#8s" :test) == " :test"
222+
@test (@sprintf "%#-8s" :test) == ":test "
223+
104224
# reasonably complex
105225
@test (@sprintf "Test: %s%c%C%c%#-.0f." "t" 65 66 67 -42) == "Test: tABC-42.."
106226

107227
#test simple splatting
108228
@test (@sprintf "%d%d" [1 2]...) == "12"
109229

230+
# invalid format specifiers, not "diouxXDOUeEfFgGaAcCsSpn"
231+
for c in "bBhHIjJkKlLmMNPqQrRtTvVwWyYzZ"
232+
fmt_str = string("%", c)
233+
@test_throws ArgumentError eval(:(@sprintf $fmt_str 1))
234+
end
235+
110236
# combo
111237
@test (@sprintf "%f %d %d %f" 1.0 [3 4]... 5) == "1.000000 3 4 5.000000"
112238

@@ -115,3 +241,6 @@ end
115241

116242
# comprehension
117243
@test (@sprintf "%s %s %s %d %d %d %f %f %f" Any[10^x+y for x=1:3,y=1:3 ]...) == "11 101 1001 12 102 1002 13.000000 103.000000 1003.000000"
244+
245+
# @printf
246+
@test_throws ArgumentError eval(:(@printf 1))

0 commit comments

Comments
 (0)