This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
svleak.t: Disable crashing test
[perl5.git] / t / op / flip.t
old mode 100755 (executable)
new mode 100644 (file)
index 70666ac..0758623
@@ -1,21 +1,23 @@
 #!./perl
 
-# $RCSfile: flip.t,v $$Revision: 4.1 $$Date: 92/08/07 18:27:52 $
-
-chdir 't' if -d 't';
+BEGIN {
+    chdir 't' if -d 't';
+    require "./test.pl";
+}
 
-print "1..15\n";
+plan(14);
 
 @a = (1,2,3,4,5,6,7,8,9,10,11,12);
-
+@b = ();
 while ($_ = shift(@a)) {
-    if ($x = /4/../8/) { $z = $x; print "ok ", $x + 0, "\n"; }
+    if ($x = /4/../8/) { $z = $x; push @b, $x + 0; }
     $y .= /1/../2/;
 }
+is(join("*", @b), "1*2*3*4*5");
 
-if ($z eq '5E0') {print "ok 6\n";} else {print "not ok 6\n";}
+is($z, '5E0');
 
-if ($y eq '12E0123E0') {print "ok 7\n";} else {print "not ok 7\n";}
+is($y, '12E0123E0');
 
 @a = ('a','b','c','d','e','f','g');
 
@@ -28,41 +30,80 @@ while (<of>) {
 }
 $x = ($foo =~ y/\n/\n/);
 
-if ($x eq 3) {print "ok 8\n";} else {print "not ok 8 $x:$foo:\n";}
+is($x, 3);
 
 $x = 3.14;
-if (($x...$x) eq "1") {print "ok 9\n";} else {print "not ok 9\n";}
+ok(($x...$x) eq "1");
 
 {
-    # coredump reported in bug 20001018.008
+    # coredump reported in bug 20001018.008 (#4474)
     readline(UNKNOWN);
     $. = 1;
     $x = 1..10;
-    print "ok 10\n";
+    ok(1);
 }
 
 }
 
-if (!defined $.) { print "ok 11\n" } else { print "not ok 11 # $.\n" }
+ok(!defined $.);
 
 use warnings;
 my $warn='';
 $SIG{__WARN__} = sub { $warn .= join '', @_ };
 
-if (0..2) { print "ok 12\n" } else { print "not ok 12\n" }
+ok(scalar(0..2));
 
-if ($warn =~ /uninitialized/) { print "ok 13\n" } else { print "not ok 13\n" }
+like($warn, qr/uninitialized/);
 $warn = '';
 
 $x = "foo".."bar";
 
-if ((() = ($warn =~ /isn't numeric/g)) == 2) {
-    print "ok 14\n"
-}
-else {
-    print "not ok 14\n"
-}
+ok((() = ($warn =~ /isn't numeric/g)) == 2);
 $warn = '';
 
 $. = 15;
-if (15..0) { print "ok 15\n" } else { print "not ok 15\n" }
+ok(scalar(15..0));
+
+push @_, \scalar(0..0) for 1,2;
+isnt $_[0], $_[1], '\scalar($a..$b) gives a different scalar each time';
+
+# This evil little example from ticket #122829 abused the fact that each
+# recursion level maintained its own flip-flip state.  The following com-
+# ment describes how it *used* to work.
+
+# This routine maintains multiple flip-flop states, each with its own
+# numeric ID, starting from 1.  Pass the ID as the argument.
+sub f {
+    my $depth = shift() - 1;
+    return f($depth) if $depth;
+    return /3/../5/;
+}
+{
+    my $accumulator;
+    for(1..20) {
+        if (f(1)) {
+            my $outer = $_;
+            for(1..10){
+                $accumulator .= "$outer $_\n" if f(2);
+            }
+        }
+    }
+    is $accumulator, <<EOT, 'recursion shares state';
+3 1
+3 2
+3 3
+3 4
+3 5
+13 1
+13 2
+13 3
+13 4
+13 5
+EOT
+}
+
+# Void context gives parenthesized lhs scalar context
+no warnings 'void';
+sub c { $context = qw[ void scalar list ][wantarray + defined wantarray] }
+(c())x34;
+is $context, 'scalar', '(...)x... in void context';