6 set_up_inc('../lib', '.');
8 # Avoid using eq_array below as it uses .. internally.
14 is(join(':',1..5), '1:2:3:4:5');
16 @foo = (1,2,3,4,5,6,7,8,9);
17 @foo[2..4] = ('c','d','e');
19 is(join(':',@foo[$foo[0]..5]), '2:c:d:e:6');
21 @bar[2..4] = ('c','d','e');
22 is(join(':',@bar[1..5]), ':c:d:e:');
24 ($a,@bcd[0..2],$e) = ('a','b','c','d','e');
25 is(join(':',$a,@bcd[0..2],$e), 'a:b:c:d:e');
39 $x = join('','a'..'z');
40 is($x, 'abcdefghijklmnopqrstuvwxyz');
43 is (scalar @x, 27 * 26);
46 use feature 'unicode_strings';
49 utf8::upgrade($e) if $_;
51 is (scalar @x, 26, "list-context range with rhs 0xFF, utf8=$_");
56 is(join(",", @y), join(",", @x), "foreach range with rhs 0xFF, utf8=$_");
59 @x = '09' .. '08'; # should produce '09', '10',... '99' (strange but true)
60 is(join(",", @x), join(",", map {sprintf "%02d",$_} 9..99));
62 # same test with foreach (which is a separate implementation)
64 foreach ('09'..'08') {
67 is(join(",", @y), join(",", @x));
70 if ($Config{ivsize} == 8) {
71 @a = eval "0x7ffffffffffffffe..0x7fffffffffffffff";
72 $a = "9223372036854775806 9223372036854775807";
73 @b = eval "-0x7fffffffffffffff..-0x7ffffffffffffffe";
74 $b = "-9223372036854775807 -9223372036854775806";
77 @a = eval "0x7ffffffe..0x7fffffff";
78 $a = "2147483646 2147483647";
79 @b = eval "-0x7fffffff..-0x7ffffffe";
80 $b = "-2147483647 -2147483646";
90 local $SIG{'__WARN__'} = sub { $bad = 1 };
92 $x =~ s/(\w)-(\w)/join ':', $1 .. $2/e;
96 # Should use magical autoinc only when both are strings
98 my $scalar = (() = "0"..-1);
103 for my $x ("0"..-1) {
109 # [#18165] Should allow "-4".."0", broken by #4730. (AMS 20021031)
110 is(join(":","-4".."0") , "-4:-3:-2:-1:0");
111 is(join(":","-4".."-0") , "-4:-3:-2:-1:0");
112 is(join(":","-4\n".."0\n") , "-4:-3:-2:-1:0");
113 is(join(":","-4\n".."-0\n"), "-4:-3:-2:-1:0");
115 # undef should be treated as 0 for numerical range
116 is(join(":",undef..2), '0:1:2');
117 is(join(":",-2..undef), '-2:-1:0');
118 is(join(":",undef..'2'), '0:1:2');
119 is(join(":",'-2'..undef), '-2:-1:0');
121 # undef should be treated as "" for magical range
122 is(join(":", map "[$_]", "".."B"), '[]');
123 is(join(":", map "[$_]", undef.."B"), '[]');
124 is(join(":", map "[$_]", "B"..""), '');
125 is(join(":", map "[$_]", "B"..undef), '');
127 # undef..undef used to segfault
128 is(join(":", map "[$_]", undef..undef), '[]');
130 # also test undef in foreach loops
131 @foo=(); push @foo, $_ for undef..2;
132 is(join(":", @foo), '0:1:2');
134 @foo=(); push @foo, $_ for -2..undef;
135 is(join(":", @foo), '-2:-1:0');
137 @foo=(); push @foo, $_ for undef..'2';
138 is(join(":", @foo), '0:1:2');
140 @foo=(); push @foo, $_ for '-2'..undef;
141 is(join(":", @foo), '-2:-1:0');
143 @foo=(); push @foo, $_ for undef.."B";
144 is(join(":", map "[$_]", @foo), '[]');
146 @foo=(); push @foo, $_ for "".."B";
147 is(join(":", map "[$_]", @foo), '[]');
149 @foo=(); push @foo, $_ for "B"..undef;
150 is(join(":", map "[$_]", @foo), '');
152 @foo=(); push @foo, $_ for "B".."";
153 is(join(":", map "[$_]", @foo), '');
155 @foo=(); push @foo, $_ for undef..undef;
156 is(join(":", map "[$_]", @foo), '[]');
161 @foo=(); push @foo, $_ for undef..$#a;
162 is(join(":", @foo), '0:1:2');
166 @foo=(); push @foo, $_ for $#a..undef;
167 is(join(":", @foo), '-1:0');
172 @foo=(); push @foo, $_ for undef..$1;
173 is(join(":", @foo), '0:1:2');
178 @foo=(); push @foo, $_ for $1..undef;
179 is(join(":", @foo), '-2:-1:0');
184 @foo=(); push @foo, $_ for undef..$1;
185 is(join(":", map "[$_]", @foo), '[]');
190 @foo=(); push @foo, $_ for ""..$1;
191 is(join(":", map "[$_]", @foo), '[]');
196 @foo=(); push @foo, $_ for $1..undef;
197 is(join(":", map "[$_]", @foo), '');
202 @foo=(); push @foo, $_ for $1.."";
203 is(join(":", map "[$_]", @foo), '');
206 # Test upper range limit
209 foreach my $ii (-3 .. 3) {
213 for ($MAX_INT-10 .. $MAX_INT+$ii) {
214 if (! defined($first)) {
218 last if ($lim++ > 100); # Protect against integer wrap
222 ok(! $@, 'Upper bound accepted: ' . ($MAX_INT+$ii));
223 is($first, $MAX_INT-10, 'Lower bound okay');
224 is($last, $MAX_INT+$ii, 'Upper bound okay');
226 ok($@, 'Upper bound rejected: ' . ($MAX_INT+$ii));
230 foreach my $ii (-3 .. 3) {
234 for ($MAX_INT+$ii .. $MAX_INT) {
235 if (! defined($first)) {
239 last if ($lim++ > 100);
243 ok(! $@, 'Lower bound accepted: ' . ($MAX_INT+$ii));
244 is($first, $MAX_INT+$ii, 'Lower bound okay');
245 is($last, $MAX_INT, 'Upper bound okay');
247 ok($@, 'Lower bound rejected: ' . ($MAX_INT+$ii));
255 for ($MAX_INT .. $MAX_INT-1) {
256 if (! defined($first)) {
260 last if ($lim++ > 100);
263 ok(! $@, 'Range accepted');
264 ok(! defined($first), 'Range ineffectual');
267 foreach my $ii (~0, ~0+1, ~0+(~0>>4)) {
270 for ($MAX_INT-10 .. $ii) {
271 last if ($lim++ > 100);
274 ok($@, 'Upper bound rejected: ' . $ii);
277 # Test lower range limit
278 my $MIN_INT = -1-$MAX_INT;
280 if (! $Config{d_nv_preserves_uv}) {
281 # $MIN_INT needs adjustment when IV won't fit into an NV
282 my $NV = $MIN_INT - 1;
284 while (($NV + $OFFSET) == $MIN_INT) {
290 foreach my $ii (-3 .. 3) {
294 for ($MIN_INT+$ii .. $MIN_INT+10) {
295 if (! defined($first)) {
299 last if ($lim++ > 100);
303 ok(! $@, 'Lower bound accepted: ' . ($MIN_INT+$ii));
304 is($first, $MIN_INT+$ii, 'Lower bound okay');
305 is($last, $MIN_INT+10, 'Upper bound okay');
307 ok($@, 'Lower bound rejected: ' . ($MIN_INT+$ii));
311 foreach my $ii (-3 .. 3) {
315 for ($MIN_INT .. $MIN_INT+$ii) {
316 if (! defined($first)) {
320 last if ($lim++ > 100);
324 ok(! $@, 'Upper bound accepted: ' . ($MIN_INT+$ii));
325 is($first, $MIN_INT, 'Lower bound okay');
326 is($last, $MIN_INT+$ii, 'Upper bound okay');
328 ok($@, 'Upper bound rejected: ' . ($MIN_INT+$ii));
336 for ($MIN_INT+1 .. $MIN_INT) {
337 if (! defined($first)) {
341 last if ($lim++ > 100);
344 ok(! $@, 'Range accepted');
345 ok(! defined($first), 'Range ineffectual');
348 foreach my $ii (~0, ~0+1, ~0+(~0>>4)) {
351 for (-$ii .. $MIN_INT+10) {
352 last if ($lim++ > 100);
355 ok($@, 'Lower bound rejected: ' . -$ii);
358 # double/triple magic tests
359 sub TIESCALAR { bless { value => $_[1], orig => $_[1] } }
360 sub STORE { $_[0]{store}++; $_[0]{value} = $_[1] }
361 sub FETCH { $_[0]{fetch}++; $_[0]{value} }
362 sub stores { tied($_[0])->{value} = tied($_[0])->{orig};
363 delete(tied($_[0])->{store}) || 0 }
364 sub fetches { delete(tied($_[0])->{fetch}) || 0 }
397 for (reverse 4 .. $x) {
405 is( ( join ' ', map { join '', map ++$_, ($x=1)..4 } 1..2 ), '2345 2345',
406 'modifiable variable num range' );
407 is( ( join ' ', map { join '', map ++$_, 1..4 } 1..2 ), '2345 2345',
408 'modifiable const num range' ); # RT#3105
409 $s = ''; for (1..2) { for (1..4) { $s .= ++$_ } $s.=' ' if $_==1; }
410 is( $s, '2345 2345','modifiable num counting loop counter' );
413 is( ( join ' ', map { join '', map ++$_, ($x='a')..'d' } 1..2 ), 'bcde bcde',
414 'modifiable variable alpha range' );
415 is( ( join ' ', map { join '', map ++$_, 'a'..'d' } 1..2 ), 'bcde bcde',
416 'modifiable const alpha range' ); # RT#3105
417 $s = ''; for (1..2) { for ('a'..'d') { $s .= ++$_ } $s.=' ' if $_==1; }
418 is( $s, 'bcde bcde','modifiable alpha counting loop counter' );
421 # generating an extreme range triggered a croak, which if caught,
422 # left the temps stack small but with a very large PL_tmps_max
425 skip 'mem wrap check disabled' unless $Config{usemallocwrap};
426 fresh_perl_like(<<'EOF', qr/\Aok 1 ok 2\Z/, {}, "RT #130841");
427 my $max_iv = (~0 >> 1);
429 my @range = 1..($max_iv - 1);
431 if ($@ =~ /panic: memory wrap|Out of memory/) {
435 print "unexpected err status: [$@]";
438 # create and push lots of temps
440 my @ints = map $_+1, 0..($max-1);
442 $sum += $_ for @ints;
443 my $exp = $max*($max+1)/2;
448 print " unexpected sum: [$sum]; expected: [$exp]";