Permalink
Newer
Older
100644 249 lines (194 sloc) 6.47 KB
1
use v6;
2
use Test;
4
5
# L<S32::Containers/"List"/"=item map">
6
7
=begin pod
8
9
built-in map tests
10
11
=end pod
12
13
14
my @list = (1 .. 5);
16
{
17
my @result = map { $_ * 2 }, @list;
moritz
Apr 22, 2010
18
is(+@result, 5, 'sub form: we got a list back');
19
is @result.join(', '), '2, 4, 6, 8, 10', 'got the values we expected';
20
}
21
22
#?rakudo skip "adverbial block"
Dec 24, 2011
23
#?niecza skip 'No value for parameter $func in Any.map'
24
{
25
my @result = @list.map():{ $_ * 2 };
moritz
Apr 22, 2010
26
is(+@result, 5, 'adverbial block: we got a list back');
27
is @result.join(', '), '2, 4, 6, 8, 10', 'got the values we expected';
28
}
29
30
{
31
my @result = @list.map: { $_ * 2 };
moritz
Apr 22, 2010
32
is(+@result, 5, 'invcant colon method form: we got a list back');
33
is @result.join(', '), '2, 4, 6, 8, 10', 'got the values we expected';
34
}
35
36
#?rakudo skip "closure as non-final argument"
Dec 24, 2011
37
#?niecza skip "Invocant handling is NYI"
38
{
39
my @result = map { $_ * 2 }: @list;
40
is(+@result, 5, 'we got a list back');
moritz
Apr 22, 2010
41
is @result.join(', '), '2, 4, 6, 8, 10', 'got the values we expected';
42
}
43
44
# Testing map that returns an array
45
{
46
my @result = map { ($_, $_ * 2) }, @list;
moritz
Apr 22, 2010
47
is(+@result, 10, 'Parcel returned from closure: we got a list back');
48
is @result.join(', '),
49
'1, 2, 2, 4, 3, 6, 4, 8, 5, 10',
50
'got the values we expected';
51
}
52
53
# Testing multiple statements in the closure
54
{
55
my @result = map {
56
my $fullpath = "fish/$_";
57
$fullpath;
58
}, @list;
moritz
Apr 22, 2010
59
is(+@result, 5, 'multiple statements in block: we got a list back');
60
is @result.join('|'), 'fish/1|fish/2|fish/3|fish/4|fish/5',
61
'got the values we expect';
62
}
63
64
{
65
my @list = 1 .. 5;
66
is +(map {;$_ => 1 }, @list), 5,
67
'heuristic for block - looks like a closure';
68
69
my %result = map {; $_ => ($_*2) }, @list;
moritz
Dec 25, 2008
70
isa_ok(%result, Hash);
moritz
Apr 22, 2010
71
is %result<1 2 3 4 5>.join(', '), '2, 4, 6, 8, 10',
72
' got the hash we expect';
73
}
74
75
# map with n-ary functions
76
#?rakudo skip "adverbial block; RT #53804"
77
{
78
is ~(1,2,3,4).map({ $^a + $^b }), "3 7", "map() works with 2-ary functions";
Dec 24, 2011
79
#?niecza skip 'No value for parameter $b in ANON'
80
is ~(1,2,3,4).map({ $^a + $^b + $^c }), "6 4", "map() works with 3-ary functions";
81
is ~(1,2,3,4).map({ $^a + $^b + $^c + $^d }), "10", "map() works with 4-ary functions";
Dec 24, 2011
82
#?niecza skip 'No value for parameter $e in ANON'
83
is ~(1,2,3,4).map({ $^a+$^b+$^c+$^d+$^e }), "10", "map() works with 5-ary functions";
84
}
85
86
{
87
is(42.map({$_}), 42, "method form of map works on numbers");
88
is('str'.map({$_}), 'str', "method form of map works on strings");
89
}
90
91
=begin pod
92
93
Test that a constant list can have C<map> applied to it.
94
95
("foo","bar").map: { $_.substr(1,1) }
96
97
should be equivalent to
98
99
my @val = ("foo","bar");
100
@val = map { substr($_,1,1) }, @val;
101
102
=end pod
103
104
{
105
my @expected = ("foo","bar");
106
@expected = map { substr($_,1,1) }, @expected;
107
108
is((("foo","bar").map: { $_.substr(1,1) }), @expected, "map of constant list works");
109
}
110
moritz
Jun 22, 2008
111
Feb 25, 2012
112
#?pugs skip 'unimpl'
113
{
114
my @a = (1, 2, 3);
moritz
Jun 22, 2008
115
# XXX is hash { ... } legal?
116
my @b = map { hash {"v"=>$_, "d" => $_*2} }, @a;
117
is(+@b, 6, "should be 6 elements (list context)");
118
119
my @c = map { {"v"=>$_, "d" => $_*2} }, @a;
120
is(+@c, 3, "should be 3 elements (3 hashrefs)");
121
}
122
123
# Map with mutating block
124
# L<S02/Names/"$_, $!, and $/ are context<rw> by default">
125
126
{
127
my @array = <a b c d>;
Feb 25, 2012
128
#?pugs todo
129
is ~(try { @array.map: { $_ ~= "c"; $_ ~ "d" } }), "acd bcd ccd dcd",
130
'mutating $_ in map works (1)';
Feb 25, 2012
131
#?pugs todo
132
is ~@array, "ac bc cc dc",
133
'mutating $_ in map works (2)';
134
}
135
moritz
Jan 5, 2009
136
sub dbl ( Int $val ) { 2*$val };
137
is( ~((1..3).map: { 2*$_ }),'2 4 6','intern method in map');
138
is( ~((1..3).map: { dbl( $_ ) }),'2 4 6','extern method in map');
139
140
141
# map with empty lists in the block
142
# Test was primarily aimed at PIL2JS, which did not pass this test (fixed now).
143
{
144
my @array = <a b c d>;
145
my @result = map { (), }, @array;
146
147
is +@result, 0, "map works with the map body returning an empty list";
148
}
149
150
{
151
my @array = <a b c d>;
152
my @empty = ();
153
my @result = map { @empty }, @array;
154
155
is +@result, 0, "map works with the map body returning an empty array";
156
}
157
158
{
159
my @array = <a b c d>;
160
my @result = map { [] }, @array;
161
162
is +@result, 4, "map works with the map body returning an empty arrayref";
163
}
164
165
{
166
my @array = <a b c d>;
167
my $empty = [];
168
my @result = map { $empty }, @array;
169
Feb 25, 2012
170
#?pugs todo 'bug'
moritz
Jan 5, 2009
171
is +@result, 4, "map works with the map body returning an empty arrayref variable";
172
}
173
174
{
175
my @array = <a b c d>;
176
my @result = map { Mu }, @array;
moritz
Jan 5, 2009
177
178
is +@result, 4, "map works with the map body returning undefined";
moritz
Jan 5, 2009
179
}
180
181
{
182
my @array = <a b c d>;
moritz
Jan 5, 2009
184
my @result = map { $undef }, @array;
185
186
is +@result, 4, "map works with the map body returning an undefined variable";
187
}
188
189
{
190
my @array = <a b c d>;
191
my @result = map { () }, @array;
192
193
is +@result, 0, "map works with the map body returning ()";
194
}
195
196
# test map with a block that takes more than one parameter
197
{
198
my @a=(1,4,2,5,3,6);
199
my @ret=map -> $a,$b {$a+$b}, @a;
200
201
is(@ret.elems,3,'map took 2 elements at a time');
202
is(@ret[0],5,'first element ok');
203
is(@ret[1],7,'second element ok');
204
is(@ret[2],9,'third element ok');
205
206
}
207
208
# map shouldn't flatten array objects
209
# used to be a pugs regression
210
{
211
my @foo = [1, 2, 3].map: { [100+$_, 200+$_] };
212
is +@foo, 3, "map should't flatten our arrayref (1)";
213
is +@foo[0], 2, "map should't flatten our arrayref (2)";
214
is ~@foo[0], "101 201", "map should't flatten our arrayref (3)";
moritz
Jan 5, 2009
215
}
216
217
# .thing inside map blocks should still default to $_
218
# used to be a pugs regression
Feb 25, 2012
219
#?DOES 6
moritz
Jan 5, 2009
220
{
221
is ~((1,2,3).map: { $_.Int }), "1 2 3", "dependency for following test (1)";
222
$_ = 4; is .Int, 4, "dependency for following test (2)";
223
is ~((1,2,3).map: { .Int }), "1 2 3", 'int() should default to $_ inside map, too';
moritz
Jan 5, 2009
224
225
is ~(({1},{2},{3}).map: { $_; $_() }), "1 2 3", 'lone $_ in map should work (1)';
226
is ~(({1},{2},{3}).map: { $_() }), "1 2 3", 'lone $_ in map should work (2)';
227
is ~(({1},{2},{3}).map: { .() }), "1 2 3", 'lone .() in map should work (2)';
228
}
229
Feb 25, 2012
230
#?pugs skip 'unimpl'
231
#?DOES 2
232
{
233
is (1..4).map({ next if $_ % 2; 2 * $_ }).join('|'),
234
'4|8', 'next in map works';
235
is (1..10).map({ last if $_ % 5 == 0; 2 * $_}).join(' '),
236
'2 4 6 8', 'last in map works';
237
}
238
239
# RT #62332
Feb 25, 2012
240
#?pugs skip 'unimpl'
241
#?DOES 2
242
{
243
my $x = :a<5>;
244
is $x.map({ .key, .value + 1}), ('a', 6), 'map on pair works (comma)';
245
is $x.map({ ; .key => .value + 1}), ('a' => 6), 'map on pair works (=>)';
246
}
247
248
# vim: ft=perl6