/
map.t
268 lines (213 loc) · 7.07 KB
/
map.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
use v6;
use Test;
plan 59;
# L<S32::Containers/"List"/"=item map">
=begin pod
built-in map tests
=end pod
my @list = (1 .. 5);
#?pugs skip 'NYI'
#?niecza skip 'NYI'
{
my Int $s;
my Str @a;
my Num %h;
ok $s === $s.map(*), 'is $s.map(*) a noop';
ok $s === map(*,$s), 'is map(*,$s) a noop';
ok @a === @a.map(*), 'is @a.map(*) a noop';
ok @a === map(*,@a), 'is map(*,@a) a noop';
ok %h === %h.map(*), 'is %h.map(*) a noop';
ok %h === map(*,%h), 'is map(*,%h) a noop';
}
{
my @result = map { $_ * 2 }, @list;
is(+@result, 5, 'sub form: we got a list back');
is @result.join(', '), '2, 4, 6, 8, 10', 'got the values we expected';
}
#?rakudo skip "adverbial block"
#?niecza skip 'No value for parameter $func in Any.map'
{
my @result = @list.map():{ $_ * 2 };
is(+@result, 5, 'adverbial block: we got a list back');
is @result.join(', '), '2, 4, 6, 8, 10', 'got the values we expected';
}
{
my @result = @list.map: { $_ * 2 };
is(+@result, 5, 'invcant colon method form: we got a list back');
is @result.join(', '), '2, 4, 6, 8, 10', 'got the values we expected';
}
#?rakudo skip "closure as non-final argument"
#?niecza skip "Invocant handling is NYI"
{
my @result = map { $_ * 2 }: @list;
is(+@result, 5, 'we got a list back');
is @result.join(', '), '2, 4, 6, 8, 10', 'got the values we expected';
}
# Testing map that returns an array
{
my @result = map { ($_, $_ * 2) }, @list;
is(+@result, 10, 'Parcel returned from closure: we got a list back');
is @result.join(', '),
'1, 2, 2, 4, 3, 6, 4, 8, 5, 10',
'got the values we expected';
}
# Testing multiple statements in the closure
{
my @result = map {
my $fullpath = "fish/$_";
$fullpath;
}, @list;
is(+@result, 5, 'multiple statements in block: we got a list back');
is @result.join('|'), 'fish/1|fish/2|fish/3|fish/4|fish/5',
'got the values we expect';
}
{
my @list = 1 .. 5;
is +(map {;$_ => 1 }, @list), 5,
'heuristic for block - looks like a closure';
my %result = map {; $_ => ($_*2) }, @list;
isa_ok(%result, Hash);
is %result<1 2 3 4 5>.join(', '), '2, 4, 6, 8, 10',
' got the hash we expect';
}
# map with n-ary functions
#?rakudo skip "adverbial block; RT #53804"
{
is ~(1,2,3,4).map({ $^a + $^b }), "3 7", "map() works with 2-ary functions";
#?niecza skip 'No value for parameter $b in ANON'
is ~(1,2,3,4).map({ $^a + $^b + $^c }), "6 4", "map() works with 3-ary functions";
is ~(1,2,3,4).map({ $^a + $^b + $^c + $^d }), "10", "map() works with 4-ary functions";
#?niecza skip 'No value for parameter $e in ANON'
is ~(1,2,3,4).map({ $^a+$^b+$^c+$^d+$^e }), "10", "map() works with 5-ary functions";
}
{
is(42.map({$_}), 42, "method form of map works on numbers");
is('str'.map({$_}), 'str', "method form of map works on strings");
}
=begin pod
Test that a constant list can have C<map> applied to it.
("foo","bar").map: { $_.substr(1,1) }
should be equivalent to
my @val = ("foo","bar");
@val = map { substr($_,1,1) }, @val;
=end pod
{
my @expected = ("foo","bar");
@expected = map { substr($_,1,1) }, @expected;
is((("foo","bar").map: { $_.substr(1,1) }), @expected, "map of constant list works");
}
{
my @a = (1, 2, 3);
my @b = map { hash("v"=>$_, "d" => $_*2) }, @a;
is(+@b, 6, "should be 6 elements (list context)");
my @c = map { {"v"=>$_, "d" => $_*2} }, @a;
#?niecza todo
is(+@c, 6, "should be 6 elements (bare block)");
}
# Map with mutating block
# L<S02/Names/"$_, $!, and $/ are context<rw> by default">
#?pugs todo
{
my @array = <a b c d>;
is ~(try { @array.map: { $_ ~= "c"; $_ ~ "d" } }), "acd bcd ccd dcd",
'mutating $_ in map works (1)';
is ~@array, "ac bc cc dc",
'mutating $_ in map works (2)';
}
sub dbl ( Int $val ) { 2*$val };
is( ~((1..3).map: { 2*$_ }),'2 4 6','intern method in map');
is( ~((1..3).map: { dbl( $_ ) }),'2 4 6','extern method in map');
# map with empty lists in the block
# Test was primarily aimed at PIL2JS, which did not pass this test (fixed now).
{
my @array = <a b c d>;
my @result = map { (), }, @array;
is +@result, 0, "map works with the map body returning an empty list";
}
{
my @array = <a b c d>;
my @empty = ();
my @result = map { @empty }, @array;
is +@result, 0, "map works with the map body returning an empty array";
}
{
my @array = <a b c d>;
my @result = map { [] }, @array;
is +@result, 4, "map works with the map body returning an empty arrayref";
}
#?pugs todo
{
my @array = <a b c d>;
my $empty = [];
my @result = map { $empty }, @array;
is +@result, 4, "map works with the map body returning an empty arrayref variable";
}
{
my @array = <a b c d>;
my @result = map { Mu }, @array;
is +@result, 4, "map works with the map body returning undefined";
}
{
my @array = <a b c d>;
my $undef = Mu;
my @result = map { $undef }, @array;
is +@result, 4, "map works with the map body returning an undefined variable";
}
{
my @array = <a b c d>;
my @result = map { () }, @array;
is +@result, 0, "map works with the map body returning ()";
}
# test map with a block that takes more than one parameter
{
my @a=(1,4,2,5,3,6);
my @ret=map -> $a,$b {$a+$b}, @a;
is(@ret.elems,3,'map took 2 elements at a time');
is(@ret[0],5,'first element ok');
is(@ret[1],7,'second element ok');
is(@ret[2],9,'third element ok');
}
# map shouldn't flatten array objects
# used to be a pugs regression
{
my @foo = [1, 2, 3].map: { [100+$_, 200+$_] };
is +@foo, 3, "map should't flatten our arrayref (1)";
is +@foo[0], 2, "map should't flatten our arrayref (2)";
is ~@foo[0], "101 201", "map should't flatten our arrayref (3)";
}
# .thing inside map blocks should still default to $_
# used to be a pugs regression
#?DOES 6
{
is ~((1,2,3).map: { $_.Int }), "1 2 3", "dependency for following test (1)";
$_ = 4; is .Int, 4, "dependency for following test (2)";
is ~((1,2,3).map: { .Int }), "1 2 3", 'int() should default to $_ inside map, too';
is ~(({1},{2},{3}).map: { $_; $_() }), "1 2 3", 'lone $_ in map should work (1)';
is ~(({1},{2},{3}).map: { $_() }), "1 2 3", 'lone $_ in map should work (2)';
is ~(({1},{2},{3}).map: { .() }), "1 2 3", 'lone .() in map should work (2)';
}
#?pugs skip "Cannot use this control structure outside a 'loop' structure"
#?DOES 2
{
is (1..4).map({ next if $_ % 2; 2 * $_ }).join('|'),
'4|8', 'next in map works';
is (1..10).map({ last if $_ % 5 == 0; 2 * $_}).join(' '),
'2 4 6 8', 'last in map works';
}
# RT #62332
#?pugs skip 'No such method in class Str: "&key"'
#?DOES 2
{
my $x = :a<5>;
is $x.map({ .key, .value + 1}), ('a', 6), 'map on pair works (comma)';
is $x.map({ ; .key => .value + 1}), ('a' => 6), 'map on pair works (=>)';
}
# RT #112596
#?pugs skip 'hangs'
#?niecza todo "https://github.com/sorear/niecza/issues/182"
#?DOES 1
{
my @a = map &sprintf.assuming("%x"), 9..12;
is(@a, <9 a b c>, "map over a callable with a slurpy");
}
# vim: ft=perl6