8 #-- testing numeric fields in all variants (WL)
12 local $^A = ""; # don't litter, use a local bin
13 formline( $format, @_ );
18 [ '@###', 0, 1, 9999.5, 9999.4999, -999.5, 1e100 ],
19 [ '@0##', 0, 1, 9999.5, -999.4999, -999.5, 1e100 ],
22 [ '@###.', 0, 1, 9999.5, 9999.4999, -999.5 ],
23 [ '@##.##', 0, 1, 999.995, 999.99499, -100 ],
24 [ '@0#.##', 0, 1, 10, -0.0001 ],
29 my $fieldwidth = length( $fmt );
30 my $leadzero = $fmt =~ /^.0/ ? "0" : "";
31 if( $fmt =~ /\.(#*)/ ){
32 my $fractwidth = length( $1 );
33 return "%#${leadzero}${fieldwidth}.${fractwidth}f"
35 return "%${leadzero}${fieldwidth}.0f"
40 for my $tref ( @NumTests ){
41 $num_tests += @$tref - 1;
43 #---------------------------------------------------------
45 # number of tests in section 1
48 # number of tests in section 3
51 printf "1..%d\n", $bas_tests + $num_tests + $hmb_tests;
53 my $CAT = ($^O eq 'MSWin32' || $^O eq 'NetWare' || $^O eq 'VMS') ? 'type'
54 : ($^O eq 'MacOS') ? 'catenate'
73 now @<<the@>>>> for all@|||||men to come @<<<<
75 'i' . 's', "time\n", $good, 'to'
79 open(OUT, '>Op_write.tmp') || die "Can't create Op_write.tmp";
80 END { 1 while unlink 'Op_write.tmp' }
84 $multiline = "forescore\nand\nseven years\n";
85 $foo = 'when in the course of human events it becomes necessary';
87 close OUT or die "Could not close: $!";
98 now is the time for all good men to come to\n";
100 if (`$CAT Op_write.tmp` eq $right)
101 { print "ok 1\n"; 1 while unlink 'Op_write.tmp'; }
103 { print "not ok 1\n"; }
105 $fox = 'wolfishness';
106 my $fox = 'foxiness'; # Test a lexical variable.
116 now @<<the@>>>> for all@|||||men to come @<<<<
117 'i' . 's', "time\n", $good, 'to'
120 open OUT2, '>Op_write.tmp' or die "Can't create Op_write.tmp";
123 $multiline = "forescore\nand\nseven years\n";
124 $foo = 'when in the course of human events it becomes necessary';
126 close OUT2 or die "Could not close: $!";
140 now is the time for all good men to come to\n";
142 if (`$CAT Op_write.tmp` eq $right)
143 { print "ok 2\n"; 1 while unlink 'Op_write.tmp'; }
145 { print "not ok 2\n"; }
157 now @<<the@>>>> for all@|||||men to come @<<<<
158 'i' . 's', "time\n", $good, 'to'
162 open(OUT2, '>Op_write.tmp') || die "Can't create Op_write.tmp";
166 $multiline = "forescore\nand\nseven years\n";
167 $foo = 'when in the course of human events it becomes necessary';
169 close OUT2 or die "Could not close: $!";
184 now is the time for all good men to come to\n";
186 if (`$CAT Op_write.tmp` eq $right)
187 { print "ok 3\n"; 1 while unlink 'Op_write.tmp'; }
189 { print "not ok 3\n"; }
211 my $format1 = '@' . '>' x $_;
212 formline $format1, 'abc';
213 $was1 .= "$format1 $^A\n";
216 local $format2 = '@' . '>' x $_;
217 formline $format2, 'abc';
218 $was2 .= "$format2 $^A\n";
220 print $was1 eq $mustbe ? "ok 4\n" : "not ok 4\n";
221 print $was2 eq $mustbe ? "ok 5\n" : "not ok 5\n";
232 open(OUT3, '>Op_write.tmp') || die "Can't create Op_write.tmp";
236 close OUT3 or die "Could not close: $!";
241 if (`$CAT Op_write.tmp` eq $right)
242 { print "ok 6\n"; 1 while unlink 'Op_write.tmp'; }
244 { print "not ok 6\n"; }
246 # test lexicals and globals
254 open(LEX, ">&STDOUT") or die;
258 close LEX or die "Could not close: $!";
260 # LEX_INTERPNORMAL test
266 open OUT4, ">Op_write.tmp" or die "Can't create Op_write.tmp";
268 close OUT4 or die "Could not close: $!";
269 if (`$CAT Op_write.tmp` eq "1\n") {
271 1 while unlink "Op_write.tmp";
284 open(OUT10, '>Op_write.tmp') || die "Can't create Op_write.tmp";
288 close OUT10 or die "Could not close: $!";
290 $right = " 12.95 00012.95\n";
291 if (`$CAT Op_write.tmp` eq $right)
292 { print "ok 10\n"; 1 while unlink 'Op_write.tmp'; }
294 { print "not ok 10\n"; }
307 open(OUT11, '>Op_write.tmp') || die "Can't create Op_write.tmp";
311 close OUT11 or die "Could not close: $!";
317 if (`$CAT Op_write.tmp` eq $right)
318 { print "ok 11\n"; 1 while unlink 'Op_write.tmp'; }
320 { print "not ok 11\n"; }
325 ok ^<<<<<<<<<<<<<<~~ # sv_chop() naze
328 my %hash = (12 => 3);
329 open(OUT12, '>Op_write.tmp') || die "Can't create Op_write.tmp";
331 for $el (keys %hash) {
334 close OUT12 or die "Could not close: $!";
335 print `$CAT Op_write.tmp`;
340 # Bug report and testcase by Alexey Tourbin
343 tie $v, 'Tie::StdScalar';
349 open(OUT13, '>Op_write.tmp') || die "Can't create Op_write.tmp";
351 close OUT13 or die "Could not close: $!";
352 print `$CAT Op_write.tmp`;
356 # Bug #24774 format without trailing \n failed assertion, but this
357 # must fail since we have a trailing ; in the eval'ed string (WL)
359 eval "format OUT14 = \n@\n\@v";
360 print $@ ? "ok 14\n" : "not ok 14\n";
365 # text lost in ^<<< field with \r in value (WL)
366 my $txt = "line 1\rline 2";
373 open(OUT15, '>Op_write.tmp') || die "Can't create Op_write.tmp";
375 close OUT15 or die "Could not close: $!";
376 my $res = `$CAT Op_write.tmp`;
377 print $res eq "line 1\nline 2\n" ? "ok 15\n" : "not ok 15\n";
380 { # test 16: multiple use of a variable in same line with ^<
381 my $txt = "this_is_block_1 this_is_block_2 this_is_block_3 this_is_block_4";
383 ^<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<
385 ^<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<
388 open(OUT16, '>Op_write.tmp') || die "Can't create Op_write.tmp";
390 close OUT16 or die "Could not close: $!";
391 my $res = `$CAT Op_write.tmp`;
392 print $res eq <<EOD ? "ok 16\n" : "not ok 16\n";
393 this_is_block_1 this_is_block_2
394 this_is_block_3 this_is_block_4
398 { # test 17: @* "should be on a line of its own", but it should work
399 # cleanly with literals before and after. (WL)
401 my $txt = "This is line 1.\nThis is the second line.\nThird and last.\n";
403 Here we go: @* That's all, folks!
406 open(OUT17, '>Op_write.tmp') || die "Can't create Op_write.tmp";
408 close OUT17 or die "Could not close: $!";
409 my $res = `$CAT Op_write.tmp`;
412 Here we go: $txt That's all, folks!
414 print $res eq $exp ? "ok 17\n" : "not ok 17\n";
417 { # test 18: @# and ~~ would cause runaway format, but we now
418 # catch this while compiling (WL)
424 open(OUT18, '>Op_write.tmp') || die "Can't create Op_write.tmp";
425 eval { write(OUT18); };
426 print $@ ? "ok 18\n" : "not ok 18\n";
427 close OUT18 or die "Could not close: $!";
430 { # test 19: \0 in an evel'ed format, doesn't cause empty lines (WL)
432 eval "format OUT19 = \n" .
437 open(OUT19, '>Op_write.tmp') || die "Can't create Op_write.tmp";
439 my $res = `$CAT Op_write.tmp`;
440 print $res eq <<EOD ? "ok 19\n" : "not ok 19\n";
446 { # test 20: hash accesses; single '}' must not terminate format '}' (WL)
447 my %h = ( xkey => 'xval', ykey => 'yval' );
459 while( my( $k, $v ) = each( %h ) ){
460 $exp .= sprintf( "%5s %s\n", $k, $v );
462 $exp .= sprintf( "%5s %s\n", $h{xkey}, $h{ykey} );
463 $exp .= sprintf( "%5s %s\n", $h{xkey}, $h{ykey} );
465 open(OUT20, '>Op_write.tmp') || die "Can't create Op_write.tmp";
467 my $res = `$CAT Op_write.tmp`;
468 print $res eq $exp ? "ok 20\n" : "not ok 20 res=[$res]exp=[$exp]\n";
474 #####################
476 ## numeric formatting
477 #####################
480 for my $tref ( @NumTests ){
481 my $writefmt = shift( @$tref );
482 my $printfmt = mkfmt( $writefmt );
483 my $blank_when_undef = substr( $writefmt, 0, 1 ) eq '^';
484 for my $val ( @$tref ){
485 my $writeres = swrite( $writefmt, $val );
487 if( $blank_when_undef && ! defined($val) ){
488 $printres = ' ' x length( $writefmt );
490 $printres = sprintf( $printfmt, $val || 0 );
491 if( length($printres) > length( $writefmt ) ){
492 $printres = '#' x length( $writefmt );
497 print $printres eq $writeres ? "ok $nt\n" : "not ok $nt\n";
502 #####################################
504 ## Easiest to add new tests above here
505 #######################################
507 # scary format testing from H.Merijn Brand
509 my $test = $bas_tests + $num_tests + 1;
510 my $tests = $bas_tests + $num_tests + $hmb_tests;
512 if ($^O eq 'VMS' || $^O eq 'MSWin32' || $^O eq 'dos' || $^O eq 'MacOS' ||
513 ($^O eq 'os2' and not eval '$OS2::can_fork')) {
514 foreach ($test..$tests) {
515 print "ok $_ # skipped: '|-' and '-|' not supported\n";
521 use strict; # Amazed that this hackery can be made strict ...
523 # Just a complete test for format, including top-, left- and bottom marging
524 # and format detection through glob entries
535 # [ID 20020227.005] format bug with undefined _TOP
537 open STDOUT_DUP, ">&STDOUT";
538 my $oldfh = select STDOUT_DUP;
540 { local $~ = "Comment";
544 ? "ok $test # TODO\n" : "not ok $test # TODO \$- = $- instead of 9\n";
546 print $^ eq "STDOUT_DUP_TOP"
547 ? "ok $test\n" : "not ok $test\n# \$^ = $^ instead of 'STDOUT_DUP_TOP'\n";
553 $= = 7; # Page length
555 my $ps = $^L; $^L = ""; # Catch the page separator
556 my $tm = 1; # Top margin (empty lines before first output)
557 my $bm = 2; # Bottom marging (empty lines between last text and footer)
558 my $lm = 4; # Left margin (indent in spaces)
560 select ((select (STDOUT), $| = 1)[0]);
561 if ($lm > 0 and !open STDOUT, "|-") { # Left margin (in this test ALWAYS set)
562 select ((select (STDOUT), $| = 1)[0]);
566 print + ($_ eq <DATA> ? "" : "not "), "ok ", $test++, "\n";
569 print + (<DATA>?"not ":""), "ok ", $test++, "\n";
574 $= -= $bm + 1; # count one for the trailing "----"
587 $% == 1 and return "";
589 $lastmin < $= and print "\n" x $lastmin;
590 print "\n" x $bm, "----\n", $ps;
595 # Yes, this is sick ;-)
605 @{(shift @E)||["",""]}
615 exists $::{$fmt} or return 0;
616 $^O eq "MSWin32" or return defined *{$::{$fmt}}{FORMAT};
617 open my $null, "> /dev/null" or die;
618 my $fh = select $null;
625 $^ = has_format ("TOP") ? "TOP" : "EMPTY";
626 has_format ("ENTRY") or die "No format defined for ENTRY";
627 foreach my $e ( [ map { [ $_, "Test$_" ] } 1 .. 7 ],
628 [ map { [ $_, "${_}tseT" ] } 1 .. 5 ]) {
632 has_format ("EOR") or next;
636 if (has_format ("EOF")) {