[t/spec] Remove the weird 5..1 !~~ 3 brand of Range tests, on the basis that none...
[pugs.git] / t / spec / S02-builtin_data_types / range.t
blob8822dc52b6dc4b7caedca803747ee532a32ddcc0
1 use v6;
3 use Test;
5 plan 74;
7 # basic Range
8 # L<S02/Immutable types/A pair of Ordered endpoints>
10 my $r = 1..5;
11 isa_ok $r, Range, 'Type';
12 is $r.WHAT, Range, 'Type';
13 is $r.perl, '1..5', 'canonical representation';
15 # XXX unspecced: exact value of Range.perl
16 is (1..5).perl, '1..5', ".perl ..";
17 is (1^..5).perl, '1^..5', ".perl ^..";
18 is (1..^5).perl, '1..^5', ".perl ..^";
19 is (1^..^5).perl, '1^..^5', ".perl ^..^";
21 my @r = $r;
22 is @r, [1, 2, 3, 4, 5], 'got the right array';
24 # Range of Str
26 $r = 'a'..'c';
27 isa_ok $r, Range;
28 # XXX unspecced: exact value of Range.perl
29 is $r.perl, '"a".."c"', 'canonical representation';
30 @r = $r;
31 is @r, [< a b c >], 'got the right array';
33 # Stationary ranges
34 is (1..1).perl, '1..1', "stationary num .perl ..";
35 is (1..1), [1,], 'got the right array';
36 is ('a'..'a').perl, '"a".."a"', "stationary str .perl ..";
37 #?rakudo todo "Not sure if this should work, but it definitely doesn't right now"
38 is ('a'..'a'), [< a >], 'got the right array';
40 #?rakudo skip "modifer for NYI"
42     my $x = 0;
43     $x++ for (1..4).reverse;
44     is $x, 4, '(1..4).reverse still turns into a list of four items';
45     my $y = 0;
46     $y++ for @( eval((1..4).reverse.perl) );
47     is $y, 4, '(1..4).reverse.perl returns something useful';
50 # ACCEPTS and equals tests
52     my $r = 1..5;
53     ok(($r).ACCEPTS($r), 'accepts self');
54     ok(($r).ACCEPTS(1..5), 'accepts same');
55     ok($r ~~ $r, 'accepts self');
56     ok($r ~~ 1..5, 'accepts same');
57     # TODO check how to avoid "eager is", test passes but why?
58     is($r, $r, "equals to self");
59     my $s = 1..5;
60     is($r, $s, "equals");
64 # Range in comparisons
65 ok((1..5).ACCEPTS(3), 'int in range');
66 ok(3 ~~ 1..5, 'int in range');
67 ok(3 !~~ 6..8, 'int not in range');
69 ok(('a'..'z').ACCEPTS('x'), 'str in range');
70 ok('x' ~~ 'a'..'z', 'str in range');
71 ok('x' !~~ 'a'..'c', 'str not in range');
72 ok(('aa'..'zz').ACCEPTS('ax'), 'str in range');
73 ok(('a'..'zz').ACCEPTS('ax'), 'str in range');
76 is(+(6..6), 1, 'numification');
77 is(+(6^..6), 0, 'numification');
78 is(+(6..^6), 0, 'numification');
79 is(+(6..8), 3, 'numification');
81 # immutability
83     my $r = 1..5;
85     dies_ok { $r.shift       }, 'range is immutable (shift)';
86     dies_ok { $r.pop         }, 'range is immutable (pop)';
87     dies_ok { $r.push(10)    }, 'range is immutable (push)';
88     dies_ok { $r.unshift(10) }, 'range is immutable (unshift)';
90     my $s = 1..5;
91     is $r, $s, 'range has not changed';
94 # simple .to, .from
96     my $r = 1 .. 5;
97     is($r.from, 1, 'range.from');
98     is($r.to,   5, 'range.to');
100     is($r.min, 1, 'range.min');
101     is($r.max, 5, 'range.max');
102     is($r.minmax, (1,5), 'range.minmax');
104     #?rakudo 5 skip 'range reverse not in spec'
105     ### pmichaud, 2008-07-04:  XXX  no spec for .reverse
106     is($r.reverse.from, 5, 'range.reverse.from');
107     is($r.reverse.to,   1, 'range.reverse.to');
108     ### pmichaud, 2008-07-04:  XXX  doesn't test reversed min/max/minmax
109     is($r.min, 1, 'range.reverse.min');
110     is($r.max, 5, 'range.reverse.max');
111     is($r.minmax, (1,5), 'range.reverse.minmax');
114 # uneven ranges
116     my $r = 1 .. 4.5;
117     is($r.from, 1, 'uneven range.from');
118     is($r.to, 4.5, 'uneven range.to');
120     is($r.min, 1,   'range.min');
121     is($r.max, 4.5, 'range.max');
122     is($r.minmax, (1, 4.5), 'range.minmax');
124     #?rakudo 2 skip '.reverse on ranges'
125     is($r.reverse.from, 4.5, 'uneven range.reverse.from');
126     is($r.reverse.to,   1,   'uneven range.reverse.to');
129 # infinite ranges
131     my $inf = -Inf..Inf;
133     is($inf.from, -Inf, 'bottom end of -Inf..Inf is -Inf (1)');
134     is($inf.to, Inf, 'top end of -Inf..Inf is Inf (1)');
136     ok(42  ~~ $inf, 'positive integer matches -Inf..Inf');
137     ok(.2  ~~ $inf, 'positive non-int matches -Inf..Inf');
138     ok(-2  ~~ $inf, 'negative integer matches -Inf..Inf');
139     #?rakudo todo "ng thinks -Inf is not before -.2"
140     ok(-.2 ~~ $inf, 'negative non-int matches -Inf..Inf');
143 # infinite ranges using Whatever
144 #?rakudo skip "infinite ranges not implemented"
146     my $inf = *..*;
148     is($inf.from, -Inf, 'bottom end of *..* is -Inf (1)');
149     is($inf.to, Inf, 'top end of *..* is Inf (1)');
151     is($inf.elems, Inf, 'testing number of elements');
153     ok(42  ~~ $inf, 'positive integer matches *..*');
154     ok(.2  ~~ $inf, 'positive non-int matches *..*');
155     ok(-2  ~~ $inf, 'negative integer matches *..*');
156     ok(-.2 ~~ $inf, 'negative non-int matches *..*');
159 # ranges constructed from parameters, from RT#63002.
161     sub foo($a) { ~($a .. 5) };
162     is(foo(5), '5', 'range constructed from parameter OK');
165 # ranges constructed from parameters, #2
167     for 1 -> $i {
168         for $i..5 -> $j { };
169         is($i, 1, 'Iter range from param doesnt modify param (RT #66280)');
170     }
173 #?rakudo skip "Neither *-1 or slices work yet in ng"
175     is((1..8)[*-1], 8, 'postcircumfix:<[ ]> on range works');
176     is((1..8)[1,3], [2,4], 'postcircumfix:<[ ]> on range works');
179 # vim:set ft=perl6