This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Create subdirectory t/opbasic. Move 5 test files there.
[perl5.git] / t / op / reverse.t
CommitLineData
3168554e
JH
1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
a8136c1e 6 require './test.pl';
3168554e
JH
7}
8
789bd863 9plan tests => 26;
3168554e 10
af1b6079 11is(reverse("abc"), "cba", 'simple reverse');
3168554e
JH
12
13$_ = "foobar";
af1b6079 14is(reverse(), "raboof", 'reverse of the default variable');
3168554e
JH
15
16{
17 my @a = ("foo", "bar");
18 my @b = reverse @a;
19
af1b6079
BE
20 is($b[0], $a[1], 'array reversal moved second element to first');
21 is($b[1], $a[0], 'array reversal moved first element to second');
3168554e
JH
22}
23
24{
484c818f
VP
25 my @a = (1, 2, 3, 4);
26 @a = reverse @a;
af1b6079 27 is("@a", "4 3 2 1", 'four element array reversed');
484c818f
VP
28
29 delete $a[1];
30 @a = reverse @a;
af1b6079
BE
31 ok(!exists $a[2], 'array reversed with deleted second element');
32 is($a[0] . $a[1] . $a[3], '124', 'remaining elements ok after delete and reverse');
484c818f
VP
33
34 @a = (5, 6, 7, 8, 9);
35 @a = reverse @a;
af1b6079 36 is("@a", "9 8 7 6 5", 'five element array reversed');
484c818f
VP
37
38 delete $a[3];
39 @a = reverse @a;
af1b6079
BE
40 ok(!exists $a[1], 'five element array reversed with deleted fourth element');
41 is($a[0] . $a[2] . $a[3] . $a[4], '5789', 'remaining elements ok after delete and reverse');
484c818f
VP
42
43 delete $a[2];
44 @a = reverse @a;
af1b6079
BE
45 ok(!exists $a[2] && !exists $a[3], 'test position of two deleted elements after reversal');
46 is($a[0] . $a[1] . $a[4], '985', 'check value of remaining elements');
54113d22
RGS
47
48 my @empty;
49 @empty = reverse @empty;
af1b6079 50 is("@empty", "", 'reversed empty array is still empty');
484c818f
VP
51}
52
53use Tie::Array;
54
55{
56 tie my @a, 'Tie::StdArray';
57
58 @a = (1, 2, 3, 4);
59 @a = reverse @a;
af1b6079 60 is("@a", "4 3 2 1", 'tie array reversal');
484c818f
VP
61
62 delete $a[1];
63 @a = reverse @a;
af1b6079
BE
64 ok(!exists $a[2], 'deleted element position ok after reversal of tie array');
65 is($a[0] . $a[1] . $a[3], '124', 'remaining elements ok after delete and reversal for tie array');
484c818f
VP
66
67 @a = (5, 6, 7, 8, 9);
68 @a = reverse @a;
af1b6079 69 is("@a", "9 8 7 6 5", 'five element tie array reversal');
484c818f
VP
70
71 delete $a[3];
72 @a = reverse @a;
af1b6079
BE
73 ok(!exists $a[1], 'deleted element position ok after tie array reversal');
74 is($a[0] . $a[2] . $a[3] . $a[4], '5789', 'remaining elements ok after tie array delete and reversal');
484c818f
VP
75
76 delete $a[2];
77 @a = reverse @a;
af1b6079
BE
78 ok(!exists $a[2] && !exists $a[3], 'two deleted element positions ok after tie array reversal');
79 is($a[0] . $a[1] . $a[4], '985', 'remaining elements ok after two deletes and reversals');
6391ac7e
RGS
80
81 tie my @empty, "Tie::StdArray";
82 @empty = reverse @empty;
af1b6079 83 is(scalar(@empty), 0, 'reversed tie array still empty after reversal');
484c818f
VP
84}
85
86{
3168554e
JH
87 # Unicode.
88
89 my $a = "\x{263A}\x{263A}x\x{263A}y\x{263A}";
90 my $b = scalar reverse($a);
91 my $c = scalar reverse($b);
af1b6079 92 is($a, $c, 'Unicode string double reversal matches original');
3168554e 93}
789bd863
VP
94
95{
96 # Lexical $_.
90b58ec9 97 no warnings 'deprecated';
789bd863
VP
98 sub blurp { my $_ = shift; reverse }
99
af1b6079
BE
100 is(blurp("foo"), "oof", 'reversal of default variable in function');
101 is(sub { my $_ = shift; reverse }->("bar"), "rab", 'reversal of default variable in anonymous function');
789bd863
VP
102 {
103 local $_ = "XXX";
af1b6079 104 is(blurp("paz"), "zap", 'reversal of default variable with local value set' );
789bd863
VP
105 }
106}