This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re: [perl #39126] possible memory related bug when using sprintf with an utf-8 encode...
[perl5.git] / t / op / range.t
CommitLineData
a687059c
LW
1#!./perl
2
4fe3f0fa
MHM
3BEGIN {
4 chdir 't' if -d 't';
4e086238 5 @INC = ('../lib', '.');
4fe3f0fa 6}
4e086238
NC
7# Avoid using eq_array below as it uses .. internally.
8require 'test.pl';
4fe3f0fa
MHM
9
10use Config;
11
4e086238 12plan (45);
a687059c 13
4e086238 14is(join(':',1..5), '1:2:3:4:5');
a687059c
LW
15
16@foo = (1,2,3,4,5,6,7,8,9);
17@foo[2..4] = ('c','d','e');
18
4e086238 19is(join(':',@foo[$foo[0]..5]), '2:c:d:e:6');
a687059c
LW
20
21@bar[2..4] = ('c','d','e');
4e086238 22is(join(':',@bar[1..5]), ':c:d:e:');
a687059c
LW
23
24($a,@bcd[0..2],$e) = ('a','b','c','d','e');
4e086238 25is(join(':',$a,@bcd[0..2],$e), 'a:b:c:d:e');
a687059c
LW
26
27$x = 0;
28for (1..100) {
29 $x += $_;
30}
4e086238 31is($x, 5050);
a687059c
LW
32
33$x = 0;
34for ((100,2..99,1)) {
35 $x += $_;
36}
4e086238 37is($x, 5050);
0f85fab0
LW
38
39$x = join('','a'..'z');
4e086238 40is($x, 'abcdefghijklmnopqrstuvwxyz');
0f85fab0
LW
41
42@x = 'A'..'ZZ';
4e086238 43is (scalar @x, 27 * 26);
89ea2908
GA
44
45@x = '09' .. '08'; # should produce '09', '10',... '99' (strange but true)
4e086238 46is(join(",", @x), join(",", map {sprintf "%02d",$_} 9..99));
89ea2908
GA
47
48# same test with foreach (which is a separate implementation)
49@y = ();
50foreach ('09'..'08') {
51 push(@y, $_);
52}
4e086238 53is(join(",", @y), join(",", @x));
89ea2908 54
c1ab3db2 55# check bounds
4fe3f0fa
MHM
56if ($Config{ivsize} == 8) {
57 @a = eval "0x7ffffffffffffffe..0x7fffffffffffffff";
58 $a = "9223372036854775806 9223372036854775807";
59 @b = eval "-0x7fffffffffffffff..-0x7ffffffffffffffe";
60 $b = "-9223372036854775807 -9223372036854775806";
61}
62else {
63 @a = eval "0x7ffffffe..0x7fffffff";
64 $a = "2147483646 2147483647";
65 @b = eval "-0x7fffffff..-0x7ffffffe";
66 $b = "-2147483647 -2147483646";
67}
68
4e086238 69is ("@a", $a);
c1ab3db2 70
4e086238 71is ("@b", $b);
c1ab3db2 72
86cb7173
HS
73# check magic
74{
75 my $bad = 0;
76 local $SIG{'__WARN__'} = sub { $bad = 1 };
77 my $x = 'a-e';
78 $x =~ s/(\w)-(\w)/join ':', $1 .. $2/e;
4e086238 79 is ($x, 'a:b:c:d:e');
86cb7173 80}
39eb4040
GS
81
82# Should use magical autoinc only when both are strings
4e086238
NC
83{
84 my $scalar = (() = "0"..-1);
85 is ($scalar, 0);
86}
87{
88 my $fail = 0;
89 for my $x ("0"..-1) {
90 $fail++;
91 }
92 is ($fail, 0);
39eb4040 93}
545956b7
MJD
94
95# [#18165] Should allow "-4".."0", broken by #4730. (AMS 20021031)
4e086238
NC
96is(join(":","-4".."0") , "-4:-3:-2:-1:0");
97is(join(":","-4".."-0") , "-4:-3:-2:-1:0");
98is(join(":","-4\n".."0\n") , "-4:-3:-2:-1:0");
99is(join(":","-4\n".."-0\n"), "-4:-3:-2:-1:0");
b0e74086
RGS
100
101# undef should be treated as 0 for numerical range
4e086238
NC
102is(join(":",undef..2), '0:1:2');
103is(join(":",-2..undef), '-2:-1:0');
104is(join(":",undef..'2'), '0:1:2');
105is(join(":",'-2'..undef), '-2:-1:0');
b0e74086
RGS
106
107# undef should be treated as "" for magical range
4e086238
NC
108is(join(":", map "[$_]", "".."B"), '[]');
109is(join(":", map "[$_]", undef.."B"), '[]');
110is(join(":", map "[$_]", "B"..""), '');
111is(join(":", map "[$_]", "B"..undef), '');
3f63a782 112
076d9a11 113# undef..undef used to segfault
4e086238 114is(join(":", map "[$_]", undef..undef), '[]');
3f63a782
MHM
115
116# also test undef in foreach loops
117@foo=(); push @foo, $_ for undef..2;
4e086238 118is(join(":", @foo), '0:1:2');
3f63a782
MHM
119
120@foo=(); push @foo, $_ for -2..undef;
4e086238 121is(join(":", @foo), '-2:-1:0');
076d9a11
MHM
122
123@foo=(); push @foo, $_ for undef..'2';
4e086238 124is(join(":", @foo), '0:1:2');
076d9a11
MHM
125
126@foo=(); push @foo, $_ for '-2'..undef;
4e086238 127is(join(":", @foo), '-2:-1:0');
3f63a782
MHM
128
129@foo=(); push @foo, $_ for undef.."B";
4e086238 130is(join(":", map "[$_]", @foo), '[]');
6b75d741
MHM
131
132@foo=(); push @foo, $_ for "".."B";
4e086238 133is(join(":", map "[$_]", @foo), '[]');
3f63a782
MHM
134
135@foo=(); push @foo, $_ for "B"..undef;
4e086238 136is(join(":", map "[$_]", @foo), '');
6b75d741
MHM
137
138@foo=(); push @foo, $_ for "B".."";
4e086238 139is(join(":", map "[$_]", @foo), '');
6b75d741
MHM
140
141@foo=(); push @foo, $_ for undef..undef;
4e086238 142is(join(":", map "[$_]", @foo), '[]');
984a4bea
RD
143
144# again with magic
145{
146 my @a = (1..3);
147 @foo=(); push @foo, $_ for undef..$#a;
4e086238 148 is(join(":", @foo), '0:1:2');
984a4bea
RD
149}
150{
151 my @a = ();
152 @foo=(); push @foo, $_ for $#a..undef;
4e086238 153 is(join(":", @foo), '-1:0');
984a4bea
RD
154}
155{
156 local $1;
157 "2" =~ /(.+)/;
158 @foo=(); push @foo, $_ for undef..$1;
4e086238 159 is(join(":", @foo), '0:1:2');
984a4bea
RD
160}
161{
162 local $1;
163 "-2" =~ /(.+)/;
164 @foo=(); push @foo, $_ for $1..undef;
4e086238 165 is(join(":", @foo), '-2:-1:0');
984a4bea
RD
166}
167{
168 local $1;
169 "B" =~ /(.+)/;
170 @foo=(); push @foo, $_ for undef..$1;
4e086238 171 is(join(":", map "[$_]", @foo), '[]');
984a4bea
RD
172}
173{
174 local $1;
175 "B" =~ /(.+)/;
176 @foo=(); push @foo, $_ for ""..$1;
4e086238 177 is(join(":", map "[$_]", @foo), '[]');
984a4bea
RD
178}
179{
180 local $1;
181 "B" =~ /(.+)/;
182 @foo=(); push @foo, $_ for $1..undef;
4e086238 183 is(join(":", map "[$_]", @foo), '');
984a4bea
RD
184}
185{
186 local $1;
187 "B" =~ /(.+)/;
188 @foo=(); push @foo, $_ for $1.."";
4e086238 189 is(join(":", map "[$_]", @foo), '');
984a4bea 190}