This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
test case for change#3470
[perl5.git] / t / op / substr.t
CommitLineData
a687059c
LW
1#!./perl
2
5d82c453 3print "1..106\n";
84902520
TB
4
5#P = start of string Q = start of substr R = end of substr S = end of string
a687059c
LW
6
7$a = 'abcdefxyz';
84902520
TB
8BEGIN { $^W = 1 };
9
10$SIG{__WARN__} = sub {
11 if ($_[0] =~ /^substr outside of string/) {
12 $w++;
13 } elsif ($_[0] =~ /^Attempt to use reference as lvalue in substr/) {
14 $w += 2;
5d82c453
GA
15 } elsif ($_[0] =~ /^Use of uninitialized value/) {
16 $w += 3;
84902520 17 } else {
5d82c453 18 warn $_[0];
84902520
TB
19 }
20};
a687059c 21
84902520
TB
22sub fail { !defined(shift) && $w-- };
23
24print (substr($a,0,3) eq 'abc' ? "ok 1\n" : "not ok 1\n"); # P=Q R S
25print (substr($a,3,3) eq 'def' ? "ok 2\n" : "not ok 2\n"); # P Q R S
26print (substr($a,6,999) eq 'xyz' ? "ok 3\n" : "not ok 3\n"); # P Q S R
27print (fail(substr($a,999,999)) ? "ok 4\n" : "not ok 4\n"); # P R Q S
28print (substr($a,0,-6) eq 'abc' ? "ok 5\n" : "not ok 5\n"); # P=Q R S
29print (substr($a,-3,1) eq 'x' ? "ok 6\n" : "not ok 6\n"); # P Q R S
a687059c
LW
30
31$[ = 1;
32
84902520
TB
33print (substr($a,1,3) eq 'abc' ? "ok 7\n" : "not ok 7\n"); # P=Q R S
34print (substr($a,4,3) eq 'def' ? "ok 8\n" : "not ok 8\n"); # P Q R S
35print (substr($a,7,999) eq 'xyz' ? "ok 9\n" : "not ok 9\n"); # P Q S R
36print (fail(substr($a,999,999)) ? "ok 10\n" : "not ok 10\n");# P R Q S
37print (substr($a,1,-6) eq 'abc' ? "ok 11\n" : "not ok 11\n");# P=Q R S
38print (substr($a,-3,1) eq 'x' ? "ok 12\n" : "not ok 12\n"); # P Q R S
a687059c
LW
39
40$[ = 0;
41
42substr($a,3,3) = 'XYZ';
43print $a eq 'abcXYZxyz' ? "ok 13\n" : "not ok 13\n";
44substr($a,0,2) = '';
45print $a eq 'cXYZxyz' ? "ok 14\n" : "not ok 14\n";
a687059c
LW
46substr($a,0,0) = 'ab';
47print $a eq 'abcXYZxyz' ? "ok 15\n" : "not ok 15 $a\n";
48substr($a,0,0) = '12345678';
49print $a eq '12345678abcXYZxyz' ? "ok 16\n" : "not ok 16\n";
50substr($a,-3,3) = 'def';
51print $a eq '12345678abcXYZdef' ? "ok 17\n" : "not ok 17\n";
52substr($a,-3,3) = '<';
53print $a eq '12345678abcXYZ<' ? "ok 18\n" : "not ok 18\n";
54substr($a,-1,1) = '12345678';
55print $a eq '12345678abcXYZ12345678' ? "ok 19\n" : "not ok 19\n";
56
d9d8d8de
LW
57$a = 'abcdefxyz';
58
84902520
TB
59print (substr($a,6) eq 'xyz' ? "ok 20\n" : "not ok 20\n"); # P Q R=S
60print (substr($a,-3) eq 'xyz' ? "ok 21\n" : "not ok 21\n"); # P Q R=S
61print (fail(substr($a,999)) ? "ok 22\n" : "not ok 22\n"); # P R=S Q
62print (substr($a,0) eq 'abcdefxyz' ? "ok 23\n" : "not ok 23\n");# P=Q R=S
63print (substr($a,9) eq '' ? "ok 24\n" : "not ok 24\n"); # P Q=R=S
64print (substr($a,-11) eq 'abcdefxyz' ? "ok 25\n" : "not ok 25\n");# Q P R=S
65print (substr($a,-9) eq 'abcdefxyz' ? "ok 26\n" : "not ok 26\n"); # P=Q R=S
66
67$a = '54321';
68
69print (fail(substr($a,-7, 1)) ? "ok 27\n" : "not ok 27\n"); # Q R P S
70print (fail(substr($a,-7,-6)) ? "ok 28\n" : "not ok 28\n"); # Q R P S
71print (substr($a,-5,-7) eq '' ? "ok 29\n" : "not ok 29\n"); # R P=Q S
72print (substr($a, 2,-7) eq '' ? "ok 30\n" : "not ok 30\n"); # R P Q S
73print (substr($a,-3,-7) eq '' ? "ok 31\n" : "not ok 31\n"); # R P Q S
74print (substr($a, 2,-5) eq '' ? "ok 32\n" : "not ok 32\n"); # P=R Q S
75print (substr($a,-3,-5) eq '' ? "ok 33\n" : "not ok 33\n"); # P=R Q S
76print (substr($a, 2,-4) eq '' ? "ok 34\n" : "not ok 34\n"); # P R Q S
77print (substr($a,-3,-4) eq '' ? "ok 35\n" : "not ok 35\n"); # P R Q S
78print (substr($a, 5,-6) eq '' ? "ok 36\n" : "not ok 36\n"); # R P Q=S
79print (substr($a, 5,-5) eq '' ? "ok 37\n" : "not ok 37\n"); # P=R Q S
80print (substr($a, 5,-3) eq '' ? "ok 38\n" : "not ok 38\n"); # P R Q=S
81print (fail(substr($a, 7,-7)) ? "ok 39\n" : "not ok 39\n"); # R P S Q
82print (fail(substr($a, 7,-5)) ? "ok 40\n" : "not ok 40\n"); # P=R S Q
83print (fail(substr($a, 7,-3)) ? "ok 41\n" : "not ok 41\n"); # P R S Q
84print (fail(substr($a, 7, 0)) ? "ok 42\n" : "not ok 42\n"); # P S Q=R
85
86print (substr($a,-7,2) eq '' ? "ok 43\n" : "not ok 43\n"); # Q P=R S
87print (substr($a,-7,4) eq '54' ? "ok 44\n" : "not ok 44\n"); # Q P R S
88print (substr($a,-7,7) eq '54321' ? "ok 45\n" : "not ok 45\n");# Q P R=S
89print (substr($a,-7,9) eq '54321' ? "ok 46\n" : "not ok 46\n");# Q P S R
90print (substr($a,-5,0) eq '' ? "ok 47\n" : "not ok 47\n"); # P=Q=R S
91print (substr($a,-5,3) eq '543' ? "ok 48\n" : "not ok 48\n");# P=Q R S
92print (substr($a,-5,5) eq '54321' ? "ok 49\n" : "not ok 49\n");# P=Q R=S
93print (substr($a,-5,7) eq '54321' ? "ok 50\n" : "not ok 50\n");# P=Q S R
94print (substr($a,-3,0) eq '' ? "ok 51\n" : "not ok 51\n"); # P Q=R S
95print (substr($a,-3,3) eq '321' ? "ok 52\n" : "not ok 52\n");# P Q R=S
96print (substr($a,-2,3) eq '21' ? "ok 53\n" : "not ok 53\n"); # P Q S R
97print (substr($a,0,-5) eq '' ? "ok 54\n" : "not ok 54\n"); # P=Q=R S
98print (substr($a,2,-3) eq '' ? "ok 55\n" : "not ok 55\n"); # P Q=R S
99print (substr($a,0,0) eq '' ? "ok 56\n" : "not ok 56\n"); # P=Q=R S
100print (substr($a,0,5) eq '54321' ? "ok 57\n" : "not ok 57\n");# P=Q R=S
101print (substr($a,0,7) eq '54321' ? "ok 58\n" : "not ok 58\n");# P=Q S R
102print (substr($a,2,0) eq '' ? "ok 59\n" : "not ok 59\n"); # P Q=R S
103print (substr($a,2,3) eq '321' ? "ok 60\n" : "not ok 60\n"); # P Q R=S
104print (substr($a,5,0) eq '' ? "ok 61\n" : "not ok 61\n"); # P Q=R=S
105print (substr($a,5,2) eq '' ? "ok 62\n" : "not ok 62\n"); # P Q=S R
106print (substr($a,-7,-5) eq '' ? "ok 63\n" : "not ok 63\n"); # Q P=R S
107print (substr($a,-7,-2) eq '543' ? "ok 64\n" : "not ok 64\n");# Q P R S
108print (substr($a,-5,-5) eq '' ? "ok 65\n" : "not ok 65\n"); # P=Q=R S
109print (substr($a,-5,-2) eq '543' ? "ok 66\n" : "not ok 66\n");# P=Q R S
110print (substr($a,-3,-3) eq '' ? "ok 67\n" : "not ok 67\n"); # P Q=R S
111print (substr($a,-3,-1) eq '32' ? "ok 68\n" : "not ok 68\n");# P Q R S
112
113$a = '';
114
115print (substr($a,-2,2) eq '' ? "ok 69\n" : "not ok 69\n"); # Q P=R=S
116print (substr($a,0,0) eq '' ? "ok 70\n" : "not ok 70\n"); # P=Q=R=S
117print (substr($a,0,1) eq '' ? "ok 71\n" : "not ok 71\n"); # P=Q=S R
118print (substr($a,-2,3) eq '' ? "ok 72\n" : "not ok 72\n"); # Q P=S R
119print (substr($a,-2) eq '' ? "ok 73\n" : "not ok 73\n"); # Q P=R=S
120print (substr($a,0) eq '' ? "ok 74\n" : "not ok 74\n"); # P=Q=R=S
121
122
123print (substr($a,0,-1) eq '' ? "ok 75\n" : "not ok 75\n"); # R P=Q=S
124print (fail(substr($a,-2,0)) ? "ok 76\n" : "not ok 76\n"); # Q=R P=S
125print (fail(substr($a,-2,1)) ? "ok 77\n" : "not ok 77\n"); # Q R P=S
126print (fail(substr($a,-2,-1)) ? "ok 78\n" : "not ok 78\n"); # Q R P=S
127print (fail(substr($a,-2,-2)) ? "ok 79\n" : "not ok 79\n"); # Q=R P=S
128print (fail(substr($a,1,-2)) ? "ok 80\n" : "not ok 81\n"); # R P=S Q
129print (fail(substr($a,1,1)) ? "ok 81\n" : "not ok 81\n"); # P=S Q R
130print (fail(substr($a,1,0)) ? "ok 82\n" : "not ok 82\n"); # P=S Q=R
131print (fail(substr($a,1)) ? "ok 83\n" : "not ok 83\n"); # P=R=S Q
132
133
134my $a = 'zxcvbnm';
135substr($a,2,0) = '';
136print $a eq 'zxcvbnm' ? "ok 84\n" : "not ok 84\n";
137substr($a,7,0) = '';
138print $a eq 'zxcvbnm' ? "ok 85\n" : "not ok 85\n";
139substr($a,5,0) = '';
140print $a eq 'zxcvbnm' ? "ok 86\n" : "not ok 86\n";
141substr($a,0,2) = 'pq';
142print $a eq 'pqcvbnm' ? "ok 87\n" : "not ok 87\n";
143substr($a,2,0) = 'r';
144print $a eq 'pqrcvbnm' ? "ok 88\n" : "not ok 88\n";
145substr($a,8,0) = 'asd';
146print $a eq 'pqrcvbnmasd' ? "ok 89\n" : "not ok 89\n";
147substr($a,0,2) = 'iop';
148print $a eq 'ioprcvbnmasd' ? "ok 90\n" : "not ok 90\n";
149substr($a,0,5) = 'fgh';
150print $a eq 'fghvbnmasd' ? "ok 91\n" : "not ok 91\n";
151substr($a,3,5) = 'jkl';
152print $a eq 'fghjklsd' ? "ok 92\n" : "not ok 92\n";
153substr($a,3,2) = '1234';
154print $a eq 'fgh1234lsd' ? "ok 93\n" : "not ok 93\n";
155
08cb0b0d 156
157# with lexicals (and in re-entered scopes)
158for (0,1) {
159 my $txt;
160 unless ($_) {
161 $txt = "Foo";
162 substr($txt, -1) = "X";
84902520 163 print $txt eq "FoX" ? "ok 94\n" : "not ok 94\n";
08cb0b0d 164 }
165 else {
84902520 166 local $^W = 0; # because of (spurious?) "uninitialised value"
08cb0b0d 167 substr($txt, 0, 1) = "X";
84902520 168 print $txt eq "X" ? "ok 95\n" : "not ok 95\n";
08cb0b0d 169 }
170}
171
84902520 172# coercion of references
08cb0b0d 173{
174 my $s = [];
175 substr($s, 0, 1) = 'Foo';
84902520 176 print substr($s,0,7) eq "FooRRAY" && !($w-=2) ? "ok 96\n" : "not ok 96\n";
08cb0b0d 177}
84902520
TB
178
179# check no spurious warnings
180print $w ? "not ok 97\n" : "ok 97\n";
7b8d334a 181
5d82c453 182# check new 4 arg replacement syntax
7b8d334a 183$a = "abcxyz";
5d82c453 184$w = 0;
7b8d334a
GS
185print "not " unless substr($a, 0, 3, "") eq "abc" && $a eq "xyz";
186print "ok 98\n";
187print "not " unless substr($a, 0, 0, "abc") eq "" && $a eq "abcxyz";
188print "ok 99\n";
5d82c453 189print "not " unless substr($a, 3, -1, "") eq "xy" && $a eq "abcz";
7b8d334a
GS
190print "ok 100\n";
191
5d82c453
GA
192print "not " unless substr($a, 3, undef, "xy") eq "" && $a eq "abcxyz"
193 && $w == 3;
194print "ok 101\n";
195$w = 0;
196
197print "not " unless substr($a, 3, 9999999, "") eq "xyz" && $a eq "abc";
198print "ok 102\n";
199print "not " unless fail(substr($a, -99, 0, ""));
200print "ok 103\n";
201print "not " unless fail(substr($a, 99, 3, ""));
202print "ok 104\n";
203
204substr($a, 0, length($a), "foo");
205print "not " unless $a eq "foo" && !$w;
206print "ok 105\n";
207
208# using 4 arg substr as lvalue is a compile time error
209eval 'substr($a,0,0,"") = "abc"';
210print "not " unless $@ && $@ =~ /Can't modify substr/ && $a eq "foo";
211print "ok 106\n";