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