This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Tru64, gcc -O3, datasize
[perl5.git] / t / op / split.t
CommitLineData
8d063cd8
LW
1#!./perl
2
a8a2fe91
JH
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
6}
7
6de67870 8print "1..46\n";
8d063cd8
LW
9
10$FS = ':';
11
12$_ = 'a:b:c';
13
14($a,$b,$c) = split($FS,$_);
15
16if (join(';',$a,$b,$c) eq 'a;b;c') {print "ok 1\n";} else {print "not ok 1\n";}
17
18@ary = split(/:b:/);
19if (join("$_",@ary) eq 'aa:b:cc') {print "ok 2\n";} else {print "not ok 2\n";}
20
21$_ = "abc\n";
4765795a 22my @xyz = (@ary = split(//));
8d063cd8
LW
23if (join(".",@ary) eq "a.b.c.\n") {print "ok 3\n";} else {print "not ok 3\n";}
24
25$_ = "a:b:c::::";
26@ary = split(/:/);
27if (join(".",@ary) eq "a.b.c") {print "ok 4\n";} else {print "not ok 4\n";}
2e1b3b7e 28
378cc40b
LW
29$_ = join(':',split(' '," a b\tc \t d "));
30if ($_ eq 'a:b:c:d') {print "ok 5\n";} else {print "not ok 5 #$_#\n";}
2e1b3b7e
KK
31
32$_ = join(':',split(/ */,"foo bar bie\tdoll"));
33if ($_ eq "f:o:o:b:a:r:b:i:e:\t:d:o:l:l")
34 {print "ok 6\n";} else {print "not ok 6\n";}
378cc40b
LW
35
36$_ = join(':', 'foo', split(/ /,'a b c'), 'bar');
37if ($_ eq "foo:a:b::c:bar") {print "ok 7\n";} else {print "not ok 7 $_\n";}
38
a687059c
LW
39# Can we say how many fields to split to?
40$_ = join(':', split(' ','1 2 3 4 5 6', 3));
41print $_ eq '1:2:3 4 5 6' ? "ok 8\n" : "not ok 8 $_\n";
42
43# Can we do it as a variable?
44$x = 4;
45$_ = join(':', split(' ','1 2 3 4 5 6', $x));
46print $_ eq '1:2:3:4 5 6' ? "ok 9\n" : "not ok 9 $_\n";
47
48# Does the 999 suppress null field chopping?
49$_ = join(':', split(/:/,'1:2:3:4:5:6:::', 999));
50print $_ eq '1:2:3:4:5:6:::' ? "ok 10\n" : "not ok 10 $_\n";
51
52# Does assignment to a list imply split to one more field than that?
68dc0745 53if ($^O eq 'MSWin32') { $foo = `.\\perl -D1024 -e "(\$a,\$b) = split;" 2>&1` }
2986a63f 54elsif ($^O eq 'NetWare') { $foo = `perl -D1024 -e "(\$a,\$b) = split;" 2>&1` }
f0963acb 55elsif ($^O eq 'VMS') { $foo = `./perl "-D1024" -e "(\$a,\$b) = split;" 2>&1` }
95e8664e 56elsif ($^O eq 'MacOS'){ $foo = `$^X "-D1024" -e "(\$a,\$b) = split;"` }
68dc0745 57else { $foo = `./perl -D1024 -e '(\$a,\$b) = split;' 2>&1` }
bfe546ed 58print $foo =~ /DEBUGGING/ || $foo =~ /SV = (VOID|IV\(3\))/ ? "ok 11\n" : "not ok 11\n";
a687059c
LW
59
60# Can we say how many fields to split to when assigning to a list?
61($a,$b) = split(' ','1 2 3 4 5 6', 2);
62$_ = join(':',$a,$b);
63print $_ eq '1:2 3 4 5 6' ? "ok 12\n" : "not ok 12 $_\n";
64
084811a7
PP
65# do subpatterns generate additional fields (without trailing nulls)?
66$_ = join '|', split(/,|(-)/, "1-10,20,,,");
67print $_ eq "1|-|10||20" ? "ok 13\n" : "not ok 13\n";
68
69# do subpatterns generate additional fields (with a limit)?
70$_ = join '|', split(/,|(-)/, "1-10,20,,,", 10);
71print $_ eq "1|-|10||20||||||" ? "ok 14\n" : "not ok 14\n";
e1fa4fd3
HS
72
73# is the 'two undefs' bug fixed?
74(undef, $a, undef, $b) = qw(1 2 3 4);
75print "$a|$b" eq "2|4" ? "ok 15\n" : "not ok 15\n";
76
77# .. even for locals?
78{
79 local(undef, $a, undef, $b) = qw(1 2 3 4);
80 print "$a|$b" eq "2|4" ? "ok 16\n" : "not ok 16\n";
81}
fb73857a
PP
82
83# check splitting of null string
84$_ = join('|', split(/x/, '',-1), 'Z');
85print $_ eq "Z" ? "ok 17\n" : "#$_\nnot ok 17\n";
86
87$_ = join('|', split(/x/, '', 1), 'Z');
88print $_ eq "Z" ? "ok 18\n" : "#$_\nnot ok 18\n";
89
90$_ = join('|', split(/(p+)/,'',-1), 'Z');
91print $_ eq "Z" ? "ok 19\n" : "#$_\nnot ok 19\n";
92
93$_ = join('|', split(/.?/, '',-1), 'Z');
94print $_ eq "Z" ? "ok 20\n" : "#$_\nnot ok 20\n";
95
c277df42
IZ
96
97# Are /^/m patterns scanned?
98$_ = join '|', split(/^a/m, "a b a\na d a", 20);
99print $_ eq "| b a\n| d a" ? "ok 21\n" : "not ok 21\n# `$_'\n";
100
101# Are /$/m patterns scanned?
102$_ = join '|', split(/a$/m, "a b a\na d a", 20);
103print $_ eq "a b |\na d |" ? "ok 22\n" : "not ok 22\n# `$_'\n";
104
105# Are /^/m patterns scanned?
106$_ = join '|', split(/^aa/m, "aa b aa\naa d aa", 20);
107print $_ eq "| b aa\n| d aa" ? "ok 23\n" : "not ok 23\n# `$_'\n";
108
109# Are /$/m patterns scanned?
110$_ = join '|', split(/aa$/m, "aa b aa\naa d aa", 20);
111print $_ eq "aa b |\naa d |" ? "ok 24\n" : "not ok 24\n# `$_'\n";
112
113# Greedyness:
114$_ = "a : b :c: d";
115@ary = split(/\s*:\s*/);
116if (($res = join(".",@ary)) eq "a.b.c.d") {print "ok 25\n";} else {print "not ok 25\n# res=`$res' != `a.b.c.d'\n";}
815d35b9
G
117
118# use of match result as pattern (!)
cc50a203 119'p:q:r:s' eq join ':', split('abc' =~ /b/, 'p1q1r1s') or print "not ";
815d35b9 120print "ok 26\n";
1ec94568
G
121
122# /^/ treated as /^/m
123$_ = join ':', split /^/, "ab\ncd\nef\n";
124print "not " if $_ ne "ab\n:cd\n:ef\n";
125print "ok 27\n";
b3f5893f
GS
126
127# see if @a = @b = split(...) optimization works
128@list1 = @list2 = split ('p',"a p b c p");
129print "not " if @list1 != @list2 or "@list1" ne "@list2"
130 or @list1 != 2 or "@list1" ne "a b c ";
131print "ok 28\n";
0156e0fd
RB
132
133# zero-width assertion
134$_ = join ':', split /(?=\w)/, "rm b";
135print "not" if $_ ne "r:m :b";
136print "ok 29\n";
5a2d9fa2
JH
137
138# unicode splittage
974f237a 139
5a2d9fa2
JH
140@ary = map {ord} split //, v1.20.300.4000.50000.4000.300.20.1;
141print "not " unless "@ary" eq "1 20 300 4000 50000 4000 300 20 1";
142print "ok 30\n";
974f237a
JH
143
144@ary = split(/\x{FE}/, "\x{FF}\x{FE}\x{FD}"); # bug id 20010105.016
145print "not " unless @ary == 2 &&
146 $ary[0] eq "\xFF" && $ary[1] eq "\xFD" &&
147 $ary[0] eq "\x{FF}" && $ary[1] eq "\x{FD}";
148print "ok 31\n";
149
150@ary = split(/(\x{FE}\xFE)/, "\xFF\x{FF}\xFE\x{FE}\xFD\x{FD}"); # variant of 31
151print "not " unless @ary == 3 &&
152 $ary[0] eq "\xFF\xFF" &&
153 $ary[0] eq "\x{FF}\xFF" &&
154 $ary[0] eq "\x{FF}\x{FF}" &&
155 $ary[1] eq "\xFE\xFE" &&
156 $ary[1] eq "\x{FE}\xFE" &&
157 $ary[1] eq "\x{FE}\x{FE}" &&
158 $ary[2] eq "\xFD\xFD" &&
159 $ary[2] eq "\x{FD}\xFD" &&
160 $ary[2] eq "\x{FD}\x{FD}";
974f237a 161print "ok 32\n";
4765795a
JH
162
163
164{
165 my @a = map ord, split(//, join("", map chr, (1234, 123, 2345)));
166 print "not " unless "@a" eq "1234 123 2345";
167 print "ok 33\n";
168}
169
170{
31e261c7
JH
171 my $x = 'A';
172 my @a = map ord, split(/$x/, join("", map chr, (1234, ord($x), 2345)));
4765795a
JH
173 print "not " unless "@a" eq "1234 2345";
174 print "ok 34\n";
175}
176
177{
178 # bug id 20000427.003
179
180 use warnings;
181 use strict;
182
183 my $sushi = "\x{b36c}\x{5a8c}\x{ff5b}\x{5079}\x{505b}";
184
185 my @charlist = split //, $sushi;
186 my $r = '';
187 foreach my $ch (@charlist) {
188 $r = $r . " " . sprintf "U+%04X", ord($ch);
189 }
190
191 print "not " unless $r eq " U+B36C U+5A8C U+FF5B U+5079 U+505B";
192 print "ok 35\n";
193}
194
195{
dd83d948
DD
196 my $s = "\x20\x40\x{80}\x{100}\x{80}\x40\x20";
197
31e261c7
JH
198 if (ord('A') == 193) {
199 print "ok 36 # Skip: EBCDIC\n";
200 } else {
201 # bug id 20000426.003
4765795a 202
4765795a 203
31e261c7
JH
204 my ($a, $b, $c) = split(/\x40/, $s);
205 print "not "
206 unless $a eq "\x20" && $b eq "\x{80}\x{100}\x{80}" && $c eq $a;
207 print "ok 36\n";
208 }
4765795a
JH
209
210 my ($a, $b) = split(/\x{100}/, $s);
211 print "not " unless $a eq "\x20\x40\x{80}" && $b eq "\x{80}\x40\x20";
212 print "ok 37\n";
213
214 my ($a, $b) = split(/\x{80}\x{100}\x{80}/, $s);
215 print "not " unless $a eq "\x20\x40" && $b eq "\x40\x20";
216 print "ok 38\n";
217
31e261c7
JH
218 if (ord('A') == 193) {
219 print "ok 39 # Skip: EBCDIC\n";
220 } else {
221 my ($a, $b) = split(/\x40\x{80}/, $s);
222 print "not " unless $a eq "\x20" && $b eq "\x{100}\x{80}\x40\x20";
223 print "ok 39\n";
224 }
4765795a
JH
225
226 my ($a, $b, $c) = split(/[\x40\x{80}]+/, $s);
227 print "not " unless $a eq "\x20" && $b eq "\x{100}" && $c eq "\x20";
228 print "ok 40\n";
229}
230
231{
232 # 20001205.014
233
234 my $a = "ABC\x{263A}";
235
236 my @b = split( //, $a );
237
238 print "not " unless @b == 4;
239 print "ok 41\n";
240
241 print "not " unless length($b[3]) == 1 && $b[3] eq "\x{263A}";
242 print "ok 42\n";
243
244 $a =~ s/^A/Z/;
245 print "not " unless length($a) == 4 && $a eq "ZBC\x{263A}";
246 print "ok 43\n";
247}
248
249{
250 my @a = split(/\xFE/, "\xFF\xFE\xFD");
251
252 print "not " unless @a == 2 && $a[0] eq "\xFF" && $a[1] eq "\xFD";
253 print "ok 44\n";
254}
255
16bdb4ac
RG
256{
257 # check that PMf_WHITE is cleared after \s+ is used
258 # reported in <20010627113312.RWGY6087.viemta06@localhost>
259 my $r;
260 foreach my $pat ( qr/\s+/, qr/ll/ ) {
261 $r = join ':' => split($pat, "hello cruel world");
262 }
263 print "not " unless $r eq "he:o cruel world";
264 print "ok 45\n";
265}
6de67870
JP
266
267
268{
269 # split /(A)|B/, "1B2" should return (1, undef, 2)
270 my @x = split /(A)|B/, "1B2";
271 print "not " unless
272 $x[0] eq '1' and
273 (not defined $x[1]) and
274 $x[2] eq '2';
275 print "ok 46\n";
276}