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