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