/
reverse.t
101 lines (80 loc) 路 2.33 KB
/
reverse.t
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
use v6;
use Test;
# L<S32::Containers/List/"=item reverse">
plan 19;
=begin pod
Tests for "reverse", which always reverse lists now. See flip()
in S32-str for the string reversal
=end pod
my @a = reverse(1, 2, 3, 4);
my @e = (4, 3, 2, 1);
is(@a, @e, "list was reversed");
{
my $a = reverse("foo");
is($a, "foo", "string was not reversed; that's what flip() is for");
@a = reverse "foo", "bar";
is(+@a, 2, 'the reversed list has two elements');
is(@a[0], "bar", 'the list was reversed properly');
is(@a[1], "foo", 'the list was reversed properly');
}
# #?rakudo skip 'want()'
# {
# my @cxt_log;
#
# class Foo {
# my @.n;
# method foo () {
# push @cxt_log, want();
# (1, 2, 3)
# }
# method bar () {
# push @cxt_log, want();
# return @!n = do {
# push @cxt_log, want();
# reverse self.foo;
# }
# }
# }
#
# my @n = do {
# push @cxt_log, want();
# Foo.new.bar;
# };
#
# is(~@cxt_log, ~("List (Any)" xx 4), "contexts were passed correctly around masak's bug");
# is(+@n, 3, "list context reverse in masak's bug");
# is(~@n, "3 2 1", "elements seem reversed");
# }
{
my @a = "foo";
my @b = @a.reverse;
#?niecza skip "Iterable NYI"
ok(@a.reverse ~~ Iterable, '.reverse returns an Iterable');
my $b = @a.reverse;
#?niecza skip "Iterable NYI"
ok($b ~~ Iterable, '.reverse assigned to a scalar is Iterable');
is(@b[0], "foo", 'our list is reversed properly');
is($b, "foo", 'in scalar context it is still a list');
is(@a[0], "foo", "original array left untouched");
@a .= reverse;
is(@a[0], "foo", 'in place reversal works');
}
{
my @a = ("foo", "bar");
my @b = @a.reverse;
my $b = @a.reverse;
is(@b[0], "bar", 'our array is reversed');
is(@b[1], "foo", 'our array is reversed');
is($b, "bar foo", 'in scalar context it is still a list');
is(@a[0], "foo", "original array left untouched");
is(@a[1], "bar", "original array left untouched");
@a .= reverse;
is(@a[0], "bar", 'in place reversal works');
is(@a[1], "foo", 'in place reversal works');
}
# RT #77914
#?rakudo todo "RT #77914"
{
is (<a b>, <c d>).reverse.join, 'dcba', '.reverse flattens lists';
}
# vim: ft=perl6