8 # L<S02/Immutable types/A pair of Ordered endpoints>
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 ^..^";
22 is @r, [1, 2, 3, 4, 5], 'got the right array';
28 # XXX unspecced: exact value of Range.perl
29 is $r.perl, '"a".."c"', 'canonical representation';
31 is @r, [< a b c >], 'got the right array';
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"
43 $x++ for (1..4).reverse;
44 is $x, 4, '(1..4).reverse still turns into a list of four items';
46 $y++ for @( eval((1..4).reverse.perl) );
47 is $y, 4, '(1..4).reverse.perl returns something useful';
50 # ACCEPTS and equals tests
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");
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');
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)';
91 is $r, $s, 'range has not changed';
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');
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');
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"
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
169 is($i, 1, 'Iter range from param doesnt modify param (RT #66280)');
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');