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