summaryrefslogtreecommitdiff
path: root/t/op/reverse.t
blob: 916724c0df77f5512d472d152fd17f56f0553296 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!./perl

BEGIN {
    chdir 't' if -d 't';
    @INC = '../lib';
    require './test.pl';
}

plan tests => 26;

is(reverse("abc"), "cba");

$_ = "foobar";
is(reverse(), "raboof");

{
    my @a = ("foo", "bar");
    my @b = reverse @a;

    is($b[0], $a[1]);
    is($b[1], $a[0]);
}

{
    my @a = (1, 2, 3, 4);
    @a = reverse @a;
    is("@a", "4 3 2 1");

    delete $a[1];
    @a = reverse @a;
    ok(!exists $a[2]);
    is($a[0] . $a[1] . $a[3], '124');

    @a = (5, 6, 7, 8, 9);
    @a = reverse @a;
    is("@a", "9 8 7 6 5");

    delete $a[3];
    @a = reverse @a;
    ok(!exists $a[1]);
    is($a[0] . $a[2] . $a[3] . $a[4], '5789');

    delete $a[2];
    @a = reverse @a;
    ok(!exists $a[2] && !exists $a[3]);
    is($a[0] . $a[1] . $a[4], '985');

    my @empty;
    @empty = reverse @empty;
    is("@empty", "");
}

use Tie::Array;

{
    tie my @a, 'Tie::StdArray';

    @a = (1, 2, 3, 4);
    @a = reverse @a;
    is("@a", "4 3 2 1");

    delete $a[1];
    @a = reverse @a;
    ok(!exists $a[2]);
    is($a[0] . $a[1] . $a[3], '124');

    @a = (5, 6, 7, 8, 9);
    @a = reverse @a;
    is("@a", "9 8 7 6 5");

    delete $a[3];
    @a = reverse @a;
    ok(!exists $a[1]);
    is($a[0] . $a[2] . $a[3] . $a[4], '5789');

    delete $a[2];
    @a = reverse @a;
    ok(!exists $a[2] && !exists $a[3]);
    is($a[0] . $a[1] . $a[4], '985');

    tie my @empty, "Tie::StdArray";
    @empty = reverse @empty;
    is(scalar(@empty), 0);
}

{
    # Unicode.

    my $a = "\x{263A}\x{263A}x\x{263A}y\x{263A}";
    my $b = scalar reverse($a);
    my $c = scalar reverse($b);
    is($a, $c);
}

{
    # Lexical $_.
    sub blurp { my $_ = shift; reverse }

    is(blurp("foo"), "oof");
    is(sub { my $_ = shift; reverse }->("bar"), "rab");
    {
        local $_ = "XXX";
        is(blurp("paz"), "zap");
    }
}