This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix aliasing of tied filehandles
[perl5.git] / t / op / substr.t
CommitLineData
3aa33fe5 1#!./perl
a687059c 2
e84ff256 3print "1..135\n";
84902520
TB
4
5#P = start of string Q = start of substr R = end of substr S = end of string
a687059c 6
e476b1b5 7BEGIN {
3aa33fe5 8 chdir 't' if -d 't';
20822f61 9 @INC = '../lib';
e476b1b5
GS
10}
11use warnings ;
84902520 12
e476b1b5 13$a = 'abcdefxyz';
84902520
TB
14$SIG{__WARN__} = sub {
15 if ($_[0] =~ /^substr outside of string/) {
16 $w++;
17 } elsif ($_[0] =~ /^Attempt to use reference as lvalue in substr/) {
18 $w += 2;
5d82c453
GA
19 } elsif ($_[0] =~ /^Use of uninitialized value/) {
20 $w += 3;
84902520 21 } else {
5d82c453 22 warn $_[0];
84902520
TB
23 }
24};
a687059c 25
e476b1b5
GS
26sub ok { print (($_[1] ? "" : "not ") . "ok $_[0]\n") }
27
28$FATAL_MSG = '^substr outside of string' ;
84902520 29
e476b1b5
GS
30ok 1, substr($a,0,3) eq 'abc'; # P=Q R S
31ok 2, substr($a,3,3) eq 'def'; # P Q R S
32ok 3, substr($a,6,999) eq 'xyz'; # P Q S R
33$b = substr($a,999,999) ; # warn # P R Q S
34ok 4, $w-- == 1 ;
35eval{substr($a,999,999) = "" ; };# P R Q S
36ok 5, $@ =~ /$FATAL_MSG/;
37ok 6, substr($a,0,-6) eq 'abc'; # P=Q R S
38ok 7, substr($a,-3,1) eq 'x'; # P Q R S
a687059c
LW
39
40$[ = 1;
41
e476b1b5
GS
42ok 8, substr($a,1,3) eq 'abc' ; # P=Q R S
43ok 9, substr($a,4,3) eq 'def' ; # P Q R S
44ok 10, substr($a,7,999) eq 'xyz';# P Q S R
45$b = substr($a,999,999) ; # warn # P R Q S
46ok 11, $w-- == 1 ;
47eval{substr($a,999,999) = "" ; } ; # P R Q S
48ok 12, $@ =~ /$FATAL_MSG/;
49ok 13, substr($a,1,-6) eq 'abc' ;# P=Q R S
50ok 14, substr($a,-3,1) eq 'x' ; # P Q R S
a687059c
LW
51
52$[ = 0;
53
54substr($a,3,3) = 'XYZ';
e476b1b5 55ok 15, $a eq 'abcXYZxyz' ;
a687059c 56substr($a,0,2) = '';
e476b1b5 57ok 16, $a eq 'cXYZxyz' ;
a687059c 58substr($a,0,0) = 'ab';
e476b1b5 59ok 17, $a eq 'abcXYZxyz' ;
a687059c 60substr($a,0,0) = '12345678';
e476b1b5 61ok 18, $a eq '12345678abcXYZxyz' ;
a687059c 62substr($a,-3,3) = 'def';
e476b1b5 63ok 19, $a eq '12345678abcXYZdef';
a687059c 64substr($a,-3,3) = '<';
e476b1b5 65ok 20, $a eq '12345678abcXYZ<' ;
a687059c 66substr($a,-1,1) = '12345678';
e476b1b5 67ok 21, $a eq '12345678abcXYZ12345678' ;
a687059c 68
d9d8d8de
LW
69$a = 'abcdefxyz';
70
e476b1b5
GS
71ok 22, substr($a,6) eq 'xyz' ; # P Q R=S
72ok 23, substr($a,-3) eq 'xyz' ; # P Q R=S
73$b = substr($a,999,999) ; # warning # P R=S Q
74ok 24, $w-- == 1 ;
75eval{substr($a,999,999) = "" ; } ; # P R=S Q
76ok 25, $@ =~ /$FATAL_MSG/;
77ok 26, substr($a,0) eq 'abcdefxyz' ; # P=Q R=S
78ok 27, substr($a,9) eq '' ; # P Q=R=S
79ok 28, substr($a,-11) eq 'abcdefxyz'; # Q P R=S
80ok 29, substr($a,-9) eq 'abcdefxyz'; # P=Q R=S
84902520
TB
81
82$a = '54321';
83
e476b1b5
GS
84$b = substr($a,-7, 1) ; # warn # Q R P S
85ok 30, $w-- == 1 ;
86eval{substr($a,-7, 1) = "" ; }; # Q R P S
87ok 31, $@ =~ /$FATAL_MSG/;
88$b = substr($a,-7,-6) ; # warn # Q R P S
89ok 32, $w-- == 1 ;
90eval{substr($a,-7,-6) = "" ; }; # Q R P S
91ok 33, $@ =~ /$FATAL_MSG/;
92ok 34, substr($a,-5,-7) eq ''; # R P=Q S
93ok 35, substr($a, 2,-7) eq ''; # R P Q S
94ok 36, substr($a,-3,-7) eq ''; # R P Q S
95ok 37, substr($a, 2,-5) eq ''; # P=R Q S
96ok 38, substr($a,-3,-5) eq ''; # P=R Q S
97ok 39, substr($a, 2,-4) eq ''; # P R Q S
98ok 40, substr($a,-3,-4) eq ''; # P R Q S
99ok 41, substr($a, 5,-6) eq ''; # R P Q=S
100ok 42, substr($a, 5,-5) eq ''; # P=R Q S
101ok 43, substr($a, 5,-3) eq ''; # P R Q=S
102$b = substr($a, 7,-7) ; # warn # R P S Q
103ok 44, $w-- == 1 ;
104eval{substr($a, 7,-7) = "" ; }; # R P S Q
105ok 45, $@ =~ /$FATAL_MSG/;
106$b = substr($a, 7,-5) ; # warn # P=R S Q
107ok 46, $w-- == 1 ;
108eval{substr($a, 7,-5) = "" ; }; # P=R S Q
109ok 47, $@ =~ /$FATAL_MSG/;
110$b = substr($a, 7,-3) ; # warn # P Q S Q
111ok 48, $w-- == 1 ;
112eval{substr($a, 7,-3) = "" ; }; # P Q S Q
113ok 49, $@ =~ /$FATAL_MSG/;
114$b = substr($a, 7, 0) ; # warn # P S Q=R
115ok 50, $w-- == 1 ;
116eval{substr($a, 7, 0) = "" ; }; # P S Q=R
117ok 51, $@ =~ /$FATAL_MSG/;
118
119ok 52, substr($a,-7,2) eq ''; # Q P=R S
120ok 53, substr($a,-7,4) eq '54'; # Q P R S
121ok 54, substr($a,-7,7) eq '54321';# Q P R=S
122ok 55, substr($a,-7,9) eq '54321';# Q P S R
123ok 56, substr($a,-5,0) eq ''; # P=Q=R S
124ok 57, substr($a,-5,3) eq '543';# P=Q R S
125ok 58, substr($a,-5,5) eq '54321';# P=Q R=S
126ok 59, substr($a,-5,7) eq '54321';# P=Q S R
127ok 60, substr($a,-3,0) eq ''; # P Q=R S
128ok 61, substr($a,-3,3) eq '321';# P Q R=S
129ok 62, substr($a,-2,3) eq '21'; # P Q S R
130ok 63, substr($a,0,-5) eq ''; # P=Q=R S
131ok 64, substr($a,2,-3) eq ''; # P Q=R S
132ok 65, substr($a,0,0) eq ''; # P=Q=R S
133ok 66, substr($a,0,5) eq '54321';# P=Q R=S
134ok 67, substr($a,0,7) eq '54321';# P=Q S R
135ok 68, substr($a,2,0) eq ''; # P Q=R S
136ok 69, substr($a,2,3) eq '321'; # P Q R=S
137ok 70, substr($a,5,0) eq ''; # P Q=R=S
138ok 71, substr($a,5,2) eq ''; # P Q=S R
139ok 72, substr($a,-7,-5) eq ''; # Q P=R S
140ok 73, substr($a,-7,-2) eq '543';# Q P R S
141ok 74, substr($a,-5,-5) eq ''; # P=Q=R S
142ok 75, substr($a,-5,-2) eq '543';# P=Q R S
143ok 76, substr($a,-3,-3) eq ''; # P Q=R S
144ok 77, substr($a,-3,-1) eq '32';# P Q R S
84902520
TB
145
146$a = '';
147
e476b1b5
GS
148ok 78, substr($a,-2,2) eq ''; # Q P=R=S
149ok 79, substr($a,0,0) eq ''; # P=Q=R=S
150ok 80, substr($a,0,1) eq ''; # P=Q=S R
151ok 81, substr($a,-2,3) eq ''; # Q P=S R
152ok 82, substr($a,-2) eq ''; # Q P=R=S
153ok 83, substr($a,0) eq ''; # P=Q=R=S
154
155
156ok 84, substr($a,0,-1) eq ''; # R P=Q=S
157$b = substr($a,-2, 0) ; # warn # Q=R P=S
158ok 85, $w-- == 1 ;
159eval{substr($a,-2, 0) = "" ; }; # Q=R P=S
160ok 86, $@ =~ /$FATAL_MSG/;
84902520 161
e476b1b5
GS
162$b = substr($a,-2, 1) ; # warn # Q R P=S
163ok 87, $w-- == 1 ;
164eval{substr($a,-2, 1) = "" ; }; # Q R P=S
165ok 88, $@ =~ /$FATAL_MSG/;
84902520 166
e476b1b5
GS
167$b = substr($a,-2,-1) ; # warn # Q R P=S
168ok 89, $w-- == 1 ;
169eval{substr($a,-2,-1) = "" ; }; # Q R P=S
170ok 90, $@ =~ /$FATAL_MSG/;
84902520 171
e476b1b5
GS
172$b = substr($a,-2,-2) ; # warn # Q=R P=S
173ok 91, $w-- == 1 ;
174eval{substr($a,-2,-2) = "" ; }; # Q=R P=S
175ok 92, $@ =~ /$FATAL_MSG/;
176
177$b = substr($a, 1,-2) ; # warn # R P=S Q
178ok 93, $w-- == 1 ;
179eval{substr($a, 1,-2) = "" ; }; # R P=S Q
180ok 94, $@ =~ /$FATAL_MSG/;
181
182$b = substr($a, 1, 1) ; # warn # P=S Q R
183ok 95, $w-- == 1 ;
184eval{substr($a, 1, 1) = "" ; }; # P=S Q R
185ok 96, $@ =~ /$FATAL_MSG/;
186
187$b = substr($a, 1, 0) ;# warn # P=S Q=R
188ok 97, $w-- == 1 ;
189eval{substr($a, 1, 0) = "" ; }; # P=S Q=R
190ok 98, $@ =~ /$FATAL_MSG/;
191
192$b = substr($a,1) ; # warning # P=R=S Q
193ok 99, $w-- == 1 ;
194eval{substr($a,1) = "" ; }; # P=R=S Q
195ok 100, $@ =~ /$FATAL_MSG/;
84902520
TB
196
197my $a = 'zxcvbnm';
198substr($a,2,0) = '';
e476b1b5 199ok 101, $a eq 'zxcvbnm';
84902520 200substr($a,7,0) = '';
e476b1b5 201ok 102, $a eq 'zxcvbnm';
84902520 202substr($a,5,0) = '';
e476b1b5 203ok 103, $a eq 'zxcvbnm';
84902520 204substr($a,0,2) = 'pq';
e476b1b5 205ok 104, $a eq 'pqcvbnm';
84902520 206substr($a,2,0) = 'r';
e476b1b5 207ok 105, $a eq 'pqrcvbnm';
84902520 208substr($a,8,0) = 'asd';
e476b1b5 209ok 106, $a eq 'pqrcvbnmasd';
84902520 210substr($a,0,2) = 'iop';
e476b1b5 211ok 107, $a eq 'ioprcvbnmasd';
84902520 212substr($a,0,5) = 'fgh';
e476b1b5 213ok 108, $a eq 'fghvbnmasd';
84902520 214substr($a,3,5) = 'jkl';
e476b1b5 215ok 109, $a eq 'fghjklsd';
84902520 216substr($a,3,2) = '1234';
e476b1b5 217ok 110, $a eq 'fgh1234lsd';
84902520 218
08cb0b0d
PP
219
220# with lexicals (and in re-entered scopes)
221for (0,1) {
222 my $txt;
223 unless ($_) {
224 $txt = "Foo";
225 substr($txt, -1) = "X";
e476b1b5 226 ok 111, $txt eq "FoX";
08cb0b0d
PP
227 }
228 else {
229 substr($txt, 0, 1) = "X";
e476b1b5 230 ok 112, $txt eq "X";
08cb0b0d
PP
231 }
232}
233
e476b1b5 234$w = 0 ;
84902520 235# coercion of references
08cb0b0d
PP
236{
237 my $s = [];
238 substr($s, 0, 1) = 'Foo';
e476b1b5 239 ok 113, substr($s,0,7) eq "FooRRAY" && !($w-=2);
08cb0b0d 240}
84902520
TB
241
242# check no spurious warnings
e476b1b5 243ok 114, $w == 0;
7b8d334a 244
5d82c453 245# check new 4 arg replacement syntax
7b8d334a 246$a = "abcxyz";
5d82c453 247$w = 0;
e476b1b5
GS
248ok 115, substr($a, 0, 3, "") eq "abc" && $a eq "xyz";
249ok 116, substr($a, 0, 0, "abc") eq "" && $a eq "abcxyz";
250ok 117, substr($a, 3, -1, "") eq "xy" && $a eq "abcz";
251
252ok 118, substr($a, 3, undef, "xy") eq "" && $a eq "abcxyz"
5d82c453 253 && $w == 3;
e476b1b5 254
5d82c453
GA
255$w = 0;
256
e476b1b5
GS
257ok 119, substr($a, 3, 9999999, "") eq "xyz" && $a eq "abc";
258eval{substr($a, -99, 0, "") };
259ok 120, $@ =~ /$FATAL_MSG/;
260eval{substr($a, 99, 3, "") };
261ok 121, $@ =~ /$FATAL_MSG/;
5d82c453
GA
262
263substr($a, 0, length($a), "foo");
e476b1b5 264ok 122, $a eq "foo" && !$w;
5d82c453
GA
265
266# using 4 arg substr as lvalue is a compile time error
267eval 'substr($a,0,0,"") = "abc"';
e476b1b5 268ok 123, $@ && $@ =~ /Can't modify substr/ && $a eq "foo";
c8faf1c5
GS
269
270$a = "abcdefgh";
e476b1b5
GS
271ok 124, sub { shift }->(substr($a, 0, 4, "xxxx")) eq 'abcd';
272ok 125, $a eq 'xxxxefgh';
7f66633b 273
e84ff256
GS
274{
275 my $y = 10;
276 $y = "2" . $y;
277 ok 126, $y+0 == 210;
278}
279
7f66633b
GS
280# utf8 sanity
281{
282 my $x = substr("a\x{263a}b",0);
e84ff256 283 ok 127, length($x) == 3;
7f66633b 284 $x = substr($x,1,1);
e84ff256 285 ok 128, $x eq "\x{263a}";
dfcb284a 286 $x = $x x 2;
e84ff256 287 ok 129, length($x) == 2;
7f66633b 288 substr($x,0,1) = "abcd";
e84ff256 289 ok 130, $x eq "abcd\x{263a}";
3aa33fe5 290 ok 131, length($x) == 5;
e84ff256
GS
291 $x = reverse $x;
292 ok 132, length($x) == 5;
293 ok 133, $x eq "\x{263a}dcba";
294
295 my $z = 10;
296 $z = "21\x{263a}" . $z;
297 ok 134, length($z) == 5;
298 ok 135, $z eq "21\x{263a}10";
7f66633b 299}