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