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 # template data result
81 >%6. 6s< >''< >%6. 6s INVALID< >(See use of $w in code above)<
82 >%6 .6s< >''< >%6 .6s INVALID<
83 >%6.6 s< >''< >%6.6 s INVALID<
84 >%A< >''< >%A INVALID<
85 >%B< >''< >%B INVALID<
86 >%C< >''< >%C INVALID<
87 >%D< >0x7fffffff< >2147483647< >Synonym for %ld<
88 >%E< >123456.789< >1.234568E+05< >Like %e, but using upper-case "E"<
89 >%F< >123456.789< >123456.789000< >Synonym for %f<
90 >%G< >1234567.89< >1.23457E+06< >Like %g, but using upper-case "E"<
91 >%G< >1234567e96< >1.23457E+102<
92 >%G< >.1234567e-101< >1.23457E-102<
93 >%G< >12345.6789< >12345.7<
94 >%H< >''< >%H INVALID<
95 >%I< >''< >%I INVALID<
96 >%J< >''< >%J INVALID<
97 >%K< >''< >%K INVALID<
98 >%L< >''< >%L INVALID<
99 >%M< >''< >%M INVALID<
100 >%N< >''< >%N INVALID<
101 >%O< >2**32-1< >37777777777< >Synonum for %lo<
102 >%P< >''< >%P INVALID<
103 >%Q< >''< >%Q INVALID<
104 >%R< >''< >%R INVALID<
105 >%S< >''< >%S INVALID<
106 >%T< >''< >%T INVALID<
107 >%U< >2**32-1< >4294967295< >Synonum for %lu<
108 >%V< >''< >%V INVALID<
109 >%W< >''< >%W INVALID<
110 >%X< >2**32-1< >FFFFFFFF< >Like %x, but with u/c letters<
111 >%#X< >2**32-1< >0XFFFFFFFF<
112 >%Y< >''< >%Y INVALID<
113 >%Z< >''< >%Z INVALID<
114 >%a< >''< >%a INVALID<
115 >%b< >2**32-1< >11111111111111111111111111111111<
116 >%+b< >2**32-1< >11111111111111111111111111111111<
117 >%#b< >2**32-1< >0b11111111111111111111111111111111<
118 >%34b< >2**32-1< > 11111111111111111111111111111111<
119 >%034b< >2**32-1< >0011111111111111111111111111111111<
120 >%-34b< >2**32-1< >11111111111111111111111111111111 <
121 >%-034b< >2**32-1< >11111111111111111111111111111111 <
123 >%10c< >ord('A')< > A<
124 >%#10c< >ord('A')< > A< ># modifier: no effect<
125 >%010c< >ord('A')< >000000000A<
126 >%10lc< >ord('A')< > A< >l modifier: no effect<
127 >%10hc< >ord('A')< > A< >h modifier: no effect<
128 >%10.5c< >ord('A')< > A< >precision: no effect<
129 >%-10c< >ord('A')< >A <
130 >%d< >123456.789< >123456<
131 >%d< >-123456.789< >-123456<
140 >%#3.2d< >1< > 01< ># modifier: no effect<
144 >%-03.2d< >1< >01 < >zero pad + left just.: no effect<
147 >%hd< >1< >1< >More extensive testing of<
148 >%ld< >1< >1< >length modifiers would be<
149 >%Vd< >1< >1< >platform-specific<
153 >%vd< >"\01\02\03"< >1.2.3<
154 >%v.3d< >"\01\02\03"< >001.002.003<
155 >%v03d< >"\01\02\03"< >001.002.003<
156 >%v-3d< >"\01\02\03"< >1 .2 .3 <
157 >%v+-3d< >"\01\02\03"< >+1 .2 .3 <
158 >%v4.3d< >"\01\02\03"< > 001. 002. 003<
159 >%v04.3d< >"\01\02\03"< >0001.0002.0003<
160 >%*v02d< >['-', "\0\7\13"]< >00-07-11<
161 >%e< >1234.875< >1.234875e+03<
162 >%e< >0.000012345< >1.234500e-05<
163 >%e< >1234567E96< >1.234567e+102<
164 >%e< >0< >0.000000e+00<
165 >%e< >.1234567E-101< >1.234567e-102<
166 >%+e< >1234.875< >+1.234875e+03<
167 >%#e< >1234.875< >1.234875e+03<
168 >%e< >-1234.875< >-1.234875e+03<
169 >%+e< >-1234.875< >-1.234875e+03<
170 >%#e< >-1234.875< >-1.234875e+03<
171 >%.0e< >1234.875< >1e+03<
172 >%.*e< >[0, 1234.875]< >1e+03<
173 >%.1e< >1234.875< >1.2e+03<
174 >%-12.4e< >1234.875< >1.2349e+03 <
175 >%12.4e< >1234.875< > 1.2349e+03<
176 >%+-12.4e< >1234.875< >+1.2349e+03 <
177 >%+12.4e< >1234.875< > +1.2349e+03<
178 >%+-12.4e< >-1234.875< >-1.2349e+03 <
179 >%+12.4e< >-1234.875< > -1.2349e+03<
180 >%f< >1234.875< >1234.875000<
181 >%+f< >1234.875< >+1234.875000<
182 >%#f< >1234.875< >1234.875000<
183 >%f< >-1234.875< >-1234.875000<
184 >%+f< >-1234.875< >-1234.875000<
185 >%#f< >-1234.875< >-1234.875000<
186 >%6f< >1234.875< >1234.875000<
187 >%*f< >[6, 1234.875]< >1234.875000<
188 >%.0f< >1234.875< >1235<
189 >%.1f< >1234.875< >1234.9<
190 >%-8.1f< >1234.875< >1234.9 <
191 >%8.1f< >1234.875< > 1234.9<
192 >%+-8.1f< >1234.875< >+1234.9 <
193 >%+8.1f< >1234.875< > +1234.9<
194 >%+-8.1f< >-1234.875< >-1234.9 <
195 >%+8.1f< >-1234.875< > -1234.9<
196 >%*.*f< >[5, 2, 12.3456]< >12.35<
199 >%.0f< >2**38< >274877906944< >Should have exact int'l rep'n<
202 >%g< >12345.6789< >12345.7<
203 >%+g< >12345.6789< >+12345.7<
204 >%#g< >12345.6789< >12345.7<
205 >%.0g< >12345.6789< >1e+04<
206 >%.2g< >12345.6789< >1.2e+04<
207 >%.*g< >[2, 12345.6789]< >1.2e+04<
208 >%.9g< >12345.6789< >12345.6789<
209 >%12.9g< >12345.6789< > 12345.6789<
210 >%012.9g< >12345.6789< >0012345.6789<
211 >%-12.9g< >12345.6789< >12345.6789 <
212 >%*.*g< >[-12, 9, 12345.6789]< >12345.6789 <
213 >%-012.9g< >12345.6789< >12345.6789 <
214 >%g< >-12345.6789< >-12345.7<
215 >%+g< >-12345.6789< >-12345.7<
216 >%g< >1234567.89< >1.23457e+06<
217 >%+g< >1234567.89< >+1.23457e+06<
218 >%#g< >1234567.89< >1.23457e+06<
219 >%g< >-1234567.89< >-1.23457e+06<
220 >%+g< >-1234567.89< >-1.23457e+06<
221 >%#g< >-1234567.89< >-1.23457e+06<
222 >%g< >0.00012345< >0.00012345<
223 >%g< >0.000012345< >1.2345e-05<
224 >%g< >1234567E96< >1.23457e+102<
225 >%g< >.1234567E-101< >1.23457e-102<
227 >%13g< >1234567.89< > 1.23457e+06<
228 >%+13g< >1234567.89< > +1.23457e+06<
229 >%013g< >1234567.89< >001.23457e+06<
230 >%-13g< >1234567.89< >1.23457e+06 <
231 >%h< >''< >%h INVALID<
232 >%i< >123456.789< >123456< >Synonym for %d<
233 >%j< >''< >%j INVALID<
234 >%k< >''< >%k INVALID<
235 >%l< >''< >%l INVALID<
236 >%m< >''< >%m INVALID<
237 >%s< >sprintf('%%n%n %d', $n, $n)< >%n 2< >Slight sneakiness to test %n<
238 >%o< >2**32-1< >37777777777<
239 >%+o< >2**32-1< >37777777777<
240 >%#o< >2**32-1< >037777777777<
241 >%d< >$p=sprintf('%p',$p);$p=~/^[0-9a-f]+$/< >1< >Coarse hack: hex from %p?<
242 >%q< >''< >%q INVALID<
243 >%r< >''< >%r INVALID<
244 >%s< >'string'< >string<
245 >%10s< >'string'< > string<
246 >%+10s< >'string'< > string<
247 >%#10s< >'string'< > string<
248 >%010s< >'string'< >0000string<
249 >%0*s< >[10, 'string']< >0000string<
250 >%-10s< >'string'< >string <
251 >%3s< >'string'< >string<
252 >%.3s< >'string'< >str<
253 >%.*s< >[3, 'string']< >str<
254 >%t< >''< >%t INVALID<
255 >%u< >2**32-1< >4294967295<
256 >%+u< >2**32-1< >4294967295<
257 >%#u< >2**32-1< >4294967295<
258 >%12u< >2**32-1< > 4294967295<
259 >%012u< >2**32-1< >004294967295<
260 >%-12u< >2**32-1< >4294967295 <
261 >%-012u< >2**32-1< >4294967295 <
262 >%v< >''< >%v INVALID<
263 >%w< >''< >%w INVALID<
264 >%x< >2**32-1< >ffffffff<
265 >%+x< >2**32-1< >ffffffff<
266 >%#x< >2**32-1< >0xffffffff<
267 >%10x< >2**32-1< > ffffffff<
268 >%010x< >2**32-1< >00ffffffff<
269 >%-10x< >2**32-1< >ffffffff <
270 >%-010x< >2**32-1< >ffffffff <
271 >%0-10x< >2**32-1< >ffffffff <
272 >%0*x< >[-10, ,2**32-1]< >ffffffff <
273 >%y< >''< >%y INVALID<
274 >%z< >''< >%z INVALID<