8 #-- testing numeric fields in all variants (WL)
12 local $^A = ""; # don't litter, use a local bin
13 formline( $format, @_ );
18 # [ format, value1, expected1, value2, expected2, .... ]
19 [ '@###', 0, ' 0', 1, ' 1', 9999.5, '####',
20 9999.4999, '9999', -999.5, '####', 1e+100, '####' ],
22 [ '@0##', 0, '0000', 1, '0001', 9999.5, '####',
23 -999.4999, '-999', -999.5, '####', 1e+100, '####' ],
25 [ '^###', 0, ' 0', undef, ' ' ],
27 [ '^0##', 0, '0000', undef, ' ' ],
29 [ '@###.', 0, ' 0.', 1, ' 1.', 9999.5, '#####',
30 9999.4999, '9999.', -999.5, '#####' ],
32 [ '@##.##', 0, ' 0.00', 1, ' 1.00', 999.995, '######',
33 999.99499, '999.99', -100, '######' ],
35 [ '@0#.##', 0, '000.00', 1, '001.00', 10, '010.00',
36 -0.0001, qr/^[\-0]00\.00$/ ],
42 for my $tref ( @NumTests ){
43 $num_tests += (@$tref - 1)/2;
45 #---------------------------------------------------------
47 # number of tests in section 1
50 # number of tests in section 3
53 printf "1..%d\n", $bas_tests + $num_tests + $hmb_tests;
55 my $CAT = ($^O eq 'MSWin32' || $^O eq 'NetWare' || $^O eq 'VMS') ? 'type'
56 : ($^O eq 'MacOS') ? 'catenate'
75 now @<<the@>>>> for all@|||||men to come @<<<<
77 'i' . 's', "time\n", $good, 'to'
81 open(OUT, '>Op_write.tmp') || die "Can't create Op_write.tmp";
82 END { 1 while unlink 'Op_write.tmp' }
86 $multiline = "forescore\nand\nseven years\n";
87 $foo = 'when in the course of human events it becomes necessary';
89 close OUT or die "Could not close: $!";
100 now is the time for all good men to come to\n";
102 if (`$CAT Op_write.tmp` eq $right)
103 { print "ok 1\n"; 1 while unlink 'Op_write.tmp'; }
105 { print "not ok 1\n"; }
107 $fox = 'wolfishness';
108 my $fox = 'foxiness'; # Test a lexical variable.
118 now @<<the@>>>> for all@|||||men to come @<<<<
119 'i' . 's', "time\n", $good, 'to'
122 open OUT2, '>Op_write.tmp' or die "Can't create Op_write.tmp";
125 $multiline = "forescore\nand\nseven years\n";
126 $foo = 'when in the course of human events it becomes necessary';
128 close OUT2 or die "Could not close: $!";
142 now is the time for all good men to come to\n";
144 if (`$CAT Op_write.tmp` eq $right)
145 { print "ok 2\n"; 1 while unlink 'Op_write.tmp'; }
147 { print "not ok 2\n"; }
159 now @<<the@>>>> for all@|||||men to come @<<<<
160 'i' . 's', "time\n", $good, 'to'
164 open(OUT2, '>Op_write.tmp') || die "Can't create Op_write.tmp";
168 $multiline = "forescore\nand\nseven years\n";
169 $foo = 'when in the course of human events it becomes necessary';
171 close OUT2 or die "Could not close: $!";
186 now is the time for all good men to come to\n";
188 if (`$CAT Op_write.tmp` eq $right)
189 { print "ok 3\n"; 1 while unlink 'Op_write.tmp'; }
191 { print "not ok 3\n"; }
213 my $format1 = '@' . '>' x $_;
214 formline $format1, 'abc';
215 $was1 .= "$format1 $^A\n";
218 local $format2 = '@' . '>' x $_;
219 formline $format2, 'abc';
220 $was2 .= "$format2 $^A\n";
222 print $was1 eq $mustbe ? "ok 4\n" : "not ok 4\n";
223 print $was2 eq $mustbe ? "ok 5\n" : "not ok 5\n";
234 open(OUT3, '>Op_write.tmp') || die "Can't create Op_write.tmp";
238 close OUT3 or die "Could not close: $!";
243 if (`$CAT Op_write.tmp` eq $right)
244 { print "ok 6\n"; 1 while unlink 'Op_write.tmp'; }
246 { print "not ok 6\n"; }
248 # test lexicals and globals
256 open(LEX, ">&STDOUT") or die;
260 close LEX or die "Could not close: $!";
262 # LEX_INTERPNORMAL test
268 open OUT4, ">Op_write.tmp" or die "Can't create Op_write.tmp";
270 close OUT4 or die "Could not close: $!";
271 if (`$CAT Op_write.tmp` eq "1\n") {
273 1 while unlink "Op_write.tmp";
286 open(OUT10, '>Op_write.tmp') || die "Can't create Op_write.tmp";
290 close OUT10 or die "Could not close: $!";
292 $right = " 12.95 00012.95\n";
293 if (`$CAT Op_write.tmp` eq $right)
294 { print "ok 10\n"; 1 while unlink 'Op_write.tmp'; }
296 { print "not ok 10\n"; }
309 open(OUT11, '>Op_write.tmp') || die "Can't create Op_write.tmp";
313 close OUT11 or die "Could not close: $!";
319 if (`$CAT Op_write.tmp` eq $right)
320 { print "ok 11\n"; 1 while unlink 'Op_write.tmp'; }
322 { print "not ok 11\n"; }
327 ok ^<<<<<<<<<<<<<<~~ # sv_chop() naze
330 my %hash = (12 => 3);
331 open(OUT12, '>Op_write.tmp') || die "Can't create Op_write.tmp";
333 for $el (keys %hash) {
336 close OUT12 or die "Could not close: $!";
337 print `$CAT Op_write.tmp`;
342 # Bug report and testcase by Alexey Tourbin
345 tie $v, 'Tie::StdScalar';
351 open(OUT13, '>Op_write.tmp') || die "Can't create Op_write.tmp";
353 close OUT13 or die "Could not close: $!";
354 print `$CAT Op_write.tmp`;
358 # Bug #24774 format without trailing \n failed assertion, but this
359 # must fail since we have a trailing ; in the eval'ed string (WL)
361 eval "format OUT14 = \n@\n\@v";
362 print $@ ? "ok 14\n" : "not ok 14\n";
367 # text lost in ^<<< field with \r in value (WL)
368 my $txt = "line 1\rline 2";
375 open(OUT15, '>Op_write.tmp') || die "Can't create Op_write.tmp";
377 close OUT15 or die "Could not close: $!";
378 my $res = `$CAT Op_write.tmp`;
379 print $res eq "line 1\nline 2\n" ? "ok 15\n" : "not ok 15\n";
382 { # test 16: multiple use of a variable in same line with ^<
383 my $txt = "this_is_block_1 this_is_block_2 this_is_block_3 this_is_block_4";
385 ^<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<
387 ^<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<
390 open(OUT16, '>Op_write.tmp') || die "Can't create Op_write.tmp";
392 close OUT16 or die "Could not close: $!";
393 my $res = `$CAT Op_write.tmp`;
394 print $res eq <<EOD ? "ok 16\n" : "not ok 16\n";
395 this_is_block_1 this_is_block_2
396 this_is_block_3 this_is_block_4
400 { # test 17: @* "should be on a line of its own", but it should work
401 # cleanly with literals before and after. (WL)
403 my $txt = "This is line 1.\nThis is the second line.\nThird and last.\n";
405 Here we go: @* That's all, folks!
408 open(OUT17, '>Op_write.tmp') || die "Can't create Op_write.tmp";
410 close OUT17 or die "Could not close: $!";
411 my $res = `$CAT Op_write.tmp`;
414 Here we go: $txt That's all, folks!
416 print $res eq $exp ? "ok 17\n" : "not ok 17\n";
419 { # test 18: @# and ~~ would cause runaway format, but we now
420 # catch this while compiling (WL)
426 open(OUT18, '>Op_write.tmp') || die "Can't create Op_write.tmp";
427 eval { write(OUT18); };
428 print $@ ? "ok 18\n" : "not ok 18\n";
429 close OUT18 or die "Could not close: $!";
432 { # test 19: \0 in an evel'ed format, doesn't cause empty lines (WL)
434 eval "format OUT19 = \n" .
439 open(OUT19, '>Op_write.tmp') || die "Can't create Op_write.tmp";
441 my $res = `$CAT Op_write.tmp`;
442 print $res eq <<EOD ? "ok 19\n" : "not ok 19\n";
448 { # test 20: hash accesses; single '}' must not terminate format '}' (WL)
449 my %h = ( xkey => 'xval', ykey => 'yval' );
461 while( my( $k, $v ) = each( %h ) ){
462 $exp .= sprintf( "%5s %s\n", $k, $v );
464 $exp .= sprintf( "%5s %s\n", $h{xkey}, $h{ykey} );
465 $exp .= sprintf( "%5s %s\n", $h{xkey}, $h{ykey} );
467 open(OUT20, '>Op_write.tmp') || die "Can't create Op_write.tmp";
469 my $res = `$CAT Op_write.tmp`;
470 print $res eq $exp ? "ok 20\n" : "not ok 20 res=[$res]exp=[$exp]\n";
476 #####################
478 ## numeric formatting
479 #####################
482 for my $tref ( @NumTests ){
483 my $writefmt = shift( @$tref );
485 my $val = shift @$tref;
486 my $expected = shift @$tref;
487 my $writeres = swrite( $writefmt, $val );
489 my $ok = ref($expected)
490 ? $writeres =~ $expected
491 : $writeres eq $expected;
495 : "not ok $nt\n# f=[$writefmt] exp=[$expected] got=[$writeres]\n";
500 #####################################
502 ## Easiest to add new tests above here
503 #######################################
505 # scary format testing from H.Merijn Brand
507 my $test = $bas_tests + $num_tests + 1;
508 my $tests = $bas_tests + $num_tests + $hmb_tests;
510 if ($^O eq 'VMS' || $^O eq 'MSWin32' || $^O eq 'dos' || $^O eq 'MacOS' ||
511 ($^O eq 'os2' and not eval '$OS2::can_fork')) {
512 foreach ($test..$tests) {
513 print "ok $_ # skipped: '|-' and '-|' not supported\n";
519 use strict; # Amazed that this hackery can be made strict ...
521 # Just a complete test for format, including top-, left- and bottom marging
522 # and format detection through glob entries
533 # [ID 20020227.005] format bug with undefined _TOP
535 open STDOUT_DUP, ">&STDOUT";
536 my $oldfh = select STDOUT_DUP;
538 { local $~ = "Comment";
542 ? "ok $test # TODO\n" : "not ok $test # TODO \$- = $- instead of 9\n";
544 print $^ eq "STDOUT_DUP_TOP"
545 ? "ok $test\n" : "not ok $test\n# \$^ = $^ instead of 'STDOUT_DUP_TOP'\n";
551 $= = 7; # Page length
553 my $ps = $^L; $^L = ""; # Catch the page separator
554 my $tm = 1; # Top margin (empty lines before first output)
555 my $bm = 2; # Bottom marging (empty lines between last text and footer)
556 my $lm = 4; # Left margin (indent in spaces)
558 select ((select (STDOUT), $| = 1)[0]);
559 if ($lm > 0 and !open STDOUT, "|-") { # Left margin (in this test ALWAYS set)
560 select ((select (STDOUT), $| = 1)[0]);
564 print + ($_ eq <DATA> ? "" : "not "), "ok ", $test++, "\n";
567 print + (<DATA>?"not ":""), "ok ", $test++, "\n";
572 $= -= $bm + 1; # count one for the trailing "----"
585 $% == 1 and return "";
587 $lastmin < $= and print "\n" x $lastmin;
588 print "\n" x $bm, "----\n", $ps;
593 # Yes, this is sick ;-)
603 @{(shift @E)||["",""]}
613 exists $::{$fmt} or return 0;
614 $^O eq "MSWin32" or return defined *{$::{$fmt}}{FORMAT};
615 open my $null, "> /dev/null" or die;
616 my $fh = select $null;
623 $^ = has_format ("TOP") ? "TOP" : "EMPTY";
624 has_format ("ENTRY") or die "No format defined for ENTRY";
625 foreach my $e ( [ map { [ $_, "Test$_" ] } 1 .. 7 ],
626 [ map { [ $_, "${_}tseT" ] } 1 .. 5 ]) {
630 has_format ("EOR") or next;
634 if (has_format ("EOF")) {