9 use strict; # Amazed that this hackery can be made strict ...
15 open my $fh, $file or die "can't open '$file': $!";
21 #-- testing numeric fields in all variants (WL)
25 local $^A = ""; # don't litter, use a local bin
26 formline( $format, @_ );
31 # [ format, value1, expected1, value2, expected2, .... ]
32 [ '@###', 0, ' 0', 1, ' 1', 9999.6, '####',
33 9999.4999, '9999', -999.6, '####', 1e+100, '####' ],
35 [ '@0##', 0, '0000', 1, '0001', 9999.6, '####',
36 -999.4999, '-999', -999.6, '####', 1e+100, '####' ],
38 [ '^###', 0, ' 0', undef, ' ' ],
40 [ '^0##', 0, '0000', undef, ' ' ],
42 [ '@###.', 0, ' 0.', 1, ' 1.', 9999.6, '#####',
43 9999.4999, '9999.', -999.6, '#####' ],
45 [ '@##.##', 0, ' 0.00', 1, ' 1.00', 999.996, '######',
46 999.99499, '999.99', -100, '######' ],
48 [ '@0#.##', 0, '000.00', 1, '001.00', 10, '010.00',
49 -0.0001, qr/^[\-0]00\.00$/ ],
55 for my $tref ( @NumTests ){
56 $num_tests += (@$tref - 1)/2;
58 #---------------------------------------------------------
60 # number of tests in section 1
63 # number of tests in section 3
64 my $bug_tests = 4 + 3 * 3 * 5 * 2 * 3 + 2 + 66 + 4 + 2 + 1 + 1;
66 # number of tests in section 4
69 my $tests = $bas_tests + $num_tests + $bug_tests + $hmb_tests;
77 use vars qw($fox $multiline $foo $good);
91 now @<<the@>>>> for all@|||||men to come @<<<<
93 'i' . 's', "time\n", $good, 'to'
97 open(OUT, '>Op_write.tmp') || die "Can't create Op_write.tmp";
98 END { unlink_all 'Op_write.tmp' }
102 $multiline = "forescore\nand\nseven years\n";
103 $foo = 'when in the course of human events it becomes necessary';
105 close OUT or die "Could not close: $!";
116 now is the time for all good men to come to\n";
118 is cat('Op_write.tmp'), $right and unlink_all 'Op_write.tmp';
120 $fox = 'wolfishness';
121 my $fox = 'foxiness'; # Test a lexical variable.
131 now @<<the@>>>> for all@|||||men to come @<<<<
132 'i' . 's', "time\n", $good, 'to'
135 open OUT2, '>Op_write.tmp' or die "Can't create Op_write.tmp";
138 $multiline = "forescore\nand\nseven years\n";
139 $foo = 'when in the course of human events it becomes necessary';
141 close OUT2 or die "Could not close: $!";
155 now is the time for all good men to come to\n";
157 is cat('Op_write.tmp'), $right and unlink_all 'Op_write.tmp';
169 now @<<the@>>>> for all@|||||men to come @<<<<
170 'i' . 's', "time\n", $good, 'to'
174 open(OUT2, '>Op_write.tmp') || die "Can't create Op_write.tmp";
178 $multiline = "forescore\nand\nseven years\n";
179 $foo = 'when in the course of human events it becomes necessary';
181 close OUT2 or die "Could not close: $!";
196 now is the time for all good men to come to\n";
198 is cat('Op_write.tmp'), $right and unlink_all 'Op_write.tmp';
216 my $was1 = my $was2 = '';
221 my $format1 = '@' . '>' x $_;
222 formline $format1, 'abc';
223 $was1 .= "$format1 $^A\n";
226 local $format2 = '@' . '>' x $_;
227 formline $format2, 'abc';
228 $was2 .= "$format2 $^A\n";
242 open(OUT3, '>Op_write.tmp') || die "Can't create Op_write.tmp";
246 close OUT3 or die "Could not close: $!";
251 is cat('Op_write.tmp'), $right and unlink_all 'Op_write.tmp';
254 # test lexicals and globals
256 my $test = curr_test();
263 open(LEX, ">&STDOUT") or die;
267 close LEX or die "Could not close: $!";
268 curr_test($test + 1);
270 # LEX_INTERPNORMAL test
276 open OUT4, ">Op_write.tmp" or die "Can't create Op_write.tmp";
278 close OUT4 or die "Could not close: $!";
279 is cat('Op_write.tmp'), "1\n" and unlink_all "Op_write.tmp";
288 open(OUT10, '>Op_write.tmp') || die "Can't create Op_write.tmp";
293 close OUT10 or die "Could not close: $!";
295 $right = " 12.95 00012.95\n";
296 is cat('Op_write.tmp'), $right and unlink_all 'Op_write.tmp';
309 open(OUT11, '>Op_write.tmp') || die "Can't create Op_write.tmp";
313 close OUT11 or die "Could not close: $!";
319 is cat('Op_write.tmp'), $right and unlink_all 'Op_write.tmp';
322 my $test = curr_test();
325 ok ^<<<<<<<<<<<<<<~~ # sv_chop() naze
328 my %hash = ($test => 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');
336 curr_test($test + 1);
340 my $test = curr_test();
341 # Bug report and testcase by Alexey Tourbin
344 tie $v, 'Tie::StdScalar';
350 open(OUT13, '>Op_write.tmp') || die "Can't create Op_write.tmp";
352 close OUT13 or die "Could not close: $!";
353 print cat('Op_write.tmp');
354 curr_test($test + 1);
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 like $@, qr/Format not terminated/;
366 # text lost in ^<<< field with \r in value (WL)
367 my $txt = "line 1\rline 2";
374 open(OUT15, '>Op_write.tmp') || die "Can't create Op_write.tmp";
376 close OUT15 or die "Could not close: $!";
377 my $res = cat('Op_write.tmp');
378 is $res, "line 1\nline 2\n";
381 { # test 16: multiple use of a variable in same line with ^<
382 my $txt = "this_is_block_1 this_is_block_2 this_is_block_3 this_is_block_4";
384 ^<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<
386 ^<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<
389 open(OUT16, '>Op_write.tmp') || die "Can't create Op_write.tmp";
391 close OUT16 or die "Could not close: $!";
392 my $res = cat('Op_write.tmp');
394 this_is_block_1 this_is_block_2
395 this_is_block_3 this_is_block_4
399 { # test 17: @* "should be on a line of its own", but it should work
400 # cleanly with literals before and after. (WL)
402 my $txt = "This is line 1.\nThis is the second line.\nThird and last.\n";
404 Here we go: @* That's all, folks!
407 open(OUT17, '>Op_write.tmp') || die "Can't create Op_write.tmp";
409 close OUT17 or die "Could not close: $!";
410 my $res = cat('Op_write.tmp');
413 Here we go: $txt That's all, folks!
418 { # test 18: @# and ~~ would cause runaway format, but we now
419 # catch this while compiling (WL)
425 open(OUT18, '>Op_write.tmp') || die "Can't create Op_write.tmp";
426 eval { write(OUT18); };
427 like $@, qr/Repeated format line will never terminate/;
428 close OUT18 or die "Could not close: $!";
431 { # test 19: \0 in an evel'ed format, doesn't cause empty lines (WL)
433 eval "format OUT19 = \n" .
438 open(OUT19, '>Op_write.tmp') || die "Can't create Op_write.tmp";
440 close OUT19 or die "Could not close: $!";
441 my $res = cat('Op_write.tmp');
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 close OUT20 or die "Could not close: $!";
470 my $res = cat('Op_write.tmp');
475 #####################
477 ## numeric formatting
478 #####################
480 curr_test($bas_tests + 1);
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 like $writeres, $expected, $writefmt;
491 is $writeres, $expected, $writefmt;
497 #####################################
499 ## Easiest to add new tests just here
500 #####################################
502 # DAPM. Exercise a couple of error codepaths
507 like $@, qr/Not a format reference/, 'format reference';
511 like $@, qr/Undefined format/, 'no such format';
519 bless [shift, 0, 0], $class;
536 my ($pound_utf8, $pm_utf8) = map { my $a = "$_\x{100}"; chop $a; $a}
537 my ($pound, $pm) = ("\xA3", "\xB1");
539 foreach my $first ('N', $pound, $pound_utf8) {
540 foreach my $base ('N', $pm, $pm_utf8) {
541 foreach my $second ($base, "$base\n", "$base\nMoo!", "$base\nMoo!\n",
543 foreach (['^*', qr/(.+)/], ['@*', qr/(.*?)$/s]) {
544 my ($format, $re) = @$_;
545 $format = "1^*2 3${format}4";
546 foreach my $class ('', 'Count') {
547 my $name = qq{swrite("$format", "$first", "$second") class="$class"};
550 ord($1) > 126 ? sprintf("\\x{%x}",ord($1)) : $1
553 $first =~ /(.+)/ or die $first;
554 my $expect = "1${1}2";
555 $second =~ $re or die $second;
556 $expect .= " 3${1}4";
561 tie $copy2, $class, $second;
562 is swrite("$format", $copy1, $copy2), $expect, $name;
563 my $obj = tied $copy2;
564 is $obj->[1], 1, 'value read exactly once';
566 my ($copy1, $copy2) = ($first, $second);
567 is swrite("$format", $copy1, $copy2), $expect, $name;
577 # This will fail an assertion in 5.10.0 built with -DDEBUGGING (because
578 # pp_formline attempts to set SvCUR() on an SVt_RV). I suspect that it will
579 # be doing something similarly out of bounds on everything from 5.000
581 is swrite('>^*<', $ref), ">$ref<";
582 is swrite('>@*<', $ref), ">$ref<";
588 my $test = curr_test();
596 # [ID 20020227.005] format bug with undefined _TOP
598 open STDOUT_DUP, ">&STDOUT";
599 my $oldfh = select STDOUT_DUP;
602 local $~ = "Comment";
604 curr_test($test + 1);
606 local $::TODO = '[ID 20020227.005] format bug with undefined _TOP';
609 is $^, "STDOUT_DUP_TOP";
614 *CmT = *{$::{Comment}}{FORMAT};
615 ok defined *{$::{CmT}}{FORMAT}, "glob assign";
618 # RT #91032: Check that "non-real" strings like tie and overload work,
619 # especially that they re-compile the pattern on each FETCH, and that
620 # they don't overrun the buffer
626 sub TIESCALAR { bless [] }
628 sub FETCH { $i++; "A$i @> Z\n" }
630 use overload '""' => \&FETCH;
632 tie my $f, 'RT91032';
636 ::is $^A, "A1 a Z\nA2 bc Z\n", "RT 91032: tied";
639 my $g = bless []; # has overloaded stringify
642 ::is $^A, "A3 de Z\nA4 f Z\n", "RT 91032: overloaded";
646 formline $h, "junk1";
647 formline $h, "junk2";
648 ::is ref($h), 'ARRAY', "RT 91032: array ref still a ref";
649 ::like "$h", qr/^ARRAY\(0x[0-9a-f]+\)$/, "RT 91032: array stringifies ok";
650 ::is $^A, "$h$h","RT 91032: stringified array";
653 # used to overwrite the ~~ in the *original SV with spaces. Naughty!
655 my $orig = my $format = "^<<<<< ~~\n";
657 formline $format, $abc;
659 ::is $format, $orig, "RT91032: don't overwrite orig format string";
661 # check that ~ and ~~ are displayed correctly as whitespace,
662 # under the influence of various different types of border
665 for my $lhs (' ', 'Y', '^<<<', '^|||', '^>>>') {
666 for my $rhs ('', ' ', 'Z', '^<<<', '^|||', '^>>>') {
667 my $fmt = "^<B$lhs" . ('~' x $n) . "$rhs\n";
668 my $sfmt = ($fmt =~ s/~/ /gr);
670 ($a, $bc, $stop) = ('a', 'bc', 's');
671 # $stop is to stop '~~' deleting the whole line
672 formline $sfmt, $stop, $a, $bc;
675 ($a, $bc, $stop) = ('a', 'bc', 's');
676 formline $fmt, $stop, $a, $bc;
680 ::is($got, $exp, "chop munging: [$fmt]");
686 # check that '~ (delete current line if empty) works when
687 # the target gets upgraded to uft8 (and re-allocated) midstream.
690 my $format = "\x{100}@~\n"; # format is utf8
691 # this target is not utf8, but will expand (and get reallocated)
692 # when upgraded to utf8.
693 my $orig = "\x80\x81\x82";
696 formline $format, $empty;
697 is $^A , $orig, "~ and realloc";
699 # check similarly that trailing blank removal works ok
701 $format = "@<\n\x{100}"; # format is utf8
705 formline $format, " ";
706 is $^A, "$orig\n", "end-of-line blanks and realloc";
708 # and check this doesn't overflow the buffer
711 $format = "@* @####\n";
712 $orig = "x" x 100 . "\n";
713 formline $format, $orig, 12345;
714 is $^A, ("x" x 100) . " 12345\n", "\@* doesn't overflow";
716 # make sure it can cope with formats > 64k
718 $format = 'x' x 65537;
721 # don't use 'is' here, as the diag output will be too long!
722 ok $^A eq $format, ">64K";
727 skip_if_miniperl('miniperl does not support scalario');
729 open my $fh, ">", \$buf;
730 my $old_fh = select $fh;
735 is $buf, "ok $test\n", "write to duplicated format";
738 fresh_perl_like(<<'EOP', qr/^Format STDOUT redefined at/, {stderr => 1}, '#64562 - Segmentation fault with redefined formats and warnings');
742 use warnings; # crashes!
755 fresh_perl_is(<<'EOP', ">ARRAY<\ncrunch_eth\n", {stderr => 1}, '#79532 - formline coerces its arguments');
758 my $zamm = ['crunch_eth'];
760 printf ">%s<\n", ref $zamm;
761 print "$zamm->[0]\n";
764 #############################
766 ## Add new tests *above* here
767 #############################
769 # scary format testing from H.Merijn Brand
771 # Just a complete test for format, including top-, left- and bottom marging
772 # and format detection through glob entries
774 if ($^O eq 'VMS' || $^O eq 'MSWin32' || $^O eq 'dos' ||
775 ($^O eq 'os2' and not eval '$OS2::can_fork')) {
778 skip "'|-' and '-|' not supported", $tests - $test + 1;
785 $= = 7; # Page length
787 my $ps = $^L; $^L = ""; # Catch the page separator
788 my $tm = 1; # Top margin (empty lines before first output)
789 my $bm = 2; # Bottom marging (empty lines between last text and footer)
790 my $lm = 4; # Left margin (indent in spaces)
792 # -----------------------------------------------------------------------
794 # execute the rest of the script in a child process. The parent reads the
795 # output from the child and compares it with <DATA>.
799 select ((select (STDOUT), $| = 1)[0]); # flush STDOUT
801 my $opened = open FROM_CHILD, "-|";
802 unless (defined $opened) {
812 while (<FROM_CHILD>) {
814 fail 'too much output';
818 my $exp = shift @data;
822 is "@data", "", "correct length of output";
829 select ((select (STDOUT), $| = 1)[0]);
831 $= -= $bm + 1; # count one for the trailing "----"
844 $% == 1 and return "";
846 $lastmin < $= and print "\n" x $lastmin;
847 print "\n" x $bm, "----\n", $ps;
852 # Yes, this is sick ;-)
862 @{(shift @E)||["",""]}
872 exists $::{$fmt} or return 0;
873 $^O eq "MSWin32" or return defined *{$::{$fmt}}{FORMAT};
874 open my $null, "> /dev/null" or die;
875 my $fh = select $null;
882 $^ = has_format ("TOP") ? "TOP" : "EMPTY";
883 has_format ("ENTRY") or die "No format defined for ENTRY";
884 foreach my $e ( [ map { [ $_, "Test$_" ] } 1 .. 7 ],
885 [ map { [ $_, "${_}tseT" ] } 1 .. 5 ]) {
889 has_format ("EOR") or next;
893 if (has_format ("EOF")) {