-
Notifications
You must be signed in to change notification settings - Fork 134
/
list.t
194 lines (157 loc) · 6.12 KB
/
list.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
use v6;
use lib <t/spec/packages/>;
use Test;
use Test::Util;
plan 76;
isa-ok (5, 7, 8), List, '(5, 7, 8) is List';
is +(5, 7, 8), 3, 'prefix:<+> on a List';
is ~(5, 7, 8), '5 7 8', 'prefix:<~> on a List';
is (5, 7, 8).Str, '5 7 8', '.Str on a List';
# .perl
is ().perl, '()', '.perl on empty List';
is ().item.perl, '$( )', '.item.perl on empty List';
is-deeply ().item, ().item.perl.EVAL, 'can roundtrip ().item';
#?rakudo.jvm skip 'dies with t/harness5'
cmp-ok ().item.VAR, '===', ().item.perl.EVAL.VAR,
'().item .perl.EVAL roundtrip preserves itemization';
# L<S02/Quoting forms/Elsewhere it is equivalent to a parenthesized list of strings>
isa-ok <5 7 8>, List, '<5 7 8> is List';
is +<5 7 8>, 3, 'prefix:<+> on an angle bracket List';
is ~<5 7 8>, '5 7 8', 'prefix:<~> on an angle bracket List';
is <5 7 8>.Str, '5 7 8', '.Str on an angle bracket List';
isa-ok (5, 7, 8).List, List, '.List returns a list';
is (5, 7, 8).List, [5,7,8], '.List contains the right items';
is (5, 7, 8).List.elems, 3, '.List contains the right number of elements';
is ?(), False, 'empty List is False';
is ?(1,2,3), True, 'non-empty List is True';
lives-ok { <5 7 8>[] }, 'can zen slice a List';
# WAS: RT #115282, modified for lolly brannch
is $(;).elems, 0, '$(;) parses, and is empty';
# .rotate
{
my $p = <a b c d e>;
is ~$p.rotate, 'b c d e a', 'List.rotate defaults to +1';
is ~$p, 'a b c d e', 'original list unmodified';
ok $p.rotate ~~ List, 'List.rotate returns a List';
is ~$p.rotate(2), 'c d e a b', '.rotate(2)';
is ~$p, 'a b c d e', 'original list still unmodified';
is ~$p.rotate(-2), 'd e a b c', '.rotate(-2)';
is ~$p, 'a b c d e', 'original still unmodified (negative)';
is ~$p.rotate(0), 'a b c d e', '.rotate(0)';
is ~$p.rotate(5), 'a b c d e', '.rotate(5)';
is ~$p.rotate(15), 'a b c d e', '.rotate(15)';
is ~$p.rotate(7), 'c d e a b', '.rotate(7)';
is ~$p, 'a b c d e', 'original still unmodified (negative)';
is ~$p.rotate(-8), 'c d e a b', '.rotate(-8)';
is ~$p, 'a b c d e', 'original still unmodified (negative)';
} #14
# all the same but rotate() sub
{
my $p = <a b c d e>;
is ~rotate($p), 'b c d e a', 'rotate(@a)';
is ~$p, 'a b c d e', 'original list unmodified';
is ~rotate($p, 2), 'c d e a b', 'rotate(@a, 2)';
is ~$p, 'a b c d e', 'original list still unmodified';
is ~rotate($p, -2), 'd e a b c', 'rotate(@a, -2)';
is ~$p, 'a b c d e', 'original still unmodified (negative)';
is ~rotate($p, 0), 'a b c d e', 'rotate(@a, 0)';
is ~rotate($p, 5), 'a b c d e', 'rotate(@a, 5)';
is ~rotate($p, 15), 'a b c d e', 'rotate(@a, 15)';
is ~rotate($p, 7), 'c d e a b', 'rotate(@a, 7)';
is ~$p, 'a b c d e', 'original still unmodified (negative)';
is ~rotate($p, -8), 'c d e a b', 'rotate(@a, -8)';
is ~$p, 'a b c d e', 'original still unmodified (negative)';
} #13
# RT125677 Make sure List.rotate is Cool with stuff
{
my $p = <a b c d e>;
is ~$p.rotate('2'), 'c d e a b', '.rotate("2")';
is ~$p.rotate(2.5), 'c d e a b', '.rotate(2.5)';
} #2
{
ok <a b c> !=== <a b c>, 'a b c !=== a b c (List is not a value type)';
ok (my $x1=42,1) !=== (42,1), '$x1 = 42,1 !=== 42,1';
ok (my $x2=42,1) !=== (my $y=42,1), '$x2 = 42,1 !=== $y = 42,1';
ok (my $x3=42,1) !=== (my $y2:=$x3,1), '$x3=42,1 !=== $y2 := $x3,1 (List is not a value type)';
} #4
{
my $p = List.new( <a b c> );
is $p.elems, 1, 'did we get the list';
is $p, <a b c>, 'did we get what we put in';
$p = List.new( 'a','b','c' );
is $p.elems, 3, 'did we get the parameters';
is $p, <a b c>, 'did we get what we put in';
} #4
#RT #116527
{
role sidecat {};
my @a = 1,2,3;
my @b = @a.pick(*).sort.list but sidecat;
is @b.gist, "[1 2 3]", "can gist a list with a role";
}
{
for <push pop shift unshift append prepend> -> $method {
throws-like { (1,2,3)."$method"(42) }, X::Immutable,
method => $method,
typename => 'List',
;
}
}
{
is (1,2,3,4).sum, 10, 'can we do .sum on a List';
is (.1,.2,.3,.4).sum, 1, 'even if they are not ints';
is <1 2 3 4>.sum, 10, 'even if they are numified strings';
throws-like { <a b c d>.sum }, X::Str::Numeric,
reason => "base-10 number must begin with valid digits or '.'",
'fail if they are non-numeric strings';
}
# RT#129044
{
is (a => 2).first(/a/), (a => 2), "first with a Regexp object";
is (a => 2).grep(/a/), (a => 2), "grep with a Regexp object";
}
subtest '.sum can handle Junctions' => {
# http://www.perlmonks.org/?node_id=1176379
plan 7;
constant @a = (2, 3|4, 5, 6|7);
my $sum = @a.sum;
is-deeply-junction sum(@a), $sum, 'sum() produces same thing as .sum';
is-deeply-junction ([+] @a), $sum, '[+] produces same thing as .sum';
cmp-ok $sum, '==', 2+3+5+6, 'sum is correct (1)';
cmp-ok $sum, '==', 2+4+5+6, 'sum is correct (2)';
cmp-ok $sum, '==', 2+3+5+7, 'sum is correct (3)';
cmp-ok $sum, '==', 2+4+5+7, 'sum is correct (4)';
ok $sum !== 42, 'sum is correct (6)';
}
#?rakudo.jvm skip 'RT #130160 works when run standalone, maybe wrong multi selected'
{ # RT#130160
my @a := <a b c>[0..2, 0..2].flat.cache;
@a.Bool;
@a.elems;
is-deeply @a, <a b c a b c>, '.flat does not skip inner iterables';
}
# RT #125964
{
my $a = (1, 2, 3);
throws-like { $a[42] = 21 }, X::Assignment::RO,
value => $a,
'cannot assign to a element of an immutable List';
my $b = (^10);
throws-like { $b[1] = 21 }, X::Assignment::RO,
value => $b,
'cannot assign to a element of an immutable Range';
my $c = [1, 2, 3];
lives-ok { $c[5] = 21 };
is $c[0, 1, 2, 5], [1, 2, 3, 21], 'can assign to a List element if it is a container';
}
# https://github.com/rakudo/rakudo/issues/1260
subtest 'List.new does not create unwanted containers' => {
plan 4;
my $l := List.new: 1, 2, my $ = 3;
is-deeply $l, (1, 2, 3), 'right values';
throws-like { $l[0] = 42 }, X::Assignment::RO,
'no containers were created';
lives-ok { $l[2] = 42 }, 'passed containers get preserved';
is-deeply $l[2], 42, 'value assigneed to passed container is right';
}
# vim: ft=perl6