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 # [#133695] "0".."-1" should be the same as 0..-1
116 is(join(":","-2".."-1") , "-2:-1");
117 is(join(":","-1".."-1") , "-1");
118 is(join(":","0".."-1") , "");
119 is(join(":","1".."-1") , "");
121 # these test the statements made in the documentation
122 # regarding the rules of string ranges
123 is(join(":","-2".."2"), join(":",-2..2));
124 is(join(":","2.18".."3.14"), "2:3");
125 is(join(":","01".."04"), "01:02:03:04");
126 is(join(":","00".."-1"), "00:01:02:03:04:05:06:07:08:09: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");
127 is(join(":","00".."31"), "00:01:02:03:04:05:06:07:08:09:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:25:26:27:28:29:30:31");
128 is(join(":","ax".."az"), "ax:ay:az");
129 is(join(":","*x".."az"), "*x");
130 is(join(":","A".."Z"), "A:B:C:D:E:F:G:H:I:J:K:L:M:N:O:P:Q:R:S:T:U:V:W:X:Y:Z");
131 is(join(":", 0..9,"a".."f"), "0:1:2:3:4:5:6:7:8:9:a:b:c:d:e:f");
132 is(join(":","a".."--"), join(":","a".."zz"));
133 is(join(":","0".."xx"), "0: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");
134 is(join(":","aaa".."--"), "");
136 # undef should be treated as 0 for numerical range
137 is(join(":",undef..2), '0:1:2');
138 is(join(":",-2..undef), '-2:-1:0');
139 is(join(":",undef..'2'), '0:1:2');
140 is(join(":",'-2'..undef), '-2:-1:0');
142 # undef should be treated as "" for magical range
143 is(join(":", map "[$_]", "".."B"), '[]');
144 is(join(":", map "[$_]", undef.."B"), '[]');
145 is(join(":", map "[$_]", "B"..""), '');
146 is(join(":", map "[$_]", "B"..undef), '');
148 # undef..undef used to segfault
149 is(join(":", map "[$_]", undef..undef), '[]');
151 # also test undef in foreach loops
152 @foo=(); push @foo, $_ for undef..2;
153 is(join(":", @foo), '0:1:2');
155 @foo=(); push @foo, $_ for -2..undef;
156 is(join(":", @foo), '-2:-1:0');
158 @foo=(); push @foo, $_ for undef..'2';
159 is(join(":", @foo), '0:1:2');
161 @foo=(); push @foo, $_ for '-2'..undef;
162 is(join(":", @foo), '-2:-1:0');
164 @foo=(); push @foo, $_ for undef.."B";
165 is(join(":", map "[$_]", @foo), '[]');
167 @foo=(); push @foo, $_ for "".."B";
168 is(join(":", map "[$_]", @foo), '[]');
170 @foo=(); push @foo, $_ for "B"..undef;
171 is(join(":", map "[$_]", @foo), '');
173 @foo=(); push @foo, $_ for "B".."";
174 is(join(":", map "[$_]", @foo), '');
176 @foo=(); push @foo, $_ for undef..undef;
177 is(join(":", map "[$_]", @foo), '[]');
182 @foo=(); push @foo, $_ for undef..$#a;
183 is(join(":", @foo), '0:1:2');
187 @foo=(); push @foo, $_ for $#a..undef;
188 is(join(":", @foo), '-1:0');
193 @foo=(); push @foo, $_ for undef..$1;
194 is(join(":", @foo), '0:1:2');
199 @foo=(); push @foo, $_ for $1..undef;
200 is(join(":", @foo), '-2:-1:0');
205 @foo=(); push @foo, $_ for undef..$1;
206 is(join(":", map "[$_]", @foo), '[]');
211 @foo=(); push @foo, $_ for ""..$1;
212 is(join(":", map "[$_]", @foo), '[]');
217 @foo=(); push @foo, $_ for $1..undef;
218 is(join(":", map "[$_]", @foo), '');
223 @foo=(); push @foo, $_ for $1.."";
224 is(join(":", map "[$_]", @foo), '');
227 # Test upper range limit
230 foreach my $ii (-3 .. 3) {
234 for ($MAX_INT-10 .. $MAX_INT+$ii) {
235 if (! defined($first)) {
239 last if ($lim++ > 100); # Protect against integer wrap
243 ok(! $@, 'Upper bound accepted: ' . ($MAX_INT+$ii));
244 is($first, $MAX_INT-10, 'Lower bound okay');
245 is($last, $MAX_INT+$ii, 'Upper bound okay');
247 ok($@, 'Upper bound rejected: ' . ($MAX_INT+$ii));
251 foreach my $ii (-3 .. 3) {
255 for ($MAX_INT+$ii .. $MAX_INT) {
256 if (! defined($first)) {
260 last if ($lim++ > 100);
264 ok(! $@, 'Lower bound accepted: ' . ($MAX_INT+$ii));
265 is($first, $MAX_INT+$ii, 'Lower bound okay');
266 is($last, $MAX_INT, 'Upper bound okay');
268 ok($@, 'Lower bound rejected: ' . ($MAX_INT+$ii));
276 for ($MAX_INT .. $MAX_INT-1) {
277 if (! defined($first)) {
281 last if ($lim++ > 100);
284 ok(! $@, 'Range accepted');
285 ok(! defined($first), 'Range ineffectual');
288 foreach my $ii (~0, ~0+1, ~0+(~0>>4)) {
291 for ($MAX_INT-10 .. $ii) {
292 last if ($lim++ > 100);
295 ok($@, 'Upper bound rejected: ' . $ii);
298 # Test lower range limit
299 my $MIN_INT = -1-$MAX_INT;
301 if (! $Config{d_nv_preserves_uv}) {
302 # $MIN_INT needs adjustment when IV won't fit into an NV
303 my $NV = $MIN_INT - 1;
305 while (($NV + $OFFSET) == $MIN_INT) {
311 foreach my $ii (-3 .. 3) {
315 for ($MIN_INT+$ii .. $MIN_INT+10) {
316 if (! defined($first)) {
320 last if ($lim++ > 100);
324 ok(! $@, 'Lower bound accepted: ' . ($MIN_INT+$ii));
325 is($first, $MIN_INT+$ii, 'Lower bound okay');
326 is($last, $MIN_INT+10, 'Upper bound okay');
328 ok($@, 'Lower bound rejected: ' . ($MIN_INT+$ii));
332 foreach my $ii (-3 .. 3) {
336 for ($MIN_INT .. $MIN_INT+$ii) {
337 if (! defined($first)) {
341 last if ($lim++ > 100);
345 ok(! $@, 'Upper bound accepted: ' . ($MIN_INT+$ii));
346 is($first, $MIN_INT, 'Lower bound okay');
347 is($last, $MIN_INT+$ii, 'Upper bound okay');
349 ok($@, 'Upper bound rejected: ' . ($MIN_INT+$ii));
357 for ($MIN_INT+1 .. $MIN_INT) {
358 if (! defined($first)) {
362 last if ($lim++ > 100);
365 ok(! $@, 'Range accepted');
366 ok(! defined($first), 'Range ineffectual');
369 foreach my $ii (~0, ~0+1, ~0+(~0>>4)) {
372 for (-$ii .. $MIN_INT+10) {
373 last if ($lim++ > 100);
376 ok($@, 'Lower bound rejected: ' . -$ii);
379 # double/triple magic tests
380 sub TIESCALAR { bless { value => $_[1], orig => $_[1] } }
381 sub STORE { $_[0]{store}++; $_[0]{value} = $_[1] }
382 sub FETCH { $_[0]{fetch}++; $_[0]{value} }
383 sub stores { tied($_[0])->{value} = tied($_[0])->{orig};
384 delete(tied($_[0])->{store}) || 0 }
385 sub fetches { delete(tied($_[0])->{fetch}) || 0 }
418 for (reverse 4 .. $x) {
426 is( ( join ' ', map { join '', map ++$_, ($x=1)..4 } 1..2 ), '2345 2345',
427 'modifiable variable num range' );
428 is( ( join ' ', map { join '', map ++$_, 1..4 } 1..2 ), '2345 2345',
429 'modifiable const num range' ); # RT#3105
430 $s = ''; for (1..2) { for (1..4) { $s .= ++$_ } $s.=' ' if $_==1; }
431 is( $s, '2345 2345','modifiable num counting loop counter' );
434 is( ( join ' ', map { join '', map ++$_, ($x='a')..'d' } 1..2 ), 'bcde bcde',
435 'modifiable variable alpha range' );
436 is( ( join ' ', map { join '', map ++$_, 'a'..'d' } 1..2 ), 'bcde bcde',
437 'modifiable const alpha range' ); # RT#3105
438 $s = ''; for (1..2) { for ('a'..'d') { $s .= ++$_ } $s.=' ' if $_==1; }
439 is( $s, 'bcde bcde','modifiable alpha counting loop counter' );
442 # generating an extreme range triggered a croak, which if caught,
443 # left the temps stack small but with a very large PL_tmps_max
446 skip 'mem wrap check disabled' unless $Config{usemallocwrap};
447 fresh_perl_like(<<'EOF', qr/\Aok 1 ok 2\Z/, {}, "RT #130841");
448 my $max_iv = (~0 >> 1);
450 my @range = 1..($max_iv - 1);
452 if ($@ =~ /panic: memory wrap|Out of memory/) {
456 print "unexpected err status: [$@]";
459 # create and push lots of temps
461 my @ints = map $_+1, 0..($max-1);
463 $sum += $_ for @ints;
464 my $exp = $max*($max+1)/2;
469 print " unexpected sum: [$sum]; expected: [$exp]";