Re: [perl #34195] Regex: Alternations within negative lookahead assertions
[p5sagit/p5-mst-13.2.git] / t / op / array.t
index f307655..77ea646 100755 (executable)
@@ -1,8 +1,15 @@
 #!./perl
 
-# $RCSfile: array.t,v $$Revision: 4.1 $$Date: 92/08/07 18:27:37 $
+BEGIN {
+    chdir 't' if -d 't';
+    @INC = '../lib';
+}
+
+print "1..82\n";
 
-print "1..40\n";
+#
+# @foo, @bar, and @ary are also used from tie-stdarray after tie-ing them
+#
 
 @ary = (1,2,3,4,5);
 if (join('',@ary) eq '12345') {print "ok 1\n";} else {print "not ok 1\n";}
@@ -119,32 +126,174 @@ print $foo eq 'e' ? "ok 35\n" : "not ok 35\n";
 $foo = ('a','b','c','d','e','f')[1];
 print $foo eq 'b' ? "ok 36\n" : "not ok 36\n";
 
-# Test pseudo-hashes and %FIELDS. Real programs would "use fields..."
-# but we assign to %FIELDS manually since the real module tests come later.
+@foo = ( 'foo', 'bar', 'burbl');
+push(foo, 'blah');
+print $#foo == 3 ? "ok 37\n" : "not ok 37\n";
 
-BEGIN {
-    %Base::WithFields::FIELDS = (foo => 1, bar => 2, baz => 3, __MAX__ => 3);
-    %OtherBase::WithFields::FIELDS = (one => 1, two => 2, __MAX__ => 2);
+# various AASSIGN_COMMON checks (see newASSIGNOP() in op.c)
+
+$test = 37;
+sub t { ++$test; print "not " unless $_[0]; print "ok $test\n"; }
+
+@foo = @foo;
+t("@foo" eq "foo bar burbl blah");                             # 38
+
+(undef,@foo) = @foo;
+t("@foo" eq "bar burbl blah");                                 # 39
+
+@foo = ('XXX',@foo, 'YYY');
+t("@foo" eq "XXX bar burbl blah YYY");                         # 40
+
+@foo = @foo = qw(foo b\a\r bu\\rbl blah);
+t("@foo" eq 'foo b\a\r bu\\rbl blah');                         # 41
+
+@bar = @foo = qw(foo bar);                                     # 42
+t("@foo" eq "foo bar");
+t("@bar" eq "foo bar");                                                # 43
+
+# try the same with local
+# XXX tie-stdarray fails the tests involving local, so we use
+# different variable names to escape the 'tie'
+
+@bee = ( 'foo', 'bar', 'burbl', 'blah');
+{
+
+    local @bee = @bee;
+    t("@bee" eq "foo bar burbl blah");                         # 44
+    {
+       local (undef,@bee) = @bee;
+       t("@bee" eq "bar burbl blah");                          # 45
+       {
+           local @bee = ('XXX',@bee,'YYY');
+           t("@bee" eq "XXX bar burbl blah YYY");              # 46
+           {
+               local @bee = local(@bee) = qw(foo bar burbl blah);
+               t("@bee" eq "foo bar burbl blah");              # 47
+               {
+                   local (@bim) = local(@bee) = qw(foo bar);
+                   t("@bee" eq "foo bar");                     # 48
+                   t("@bim" eq "foo bar");                     # 49
+               }
+               t("@bee" eq "foo bar burbl blah");              # 50
+           }
+           t("@bee" eq "XXX bar burbl blah YYY");              # 51
+       }
+       t("@bee" eq "bar burbl blah");                          # 52
+    }
+    t("@bee" eq "foo bar burbl blah");                         # 53
 }
+
+# try the same with my
 {
-    package Base::WithoutFields;
+
+    my @bee = @bee;
+    t("@bee" eq "foo bar burbl blah");                         # 54
+    {
+       my (undef,@bee) = @bee;
+       t("@bee" eq "bar burbl blah");                          # 55
+       {
+           my @bee = ('XXX',@bee,'YYY');
+           t("@bee" eq "XXX bar burbl blah YYY");              # 56
+           {
+               my @bee = my @bee = qw(foo bar burbl blah);
+               t("@bee" eq "foo bar burbl blah");              # 57
+               {
+                   my (@bim) = my(@bee) = qw(foo bar);
+                   t("@bee" eq "foo bar");                     # 58
+                   t("@bim" eq "foo bar");                     # 59
+               }
+               t("@bee" eq "foo bar burbl blah");              # 60
+           }
+           t("@bee" eq "XXX bar burbl blah YYY");              # 61
+       }
+       t("@bee" eq "bar burbl blah");                          # 62
+    }
+    t("@bee" eq "foo bar burbl blah");                         # 63
 }
-@ISA = qw(Base::WithoutFields Base::WithFields);
-@k = sort keys %FIELDS;
-print "not " unless "@k" eq "__MAX__ bar baz foo";
-print "ok 37\n";
-eval {
-    @ISA = 'OtherBase::WithFields';
-};
-print "not " unless $@ =~ /Inherited %FIELDS can't override existing %FIELDS/;
-print "ok 38\n";
-undef %FIELDS;
-eval {
-    @ISA = qw(Base::WithFields OtherBase::WithFields);
-};
-print "not " unless $@ =~ /Can't multiply inherit %FIELDS/;
-print "ok 39\n";
 
-@foo = ( 'foo', 'bar', 'burbl');
-push(foo, 'blah');
-print $#foo == 3 ? "ok 40\n" : "not ok 40\n";
+# make sure reification behaves
+my $t = 63;
+sub reify { $_[1] = ++$t; print "@_\n"; }
+reify('ok');
+reify('ok');
+
+# qw() is no more a runtime split, it's compiletime.
+print "not " unless qw(foo bar snorfle)[2] eq 'snorfle';
+print "ok 66\n";
+
+@ary = (12,23,34,45,56);
+
+print "not " unless shift(@ary) == 12;
+print "ok 67\n";
+
+print "not " unless pop(@ary) == 56;
+print "ok 68\n";
+
+print "not " unless push(@ary,56) == 4;
+print "ok 69\n";
+
+print "not " unless unshift(@ary,12) == 5;
+print "ok 70\n";
+
+sub foo { "a" }
+@foo=(foo())[0,0];
+$foo[1] eq "a" or print "not ";
+print "ok 71\n";
+
+# $[ should have the same effect regardless of whether the aelem
+#    op is optimized to aelemfast.
+
+sub tary {
+  local $[ = 10;
+  my $five = 5;
+  print "not " unless $tary[5] == $tary[$five];
+  print "ok 72\n";
+}
+
+@tary = (0..50);
+tary();
+
+
+require './test.pl';
+
+# bugid #15439 - clearing an array calls destructors which may try
+# to modify the array - caused 'Attempt to free unreferenced scalar'
+
+my $got = runperl (
+       prog => q{
+                   sub X::DESTROY { @a = () }
+                   @a = (bless {}, 'X');
+                   @a = ();
+               },
+       stderr => 1
+    );
+
+$got =~ s/\n/ /g;
+print "# $got\nnot " unless $got eq '';
+print "ok 73\n";
+
+# Test negative and funky indices.
+
+{
+    my @a = 0..4;
+    print $a[-1] == 4 ? "ok 74\n" : "not ok 74\n";
+    print $a[-2] == 3 ? "ok 75\n" : "not ok 75\n";
+    print $a[-5] == 0 ? "ok 76\n" : "not ok 76\n";
+    print defined $a[-6] ? "not ok 77\n" : "ok 77\n";
+
+    print $a[2.1]   == 2 ? "ok 78\n" : "not ok 78\n";
+    print $a[2.9]   == 2 ? "ok 79\n" : "not ok 79\n";
+    print $a[undef] == 0 ? "ok 80\n" : "not ok 80\n";
+    print $a["3rd"] == 3 ? "ok 81\n" : "not ok 81\n";
+}
+
+sub kindalike { # TODO: test.pl-ize the array.t.
+    my ($s, $r, $m, $n) = @_;
+    print $s =~ /$r/ ? "ok $n - $m\n" : "not ok $n - $m ($s)\n";
+}
+
+{
+    my @a;
+    eval '$a[-1] = 0';
+    kindalike($@, qr/Modification of non-creatable array value attempted, subscript -1/, "\$a[-1] = 0", 82);
+}