1N/A#!./perl
1N/A
1N/A# Tests sprintf, excluding handling of 64-bit integers or long
1N/A# doubles (if supported), of machine-specific short and long
1N/A# integers, machine-specific floating point exceptions (infinity,
1N/A# not-a-number ...), of the effects of locale, and of features
1N/A# specific to multi-byte characters (under the utf8 pragma and such).
1N/A
1N/ABEGIN {
1N/A chdir 't' if -d 't';
1N/A @INC = '../lib';
1N/A}
1N/Ause warnings;
1N/A# we do not load %Config since this test resides in op and needs
1N/A# to run under the minitest target even without Config.pm working.
1N/A
1N/A# strictness
1N/Amy @tests = ();
1N/Amy ($i, $template, $data, $result, $comment, $w, $x, $evalData, $n, $p);
1N/A
1N/Awhile (<DATA>) {
1N/A s/^\s*>//; s/<\s*$//;
1N/A push @tests, [split(/<\s*>/, $_, 4)];
1N/A}
1N/A
1N/Aprint '1..', scalar @tests, "\n";
1N/A
1N/A$SIG{__WARN__} = sub {
1N/A if ($_[0] =~ /^Invalid conversion/) {
1N/A $w = ' INVALID';
1N/A } elsif ($_[0] =~ /^Use of uninitialized value/) {
1N/A $w = ' UNINIT';
1N/A } else {
1N/A warn @_;
1N/A }
1N/A};
1N/A
1N/Amy $Is_VMS_VAX = 0;
1N/A# We use HW_MODEL since ARCH_NAME was not in VMS V5.*
1N/Aif ($^O eq 'VMS') {
1N/A my $hw_model;
1N/A chomp($hw_model = `write sys\$output f\$getsyi("HW_MODEL")`);
1N/A $Is_VMS_VAX = $hw_model < 1024 ? 1 : 0;
1N/A}
1N/A
1N/A# No %Config.
1N/Amy $Is_Ultrix_VAX = $^O eq 'ultrix' && `uname -m` =~ /^VAX$/;
1N/A
1N/Afor ($i = 1; @tests; $i++) {
1N/A ($template, $data, $result, $comment) = @{shift @tests};
1N/A if ($^O eq 'os390' || $^O eq 's390') { # non-IEEE (s390 is UTS)
1N/A $data =~ s/([eE])96$/${1}63/; # smaller exponents
1N/A $result =~ s/([eE]\+)102$/${1}69/; # " "
1N/A $data =~ s/([eE])\-101$/${1}-56/; # larger exponents
1N/A $result =~ s/([eE])\-102$/${1}-57/; # " "
1N/A }
1N/A if ($Is_VMS_VAX || $Is_Ultrix_VAX) {
1N/A # VAX DEC C 5.3 at least since there is no
1N/A # ccflags =~ /float=ieee/ on VAX.
1N/A # AXP is unaffected whether or not it's using ieee.
1N/A $data =~ s/([eE])96$/${1}26/; # smaller exponents
1N/A $result =~ s/([eE]\+)102$/${1}32/; # " "
1N/A $data =~ s/([eE])\-101$/${1}-24/; # larger exponents
1N/A $result =~ s/([eE])\-102$/${1}-25/; # " "
1N/A }
1N/A $evalData = eval $data;
1N/A $w = undef;
1N/A $x = sprintf(">$template<",
1N/A defined @$evalData ? @$evalData : $evalData);
1N/A substr($x, -1, 0) = $w if $w;
1N/A # $x may have 3 exponent digits, not 2
1N/A my $y = $x;
1N/A if ($y =~ s/([Ee][-+])0(\d)/$1$2/) {
1N/A # if result is left-adjusted, append extra space
1N/A if ($template =~ /%\+?\-/ and $result =~ / $/) {
1N/A $y =~ s/<$/ </;
1N/A }
1N/A # if result is zero-filled, add extra zero
1N/A elsif ($template =~ /%\+?0/ and $result =~ /^0/) {
1N/A $y =~ s/^>0/>00/;
1N/A }
1N/A # if result is right-adjusted, prepend extra space
1N/A elsif ($result =~ /^ /) {
1N/A $y =~ s/^>/> /;
1N/A }
1N/A }
1N/A
1N/A if ($x eq ">$result<") {
1N/A print "ok $i\n";
1N/A }
1N/A elsif ($y eq ">$result<") # Some C libraries always give
1N/A { # three-digit exponent
1N/A print("ok $i # >$result< $x three-digit exponent accepted\n");
1N/A }
1N/A elsif ($result =~ /[-+]\d{3}$/ &&
1N/A # Suppress tests with modulo of exponent >= 100 on platforms
1N/A # which can't handle such magnitudes (or where we can't tell).
1N/A ((!eval {require POSIX}) || # Costly: only do this if we must!
1N/A (length(&POSIX::DBL_MAX) - rindex(&POSIX::DBL_MAX, '+')) == 3))
1N/A {
1N/A print("ok $i # >$template< >$data< >$result<",
1N/A " Suppressed: exponent out of range?\n")
1N/A }
1N/A else {
1N/A $y = ($x eq $y ? "" : " => $y");
1N/A print("not ok $i >$template< >$data< >$result< $x$y",
1N/A $comment ? " # $comment\n" : "\n");
1N/A }
1N/A}
1N/A
1N/A# In each of the following lines, there are three required fields:
1N/A# printf template, data to be formatted (as a Perl expression), and
1N/A# expected result of formatting. An optional fourth field can contain
1N/A# a comment. Each field is delimited by a starting '>' and a
1N/A# finishing '<'; any whitespace outside these start and end marks is
1N/A# not part of the field. If formatting requires more than one data
1N/A# item (for example, if variable field widths are used), the Perl data
1N/A# expression should return a reference to an array having the requisite
1N/A# number of elements. Even so, subterfuge is sometimes required: see
1N/A# tests for %n and %p.
1N/A#
1N/A# The following tests are not currently run, for the reasons stated:
1N/A
1N/A=pod
1N/A
1N/A=begin problematic
1N/A
1N/A>%.0f< >-0.1< >-0< >C library bug: no minus on VMS, HP-UX<
1N/A>%.0f< >1.5< >2< >Standard vague: no rounding rules<
1N/A>%.0f< >2.5< >2< >Standard vague: no rounding rules<
1N/A>%G< >1234567e96< >1.23457E+102< >exponent too big for OS/390<
1N/A>%G< >.1234567e-101< >1.23457E-102< >exponent too small for OS/390<
1N/A>%e< >1234567E96< >1.234567e+102< >exponent too big for OS/390<
1N/A>%e< >.1234567E-101< >1.234567e-102< >exponent too small for OS/390<
1N/A>%g< >.1234567E-101< >1.23457e-102< >exponent too small for OS/390<
1N/A>%g< >1234567E96< >1.23457e+102< >exponent too big for OS/390<
1N/A
1N/A=end problematic
1N/A
1N/A=cut
1N/A
1N/A# template data result
1N/A__END__
1N/A>%6. 6s< >''< >%6. 6s INVALID< >(See use of $w in code above)<
1N/A>%6 .6s< >''< >%6 .6s INVALID<
1N/A>%6.6 s< >''< >%6.6 s INVALID<
1N/A>%A< >''< >%A INVALID<
1N/A>%B< >''< >%B INVALID<
1N/A>%C< >''< >%C INVALID<
1N/A>%D< >0x7fffffff< >2147483647< >Synonym for %ld<
1N/A>%E< >123456.789< >1.234568E+05< >Like %e, but using upper-case "E"<
1N/A>%F< >123456.789< >123456.789000< >Synonym for %f<
1N/A>%G< >1234567.89< >1.23457E+06< >Like %g, but using upper-case "E"<
1N/A>%G< >1234567e96< >1.23457E+102<
1N/A>%G< >.1234567e-101< >1.23457E-102<
1N/A>%G< >12345.6789< >12345.7<
1N/A>%H< >''< >%H INVALID<
1N/A>%I< >''< >%I INVALID<
1N/A>%J< >''< >%J INVALID<
1N/A>%K< >''< >%K INVALID<
1N/A>%L< >''< >%L INVALID<
1N/A>%M< >''< >%M INVALID<
1N/A>%N< >''< >%N INVALID<
1N/A>%O< >2**32-1< >37777777777< >Synonym for %lo<
1N/A>%P< >''< >%P INVALID<
1N/A>%Q< >''< >%Q INVALID<
1N/A>%R< >''< >%R INVALID<
1N/A>%S< >''< >%S INVALID<
1N/A>%T< >''< >%T INVALID<
1N/A>%U< >2**32-1< >4294967295< >Synonym for %lu<
1N/A>%V< >''< >%V INVALID<
1N/A>%W< >''< >%W INVALID<
1N/A>%X< >2**32-1< >FFFFFFFF< >Like %x, but with u/c letters<
1N/A>%#X< >2**32-1< >0XFFFFFFFF<
1N/A>%Y< >''< >%Y INVALID<
1N/A>%Z< >''< >%Z INVALID<
1N/A>%a< >''< >%a INVALID<
1N/A>%b< >2**32-1< >11111111111111111111111111111111<
1N/A>%+b< >2**32-1< >11111111111111111111111111111111<
1N/A>%#b< >2**32-1< >0b11111111111111111111111111111111<
1N/A>%34b< >2**32-1< > 11111111111111111111111111111111<
1N/A>%034b< >2**32-1< >0011111111111111111111111111111111<
1N/A>%-34b< >2**32-1< >11111111111111111111111111111111 <
1N/A>%-034b< >2**32-1< >11111111111111111111111111111111 <
1N/A>%c< >ord('A')< >A<
1N/A>%10c< >ord('A')< > A<
1N/A>%#10c< >ord('A')< > A< ># modifier: no effect<
1N/A>%010c< >ord('A')< >000000000A<
1N/A>%10lc< >ord('A')< > A< >l modifier: no effect<
1N/A>%10hc< >ord('A')< > A< >h modifier: no effect<
1N/A>%10.5c< >ord('A')< > A< >precision: no effect<
1N/A>%-10c< >ord('A')< >A <
1N/A>%d< >123456.789< >123456<
1N/A>%d< >-123456.789< >-123456<
1N/A>%d< >0< >0<
1N/A>%+d< >0< >+0<
1N/A>%0d< >0< >0<
1N/A>%.0d< >0< ><
1N/A>%+.0d< >0< >+<
1N/A>%.0d< >1< >1<
1N/A>%d< >1< >1<
1N/A>%+d< >1< >+1<
1N/A>%#3.2d< >1< > 01< ># modifier: no effect<
1N/A>%3.2d< >1< > 01<
1N/A>%03.2d< >1< >001<
1N/A>%-3.2d< >1< >01 <
1N/A>%-03.2d< >1< >01 < >zero pad + left just.: no effect<
1N/A>%d< >-1< >-1<
1N/A>%+d< >-1< >-1<
1N/A>%hd< >1< >1< >More extensive testing of<
1N/A>%ld< >1< >1< >length modifiers would be<
1N/A>%Vd< >1< >1< >platform-specific<
1N/A>%vd< >chr(1)< >1<
1N/A>%+vd< >chr(1)< >+1<
1N/A>%#vd< >chr(1)< >1<
1N/A>%vd< >"\01\02\03"< >1.2.3<
1N/A>%v.3d< >"\01\02\03"< >001.002.003<
1N/A>%0v3d< >"\01\02\03"< >001.002.003<
1N/A>%-v3d< >"\01\02\03"< >1 .2 .3 <
1N/A>%+-v3d< >"\01\02\03"< >+1 .2 .3 <
1N/A>%v4.3d< >"\01\02\03"< > 001. 002. 003<
1N/A>%0v4.3d< >"\01\02\03"< >0001.0002.0003<
1N/A>%0*v2d< >['-', "\0\7\14"]< >00-07-12<
1N/A>%v.*d< >["\01\02\03", 3]< >001.002.003<
1N/A>%0v*d< >["\01\02\03", 3]< >001.002.003<
1N/A>%-v*d< >["\01\02\03", 3]< >1 .2 .3 <
1N/A>%+-v*d< >["\01\02\03", 3]< >+1 .2 .3 <
1N/A>%v*.*d< >["\01\02\03", 4, 3]< > 001. 002. 003<
1N/A>%0v*.*d< >["\01\02\03", 4, 3]< >0001.0002.0003<
1N/A>%0*v*d< >['-', "\0\7\13", 2]< >00-07-11<
1N/A>%e< >1234.875< >1.234875e+03<
1N/A>%e< >0.000012345< >1.234500e-05<
1N/A>%e< >1234567E96< >1.234567e+102<
1N/A>%e< >0< >0.000000e+00<
1N/A>%e< >.1234567E-101< >1.234567e-102<
1N/A>%+e< >1234.875< >+1.234875e+03<
1N/A>%#e< >1234.875< >1.234875e+03<
1N/A>%e< >-1234.875< >-1.234875e+03<
1N/A>%+e< >-1234.875< >-1.234875e+03<
1N/A>%#e< >-1234.875< >-1.234875e+03<
1N/A>%.0e< >1234.875< >1e+03<
1N/A>%#.0e< >1234.875< >1.e+03<
1N/A>%.0e< >1.875< >2e+00<
1N/A>%.0e< >0.875< >9e-01<
1N/A>%.*e< >[0, 1234.875]< >1e+03<
1N/A>%.1e< >1234.875< >1.2e+03<
1N/A>%-12.4e< >1234.875< >1.2349e+03 <
1N/A>%12.4e< >1234.875< > 1.2349e+03<
1N/A>%+-12.4e< >1234.875< >+1.2349e+03 <
1N/A>%+12.4e< >1234.875< > +1.2349e+03<
1N/A>%+-12.4e< >-1234.875< >-1.2349e+03 <
1N/A>%+12.4e< >-1234.875< > -1.2349e+03<
1N/A>%f< >1234.875< >1234.875000<
1N/A>%+f< >1234.875< >+1234.875000<
1N/A>%#f< >1234.875< >1234.875000<
1N/A>%f< >-1234.875< >-1234.875000<
1N/A>%+f< >-1234.875< >-1234.875000<
1N/A>%#f< >-1234.875< >-1234.875000<
1N/A>%6f< >1234.875< >1234.875000<
1N/A>%*f< >[6, 1234.875]< >1234.875000<
1N/A>%.0f< >1234.875< >1235<
1N/A>%.1f< >1234.875< >1234.9<
1N/A>%-8.1f< >1234.875< >1234.9 <
1N/A>%8.1f< >1234.875< > 1234.9<
1N/A>%+-8.1f< >1234.875< >+1234.9 <
1N/A>%+8.1f< >1234.875< > +1234.9<
1N/A>%+-8.1f< >-1234.875< >-1234.9 <
1N/A>%+8.1f< >-1234.875< > -1234.9<
1N/A>%*.*f< >[5, 2, 12.3456]< >12.35<
1N/A>%f< >0< >0.000000<
1N/A>%.0f< >0< >0<
1N/A>%.0f< >2**38< >274877906944< >Should have exact int'l rep'n<
1N/A>%.0f< >0.1< >0<
1N/A>%.0f< >0.6< >1< >Known to fail with sfio, (irix|nonstop-ux|powerux); -DHAS_LDBL_SPRINTF_BUG may fix<
1N/A>%.0f< >-0.6< >-1< >Known to fail with sfio, (irix|nonstop-ux|powerux); -DHAS_LDBL_SPRINTF_BUG may fix<
1N/A>%.0f< >1.6< >2<
1N/A>%.0f< >-1.6< >-2<
1N/A>%.0f< >1< >1<
1N/A>%#.0f< >1< >1.<
1N/A>%.0lf< >1< >1< >'l' should have no effect<
1N/A>%.0hf< >1< >%.0hf INVALID< >'h' should be rejected<
1N/A>%g< >12345.6789< >12345.7<
1N/A>%+g< >12345.6789< >+12345.7<
1N/A>%#g< >12345.6789< >12345.7<
1N/A>%.0g< >12345.6789< >1e+04<
1N/A>%#.0g< >12345.6789< >1.e+04<
1N/A>%.2g< >12345.6789< >1.2e+04<
1N/A>%.*g< >[2, 12345.6789]< >1.2e+04<
1N/A>%.9g< >12345.6789< >12345.6789<
1N/A>%12.9g< >12345.6789< > 12345.6789<
1N/A>%012.9g< >12345.6789< >0012345.6789<
1N/A>%-12.9g< >12345.6789< >12345.6789 <
1N/A>%*.*g< >[-12, 9, 12345.6789]< >12345.6789 <
1N/A>%-012.9g< >12345.6789< >12345.6789 <
1N/A>%g< >-12345.6789< >-12345.7<
1N/A>%+g< >-12345.6789< >-12345.7<
1N/A>%g< >1234567.89< >1.23457e+06<
1N/A>%+g< >1234567.89< >+1.23457e+06<
1N/A>%#g< >1234567.89< >1.23457e+06<
1N/A>%g< >-1234567.89< >-1.23457e+06<
1N/A>%+g< >-1234567.89< >-1.23457e+06<
1N/A>%#g< >-1234567.89< >-1.23457e+06<
1N/A>%g< >0.00012345< >0.00012345<
1N/A>%g< >0.000012345< >1.2345e-05<
1N/A>%g< >1234567E96< >1.23457e+102<
1N/A>%g< >.1234567E-101< >1.23457e-102<
1N/A>%g< >0< >0<
1N/A>%13g< >1234567.89< > 1.23457e+06<
1N/A>%+13g< >1234567.89< > +1.23457e+06<
1N/A>%013g< >1234567.89< >001.23457e+06<
1N/A>%-13g< >1234567.89< >1.23457e+06 <
1N/A>%h< >''< >%h INVALID<
1N/A>%i< >123456.789< >123456< >Synonym for %d<
1N/A>%j< >''< >%j INVALID<
1N/A>%k< >''< >%k INVALID<
1N/A>%l< >''< >%l INVALID<
1N/A>%m< >''< >%m INVALID<
1N/A>%s< >sprintf('%%n%n %d', $n, $n)< >%n 2< >Slight sneakiness to test %n<
1N/A>%o< >2**32-1< >37777777777<
1N/A>%+o< >2**32-1< >37777777777<
1N/A>%#o< >2**32-1< >037777777777<
1N/A>%o< >642< >1202< >check smaller octals across platforms<
1N/A>%+o< >642< >1202<
1N/A>%#o< >642< >01202<
1N/A>%d< >$p=sprintf('%p',$p);$p=~/^[0-9a-f]+$/< >1< >Coarse hack: hex from %p?<
1N/A>%#p< >''< >%#p INVALID<
1N/A>%q< >''< >%q INVALID<
1N/A>%r< >''< >%r INVALID<
1N/A>%s< >'string'< >string<
1N/A>%10s< >'string'< > string<
1N/A>%+10s< >'string'< > string<
1N/A>%#10s< >'string'< > string<
1N/A>%010s< >'string'< >0000string<
1N/A>%0*s< >[10, 'string']< >0000string<
1N/A>%-10s< >'string'< >string <
1N/A>%3s< >'string'< >string<
1N/A>%.3s< >'string'< >str<
1N/A>%.*s< >[3, 'string']< >str<
1N/A>%t< >''< >%t INVALID<
1N/A>%u< >2**32-1< >4294967295<
1N/A>%+u< >2**32-1< >4294967295<
1N/A>%#u< >2**32-1< >4294967295<
1N/A>%12u< >2**32-1< > 4294967295<
1N/A>%012u< >2**32-1< >004294967295<
1N/A>%-12u< >2**32-1< >4294967295 <
1N/A>%-012u< >2**32-1< >4294967295 <
1N/A>%v< >''< >%v INVALID<
1N/A>%w< >''< >%w INVALID<
1N/A>%x< >2**32-1< >ffffffff<
1N/A>%+x< >2**32-1< >ffffffff<
1N/A>%#x< >2**32-1< >0xffffffff<
1N/A>%10x< >2**32-1< > ffffffff<
1N/A>%010x< >2**32-1< >00ffffffff<
1N/A>%-10x< >2**32-1< >ffffffff <
1N/A>%-010x< >2**32-1< >ffffffff <
1N/A>%0-10x< >2**32-1< >ffffffff <
1N/A>%0*x< >[-10, ,2**32-1]< >ffffffff <
1N/A>%y< >''< >%y INVALID<
1N/A>%z< >''< >%z INVALID<
1N/A>%2$d %1$d< >[12, 34]< >34 12<
1N/A>%*2$d< >[12, 3]< > 12<
1N/A>%2$d %d< >[12, 34]< >34 12<
1N/A>%2$d %d %d< >[12, 34]< >34 12 34<
1N/A>%3$d %d %d< >[12, 34, 56]< >56 12 34<
1N/A>%2$*3$d %d< >[12, 34, 3]< > 34 12<
1N/A>%*3$2$d %d< >[12, 34, 3]< >%*3$2$d 12 INVALID<
1N/A>%2$d< >12< >0 UNINIT<
1N/A>%0$d< >12< >%0$d INVALID<
1N/A>%1$$d< >12< >%1$$d INVALID<
1N/A>%1$1$d< >12< >%1$1$d INVALID<
1N/A>%*2$*2$d< >[12, 3]< >%*2$*2$d INVALID<
1N/A>%*2*2$d< >[12, 3]< >%*2*2$d INVALID<
1N/A>%0v2.2d< >''< ><
1N/A>%vc,%d< >[63, 64, 65]< >?,64<
1N/A>%vd,%d< >[1, 2, 3]< >49,2<
1N/A>%vf,%d< >[1, 2, 3]< >1.000000,2<
1N/A>%vp< >''< >%vp INVALID<
1N/A>%vs,%d< >[1, 2, 3]< >1,2<
1N/A>%v_< >''< >%v_ INVALID<
1N/A>%v#x< >''< >%v#x INVALID<
1N/A>%v02x< >"foo\012"< >66.6f.6f.0a<
1N/A>%V-%s< >["Hello"]< >%V-Hello INVALID<
1N/A>%K %d %d< >[13, 29]< >%K 13 29 INVALID<
1N/A>%*.*K %d< >[13, 29, 76]< >%*.*K 13 INVALID<
1N/A>%4$K %d< >[45, 67]< >%4$K 45 INVALID<
1N/A>%d %K %d< >[23, 45]< >23 %K 45 INVALID<
1N/A>%*v*999\$d %d %d< >[11, 22, 33]< >%*v*999\$d 11 22 INVALID<
1N/A>%#b< >0< >0<
1N/A>%#o< >0< >0<
1N/A>%#x< >0< >0<
1N/A>%2918905856$v2d< >''< ><
1N/A>%*2918905856$v2d< >''< > UNINIT<