This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
-M, -C, -A for File::stat.
[perl5.git] / t / op / delete.t
index 10a218b..493717e 100755 (executable)
@@ -1,6 +1,12 @@
 #!./perl
 
-print "1..36\n";
+BEGIN {
+    chdir 't' if -d 't';
+    @INC = qw(. ../lib);
+}
+
+require "test.pl";
+plan( tests => 38 );
 
 # delete() on hash elements
 
@@ -12,25 +18,25 @@ $foo{5} = 'e';
 
 $foo = delete $foo{2};
 
-if ($foo eq 'b') {print "ok 1\n";} else {print "not ok 1 $foo\n";}
-unless (exists $foo{2}) {print "ok 2\n";} else {print "not ok 2 $foo{2}\n";}
-if ($foo{1} eq 'a') {print "ok 3\n";} else {print "not ok 3\n";}
-if ($foo{3} eq 'c') {print "ok 4\n";} else {print "not ok 4\n";}
-if ($foo{4} eq 'd') {print "ok 5\n";} else {print "not ok 5\n";}
-if ($foo{5} eq 'e') {print "ok 6\n";} else {print "not ok 6\n";}
+cmp_ok($foo,'eq','b','delete 2');
+ok(!(exists $foo{2}),'b absent');
+cmp_ok($foo{1},'eq','a','a exists');
+cmp_ok($foo{3},'eq','c','c exists');
+cmp_ok($foo{4},'eq','d','d exists');
+cmp_ok($foo{5},'eq','e','e exists');
 
 @foo = delete @foo{4, 5};
 
-if (@foo == 2) {print "ok 7\n";} else {print "not ok 7 ", @foo+0, "\n";}
-if ($foo[0] eq 'd') {print "ok 8\n";} else {print "not ok 8 ", $foo[0], "\n";}
-if ($foo[1] eq 'e') {print "ok 9\n";} else {print "not ok 9 ", $foo[1], "\n";}
-unless (exists $foo{4}) {print "ok 10\n";} else {print "not ok 10 $foo{4}\n";}
-unless (exists $foo{5}) {print "ok 11\n";} else {print "not ok 11 $foo{5}\n";}
-if ($foo{1} eq 'a') {print "ok 12\n";} else {print "not ok 12\n";}
-if ($foo{3} eq 'c') {print "ok 13\n";} else {print "not ok 13\n";}
+cmp_ok(scalar(@foo),'==',2,'deleted slice');
+cmp_ok($foo[0],'eq','d','slice 1');
+cmp_ok($foo[1],'eq','e','slice 2');
+ok(!(exists $foo{4}),'d absent');
+ok(!(exists $foo{5}),'e absent');
+cmp_ok($foo{1},'eq','a','a still exists');
+cmp_ok($foo{3},'eq','c','c still exists');
 
 $foo = join('',values(%foo));
-if ($foo eq 'ac' || $foo eq 'ca') {print "ok 14\n";} else {print "not ok 14\n";}
+ok($foo eq 'ac' || $foo eq 'ca','remaining keys');
 
 foreach $key (keys %foo) {
     delete $foo{$key};
@@ -40,7 +46,7 @@ $foo{'foo'} = 'x';
 $foo{'bar'} = 'y';
 
 $foo = join('',values(%foo));
-print +($foo eq 'xy' || $foo eq 'yx') ? "ok 15\n" : "not ok 15\n";
+ok($foo eq 'xy' || $foo eq 'yx','fresh keys');
 
 $refhash{"top"}->{"foo"} = "FOO";
 $refhash{"top"}->{"bar"} = "BAR";
@@ -48,7 +54,7 @@ $refhash{"top"}->{"bar"} = "BAR";
 delete $refhash{"top"}->{"bar"};
 @list = keys %{$refhash{"top"}};
 
-print "@list" eq "foo" ? "ok 16\n" : "not ok 16 @list\n";
+cmp_ok("@list",'eq',"foo", 'autoviv and delete hashref');
 
 {
     my %a = ('bar', 33);
@@ -56,8 +62,7 @@ print "@list" eq "foo" ? "ok 16\n" : "not ok 16 @list\n";
     my $b = \$a{bar};
     my $c = \delete $a{bar};
 
-    print "not " unless $a == $b && $b == $c;
-    print "ok 17\n";
+    ok($a == $b && $b == $c,'a b c equivalent');
 }
 
 # delete() on array elements
@@ -71,46 +76,45 @@ $foo[5] = 'e';
 
 $foo = delete $foo[2];
 
-if ($foo eq 'b') {print "ok 18\n";} else {print "not ok 18 $foo\n";}
-unless (exists $foo[2]) {print "ok 19\n";} else {print "not ok 19 $foo[2]\n";}
-if ($foo[1] eq 'a') {print "ok 20\n";} else {print "not ok 20\n";}
-if ($foo[3] eq 'c') {print "ok 21\n";} else {print "not ok 21\n";}
-if ($foo[4] eq 'd') {print "ok 22\n";} else {print "not ok 22\n";}
-if ($foo[5] eq 'e') {print "ok 23\n";} else {print "not ok 23\n";}
+cmp_ok($foo,'eq','b','ary delete 2');
+ok(!(exists $foo[2]),'ary b absent');
+cmp_ok($foo[1],'eq','a','ary a exists');
+cmp_ok($foo[3],'eq','c','ary c exists');
+cmp_ok($foo[4],'eq','d','ary d exists');
+cmp_ok($foo[5],'eq','e','ary e exists');
 
 @bar = delete @foo[4,5];
 
-if (@bar == 2) {print "ok 24\n";} else {print "not ok 24 ", @bar+0, "\n";}
-if ($bar[0] eq 'd') {print "ok 25\n";} else {print "not ok 25 ", $bar[0], "\n";}
-if ($bar[1] eq 'e') {print "ok 26\n";} else {print "not ok 26 ", $bar[1], "\n";}
-unless (exists $foo[4]) {print "ok 27\n";} else {print "not ok 27 $foo[4]\n";}
-unless (exists $foo[5]) {print "ok 28\n";} else {print "not ok 28 $foo[5]\n";}
-if ($foo[1] eq 'a') {print "ok 29\n";} else {print "not ok 29\n";}
-if ($foo[3] eq 'c') {print "ok 30\n";} else {print "not ok 30\n";}
+cmp_ok(scalar(@bar),'==',2,'ary deleted slice');
+cmp_ok($bar[0],'eq','d','ary slice 1');
+cmp_ok($bar[1],'eq','e','ary slice 2');
+ok(!(exists $foo[4]),'ary d absent');
+ok(!(exists $foo[5]),'ary e absent');
+cmp_ok($foo[1],'eq','a','ary a still exists');
+cmp_ok($foo[3],'eq','c','ary c still exists');
 
 $foo = join('',@foo);
-if ($foo eq 'ac') {print "ok 31\n";} else {print "not ok 31\n";}
-
-if (@foo == 4) {print "ok 32\n";} else {print "not ok 32\n";}
+cmp_ok($foo,'eq','ac','ary elems');
+cmp_ok(scalar(@foo),'==',4,'four is the number thou shalt count');
 
 foreach $key (0 .. $#foo) {
     delete $foo[$key];
 }
 
-if (@foo == 0) {print "ok 33\n";} else {print "not ok 33\n";}
+cmp_ok(scalar(@foo),'==',0,'and then there were none');
 
 $foo[0] = 'x';
 $foo[1] = 'y';
 
 $foo = "@foo";
-print +($foo eq 'x y') ? "ok 34\n" : "not ok 34\n";
+cmp_ok($foo,'eq','x y','two fresh');
 
 $refary[0]->[0] = "FOO";
 $refary[0]->[3] = "BAR";
 
 delete $refary[0]->[3];
 
-print @{$refary[0]} == 1 ? "ok 35\n" : "not ok 35 @list\n";
+cmp_ok( scalar(@{$refary[0]}),'==',1,'one down');
 
 {
     my @a = 33;
@@ -118,6 +122,22 @@ print @{$refary[0]} == 1 ? "ok 35\n" : "not ok 35 @list\n";
     my $b = \$a[0];
     my $c = \delete $a[bar];
 
-    print "not " unless $a == $b && $b == $c;
-    print "ok 36\n";
+    ok($a == $b && $b == $c,'a b c also equivalent');
+}
+
+{
+    my %h;
+    my ($x,$y) = (1, scalar delete @h{()});
+    ok(!defined($y),q([perl #29127] scalar delete of empty slice returned garbage));
+}
+
+{
+    my $x = 0;
+    sub X::DESTROY { $x++ }
+    {
+       my @a;
+       $a[0] = bless [], 'X';
+       my $y = delete $a[0];
+    }
+    cmp_ok($x,'==',1,q([perl #30733] array delete didn't free returned element));
 }