summaryrefslogtreecommitdiff
path: root/t
diff options
context:
space:
mode:
authorNicholas Clark <nick@ccl4.org>2004-07-14 10:40:54 +0000
committerNicholas Clark <nick@ccl4.org>2004-07-14 10:40:54 +0000
commit4d46db716459905a58c7a86181b69d8dadaf4951 (patch)
treea7c993dd53a6b1a350291a386cd0c8c31c701890 /t
parenta5c75c1d181494e06a58988f267a8b3d081df483 (diff)
downloadperl-4d46db716459905a58c7a86181b69d8dadaf4951.tar.gz
Lots of tests for for reverse ...
p4raw-id: //depot/perl@23105
Diffstat (limited to 't')
-rwxr-xr-xt/cmd/for.t255
1 files changed, 254 insertions, 1 deletions
diff --git a/t/cmd/for.t b/t/cmd/for.t
index 27fb5a2517..19fab8ce6e 100755
--- a/t/cmd/for.t
+++ b/t/cmd/for.t
@@ -1,6 +1,6 @@
#!./perl
-print "1..14\n";
+print "1..58\n";
for ($i = 0; $i <= 10; $i++) {
$x[$i] = $i;
@@ -95,3 +95,256 @@ print $@ =~ /Use of freed value in iteration/ ? "ok" : "not ok", " 13\n";
print $x == 1 ? "ok" : "not ok", " 14 - double destroy, x=$x\n";
}
+# A lot of tests to check that reversed for works.
+my $test = 14;
+sub is {
+ my ($got, $expected, $name) = @_;
+ ++$test;
+ if ($got eq $expected) {
+ print "ok $test # $name\n";
+ return 1;
+ }
+ print "not ok $test # $name\n";
+ print "# got '$got', expected '$expected'\n";
+ return 0;
+}
+
+@array = ('A', 'B', 'C');
+for (@array) {
+ $r .= $_;
+}
+is ($r, 'ABC', 'Forwards for array');
+$r = '';
+for (1,2,3) {
+ $r .= $_;
+}
+is ($r, '123', 'Forwards for list');
+$r = '';
+for (map {$_} @array) {
+ $r .= $_;
+}
+is ($r, 'ABC', 'Forwards for array via map');
+$r = '';
+for (map {$_} 1,2,3) {
+ $r .= $_;
+}
+is ($r, '123', 'Forwards for list via map');
+
+$r = '';
+for (reverse @array) {
+ $r .= $_;
+}
+is ($r, 'CBA', 'Reverse for array');
+$r = '';
+for (reverse 1,2,3) {
+ $r .= $_;
+}
+is ($r, '321', 'Reverse for list');
+$r = '';
+for (reverse map {$_} @array) {
+ $r .= $_;
+}
+is ($r, 'CBA', 'Reverse for array via map');
+$r = '';
+for (reverse map {$_} 1,2,3) {
+ $r .= $_;
+}
+is ($r, '321', 'Reverse for list via map');
+
+$r = '';
+for my $i (@array) {
+ $r .= $i;
+}
+is ($r, 'ABC', 'Forwards for array with var');
+$r = '';
+for my $i (1,2,3) {
+ $r .= $i;
+}
+is ($r, '123', 'Forwards for list with var');
+$r = '';
+for my $i (map {$_} @array) {
+ $r .= $i;
+}
+is ($r, 'ABC', 'Forwards for array via map with var');
+$r = '';
+for my $i (map {$_} 1,2,3) {
+ $r .= $i;
+}
+is ($r, '123', 'Forwards for list via map with var');
+
+$r = '';
+for my $i (reverse @array) {
+ $r .= $i;
+}
+is ($r, 'CBA', 'Reverse for array with var');
+$r = '';
+for my $i (reverse 1,2,3) {
+ $r .= $i;
+}
+is ($r, '321', 'Reverse for list with var');
+$r = '';
+for my $i (reverse map {$_} @array) {
+ $r .= $i;
+}
+is ($r, 'CBA', 'Reverse for array via map with var');
+$r = '';
+for my $i (reverse map {$_} 1,2,3) {
+ $r .= $i;
+}
+is ($r, '321', 'Reverse for list via map with var');
+
+
+# I don't think that my is that different from our in the optree. But test a
+# few:
+$r = '';
+for our $i (reverse @array) {
+ $r .= $i;
+}
+is ($r, 'CBA', 'Reverse for array with our var');
+$r = '';
+for our $i (reverse 1,2,3) {
+ $r .= $i;
+}
+is ($r, '321', 'Reverse for list with our var');
+$r = '';
+for our $i (reverse map {$_} @array) {
+ $r .= $i;
+}
+is ($r, 'CBA', 'Reverse for array via map with our var');
+$r = '';
+for our $i (reverse map {$_} 1,2,3) {
+ $r .= $i;
+}
+is ($r, '321', 'Reverse for list via map with our var');
+
+
+$r = '';
+for (1, reverse @array) {
+ $r .= $_;
+}
+is ($r, '1CBA', 'Reverse for array with leading value');
+$r = '';
+for ('A', reverse 1,2,3) {
+ $r .= $_;
+}
+is ($r, 'A321', 'Reverse for list with leading value');
+$r = '';
+for (1, reverse map {$_} @array) {
+ $r .= $_;
+}
+is ($r, '1CBA', 'Reverse for array via map with leading value');
+$r = '';
+for ('A', reverse map {$_} 1,2,3) {
+ $r .= $_;
+}
+is ($r, 'A321', 'Reverse for list via map with leading value');
+
+$r = '';
+for (reverse (@array), 1) {
+ $r .= $_;
+}
+is ($r, 'CBA1', 'Reverse for array with trailing value');
+$r = '';
+for (reverse (1,2,3), 'A') {
+ $r .= $_;
+}
+is ($r, '321A', 'Reverse for list with trailing value');
+$r = '';
+for (reverse (map {$_} @array), 1) {
+ $r .= $_;
+}
+is ($r, 'CBA1', 'Reverse for array via map with trailing value');
+$r = '';
+for (reverse (map {$_} 1,2,3), 'A') {
+ $r .= $_;
+}
+is ($r, '321A', 'Reverse for list via map with trailing value');
+
+
+$r = '';
+for my $i (1, reverse @array) {
+ $r .= $i;
+}
+is ($r, '1CBA', 'Reverse for array with leading value and var');
+$r = '';
+for my $i ('A', reverse 1,2,3) {
+ $r .= $i;
+}
+is ($r, 'A321', 'Reverse for list with leading value and var');
+$r = '';
+for my $i (1, reverse map {$_} @array) {
+ $r .= $i;
+}
+is ($r, '1CBA', 'Reverse for array via map with leading value and var');
+$r = '';
+for my $i ('A', reverse map {$_} 1,2,3) {
+ $r .= $i;
+}
+is ($r, 'A321', 'Reverse for list via map with leading value and var');
+
+$r = '';
+for my $i (reverse (@array), 1) {
+ $r .= $i;
+}
+is ($r, 'CBA1', 'Reverse for array with trailing value and var');
+$r = '';
+for my $i (reverse (1,2,3), 'A') {
+ $r .= $i;
+}
+is ($r, '321A', 'Reverse for list with trailing value and var');
+$r = '';
+for my $i (reverse (map {$_} @array), 1) {
+ $r .= $i;
+}
+is ($r, 'CBA1', 'Reverse for array via map with trailing value and var');
+$r = '';
+for my $i (reverse (map {$_} 1,2,3), 'A') {
+ $r .= $i;
+}
+is ($r, '321A', 'Reverse for list via map with trailing value and var');
+
+
+$r = '';
+for (reverse 1, @array) {
+ $r .= $_;
+}
+is ($r, 'CBA1', 'Reverse for value and array');
+$r = '';
+for (reverse map {$_} 1, @array) {
+ $r .= $_;
+}
+is ($r, 'CBA1', 'Reverse for value and array via map');
+
+$r = '';
+for (reverse (@array, 1)) {
+ $r .= $_;
+}
+is ($r, '1CBA', 'Reverse for array and value');
+$r = '';
+for (reverse (map {$_} @array, 1)) {
+ $r .= $_;
+}
+is ($r, '1CBA', 'Reverse for array and value via map');
+
+
+$r = '';
+for my $i (reverse 1, @array) {
+ $r .= $i;
+}
+is ($r, 'CBA1', 'Reverse for value and array with var');
+$r = '';
+for my $i (reverse map {$_} 1, @array) {
+ $r .= $i;
+}
+is ($r, 'CBA1', 'Reverse for value and array via map with var');
+
+$r = '';
+for my $i (reverse (@array, 1)) {
+ $r .= $i;
+}
+is ($r, '1CBA', 'Reverse for array and value with var');
+$r = '';
+for my $i (reverse (map {$_} @array, 1)) {
+ $r .= $i;
+}
+is ($r, '1CBA', 'Reverse for array and value via map with var');