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).
11 unshift @INC, '../lib';
17 push @tests, [split(/<\s*>/, $_, 4)];
20 print '1..', scalar @tests, "\n";
22 $SIG{__WARN__} = sub {
23 if ($_[0] =~ /^Invalid conversion/) {
30 for ($i = 1; @tests; $i++) {
31 ($template, $data, $result, $comment) = @{shift @tests};
32 $evalData = eval $data;
34 $x = sprintf(">$template<",
35 defined @$evalData ? @$evalData : $evalData);
36 substr($x, -1, 0) = $w if $w;
37 # $x may have 3 exponent digits, not 2
39 if ($y =~ s/([Ee][-+])0(\d)/$1$2/) {
40 # if result is left-adjusted, append extra space
41 if ($template =~ /%\+?\-/ and $result =~ / $/) {
44 # if result is zero-filled, add extra zero
45 elsif ($template =~ /%\+?0/ and $result =~ /^0/) {
48 # if result is right-adjusted, prepend extra space
49 elsif ($result =~ /^ /) {
54 if ($x eq ">$result<") {
57 elsif ($y eq ">$result<") # Some C libraries always give
58 { # three-digit exponent
59 print("ok $i >$result< $x # three-digit exponent accepted\n");
62 $y = ($x eq $y ? "" : " => $y");
63 print("not ok $i >$template< >$data< >$result< $x$y",
64 $comment ? " # $comment\n" : "\n");
68 # In each of the the following lines, there are three required fields:
69 # printf template, data to be formatted (as a Perl expression), and
70 # expected result of formatting. An optional fourth field can contain
71 # a comment. Each field is delimited by a starting '>' and a
72 # finishing '<'; any whitespace outside these start and end marks is
73 # not part of the field. If formatting requires more than one data
74 # item (for example, if variable field widths are used), the Perl data
75 # expression should return a reference to an array having the requisite
76 # number of elements. Even so, subterfuge is sometimes required: see
77 # tests for %n and %p.
79 # The following tests are not currently run, for the reasons stated:
85 >%.0f< >-0.1< >-0< >C library bug: no minus on VMS, HP-UX<
86 >%.0f< >1.5< >2< >Standard vague: no rounding rules<
87 >%.0f< >2.5< >2< >Standard vague: no rounding rules<
93 # template data result
95 >%6. 6s< >''< >%6. 6s INVALID< >(See use of $w in code above)<
96 >%6 .6s< >''< >%6 .6s INVALID<
97 >%6.6 s< >''< >%6.6 s INVALID<
98 >%A< >''< >%A INVALID<
99 >%B< >''< >%B INVALID<
100 >%C< >''< >%C INVALID<
101 >%D< >0x7fffffff< >2147483647< >Synonym for %ld<
102 >%E< >123456.789< >1.234568E+05< >Like %e, but using upper-case "E"<
103 >%F< >123456.789< >123456.789000< >Synonym for %f<
104 >%G< >1234567.89< >1.23457E+06< >Like %g, but using upper-case "E"<
105 >%G< >1234567e96< >1.23457E+102<
106 >%G< >.1234567e-101< >1.23457E-102<
107 >%G< >12345.6789< >12345.7<
108 >%H< >''< >%H INVALID<
109 >%I< >''< >%I INVALID<
110 >%J< >''< >%J INVALID<
111 >%K< >''< >%K INVALID<
112 >%L< >''< >%L INVALID<
113 >%M< >''< >%M INVALID<
114 >%N< >''< >%N INVALID<
115 >%O< >2**32-1< >37777777777< >Synonum for %lo<
116 >%P< >''< >%P INVALID<
117 >%Q< >''< >%Q INVALID<
118 >%R< >''< >%R INVALID<
119 >%S< >''< >%S INVALID<
120 >%T< >''< >%T INVALID<
121 >%U< >2**32-1< >4294967295< >Synonum for %lu<
122 >%V< >''< >%V INVALID<
123 >%W< >''< >%W INVALID<
124 >%X< >2**32-1< >FFFFFFFF< >Like %x, but with u/c letters<
125 >%#X< >2**32-1< >0XFFFFFFFF<
126 >%Y< >''< >%Y INVALID<
127 >%Z< >''< >%Z INVALID<
128 >%a< >''< >%a INVALID<
129 >%b< >2**32-1< >11111111111111111111111111111111<
130 >%+b< >2**32-1< >11111111111111111111111111111111<
131 >%#b< >2**32-1< >0b11111111111111111111111111111111<
132 >%34b< >2**32-1< > 11111111111111111111111111111111<
133 >%034b< >2**32-1< >0011111111111111111111111111111111<
134 >%-34b< >2**32-1< >11111111111111111111111111111111 <
135 >%-034b< >2**32-1< >11111111111111111111111111111111 <
137 >%10c< >ord('A')< > A<
138 >%#10c< >ord('A')< > A< ># modifier: no effect<
139 >%010c< >ord('A')< >000000000A<
140 >%10lc< >ord('A')< > A< >l modifier: no effect<
141 >%10hc< >ord('A')< > A< >h modifier: no effect<
142 >%10.5c< >ord('A')< > A< >precision: no effect<
143 >%-10c< >ord('A')< >A <
144 >%d< >123456.789< >123456<
145 >%d< >-123456.789< >-123456<
154 >%#3.2d< >1< > 01< ># modifier: no effect<
158 >%-03.2d< >1< >01 < >zero pad + left just.: no effect<
161 >%hd< >1< >1< >More extensive testing of<
162 >%ld< >1< >1< >length modifiers would be<
163 >%Vd< >1< >1< >platform-specific<
167 >%vd< >"\01\02\03"< >1.2.3<
168 >%v.3d< >"\01\02\03"< >001.002.003<
169 >%v03d< >"\01\02\03"< >001.002.003<
170 >%v-3d< >"\01\02\03"< >1 .2 .3 <
171 >%v+-3d< >"\01\02\03"< >+1 .2 .3 <
172 >%v4.3d< >"\01\02\03"< > 001. 002. 003<
173 >%v04.3d< >"\01\02\03"< >0001.0002.0003<
174 >%*v02d< >['-', "\0\7\14"]< >00-07-12<
175 >%v.*d< >[3, "\01\02\03"]< >001.002.003<
176 >%v0*d< >[3, "\01\02\03"]< >001.002.003<
177 >%v-*d< >[3, "\01\02\03"]< >1 .2 .3 <
178 >%v+-*d< >[3, "\01\02\03"]< >+1 .2 .3 <
179 >%v*.*d< >[4, 3, "\01\02\03"]< > 001. 002. 003<
180 >%v0*.*d< >[4, 3, "\01\02\03"]< >0001.0002.0003<
181 >%*v0*d< >['-', 2, "\0\7\13"]< >00-07-11<
182 >%e< >1234.875< >1.234875e+03<
183 >%e< >0.000012345< >1.234500e-05<
184 >%e< >1234567E96< >1.234567e+102<
185 >%e< >0< >0.000000e+00<
186 >%e< >.1234567E-101< >1.234567e-102<
187 >%+e< >1234.875< >+1.234875e+03<
188 >%#e< >1234.875< >1.234875e+03<
189 >%e< >-1234.875< >-1.234875e+03<
190 >%+e< >-1234.875< >-1.234875e+03<
191 >%#e< >-1234.875< >-1.234875e+03<
192 >%.0e< >1234.875< >1e+03<
193 >%#.0e< >1234.875< >1.e+03<
194 >%.*e< >[0, 1234.875]< >1e+03<
195 >%.1e< >1234.875< >1.2e+03<
196 >%-12.4e< >1234.875< >1.2349e+03 <
197 >%12.4e< >1234.875< > 1.2349e+03<
198 >%+-12.4e< >1234.875< >+1.2349e+03 <
199 >%+12.4e< >1234.875< > +1.2349e+03<
200 >%+-12.4e< >-1234.875< >-1.2349e+03 <
201 >%+12.4e< >-1234.875< > -1.2349e+03<
202 >%f< >1234.875< >1234.875000<
203 >%+f< >1234.875< >+1234.875000<
204 >%#f< >1234.875< >1234.875000<
205 >%f< >-1234.875< >-1234.875000<
206 >%+f< >-1234.875< >-1234.875000<
207 >%#f< >-1234.875< >-1234.875000<
208 >%6f< >1234.875< >1234.875000<
209 >%*f< >[6, 1234.875]< >1234.875000<
210 >%.0f< >1234.875< >1235<
211 >%.1f< >1234.875< >1234.9<
212 >%-8.1f< >1234.875< >1234.9 <
213 >%8.1f< >1234.875< > 1234.9<
214 >%+-8.1f< >1234.875< >+1234.9 <
215 >%+8.1f< >1234.875< > +1234.9<
216 >%+-8.1f< >-1234.875< >-1234.9 <
217 >%+8.1f< >-1234.875< > -1234.9<
218 >%*.*f< >[5, 2, 12.3456]< >12.35<
221 >%.0f< >2**38< >274877906944< >Should have exact int'l rep'n<
223 >%.0f< >0.6< >1< >Known to fail with sfio<
224 >%.0f< >-0.6< >-1< >Known to fail with sfio<
227 >%g< >12345.6789< >12345.7<
228 >%+g< >12345.6789< >+12345.7<
229 >%#g< >12345.6789< >12345.7<
230 >%.0g< >12345.6789< >1e+04<
231 >%#.0g< >12345.6789< >1.e+04<
232 >%.2g< >12345.6789< >1.2e+04<
233 >%.*g< >[2, 12345.6789]< >1.2e+04<
234 >%.9g< >12345.6789< >12345.6789<
235 >%12.9g< >12345.6789< > 12345.6789<
236 >%012.9g< >12345.6789< >0012345.6789<
237 >%-12.9g< >12345.6789< >12345.6789 <
238 >%*.*g< >[-12, 9, 12345.6789]< >12345.6789 <
239 >%-012.9g< >12345.6789< >12345.6789 <
240 >%g< >-12345.6789< >-12345.7<
241 >%+g< >-12345.6789< >-12345.7<
242 >%g< >1234567.89< >1.23457e+06<
243 >%+g< >1234567.89< >+1.23457e+06<
244 >%#g< >1234567.89< >1.23457e+06<
245 >%g< >-1234567.89< >-1.23457e+06<
246 >%+g< >-1234567.89< >-1.23457e+06<
247 >%#g< >-1234567.89< >-1.23457e+06<
248 >%g< >0.00012345< >0.00012345<
249 >%g< >0.000012345< >1.2345e-05<
250 >%g< >1234567E96< >1.23457e+102<
251 >%g< >.1234567E-101< >1.23457e-102<
253 >%13g< >1234567.89< > 1.23457e+06<
254 >%+13g< >1234567.89< > +1.23457e+06<
255 >%013g< >1234567.89< >001.23457e+06<
256 >%-13g< >1234567.89< >1.23457e+06 <
257 >%h< >''< >%h INVALID<
258 >%i< >123456.789< >123456< >Synonym for %d<
259 >%j< >''< >%j INVALID<
260 >%k< >''< >%k INVALID<
261 >%l< >''< >%l INVALID<
262 >%m< >''< >%m INVALID<
263 >%s< >sprintf('%%n%n %d', $n, $n)< >%n 2< >Slight sneakiness to test %n<
264 >%o< >2**32-1< >37777777777<
265 >%+o< >2**32-1< >37777777777<
266 >%#o< >2**32-1< >037777777777<
267 >%d< >$p=sprintf('%p',$p);$p=~/^[0-9a-f]+$/< >1< >Coarse hack: hex from %p?<
268 >%#p< >''< >%#p INVALID<
269 >%q< >''< >%q INVALID<
270 >%r< >''< >%r INVALID<
271 >%s< >'string'< >string<
272 >%10s< >'string'< > string<
273 >%+10s< >'string'< > string<
274 >%#10s< >'string'< > string<
275 >%010s< >'string'< >0000string<
276 >%0*s< >[10, 'string']< >0000string<
277 >%-10s< >'string'< >string <
278 >%3s< >'string'< >string<
279 >%.3s< >'string'< >str<
280 >%.*s< >[3, 'string']< >str<
281 >%t< >''< >%t INVALID<
282 >%u< >2**32-1< >4294967295<
283 >%+u< >2**32-1< >4294967295<
284 >%#u< >2**32-1< >4294967295<
285 >%12u< >2**32-1< > 4294967295<
286 >%012u< >2**32-1< >004294967295<
287 >%-12u< >2**32-1< >4294967295 <
288 >%-012u< >2**32-1< >4294967295 <
289 >%v< >''< >%v INVALID<
290 >%w< >''< >%w INVALID<
291 >%x< >2**32-1< >ffffffff<
292 >%+x< >2**32-1< >ffffffff<
293 >%#x< >2**32-1< >0xffffffff<
294 >%10x< >2**32-1< > ffffffff<
295 >%010x< >2**32-1< >00ffffffff<
296 >%-10x< >2**32-1< >ffffffff <
297 >%-010x< >2**32-1< >ffffffff <
298 >%0-10x< >2**32-1< >ffffffff <
299 >%0*x< >[-10, ,2**32-1]< >ffffffff <
300 >%y< >''< >%y INVALID<
301 >%z< >''< >%z INVALID<