Commit | Line | Data |
8d063cd8 |
1 | #!./perl |
2 | |
be3174d2 |
3 | # Tests sprintf, excluding handling of 64-bit integers or long |
4 | # doubles (if supported), of machine-specific short and long |
5 | # integers, machine-specific floating point exceptions (infinity, |
6 | # not-a-number ...), of the effects of locale, and of features |
7 | # specific to multi-byte characters (under use utf8 and such). |
8d063cd8 |
8 | |
9f1b1f2d |
9 | BEGIN { |
10 | chdir 't' if -d 't'; |
20822f61 |
11 | @INC = '../lib'; |
9f1b1f2d |
12 | } |
13 | use warnings; |
14 | |
be3174d2 |
15 | while (<DATA>) { |
16 | s/^\s*>//; s/<\s*$//; |
17 | push @tests, [split(/<\s*>/, $_, 4)]; |
18 | } |
19 | |
20 | print '1..', scalar @tests, "\n"; |
8d063cd8 |
21 | |
fb73857a |
22 | $SIG{__WARN__} = sub { |
23 | if ($_[0] =~ /^Invalid conversion/) { |
211dfcf1 |
24 | $w = ' INVALID'; |
25 | } elsif ($_[0] =~ /^Use of uninitialized value/) { |
26 | $w = ' UNINIT'; |
fb73857a |
27 | } else { |
65c97e0f |
28 | warn @_; |
fb73857a |
29 | } |
30 | }; |
31 | |
be3174d2 |
32 | for ($i = 1; @tests; $i++) { |
33 | ($template, $data, $result, $comment) = @{shift @tests}; |
12ebcc11 |
34 | if ($^O eq 'os390') { |
35 | $data =~ s/([eE])96$/${1}63/; # smaller exponents |
36 | $result =~ s/([eE]\+)102$/${1}69/; # " " |
37 | $data =~ s/([eE])\-101$/${1}-56/; # larger exponents |
38 | $result =~ s/([eE])\-102$/${1}-57/; # " " |
39 | } |
be3174d2 |
40 | $evalData = eval $data; |
41 | $w = undef; |
42 | $x = sprintf(">$template<", |
43 | defined @$evalData ? @$evalData : $evalData); |
44 | substr($x, -1, 0) = $w if $w; |
48237dde |
45 | # $x may have 3 exponent digits, not 2 |
46 | my $y = $x; |
47 | if ($y =~ s/([Ee][-+])0(\d)/$1$2/) { |
48 | # if result is left-adjusted, append extra space |
49 | if ($template =~ /%\+?\-/ and $result =~ / $/) { |
50 | $y =~ s/<$/ </; |
51 | } |
52 | # if result is zero-filled, add extra zero |
53 | elsif ($template =~ /%\+?0/ and $result =~ /^0/) { |
54 | $y =~ s/^>0/>00/; |
55 | } |
56 | # if result is right-adjusted, prepend extra space |
57 | elsif ($result =~ /^ /) { |
58 | $y =~ s/^>/> /; |
59 | } |
65c97e0f |
60 | } |
61 | |
be3174d2 |
62 | if ($x eq ">$result<") { |
63 | print "ok $i\n"; |
64 | } |
48237dde |
65 | elsif ($y eq ">$result<") # Some C libraries always give |
65c97e0f |
66 | { # three-digit exponent |
00b6c67e |
67 | print("ok $i # >$result< $x three-digit exponent accepted\n"); |
65c97e0f |
68 | } |
f7137e37 |
69 | elsif ($result =~ /[-+]\d{3}$/ && |
70 | # Suppress tests with modulo of exponent >= 100 on platforms |
71 | # which can't handle such magnitudes (or where we can't tell). |
72 | ((!eval {require POSIX}) || # Costly: only do this if we must! |
73 | (length(&POSIX::DBL_MAX) - rindex(&POSIX::DBL_MAX, '+')) == 3)) |
74 | { |
00b6c67e |
75 | print("ok $i # >$template< >$data< >$result<", |
76 | " Suppressed: exponent out of range?\n") |
f7137e37 |
77 | } |
be3174d2 |
78 | else { |
48237dde |
79 | $y = ($x eq $y ? "" : " => $y"); |
80 | print("not ok $i >$template< >$data< >$result< $x$y", |
65c97e0f |
81 | $comment ? " # $comment\n" : "\n"); |
fb73857a |
82 | } |
83 | } |
48237dde |
84 | |
be3174d2 |
85 | # In each of the the following lines, there are three required fields: |
86 | # printf template, data to be formatted (as a Perl expression), and |
87 | # expected result of formatting. An optional fourth field can contain |
88 | # a comment. Each field is delimited by a starting '>' and a |
89 | # finishing '<'; any whitespace outside these start and end marks is |
90 | # not part of the field. If formatting requires more than one data |
91 | # item (for example, if variable field widths are used), the Perl data |
92 | # expression should return a reference to an array having the requisite |
93 | # number of elements. Even so, subterfuge is sometimes required: see |
94 | # tests for %n and %p. |
95 | # |
02a4ca6d |
96 | # The following tests are not currently run, for the reasons stated: |
97 | |
98 | =pod |
99 | |
100 | =begin problematic |
101 | |
102 | >%.0f< >-0.1< >-0< >C library bug: no minus on VMS, HP-UX< |
103 | >%.0f< >1.5< >2< >Standard vague: no rounding rules< |
104 | >%.0f< >2.5< >2< >Standard vague: no rounding rules< |
12ebcc11 |
105 | >%G< >1234567e96< >1.23457E+102< >exponent too big for OS/390< |
106 | >%G< >.1234567e-101< >1.23457E-102< >exponent too small for OS/390< |
107 | >%e< >1234567E96< >1.234567e+102< >exponent too big for OS/390< |
108 | >%e< >.1234567E-101< >1.234567e-102< >exponent too small for OS/390< |
109 | >%g< >.1234567E-101< >1.23457e-102< >exponent too small for OS/390< |
110 | >%g< >1234567E96< >1.23457e+102< >exponent too big for OS/390< |
02a4ca6d |
111 | |
112 | =end problematic |
113 | |
114 | =cut |
115 | |
be3174d2 |
116 | # template data result |
117 | __END__ |
be3174d2 |
118 | >%6. 6s< >''< >%6. 6s INVALID< >(See use of $w in code above)< |
c2e66d9e |
119 | >%6 .6s< >''< >%6 .6s INVALID< |
be3174d2 |
120 | >%6.6 s< >''< >%6.6 s INVALID< |
c2e66d9e |
121 | >%A< >''< >%A INVALID< |
be3174d2 |
122 | >%B< >''< >%B INVALID< |
123 | >%C< >''< >%C INVALID< |
124 | >%D< >0x7fffffff< >2147483647< >Synonym for %ld< |
125 | >%E< >123456.789< >1.234568E+05< >Like %e, but using upper-case "E"< |
126 | >%F< >123456.789< >123456.789000< >Synonym for %f< |
127 | >%G< >1234567.89< >1.23457E+06< >Like %g, but using upper-case "E"< |
c2e66d9e |
128 | >%G< >1234567e96< >1.23457E+102< |
129 | >%G< >.1234567e-101< >1.23457E-102< |
be3174d2 |
130 | >%G< >12345.6789< >12345.7< |
131 | >%H< >''< >%H INVALID< |
132 | >%I< >''< >%I INVALID< |
133 | >%J< >''< >%J INVALID< |
134 | >%K< >''< >%K INVALID< |
135 | >%L< >''< >%L INVALID< |
136 | >%M< >''< >%M INVALID< |
137 | >%N< >''< >%N INVALID< |
c2e66d9e |
138 | >%O< >2**32-1< >37777777777< >Synonum for %lo< |
be3174d2 |
139 | >%P< >''< >%P INVALID< |
140 | >%Q< >''< >%Q INVALID< |
141 | >%R< >''< >%R INVALID< |
142 | >%S< >''< >%S INVALID< |
143 | >%T< >''< >%T INVALID< |
c2e66d9e |
144 | >%U< >2**32-1< >4294967295< >Synonum for %lu< |
be3174d2 |
145 | >%V< >''< >%V INVALID< |
146 | >%W< >''< >%W INVALID< |
147 | >%X< >2**32-1< >FFFFFFFF< >Like %x, but with u/c letters< |
148 | >%#X< >2**32-1< >0XFFFFFFFF< |
149 | >%Y< >''< >%Y INVALID< |
150 | >%Z< >''< >%Z INVALID< |
151 | >%a< >''< >%a INVALID< |
152 | >%b< >2**32-1< >11111111111111111111111111111111< |
153 | >%+b< >2**32-1< >11111111111111111111111111111111< |
154 | >%#b< >2**32-1< >0b11111111111111111111111111111111< |
155 | >%34b< >2**32-1< > 11111111111111111111111111111111< |
156 | >%034b< >2**32-1< >0011111111111111111111111111111111< |
157 | >%-34b< >2**32-1< >11111111111111111111111111111111 < |
158 | >%-034b< >2**32-1< >11111111111111111111111111111111 < |
159 | >%c< >ord('A')< >A< |
160 | >%10c< >ord('A')< > A< |
161 | >%#10c< >ord('A')< > A< ># modifier: no effect< |
162 | >%010c< >ord('A')< >000000000A< |
163 | >%10lc< >ord('A')< > A< >l modifier: no effect< |
164 | >%10hc< >ord('A')< > A< >h modifier: no effect< |
165 | >%10.5c< >ord('A')< > A< >precision: no effect< |
166 | >%-10c< >ord('A')< >A < |
167 | >%d< >123456.789< >123456< |
168 | >%d< >-123456.789< >-123456< |
169 | >%d< >0< >0< |
170 | >%+d< >0< >+0< |
171 | >%0d< >0< >0< |
172 | >%.0d< >0< >< |
173 | >%+.0d< >0< >+< |
174 | >%.0d< >1< >1< |
175 | >%d< >1< >1< |
176 | >%+d< >1< >+1< |
177 | >%#3.2d< >1< > 01< ># modifier: no effect< |
178 | >%3.2d< >1< > 01< |
179 | >%03.2d< >1< >001< |
180 | >%-3.2d< >1< >01 < |
181 | >%-03.2d< >1< >01 < >zero pad + left just.: no effect< |
182 | >%d< >-1< >-1< |
183 | >%+d< >-1< >-1< |
184 | >%hd< >1< >1< >More extensive testing of< |
185 | >%ld< >1< >1< >length modifiers would be< |
186 | >%Vd< >1< >1< >platform-specific< |
187 | >%vd< >chr(1)< >1< |
188 | >%+vd< >chr(1)< >+1< |
189 | >%#vd< >chr(1)< >1< |
190 | >%vd< >"\01\02\03"< >1.2.3< |
191 | >%v.3d< >"\01\02\03"< >001.002.003< |
211dfcf1 |
192 | >%0v3d< >"\01\02\03"< >001.002.003< |
193 | >%-v3d< >"\01\02\03"< >1 .2 .3 < |
194 | >%+-v3d< >"\01\02\03"< >+1 .2 .3 < |
be3174d2 |
195 | >%v4.3d< >"\01\02\03"< > 001. 002. 003< |
211dfcf1 |
196 | >%0v4.3d< >"\01\02\03"< >0001.0002.0003< |
197 | >%0*v2d< >['-', "\0\7\14"]< >00-07-12< |
198 | >%v.*d< >["\01\02\03", 3]< >001.002.003< |
199 | >%0v*d< >["\01\02\03", 3]< >001.002.003< |
200 | >%-v*d< >["\01\02\03", 3]< >1 .2 .3 < |
201 | >%+-v*d< >["\01\02\03", 3]< >+1 .2 .3 < |
202 | >%v*.*d< >["\01\02\03", 4, 3]< > 001. 002. 003< |
203 | >%0v*.*d< >["\01\02\03", 4, 3]< >0001.0002.0003< |
204 | >%0*v*d< >['-', "\0\7\13", 2]< >00-07-11< |
be3174d2 |
205 | >%e< >1234.875< >1.234875e+03< |
c2e66d9e |
206 | >%e< >0.000012345< >1.234500e-05< |
207 | >%e< >1234567E96< >1.234567e+102< |
208 | >%e< >0< >0.000000e+00< |
209 | >%e< >.1234567E-101< >1.234567e-102< |
be3174d2 |
210 | >%+e< >1234.875< >+1.234875e+03< |
211 | >%#e< >1234.875< >1.234875e+03< |
212 | >%e< >-1234.875< >-1.234875e+03< |
213 | >%+e< >-1234.875< >-1.234875e+03< |
214 | >%#e< >-1234.875< >-1.234875e+03< |
215 | >%.0e< >1234.875< >1e+03< |
02a4ca6d |
216 | >%#.0e< >1234.875< >1.e+03< |
be3174d2 |
217 | >%.*e< >[0, 1234.875]< >1e+03< |
218 | >%.1e< >1234.875< >1.2e+03< |
219 | >%-12.4e< >1234.875< >1.2349e+03 < |
220 | >%12.4e< >1234.875< > 1.2349e+03< |
221 | >%+-12.4e< >1234.875< >+1.2349e+03 < |
222 | >%+12.4e< >1234.875< > +1.2349e+03< |
223 | >%+-12.4e< >-1234.875< >-1.2349e+03 < |
224 | >%+12.4e< >-1234.875< > -1.2349e+03< |
225 | >%f< >1234.875< >1234.875000< |
226 | >%+f< >1234.875< >+1234.875000< |
227 | >%#f< >1234.875< >1234.875000< |
228 | >%f< >-1234.875< >-1234.875000< |
229 | >%+f< >-1234.875< >-1234.875000< |
230 | >%#f< >-1234.875< >-1234.875000< |
231 | >%6f< >1234.875< >1234.875000< |
232 | >%*f< >[6, 1234.875]< >1234.875000< |
233 | >%.0f< >1234.875< >1235< |
234 | >%.1f< >1234.875< >1234.9< |
235 | >%-8.1f< >1234.875< >1234.9 < |
236 | >%8.1f< >1234.875< > 1234.9< |
237 | >%+-8.1f< >1234.875< >+1234.9 < |
238 | >%+8.1f< >1234.875< > +1234.9< |
239 | >%+-8.1f< >-1234.875< >-1234.9 < |
240 | >%+8.1f< >-1234.875< > -1234.9< |
241 | >%*.*f< >[5, 2, 12.3456]< >12.35< |
c2e66d9e |
242 | >%f< >0< >0.000000< |
243 | >%.0f< >0< >0< |
244 | >%.0f< >2**38< >274877906944< >Should have exact int'l rep'n< |
d5365ef1 |
245 | >%.0f< >0.1< >0< |
917b2492 |
246 | >%.0f< >0.6< >1< >Known to fail with sfio and (irix|nonstop-ux|powerux)< |
247 | >%.0f< >-0.6< >-1< >Known to fail with sfio and (irix|nonstop-ux|powerux)< |
02a4ca6d |
248 | >%.0f< >1< >1< |
249 | >%#.0f< >1< >1.< |
be3174d2 |
250 | >%g< >12345.6789< >12345.7< |
251 | >%+g< >12345.6789< >+12345.7< |
252 | >%#g< >12345.6789< >12345.7< |
253 | >%.0g< >12345.6789< >1e+04< |
02a4ca6d |
254 | >%#.0g< >12345.6789< >1.e+04< |
be3174d2 |
255 | >%.2g< >12345.6789< >1.2e+04< |
256 | >%.*g< >[2, 12345.6789]< >1.2e+04< |
257 | >%.9g< >12345.6789< >12345.6789< |
258 | >%12.9g< >12345.6789< > 12345.6789< |
259 | >%012.9g< >12345.6789< >0012345.6789< |
260 | >%-12.9g< >12345.6789< >12345.6789 < |
261 | >%*.*g< >[-12, 9, 12345.6789]< >12345.6789 < |
262 | >%-012.9g< >12345.6789< >12345.6789 < |
263 | >%g< >-12345.6789< >-12345.7< |
264 | >%+g< >-12345.6789< >-12345.7< |
265 | >%g< >1234567.89< >1.23457e+06< |
266 | >%+g< >1234567.89< >+1.23457e+06< |
267 | >%#g< >1234567.89< >1.23457e+06< |
268 | >%g< >-1234567.89< >-1.23457e+06< |
269 | >%+g< >-1234567.89< >-1.23457e+06< |
270 | >%#g< >-1234567.89< >-1.23457e+06< |
c2e66d9e |
271 | >%g< >0.00012345< >0.00012345< |
272 | >%g< >0.000012345< >1.2345e-05< |
273 | >%g< >1234567E96< >1.23457e+102< |
274 | >%g< >.1234567E-101< >1.23457e-102< |
275 | >%g< >0< >0< |
be3174d2 |
276 | >%13g< >1234567.89< > 1.23457e+06< |
277 | >%+13g< >1234567.89< > +1.23457e+06< |
278 | >%013g< >1234567.89< >001.23457e+06< |
279 | >%-13g< >1234567.89< >1.23457e+06 < |
280 | >%h< >''< >%h INVALID< |
281 | >%i< >123456.789< >123456< >Synonym for %d< |
282 | >%j< >''< >%j INVALID< |
283 | >%k< >''< >%k INVALID< |
284 | >%l< >''< >%l INVALID< |
285 | >%m< >''< >%m INVALID< |
286 | >%s< >sprintf('%%n%n %d', $n, $n)< >%n 2< >Slight sneakiness to test %n< |
287 | >%o< >2**32-1< >37777777777< |
288 | >%+o< >2**32-1< >37777777777< |
289 | >%#o< >2**32-1< >037777777777< |
290 | >%d< >$p=sprintf('%p',$p);$p=~/^[0-9a-f]+$/< >1< >Coarse hack: hex from %p?< |
d5365ef1 |
291 | >%#p< >''< >%#p INVALID< |
be3174d2 |
292 | >%q< >''< >%q INVALID< |
293 | >%r< >''< >%r INVALID< |
294 | >%s< >'string'< >string< |
295 | >%10s< >'string'< > string< |
296 | >%+10s< >'string'< > string< |
297 | >%#10s< >'string'< > string< |
298 | >%010s< >'string'< >0000string< |
299 | >%0*s< >[10, 'string']< >0000string< |
300 | >%-10s< >'string'< >string < |
301 | >%3s< >'string'< >string< |
302 | >%.3s< >'string'< >str< |
303 | >%.*s< >[3, 'string']< >str< |
304 | >%t< >''< >%t INVALID< |
305 | >%u< >2**32-1< >4294967295< |
306 | >%+u< >2**32-1< >4294967295< |
307 | >%#u< >2**32-1< >4294967295< |
308 | >%12u< >2**32-1< > 4294967295< |
309 | >%012u< >2**32-1< >004294967295< |
310 | >%-12u< >2**32-1< >4294967295 < |
311 | >%-012u< >2**32-1< >4294967295 < |
312 | >%v< >''< >%v INVALID< |
313 | >%w< >''< >%w INVALID< |
314 | >%x< >2**32-1< >ffffffff< |
315 | >%+x< >2**32-1< >ffffffff< |
316 | >%#x< >2**32-1< >0xffffffff< |
317 | >%10x< >2**32-1< > ffffffff< |
318 | >%010x< >2**32-1< >00ffffffff< |
319 | >%-10x< >2**32-1< >ffffffff < |
320 | >%-010x< >2**32-1< >ffffffff < |
321 | >%0-10x< >2**32-1< >ffffffff < |
322 | >%0*x< >[-10, ,2**32-1]< >ffffffff < |
323 | >%y< >''< >%y INVALID< |
324 | >%z< >''< >%z INVALID< |
eb3fce90 |
325 | >%2$d %1$d< >[12, 34]< >34 12< |
326 | >%*2$d< >[12, 3]< > 12< |
327 | >%2$d %d< >[12, 34]< >34 12< |
328 | >%2$d %d %d< >[12, 34]< >34 12 34< |
329 | >%3$d %d %d< >[12, 34, 56]< >56 12 34< |
330 | >%2$*3$d %d< >[12, 34, 3]< > 34 12< |
211dfcf1 |
331 | >%*3$2$d %d< >[12, 34, 3]< >%*3$2$d 34 INVALID< |
332 | >%2$d< >12< >0 UNINIT< |
eb3fce90 |
333 | >%0$d< >12< >%0$d INVALID< |
334 | >%1$$d< >12< >%1$$d INVALID< |
335 | >%1$1$d< >12< >%1$1$d INVALID< |
336 | >%*2$*2$d< >[12, 3]< >%*2$*2$d INVALID< |
337 | >%*2*2$d< >[12, 3]< >%*2*2$d INVALID< |
211dfcf1 |
338 | >%0v2.2d< >''< >< |