Integrate mainperl.
[p5sagit/p5-mst-13.2.git] / t / op / subst.t
CommitLineData
d9d8d8de 1#!./perl
2
344462d3 3BEGIN { @INC = ('../lib') }
4
2beec16e 5print "1..71\n";
d9d8d8de 6
7$x = 'foo';
8$_ = "x";
9s/x/\$x/;
10print "#1\t:$_: eq :\$x:\n";
11if ($_ eq '$x') {print "ok 1\n";} else {print "not ok 1\n";}
12
13$_ = "x";
14s/x/$x/;
15print "#2\t:$_: eq :foo:\n";
16if ($_ eq 'foo') {print "ok 2\n";} else {print "not ok 2\n";}
17
18$_ = "x";
19s/x/\$x $x/;
20print "#3\t:$_: eq :\$x foo:\n";
21if ($_ eq '$x foo') {print "ok 3\n";} else {print "not ok 3\n";}
22
23$b = 'cd';
79072805 24($a = 'abcdef') =~ s<(b${b}e)>'\n$1';
d9d8d8de 25print "#4\t:$1: eq :bcde:\n";
26print "#4\t:$a: eq :a\\n\$1f:\n";
27if ($1 eq 'bcde' && $a eq 'a\n$1f') {print "ok 4\n";} else {print "not ok 4\n";}
28
29$a = 'abacada';
30if (($a =~ s/a/x/g) == 4 && $a eq 'xbxcxdx')
31 {print "ok 5\n";} else {print "not ok 5\n";}
32
33if (($a =~ s/a/y/g) == 0 && $a eq 'xbxcxdx')
34 {print "ok 6\n";} else {print "not ok 6 $a\n";}
35
36if (($a =~ s/b/y/g) == 1 && $a eq 'xyxcxdx')
37 {print "ok 7\n";} else {print "not ok 7 $a\n";}
38
39$_ = 'ABACADA';
40if (/a/i && s///gi && $_ eq 'BCD') {print "ok 8\n";} else {print "not ok 8 $_\n";}
41
42$_ = '\\' x 4;
43if (length($_) == 4) {print "ok 9\n";} else {print "not ok 9\n";}
44s/\\/\\\\/g;
45if ($_ eq '\\' x 8) {print "ok 10\n";} else {print "not ok 10 $_\n";}
46
47$_ = '\/' x 4;
48if (length($_) == 8) {print "ok 11\n";} else {print "not ok 11\n";}
49s/\//\/\//g;
50if ($_ eq '\\//' x 4) {print "ok 12\n";} else {print "not ok 12\n";}
51if (length($_) == 12) {print "ok 13\n";} else {print "not ok 13\n";}
52
53$_ = 'aaaXXXXbbb';
54s/^a//;
55print $_ eq 'aaXXXXbbb' ? "ok 14\n" : "not ok 14\n";
56
57$_ = 'aaaXXXXbbb';
58s/a//;
59print $_ eq 'aaXXXXbbb' ? "ok 15\n" : "not ok 15\n";
60
61$_ = 'aaaXXXXbbb';
62s/^a/b/;
63print $_ eq 'baaXXXXbbb' ? "ok 16\n" : "not ok 16\n";
64
65$_ = 'aaaXXXXbbb';
66s/a/b/;
67print $_ eq 'baaXXXXbbb' ? "ok 17\n" : "not ok 17\n";
68
69$_ = 'aaaXXXXbbb';
70s/aa//;
71print $_ eq 'aXXXXbbb' ? "ok 18\n" : "not ok 18\n";
72
73$_ = 'aaaXXXXbbb';
74s/aa/b/;
75print $_ eq 'baXXXXbbb' ? "ok 19\n" : "not ok 19\n";
76
77$_ = 'aaaXXXXbbb';
78s/b$//;
79print $_ eq 'aaaXXXXbb' ? "ok 20\n" : "not ok 20\n";
80
81$_ = 'aaaXXXXbbb';
82s/b//;
83print $_ eq 'aaaXXXXbb' ? "ok 21\n" : "not ok 21\n";
84
85$_ = 'aaaXXXXbbb';
86s/bb//;
87print $_ eq 'aaaXXXXb' ? "ok 22\n" : "not ok 22\n";
88
89$_ = 'aaaXXXXbbb';
90s/aX/y/;
91print $_ eq 'aayXXXbbb' ? "ok 23\n" : "not ok 23\n";
92
93$_ = 'aaaXXXXbbb';
94s/Xb/z/;
95print $_ eq 'aaaXXXzbb' ? "ok 24\n" : "not ok 24\n";
96
97$_ = 'aaaXXXXbbb';
98s/aaX.*Xbb//;
99print $_ eq 'ab' ? "ok 25\n" : "not ok 25\n";
100
101$_ = 'aaaXXXXbbb';
102s/bb/x/;
103print $_ eq 'aaaXXXXxb' ? "ok 26\n" : "not ok 26\n";
104
105# now for some unoptimized versions of the same.
106
107$_ = 'aaaXXXXbbb';
108$x ne $x || s/^a//;
109print $_ eq 'aaXXXXbbb' ? "ok 27\n" : "not ok 27\n";
110
111$_ = 'aaaXXXXbbb';
112$x ne $x || s/a//;
113print $_ eq 'aaXXXXbbb' ? "ok 28\n" : "not ok 28\n";
114
115$_ = 'aaaXXXXbbb';
116$x ne $x || s/^a/b/;
117print $_ eq 'baaXXXXbbb' ? "ok 29\n" : "not ok 29\n";
118
119$_ = 'aaaXXXXbbb';
120$x ne $x || s/a/b/;
121print $_ eq 'baaXXXXbbb' ? "ok 30\n" : "not ok 30\n";
122
123$_ = 'aaaXXXXbbb';
124$x ne $x || s/aa//;
125print $_ eq 'aXXXXbbb' ? "ok 31\n" : "not ok 31\n";
126
127$_ = 'aaaXXXXbbb';
128$x ne $x || s/aa/b/;
129print $_ eq 'baXXXXbbb' ? "ok 32\n" : "not ok 32\n";
130
131$_ = 'aaaXXXXbbb';
132$x ne $x || s/b$//;
133print $_ eq 'aaaXXXXbb' ? "ok 33\n" : "not ok 33\n";
134
135$_ = 'aaaXXXXbbb';
136$x ne $x || s/b//;
137print $_ eq 'aaaXXXXbb' ? "ok 34\n" : "not ok 34\n";
138
139$_ = 'aaaXXXXbbb';
140$x ne $x || s/bb//;
141print $_ eq 'aaaXXXXb' ? "ok 35\n" : "not ok 35\n";
142
143$_ = 'aaaXXXXbbb';
144$x ne $x || s/aX/y/;
145print $_ eq 'aayXXXbbb' ? "ok 36\n" : "not ok 36\n";
146
147$_ = 'aaaXXXXbbb';
148$x ne $x || s/Xb/z/;
149print $_ eq 'aaaXXXzbb' ? "ok 37\n" : "not ok 37\n";
150
151$_ = 'aaaXXXXbbb';
152$x ne $x || s/aaX.*Xbb//;
153print $_ eq 'ab' ? "ok 38\n" : "not ok 38\n";
154
155$_ = 'aaaXXXXbbb';
156$x ne $x || s/bb/x/;
157print $_ eq 'aaaXXXXxb' ? "ok 39\n" : "not ok 39\n";
158
159$_ = 'abc123xyz';
c277df42 160s/(\d+)/$1*2/e; # yields 'abc246xyz'
d9d8d8de 161print $_ eq 'abc246xyz' ? "ok 40\n" : "not ok 40\n";
c277df42 162s/(\d+)/sprintf("%5d",$1)/e; # yields 'abc 246xyz'
d9d8d8de 163print $_ eq 'abc 246xyz' ? "ok 41\n" : "not ok 41\n";
c277df42 164s/(\w)/$1 x 2/eg; # yields 'aabbcc 224466xxyyzz'
d9d8d8de 165print $_ eq 'aabbcc 224466xxyyzz' ? "ok 42\n" : "not ok 42\n";
166
167$_ = "aaaaa";
168print y/a/b/ == 5 ? "ok 43\n" : "not ok 43\n";
169print y/a/b/ == 0 ? "ok 44\n" : "not ok 44\n";
170print y/b// == 5 ? "ok 45\n" : "not ok 45\n";
171print y/b/c/s == 5 ? "ok 46\n" : "not ok 46\n";
172print y/c// == 1 ? "ok 47\n" : "not ok 47\n";
173print y/c//d == 1 ? "ok 48\n" : "not ok 48\n";
174print $_ eq "" ? "ok 49\n" : "not ok 49\n";
175
176$_ = "Now is the %#*! time for all good men...";
177print (($x=(y/a-zA-Z //cd)) == 7 ? "ok 50\n" : "not ok 50\n");
178print y/ / /s == 8 ? "ok 51\n" : "not ok 51\n";
179
79072805 180$_ = 'abcdefghijklmnopqrstuvwxyz0123456789';
181tr/a-z/A-Z/;
182
183print $_ eq 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' ? "ok 52\n" : "not ok 52\n";
184
185# same as tr/A-Z/a-z/;
9d116dd7 186if ($^O eq 'os390') { # An EBCDIC variant.
6e68dac8 187 no utf8;
9d116dd7 188 y[\301-\351][\201-\251];
189} else { # Ye Olde ASCII. Or something like it.
190 y[\101-\132][\141-\172];
191}
79072805 192
193print $_ eq 'abcdefghijklmnopqrstuvwxyz0123456789' ? "ok 53\n" : "not ok 53\n";
194
9d116dd7 195if (ord("+") == ord(",") - 1 && ord(",") == ord("-") - 1 &&
196 ord("a") == ord("b") - 1 && ord("b") == ord("c") - 1) {
197 $_ = '+,-';
198 tr/+--/a-c/;
199 print "not " unless $_ eq 'abc';
200}
201print "ok 54\n";
79072805 202
203$_ = '+,-';
204tr/+\--/a\/c/;
205print $_ eq 'a,/' ? "ok 55\n" : "not ok 55\n";
206
207$_ = '+,-';
208tr/-+,/ab\-/;
209print $_ eq 'b-a' ? "ok 56\n" : "not ok 56\n";
843b4603 210
211
212# test recursive substitutions
213# code based on the recursive expansion of makefile variables
214
215my %MK = (
216 AAAAA => '$(B)', B=>'$(C)', C => 'D', # long->short
217 E => '$(F)', F=>'p $(G) q', G => 'HHHHH', # short->long
218 DIR => '$(UNDEFINEDNAME)/xxx',
219);
220sub var {
221 my($var,$level) = @_;
222 return "\$($var)" unless exists $MK{$var};
223 return exp_vars($MK{$var}, $level+1); # can recurse
224}
225sub exp_vars {
226 my($str,$level) = @_;
227 $str =~ s/\$\((\w+)\)/var($1, $level+1)/ge; # can recurse
228 #warn "exp_vars $level = '$str'\n";
229 $str;
230}
231
232print exp_vars('$(AAAAA)',0) eq 'D'
233 ? "ok 57\n" : "not ok 57\n";
234print exp_vars('$(E)',0) eq 'p HHHHH q'
235 ? "ok 58\n" : "not ok 58\n";
236print exp_vars('$(DIR)',0) eq '$(UNDEFINEDNAME)/xxx'
237 ? "ok 59\n" : "not ok 59\n";
238print exp_vars('foo $(DIR)/yyy bar',0) eq 'foo $(UNDEFINEDNAME)/xxx/yyy bar'
239 ? "ok 60\n" : "not ok 60\n";
240
3e3baf6d 241# a match nested in the RHS of a substitution:
242
243$_ = "abcd";
c277df42 244s/(..)/$x = $1, m#.#/eg;
3e3baf6d 245print $x eq "cd" ? "ok 61\n" : "not ok 61\n";
fb73857a 246
c277df42 247# Subst and lookbehind
248
249$_="ccccc";
250s/(?<!x)c/x/g;
251print $_ eq "xxxxx" ? "ok 62\n" : "not ok 62 # `$_' ne `xxxxx'\n";
252
253$_="ccccc";
254s/(?<!x)(c)/x/g;
255print $_ eq "xxxxx" ? "ok 63\n" : "not ok 63 # `$_' ne `xxxxx'\n";
256
257$_="foobbarfoobbar";
258s/(?<!r)foobbar/foobar/g;
259print $_ eq "foobarfoobbar" ? "ok 64\n" : "not ok 64 # `$_' ne `foobarfoobbar'\n";
260
261$_="foobbarfoobbar";
262s/(?<!ar)(foobbar)/foobar/g;
263print $_ eq "foobarfoobbar" ? "ok 65\n" : "not ok 65 # `$_' ne `foobarfoobbar'\n";
264
265$_="foobbarfoobbar";
266s/(?<!ar)foobbar/foobar/g;
267print $_ eq "foobarfoobbar" ? "ok 66\n" : "not ok 66 # `$_' ne `foobarfoobbar'\n";
268
fb73857a 269# check parsing of split subst with comment
270eval 's{foo} # this is a comment, not a delimiter
271 {bar};';
c277df42 272print @? ? "not ok 67\n" : "ok 67\n";
f3ea7b5e 273
274# check if squashing works at the end of string
275$_="baacbaa";
276tr/a/b/s;
277print $_ eq "bbcbb" ? "ok 68\n" : "not ok 68 # `$_' ne `bbcbb'\n";
278
2216f30a 279# XXX TODO: Most tests above don't test return values of the ops. They should.
280$_ = "ab";
281print (s/a/b/ == 1 ? "ok 69\n" : "not ok 69\n");
ce862d02 282
283$_ = <<'EOL';
284 $url = new URI::URL "http://www/"; die if $url eq "xXx";
285EOL
286$^R = 'junk';
287
288$foo = ' $@%#lowercase $@%# lowercase UPPERCASE$@%#UPPERCASE' .
289 ' $@%#lowercase$@%#lowercase$@%# lowercase lowercase $@%#lowercase' .
290 ' lowercase $@%#MiXeD$@%# ';
291
292s{ \d+ \b [,.;]? (?{ 'digits' })
293 |
294 [a-z]+ \b [,.;]? (?{ 'lowercase' })
295 |
296 [A-Z]+ \b [,.;]? (?{ 'UPPERCASE' })
297 |
298 [A-Z] [a-z]+ \b [,.;]? (?{ 'Capitalized' })
299 |
300 [A-Za-z]+ \b [,.;]? (?{ 'MiXeD' })
301 |
302 [A-Za-z0-9]+ \b [,.;]? (?{ 'alphanumeric' })
303 |
304 \s+ (?{ ' ' })
305 |
306 [^A-Za-z0-9\s]+ (?{ '$@%#' })
307}{$^R}xg;
308print ($_ eq $foo ? "ok 70\n" : "not ok 70\n#'$_'\n#'$foo'\n");
309
2beec16e 310$_ = 'x' x 20;
311s/\d*|x/<$&>/g;
312$foo = '<>' . ('<x><>' x 20) ;
313print ($_ eq $foo ? "ok 71\n" : "not ok 71\n#'$_'\n#'$foo'\n");