/
range.t
456 lines (376 loc) · 16.6 KB
/
range.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
use v6;
use Test;
plan 192;
# basic Range
# L<S02/Immutable types/A pair of Ordered endpoints>
my $r = 1..5;
isa-ok $r, Range, 'Type';
is $r.WHAT.gist, Range.gist, 'Type';
is $r.perl, '1..5', 'canonical representation';
# XXX unspecced: exact value of Range.perl
is (1..5).perl, '1..5', ".perl ..";
is (1^..5).perl, '1^..5', ".perl ^..";
is (1..^5).perl, '1..^5', ".perl ..^";
is (1^..^5).perl, '1^..^5', ".perl ^..^";
my @r = $r;
is @r.perl, "[1..5,]", 'got the right array';
# Range of Str
$r = 'a'..'c';
isa-ok $r, Range;
# XXX unspecced: exact value of Range.perl
is $r.perl, '"a".."c"', 'canonical representation';
@r = $r;
is @r.perl, '["a".."c",]', 'got the right array';
# Stationary ranges
is (1..1).perl, '1..1', "stationary num .perl ..";
is (1..1), [1,], 'got the right array';
is ('a'..'a').perl, '"a".."a"', "stationary str .perl ..";
is ('a'..'a'), "a", 'got the right stationary string';
{
my $x = 0;
$x++ for (1..4).reverse;
is $x, 4, '(1..4).reverse still turns into a list of four items';
my $y = 0;
$y++ for @( EVAL((1..4).reverse.perl) );
is $y, 4, '(1..4).reverse.perl returns something useful';
}
# ACCEPTS and equals tests
{
my $r = 1..5;
ok(($r).ACCEPTS($r), 'accepts self');
ok(($r).ACCEPTS(1..5), 'accepts same');
ok($r ~~ $r, 'accepts self');
ok($r ~~ 1..5, 'accepts same');
# TODO check how to avoid "eager is", test passes but why?
is($r, $r, "equals to self");
my $s = 1..5;
is($r, $s, "equals");
}
# Range in comparisons
ok((1..5).ACCEPTS(3), 'int in range');
ok(3 ~~ 1..5, 'int in range');
ok(3 !~~ 6..8, 'int not in range');
ok(('a'..'z').ACCEPTS('x'), 'str in range');
ok('x' ~~ 'a'..'z', 'str in range');
ok('x' !~~ 'a'..'c', 'str not in range');
ok(('aa'..'zz').ACCEPTS('ax'), 'str in range');
ok(('a'..'zz').ACCEPTS('ax'), 'str in range');
is(+(6..6), 1, 'numification');
is(+(6^..6), 0, 'numification');
is(+(6..^6), 0, 'numification');
is(+(6..^6.1), 1, 'numification');
is(+(6..8), 3, 'numification');
is(+(1^..10), 9, 'numification');
is(+(1..^10), 9, 'numification');
is(+(1^..^10), 8, 'numification');
is(+(10..9), 0, 'numification');
is(+(1.2..4), 3, 'numification');
is(+(1..^3.3), 3, 'numification');
is(+(2.3..3.1), 1, 'numification');
is(+Range, 0, 'type numification');
# immutability
{
my $r = 1..5;
for <push pop shift unshift append prepend> -> $method {
throws-like { $r."$method"(42) }, X::Immutable,
method => $method,
typename => 'Range',
"range is immutable ($method)",
;
}
throws-like { $r.min = 2 }, X::Assignment::RO, "range.min ro";
throws-like { $r.max = 4 }, X::Assignment::RO, "range.max ro";
throws-like { $r.excludes-min = True }, X::Assignment::RO,
"range.excludes-min ro";
throws-like { $r.excludes-max = True }, X::Assignment::RO,
"range.excludes-max ro";
my $s = 1..5;
is $r, $s, 'range has not changed';
}
# simple range
{
my $r = 1 .. 5;
is($r.min, 1, 'range.min');
is($r.max, 5, 'range.max');
is($r.bounds, (1,5), 'range.bounds');
}
# uneven ranges
{
my $r = 1 .. 4.5;
is($r.min, 1, 'range.min');
is($r.max, 4.5, 'range.max');
is($r.bounds, (1, 4.5), 'range.bounds');
}
# infinite ranges
{
my $inf = -Inf..Inf;
ok(42 ~~ $inf, 'positive integer matches -Inf..Inf');
ok(.2 ~~ $inf, 'positive non-int matches -Inf..Inf');
ok(-2 ~~ $inf, 'negative integer matches -Inf..Inf');
ok(-.2 ~~ $inf, 'negative non-int matches -Inf..Inf');
}
# infinite ranges using Whatever
{
my $inf = *..*;
is($inf.min, -Inf, 'bottom end of *..* is -Inf (1)');
is($inf.max, Inf, 'top end of *..* is Inf (1)');
throws-like $inf.elems, X::Cannot::Lazy, :action<.elems>;
ok(42 ~~ $inf, 'positive integer matches *..*');
ok(.2 ~~ $inf, 'positive non-int matches *..*');
ok(-2 ~~ $inf, 'negative integer matches *..*');
ok(-.2 ~~ $inf, 'negative non-int matches *..*');
}
# ranges constructed from parameters, from RT #63002
{
sub foo($a) { ~($a .. 5) };
is(foo(5), '5', 'range constructed from parameter OK');
}
# ranges constructed from parameters, #2
{
for 1 -> $i {
for $i..5 -> $j { };
is($i, 1, 'Iter range from param doesnt modify param (RT #66280)');
}
}
{
is((1..8)[*-1], 8, 'postcircumfix:<[ ]> on range works');
is((1..8)[1,3], [2,4], 'postcircumfix:<[ ]> on range works');
}
{
my @b = pick(*, 1..100);
is @b.elems, 100, "pick(*, 1..100) returns the correct number of elements";
is ~@b.sort, ~(1..100), "pick(*, 1..100) returns the correct elements";
is ~@b.grep(Int).elems, 100, "pick(*, 1..100) returns Ints";
@b = (1..100).pick(*);
is @b.elems, 100, "pick(*, 1..100) returns the correct number of elements";
is ~@b.sort, ~(1..100), "pick(*, 1..100) returns the correct elements";
is ~@b.grep(Int).elems, 100, "pick(*, 1..100) returns Ints";
isa-ok (1..100).pick, Int, "picking a single element from an range of Ints produces an Int";
ok (1..100).pick ~~ 1..100, "picking a single element from an range of Ints produces one of them";
isa-ok (1..100).pick(1), Seq, "picking 1 from an range of Ints produces a Seq";
ok (1..100).pick(1)[0] ~~ 1..100, "picking 1 from an range of Ints produces one of them";
my @c = (1..100).pick(2);
isa-ok @c[0], Int, "picking 2 from an range of Ints produces an Int...";
isa-ok @c[1], Int, "... and an Int";
ok (@c[0] ~~ 1..100) && (@c[1] ~~ 1..100), "picking 2 from an range of Ints produces two of them";
ok @c[0] != @c[1], "picking 2 from an range of Ints produces two distinct results";
is (1..100).pick("25").elems, 25, ".pick works Str arguments";
is pick("25", 1..100).elems, 25, "pick works Str arguments";
}
{
my @b = pick(*, 'b' .. 'y');
is @b.elems, 24, "pick(*, 'b' .. 'y') returns the correct number of elements";
is ~@b.sort, ~('b' .. 'y'), "pick(*, 'b' .. 'y') returns the correct elements";
is ~@b.grep(Str).elems, 24, "pick(*, 'b' .. 'y') returns Strs";
@b = ('b' .. 'y').pick(*);
is @b.elems, 24, "pick(*, 'b' .. 'y') returns the correct number of elements";
is ~@b.sort, ~('b' .. 'y'), "pick(*, 'b' .. 'y') returns the correct elements";
is ~@b.grep(Str).elems, 24, "pick(*, 'b' .. 'y') returns Strs";
isa-ok ('b' .. 'y').pick, Str, "picking a single element from an range of Strs produces an Str";
ok ('b' .. 'y').pick ~~ 'b' .. 'y', "picking a single element from an range of Strs produces one of them";
isa-ok ('b' .. 'y').pick(1), Seq, "picking 1 from an range of Strs produces a Seq";
ok ('b' .. 'y').pick(1)[0] ~~ 'b' .. 'y', "picking 1 from an range of Strs produces one of them";
my @c = ('b' .. 'y').pick(2);
isa-ok @c[0], Str, "picking 2 from an range of Strs produces an Str...";
isa-ok @c[1], Str, "... and an Str";
ok (@c[0] ~~ 'b' .. 'y') && (@c[1] ~~ 'b' .. 'y'), "picking 2 from an range of Strs produces two of them";
ok @c[0] ne @c[1], "picking 2 from an range of Strs produces two distinct results";
is ('b' .. 'y').pick("10").elems, 10, ".pick works Str arguments";
is pick("10", 'b' .. 'y').elems, 10, "pick works Str arguments";
}
{
my @b = roll(100, 1..100);
is @b.elems, 100, "roll(100, 1..100) returns the correct number of elements";
is ~@b.grep(1..100).elems, 100, "roll(100, 1..100) returns elements from 1..100";
is ~@b.grep(Int).elems, 100, "roll(100, 1..100) returns Ints";
@b = (1..100).roll(100);
is @b.elems, 100, "roll(100, 1..100) returns the correct number of elements";
is ~@b.grep(1..100).elems, 100, "roll(100, 1..100) returns elements from 1..100";
is ~@b.grep(Int).elems, 100, "roll(100, 1..100) returns Ints";
isa-ok (1..100).roll, Int, "rolling a single element from an range of Ints produces an Int";
ok (1..100).roll ~~ 1..100, "rolling a single element from an range of Ints produces one of them";
isa-ok (1..100).roll(1), Seq, "rolling 1 from an range of Ints produces a Seq";
ok (1..100).roll(1)[0] ~~ 1..100, "rolling 1 from an range of Ints produces one of them";
my @c = (1..100).roll(2);
isa-ok @c[0], Int, "rolling 2 from an range of Ints produces an Int...";
isa-ok @c[1], Int, "... and an Int";
ok (@c[0] ~~ 1..100) && (@c[1] ~~ 1..100), "rolling 2 from an range of Ints produces two of them";
is (1..100).roll("25").elems, 25, ".roll works Str arguments";
is roll("25", 1..100).elems, 25, "roll works Str arguments";
}
{
my @b = roll(100, 'b' .. 'y');
is @b.elems, 100, "roll(100, 'b' .. 'y') returns the correct number of elements";
is ~@b.grep('b' .. 'y').elems, 100, "roll(100, 'b' .. 'y') returns elements from b..y";
is ~@b.grep(Str).elems, 100, "roll(100, 'b' .. 'y') returns Strs";
@b = ('b' .. 'y').roll(100);
is @b.elems, 100, "roll(100, 'b' .. 'y') returns the correct number of elements";
is ~@b.grep('b' .. 'y').elems, 100, "roll(100, 'b' .. 'y') returns elements from b..y";
is ~@b.grep(Str).elems, 100, "roll(100, 'b' .. 'y') returns Strs";
isa-ok ('b' .. 'y').roll, Str, "rolling a single element from an range of Strs produces an Str";
ok ('b' .. 'y').roll ~~ 'b' .. 'y', "rolling a single element from an range of Strs produces one of them";
isa-ok ('b' .. 'y').roll(1), Seq, "rolling 1 from an range of Strs produces a Seq";
ok ('b' .. 'y').roll(1)[0] ~~ 'b' .. 'y', "rolling 1 from an range of Strs produces one of them";
my @c = ('b' .. 'y').roll(2);
isa-ok @c[0], Str, "rolling 2 from an range of Strs produces an Str...";
isa-ok @c[1], Str, "... and an Str";
ok (@c[0] ~~ 'b' .. 'y') && (@c[1] ~~ 'b' .. 'y'), "rolling 2 from an range of Strs produces two of them";
is ('b' .. 'y').roll("10").elems, 10, ".roll works Str arguments";
is roll("10", 'b' .. 'y').elems, 10, "roll works Str arguments";
}
# Range.roll(*)/roll(N)/pick(N) with large number, from R#2090
{
my $range = (1 +< 125) .. ( 1 +< 126 -1 );
lives-ok { $range.pick(42) }, 'Range.pick(N) lives for vast range';
lives-ok { $range.roll(42) }, 'Range.roll(N) lives for vast range';
lives-ok { $range.roll(*).head(10) }, 'Range.roll(*) lives for vast range';
lives-ok { ($range.roll xx *).head(10) }, '(Range.roll xx *) lives for vast range';
}
is join(':',grep 1..3, 0..5), '1:2:3', "ranges itemize or flatten lazily";
lives-ok({'A'..'a'}, "A..a range completes");
lives-ok({"\0".."~"}, "low ascii range completes");
# shifting and scaling intervals
{
my $r = 1..10;
is ($r + 5).gist, '6..15', "can shift a left .. range up";
is (5 + $r).gist, '6..15', "can shift a right .. range up";
is ($r * 2).gist, '2..20', "can scale a left .. range up";
is (2 * $r).gist, '2..20', "can scale a right .. range up";
is ($r - 1).gist, '0..9', "can shift a .. range down";
is ($r / 2).gist, '0.5..5.0', "can scale a .. range down";
$r = 1..^10;
is ($r + 5).gist, '6..^15', "can shift a left ..^ range up";
is (5 + $r).gist, '6..^15', "can shift a right ..^ range up";
is ($r * 2).gist, '2..^20', "can scale a left ..^ range up";
is (2 * $r).gist, '2..^20', "can scale a right ..^ range up";
is ($r - 1).gist, '^9', "can shift a ..^ range down";
is ($r / 2).gist, '0.5..^5.0', "can scale a ..^ range down";
$r = 1^..10;
is ($r + 5).gist, '6^..15', "can shift a left ^.. range up";
is (5 + $r).gist, '6^..15', "can shift a right ^.. range up";
is ($r * 2).gist, '2^..20', "can scale a left ^.. range up";
is (2 * $r).gist, '2^..20', "can scale a right ^.. range up";
is ($r - 1).gist, '0^..9', "can shift a ^.. range down";
is ($r / 2).gist, '0.5^..5.0', "can scale a ^.. range down";
$r = 1^..^10;
is ($r + 5).gist, '6^..^15', "can shift a left ^..^ range up";
is (5 + $r).gist, '6^..^15', "can shift a right ^..^ range up";
is ($r * 2).gist, '2^..^20', "can scale a left ^..^ range up";
is (2 * $r).gist, '2^..^20', "can scale a right ^..^ range up";
is ($r - 1).gist, '0^..^9', "can shift a ^..^ range down";
is ($r / 2).gist, '0.5^..^5.0', "can scale a ^..^ range down";
}
{
sub test($range,$min,$max,$minbound,$maxbound) {
subtest {
plan 5;
ok $range.is-int, "is $range.gist() an integer range";
is $range.min, $min, "is $range.gist().min $min";
is $range.max, $max, "is $range.gist().max $max";
my ($low,$high) = $range.int-bounds;
is $low, $minbound, "is $range.gist().int-bounds[0] $minbound";
is $high, $maxbound, "is $range.gist().int-bounds[1] $maxbound";
}, "Testing min, max, int-bounds on $range.gist()";
}
test( ^10, 0, 10, 0, 9);
test( -1..10, -1, 10, -1, 10);
test( -1^..10, -1, 10, 0, 10);
test( -1..^10, -1, 10, -1, 9);
test(-1^..^10, -1, 10, 0, 9);
}
{
ok 0 <= (^10).rand < 10, 'simple rand';
ok 1 < (1..10).rand < 10, 'no borders excluded';
ok 0.1 < (0.1^..0.3).rand <= 0.3, 'lower border excluded';
throws-like ("a".."z").rand, Exception, 'cannot rand on string range';
}
{
is (1..10).minmax, '1 10', "simple Range.minmax on Ints";
is (3.5..4.5).minmax, '3.5 4.5', "simple Range.minmax on Rats";
is (3.5e1..4.5e1).minmax, '35 45', "simple Range.minmax on Reals";
is ("a".."z").minmax, 'a z', "simple Range.minmax on Strs";
is (-Inf..Inf).minmax, '-Inf Inf', "simple Range.minmax on Nums";
is (^10).minmax, '0 9', "Range.minmax on Ints with exclusion";
dies-ok { ^Inf .minmax }, "cannot have exclusions for minmax otherwise";
}
# RT #126990
is-deeply Int.Range, -Inf^..^Inf, 'Int.range is -Inf^..^Inf';
# RT #128887
is-deeply (eager (^10+5)/2), (2.5, 3.5, 4.5, 5.5, 6.5),
'Rat range constructed with Range ops does not explode';
# RT #129104
subtest '.rand does not generate value equal to excluded endpoints' => {
plan 3;
my $seen = 0;
for ^10000 { $seen = 1 if (1..^(1+10e-15)).rand == 1+10e-15 };
ok $seen == 0, '..^ range';
$seen = 0;
for ^10000 { $seen = 1 if (1^..(1+10e-15)).rand == 1 };
ok $seen == 0, '^.. range';
$seen = 0;
for ^10000 {
my $v = (1^..^(1+10e-15)).rand;
$seen = 1 if $v == 1 or $v == 1+10e-15
};
ok $seen == 0, '^..^ range';
}
subtest 'out of range AT-POS' => {
plan 7;
throws-like { (^5)[*-9] }, X::OutOfRange, 'effective negative index throws';
is-deeply (2..1)[^2], (Nil, Nil),
'index larger than range returns Nil (Int range)';
is-deeply (2.1..1.1)[^2], (Nil, Nil),
'index larger than range returns Nil (Rat range)';
is-deeply (2e0..1e0)[^2], (Nil, Nil),
'index larger than range returns Nil (Num range)';
my int $i2 = 2;
my int $i5 = 5;
is-deeply (2..1)[$i2, $i5], (Nil, Nil),
'index larger than range returns Nil (Int range, native int index)';
is-deeply (2.1..1.1)[$i2, $i5], (Nil, Nil),
'index larger than range returns Nil (Rat range, native int index)';
is-deeply (2e0..1e0)[$i2, $i5], (Nil, Nil),
'index larger than range returns Nil (Num range, native int index)';
}
subtest 'Complex smartmatch against Range' => {
my @false = [i, 1..10], [i, -2e300.Int..2e300.Int], [i, -2e300.Int..2e300.Int],
[<0+0i>, 1..10], [i, 'a'..Inf], [i, 'a'..'z'];
# these cases are true because the imaginary part is small enough that
# we can convert these Complex into Real
my @true = [<0+0i>, -1..10], [<42+0i>, 10..50],
[<42+0.0000000000000001i>, 40..50], [<42+0i>, 10e0..50e0];
plan @false + @true;
for @false -> $t {
is-deeply ($t[0] ~~ $t[1]), False, "{$t[0].perl} ~~ {$t[1].perl}";
}
for @true -> $t {
is-deeply ($t[0] ~~ $t[1]), True, "{$t[0].perl} ~~ {$t[1].perl}";
}
}
# https://irclog.perlgeek.de/perl6-dev/2017-03-16#i_14277938
subtest 'no .int-bounds for Infs and NaN as Range endpoints' => {
my @ranges = NaN.. NaN, NaN..1, 1..NaN, NaN..Inf, NaN..-Inf,
-Inf..-Inf, -Inf..1, 1..-Inf, -Inf..NaN, -Inf.. Inf,
Inf.. Inf, Inf..1, 1.. Inf, Inf..NaN, Inf.. Inf;
plan 1 + @ranges;
throws-like { .int-bounds }, Exception, "{.perl} throws" for @ranges;
# https://github.com/rakudo/rakudo/commit/16ef21c162
is-deeply (0..5.5).int-bounds, (0, 5),
'we can get int-bounds from non-int range with `0` end-point';
}
# RT #125336
subtest 'no floating point drifts in degenerate Ranges' => {
plan 3;
(NaN..NaN).map: {
next unless $++ > 1050;
is-deeply $_, NaN, 'NaN..NaN range keeps producing NaN ad infinitum';
last;
}
(-Inf..0).map: {
next unless $++ > 1050;
is-deeply $_, -Inf, '-Inf..Inf range keeps producing -Inf ad infinitum';
last;
}
is-deeply (Inf..0).elems, 0, 'Inf..0 Range has zero elems'
}
# vim:set ft=perl6