Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 242 lines (187 sloc) 13.838 kb
ef86bef @moritz [sequence] is the new series
moritz authored
1 use v6;
2 use Test;
3
4 # L<S03/List infix precedence/"the sequence operator">
5
1b6274b @patrickas added test for RT #78324
patrickas authored
6 plan 123;
ef86bef @moritz [sequence] is the new series
moritz authored
7
8 # single-term sequence
9
10 is ~( 1 ... 1 ), '1', '1 ... 1';
11 is ~( 'a' ... 'a' ), 'a', "'a' ... 'a'";
12
13 # finite sequence that exactly hit their limit
14
15 is (1 ... 5).join(', '), '1, 2, 3, 4, 5', 'simple sequence with one item on the LHS';
16 is (1 ... -3).join(', '), '1, 0, -1, -2, -3', 'simple decreasing sequence with one item on the LHS';
17 is (1, 3 ... 9).join(', '), '1, 3, 5, 7, 9', 'simple additive sequence with two items on the LHS';
18 is (1, 0 ... -3).join(', '), '1, 0, -1, -2, -3', 'simple decreasing additive sequence with two items on the LHS';
19 is (1, 3, 5 ... 9).join(', '), '1, 3, 5, 7, 9', 'simple additive sequence with three items on the LHS';
20 is (1, 3, 9 ... 81).join(', '), '1, 3, 9, 27, 81', 'simple multiplicative sequence with three items on the LHS';
21 is (81, 27, 9 ... 1).join(', '), '81, 27, 9, 3, 1', 'decreasing multiplicative sequence with three items on the LHS';
22 is (1, { $_ + 2 } ... 9).join(', '), '1, 3, 5, 7, 9', 'simple sequence with one item and block closure on the LHS';
23 is (1, *+2 ... 9).join(', '), '1, 3, 5, 7, 9', 'simple sequence with one item and * closure on the LHS';
24 is (1, { $_ - 2 } ... -7).join(', '), '1, -1, -3, -5, -7', 'simple sequence with one item and closure on the LHS';
25 is (1, 3, 5, { $_ + 2 } ... 13).join(', '), '1, 3, 5, 7, 9, 11, 13', 'simple sequence with three items and block closure on the LHS';
26
27 is (1, { 1 / ((1 / $_) + 1) } ... 1/5).map({.perl}).join(', '), '1, 1/2, 1/3, 1/4, 1/5', 'tricky sequence with one item and closure on the LHS';
28 is (1, { -$_ } ... 1).join(', '), '1', 'simple alternating sequence with one item and closure on the LHS';
29 is (1, { -$_ } ... 3).[^5].join(', '), '1, -1, 1, -1, 1', 'simple alternating sequence with one item and closure on the LHS';
30
31 is ({ 3+2; } ... *).[^5].join(', '), '5, 5, 5, 5, 5', 'sequence with one scalar containing Code on the LHS';
32
33 is (1 ... 5, 6, 7).join(', '), '1, 2, 3, 4, 5, 6, 7', 'simple sequence with two further terms on the RHS';
34 is (1 ... 5, 4, 3).join(', '), '1, 2, 3, 4, 5, 4, 3', 'simple sequence with two extra terms on the RHS';
35 is (1 ... 5, 'xyzzy', 'plugh').join(', '), '1, 2, 3, 4, 5, xyzzy, plugh', 'simple sequence with two weird items on the RHS';
36
37 # infinite sequence that go past their limit
38 {
39 is (1 ... 5.5).munch(6).join(', '), '1, 2, 3, 4, 5, 6', 'simple sequence with one item on the LHS';
40 is (1 ... -3.5).munch(6).join(', '), '1, 0, -1, -2, -3, -4', 'simple decreasing sequence with one item on the LHS';
41 is (1, 3 ... 10).munch(6).join(', '), '1, 3, 5, 7, 9, 11', 'simple additive sequence with two items on the LHS';
42 is (1, 0 ... -3.5).munch(6).join(', '), '1, 0, -1, -2, -3, -4', 'simple decreasing additive sequence with two items on the LHS';
43 is (1, 3, 5 ... 10).munch(6).join(', '), '1, 3, 5, 7, 9, 11', 'simple additive sequence with three items on the LHS';
44 is (1, 3, 9 ... 100).munch(6).join(', '), '1, 3, 9, 27, 81, 243', 'simple multiplicative sequence with three items on the LHS';
45 is (81, 27, 9 ... 8/9).munch(6), (81, 27, 9, 3, 1, 1/3), 'decreasing multiplicative sequence with three items on the LHS';
46 is (1, { $_ + 2 } ... 10).munch(6).join(', '), '1, 3, 5, 7, 9, 11', 'simple sequence with one item and block closure on the LHS';
47 is (1, *+2 ... 10).munch(6).join(', '), '1, 3, 5, 7, 9, 11', 'simple sequence with one item and * closure on the LHS';
48 is (1, { $_ - 2 } ... -8).munch(6).join(', '), '1, -1, -3, -5, -7, -9', 'simple sequence with one item and closure on the LHS';
49 is (1, 3, 5, { $_ + 2 } ... 14).munch(8).join(', '), '1, 3, 5, 7, 9, 11, 13, 15', 'simple sequence with three items and block closure on the LHS';
50
51 is (1, { 1 / ((1 / $_) + 1) } ... 11/60).munch(6).map({.perl}).join(', '), '1, 1/2, 1/3, 1/4, 1/5, 1/6', 'tricky sequence with one item and closure on the LHS';
52 is (1, { -$_ } ... 0).munch(4).join(', '), '1, -1, 1, -1', 'simple alternating sequence with one item and closure on the LHS';
53
54 is (1 ... 5.5, 6, 7).[^8].join(', '), '1, 2, 3, 4, 5, 6, 7, 8', 'simple sequence with two further terms on the RHS';
55 is (1 ... 5.5, 4, 3).[^8].join(', '), '1, 2, 3, 4, 5, 6, 7, 8', 'simple sequence with two extra terms on the RHS';
56 is (1 ... 5.5, 'xyzzy', 'plugh').[^8].join(', '), '1, 2, 3, 4, 5, 6, 7, 8', 'simple sequence with two weird items on the RHS';
57 }
58 # infinite sequence without limits
59
60 is (1 ... *).[^5].join(', '), '1, 2, 3, 4, 5', 'simple sequence with one item on the LHS';
61 is (1, 3 ... *).[^5].join(', '), '1, 3, 5, 7, 9', 'simple additive sequence with two items on the LHS';
62 is (1, 0 ... *).[^5].join(', '), '1, 0, -1, -2, -3', 'simple decreasing additive sequence with two items on the LHS';
63 is (1, 3, 5 ... *).[^5].join(', '), '1, 3, 5, 7, 9', 'simple additive sequence with three items on the LHS';
64 is (8, 7, 6 ... *).[^5].join(', '), '8, 7, 6, 5, 4', 'simple decreasing additive sequence with three items on the LHS';
65 is (1, 3, 9 ... *).[^5].join(', '), '1, 3, 9, 27, 81', 'simple multiplicative sequence with three items on the LHS';
66 is (81, 27, 9 ... *).[^5].join(', '), '81, 27, 9, 3, 1', 'decreasing multiplicative sequence with three items on the LHS';
67 is (1, { $_ + 2 } ... *).[^5].join(', '), '1, 3, 5, 7, 9', 'simple sequence with one item and block closure on the LHS';
68 is (1, *+2 ... *).[^5].join(', '), '1, 3, 5, 7, 9', 'simple sequence with one item and * closure on the LHS';
69 is (1, { $_ - 2 } ... *).[^5].join(', '), '1, -1, -3, -5, -7', 'simple sequence with one item and closure on the LHS';
70 is (1, 3, 5, { $_ + 2 } ... *).[^7].join(', '), '1, 3, 5, 7, 9, 11, 13', 'simple sequence with three items and block closure on the LHS';
71
72 is (1, { 1 / ((1 / $_) + 1) } ... *).[^5].map({.perl}).join(', '), '1, 1/2, 1/3, 1/4, 1/5', 'tricky sequence with one item and closure on the LHS';
73 is (1, { -$_ } ... *).[^5].join(', '), '1, -1, 1, -1, 1', 'simple alternating sequence with one item and closure on the LHS';
74
75 is (1 ... *, 6, 7).[^7].join(', '), '1, 2, 3, 4, 5, 6, 7', 'simple sequence with two further terms on the RHS';
76 is (1 ... *, 4, 3).[^7].join(', '), '1, 2, 3, 4, 5, 6, 7', 'simple sequence with two extra terms on the RHS';
77 is (1 ... *, 'xyzzy', 'plugh').[^7].join(', '), '1, 2, 3, 4, 5, 6, 7', 'simple sequence with two weird items on the RHS';
78
79 # constant sequence
80
81 is ('c', { $_ } ... *).[^10].join(', '), 'c, c, c, c, c, c, c, c, c, c', 'constant sequence started with letter and identity closure';
82 is ('c', 'c' ... *).[^10].join(', '), 'c, c, c, c, c, c, c, c, c, c', 'constant sequence started with two letters';
83 is ('c', 'c', 'c' ... *).[^10].join(', '), 'c, c, c, c, c, c, c, c, c, c', 'constant sequence started with three letters';
84 is (1, 1 ... *).[^10].join(', '), '1, 1, 1, 1, 1, 1, 1, 1, 1, 1', 'constant sequence started with two numbers';
85 is (1, 1, 1 ... *).[^10].join(', '), '1, 1, 1, 1, 1, 1, 1, 1, 1, 1', 'constant sequence started with three numbers';
86
87 # misleading starts
88
89 is (1, 1, 1, 2, 3 ... 10).[^10].join(', '), '1, 1, 1, 2, 3, 4, 5, 6, 7, 8', 'sequence started with three identical numbers, but then goes arithmetic';
90 is (1, 1, 1, 2, 4 ... 16).join(', '), '1, 1, 1, 2, 4, 8, 16', 'sequence started with three identical numbers, but then goes geometric';
91 is (4, 2, 1, 2, 4 ... 16).join(', '), '4, 2, 1, 2, 4, 8, 16', 'geometric sequence started in one direction and continues in the other';
92
93 # some tests taken from Spec
94
95 is (False, &prefix:<!> ... *).[^6].join(', '), (False, True, False, True, False, True).join(', '), "alternating False and True";
96 is (False, &prefix:<!> ... *).[^10].grep(Bool).elems, 10, "alternating False and True is always Bool";
97 is (1,2,&[+] ... 8).join(', ') , "1, 2, 3, 5, 8" , "Using &[+] works";
98 is (False, { !$_ } ... *).[^6].join(', '), (False, True, False, True, False, True).join(', '), "alternating False and True";
99 is (False, { !$_ } ... *).[^10].grep(Bool).elems, 10, "alternating False and True is always Bool";
100
101 # L<S03/List infix precedence/'"asymptotically approaching" is not the same as "equals"'>
102 # infinite sequence with limits
103
104 is ~(1, 1/2, 1/4 ... 0).[^5].map({.perl}), '1 1/2 1/4 1/8 1/16', 'geometric sequence that never reaches its limit';
105 is ~(1, -1/2, 1/4 ... 0).[^5].map({.perl}), '1 -1/2 1/4 -1/8 1/16', 'alternating geometric sequence that never reaches its limit';
106 is (1, { 1 / ((1 / $_) + 1) } ... 0).[^5].map({.perl}).join(', '), '1, 1/2, 1/3, 1/4, 1/5', '"harmonic" sequence that never reaches its limit';
107
108 # empty sequence
109
110 # L<S03/List infix precedence/'limit value is on the "wrong"'>
111 {
112 is (1, 2 ... 0).munch(3), (1,2,3), 'No more: limit value is on the wrong side';
113 }
114
115 # L<S03/List infix precedence/excludes the limit if it happens to match exactly>
116 # excluded limits via "...^"
117 {
118 is (1 ...^ 5).join(', '), '1, 2, 3, 4', 'exclusive sequence';
119 is (1 ...^ -3).join(', '), '1, 0, -1, -2', 'exclusive decreasing sequence';
120 is (1 ...^ 5.5).munch(6).join(', '), '1, 2, 3, 4, 5, 6', "exclusive sequence that couldn't hit its limit anyway";
121 is (1, 3, 9 ...^ 81).join(', '), '1, 3, 9, 27', 'exclusive geometric sequence';
122 is (81, 27, 9 ...^ 2).munch(5).join(', '), '81, 27, 9, 3, 1', "exclusive decreasing geometric sequence that couldn't hit its limit anyway";
123 is (2, -4, 8 ...^ 32).join(', '), '2, -4, 8, -16', 'exclusive alternating geometric sequence';
124 is (2, -4, 8 ...^ -32).munch(6).join(', '), '2, -4, 8, -16, 32, -64', 'exclusive alternating geometric sequence (not an exact match)';
125 is (1, { $_ + 2 } ...^ 9).join(', '), '1, 3, 5, 7', 'exclusive sequence with closure';
126 is (1 ...^ 1), (), 'empty exclusive sequence';
127 is (1, 1 ...^ 1), (), 'empty exclusive constant sequence';
128 is (1, 2 ...^ 0).munch(3), (1, 2, 3), 'empty exclusive arithmetic sequence';
129 is (1, 2 ...^ 0, 'xyzzy', 'plugh').[^3].join(', '), '1, 2, 3', 'exclusive sequence empty but for extra items';
130 is ~(1 ...^ 0), '1', 'singleton exclusive sequence';
131 is (4...^5).join(', '), '4', '4...^5 should parse as 4 ...^ 5 and not 4 ... ^5';
132 }
133
134
135 # RT #75698
136 ok ?(one((-5 ... ^5).flat) == 0), '-5 ... ^5 produces just one zero';
137
138 # RT #75316
139 isa_ok (1...()), Failure,
140 'empty list on right side of sequence operator does not cause infinite loop';
141
142 # RT #73508
143 is (1,2,4...*)[10], 1024,
144 'element from list generated using infinite sequence is accessible by index';
145
146 # RT #72914
147 is (4 ... ^5).join(', '), '4, 3, 2, 1, 0, 1, 2, 3, 4',
148 'geometric sequence started in one direction and continues in the other with exclusion';
149
150 lives_ok { (1 ... 5).perl }, 'Can take .perl of sequence';
151 is eval((1 ... 5).perl).join(','), '1,2,3,4,5',
152 'eval($sequence.perl) reproduces result list';
153
154 is ~((1 ... *) Z~ ('a' ... 'z')).munch(5), "1a 2b 3c 4d 5e", "Zipping two sequence in parallel";
155
156 {
157 is (1, 2, 4 ... 3).munch(4), (1, 2, 4, 8), "sequence that does not hit the limit";
158 is (1, 2, 4 ... 2), (1, 2), "sequence that aborts during LHS";
159
160 is (1, 2, 4 ... 1.5).munch(4), (1,2,4,8), "sequence that does not hit the limit";
161 is (1, 2, 4 ... 1), (1), "sequence that aborts during LHS";
162
163 is ~(1, -2, 4 ... 1), '1', 'geometric sequence with smaller RHS and sign change';
164 is ~(1, -2, 4 ... 2).munch(4), '1 -2 4 -8', 'geometric sequence with smaller RHS and sign change';
165 is ~(1, -2, 4 ... 3).munch(4), '1 -2 4 -8', 'geometric sequence with smaller RHS and sign change';
166 is ~(1, -2, 4 ... 25).munch(10), '1 -2 4 -8 16 -32 64 -128 256 -512', 'geometric sequence with sign-change and non-matching end point';
167
168 is (1, 2, 4, 5, 6 ... 2), (1, 2), "sequence that aborts during LHS, before actual calculations kick in";
169
170 is (1, 2, 4, 5, 6 ... 3).munch(6), (1,2,4,5,6,7), "sequence that aborts during LHS, before actual calculations kick in";
171 }
172
173 # tests for the types returned
174
175 {
176 my @a = 1, 2, 3 ... 100;
177 is @a.elems, 100, "1, 2, 3 ... 100 generates a sequence with one hundred elements...";
178 is @a.grep(Int).elems, 100, "... all of which are Ints";
179 }
180
181 {
182 my @a = 1.Rat, 2.Rat, 3.Rat ... 100;
183 is @a.elems, 100, "1.Rat, 2.Rat, 3.Rat ... 100 generates a sequence with one hundred elements...";
184 is @a.grep(Rat).elems, 100, "... all of which are Rats";
185 }
186
187 {
188 my @a = 1.Num, 2.Num, 3.Num ... 100;
189 is @a.elems, 100, "1.Num, 2.Num, 3.Num ... 100 generates a sequence with one hundred elements...";
190 is @a.grep(Num).elems, 100, "... all of which are Nums";
191 }
192
193 {
194 my @a = 1, 2, 4 ... 64;
195 is @a.elems, 7, "1, 2, 4 ... 64 generates a sequence with seven elements...";
196 is @a.grep(Int).elems, @a.elems, "... all of which are Ints";
197 }
198
199 {
200 my @a = 1.Rat, 2.Rat, 4.Rat ... 64;
201 is @a.elems, 7, "1.Rat, 2.Rat, 4.Rat ... 64 generates a sequence with seven elements...";
202 is @a.grep(Rat).elems, 7, "... all of which are Rats";
203 }
204
205 {
206 my @a = 1.Num, 2.Num, 4.Num ... 64;
207 is @a.elems, 7, "1.Num, 2.Num, 4.Num ... 64 generates a sequence with seven elements...";
208 is @a.grep(Num).elems, 7, "... all of which are Nums";
209 }
210
211 # RT #74606
212 is (1, +* ... *).[^5].join('|'), (1 xx 5).join('|'),
213 '1, +* works for sequence';
214
215 # RT #75768
216 is ~(1...10)[2...4], '3 4 5', 'can index sequence with sequence';
217
218 {
219 is (1, 2 ... *>=5), (1,2,3,4,5), "sequence with code on the rhs";
220 is (1, 2 ... *>5), (1,2,3,4,5,6), "sequence with code on the rhs";
221 is (1, 2 ...^ *>=5), (1,2,3,4), "exclusive sequence with code on the rhs";
222 is (1, 2 ...^ *>5), (1,2,3,4,5), "exclusive sequence with code on the rhs";
223 }
224
225 is (1, 2 , {last if $_>=5; $_+1} ... *), (1,2,3,4,5), "sequence that lasts in the last item of lhs";
226
227 {
228 is (1..* ... 5), (1, 2, 3, 4, 5), '1..* ... 5';
229 my @fib := (0, 1, *+* ... * );
230 is (@fib ... 8), (0 , 1, 1, 2 , 3, 5, 8), '@fib ... 8';
231 }
232
ff0784a @patrickas unfudge test
patrickas authored
233 # RT #78324
1b6274b @patrickas added test for RT #78324
patrickas authored
234 is (32,16,8 ...^ Rat), (32,16,8) , 'When the limit is a type we do not get an infinite sequence';
235
ef86bef @moritz [sequence] is the new series
moritz authored
236 # RT #75828
237 eval_dies_ok '1, 2, 3, ... 5', 'comma before sequence operator is caught';
238
239 done_testing;
240
241 # vim: ft=perl6
Something went wrong with that request. Please try again.