This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Get t/uni/cache.t working under minitest
[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
60041a09 9plan tests => 119;
8d063cd8
LW
10
11$FS = ':';
12
13$_ = 'a:b:c';
14
15($a,$b,$c) = split($FS,$_);
16
acb447f4 17is(join(';',$a,$b,$c), 'a;b;c', 'Split a simple string into scalars.');
8d063cd8
LW
18
19@ary = split(/:b:/);
c1a7495a 20$cnt = split(/:b:/);
3a2263fe 21is(join("$_",@ary), 'aa:b:cc');
c1a7495a 22is($cnt, scalar(@ary));
8d063cd8
LW
23
24$_ = "abc\n";
4765795a 25my @xyz = (@ary = split(//));
c1a7495a 26$cnt = split(//);
3a2263fe 27is(join(".",@ary), "a.b.c.\n");
c1a7495a 28is($cnt, scalar(@ary));
8d063cd8
LW
29
30$_ = "a:b:c::::";
31@ary = split(/:/);
c1a7495a 32$cnt = split(/:/);
3a2263fe 33is(join(".",@ary), "a.b.c");
c1a7495a 34is($cnt, scalar(@ary));
2e1b3b7e 35
378cc40b 36$_ = join(':',split(' '," a b\tc \t d "));
3a2263fe 37is($_, 'a:b:c:d');
c1a7495a
BB
38@ary = split(' '," a b\tc \t d ");
39$cnt = split(' '," a b\tc \t d ");
40is($cnt, scalar(@ary));
2e1b3b7e
KK
41
42$_ = join(':',split(/ */,"foo bar bie\tdoll"));
3a2263fe 43is($_ , "f:o:o:b:a:r:b:i:e:\t:d:o:l:l");
c1a7495a
BB
44@ary = split(/ */,"foo bar bie\tdoll");
45$cnt = split(/ */,"foo bar bie\tdoll");
46is($cnt, scalar(@ary));
378cc40b
LW
47
48$_ = join(':', 'foo', split(/ /,'a b c'), 'bar');
3a2263fe 49is($_, "foo:a:b::c:bar");
c1a7495a
BB
50@ary = split(/ /,'a b c');
51$cnt = split(/ /,'a b c');
52is($cnt, scalar(@ary));
378cc40b 53
a687059c
LW
54# Can we say how many fields to split to?
55$_ = join(':', split(' ','1 2 3 4 5 6', 3));
acb447f4 56is($_, '1:2:3 4 5 6', "Split into a specified number of fields, defined by a literal");
c1a7495a
BB
57@ary = split(' ','1 2 3 4 5 6', 3);
58$cnt = split(' ','1 2 3 4 5 6', 3);
acb447f4 59is($cnt, scalar(@ary), "Check element count from previous test");
a687059c
LW
60
61# Can we do it as a variable?
62$x = 4;
63$_ = join(':', split(' ','1 2 3 4 5 6', $x));
acb447f4 64is($_, '1:2:3:4 5 6', "Split into a specified number of fields, defined by a scalar variable");
c1a7495a
BB
65@ary = split(' ','1 2 3 4 5 6', $x);
66$cnt = split(' ','1 2 3 4 5 6', $x);
acb447f4 67is($cnt, scalar(@ary), "Check element count from previous test");
a687059c
LW
68
69# Does the 999 suppress null field chopping?
70$_ = join(':', split(/:/,'1:2:3:4:5:6:::', 999));
3a2263fe 71is($_ , '1:2:3:4:5:6:::');
c1a7495a
BB
72@ary = split(/:/,'1:2:3:4:5:6:::', 999);
73$cnt = split(/:/,'1:2:3:4:5:6:::', 999);
74is($cnt, scalar(@ary));
a687059c 75
a720890c
GG
76# Splitting without pattern
77$_ = "1 2 3 4";
78$_ = join(':', split);
acb447f4 79is($_ , '1:2:3:4', "Split and join without specifying a split pattern");
a720890c 80
a687059c 81# Does assignment to a list imply split to one more field than that?
6cefa69e
RU
82$foo = runperl( switches => ['-Dt'], stderr => 1, prog => '($a,$b)=split;' );
83ok($foo =~ /DEBUGGING/ || $foo =~ /const\n?\Q(IV(3))\E/);
a687059c
LW
84
85# Can we say how many fields to split to when assigning to a list?
86($a,$b) = split(' ','1 2 3 4 5 6', 2);
87$_ = join(':',$a,$b);
acb447f4 88is($_, '1:2 3 4 5 6', "Storing split output into list of scalars");
a687059c 89
084811a7 90# do subpatterns generate additional fields (without trailing nulls)?
91$_ = join '|', split(/,|(-)/, "1-10,20,,,");
3a2263fe 92is($_, "1|-|10||20");
c1a7495a
BB
93@ary = split(/,|(-)/, "1-10,20,,,");
94$cnt = split(/,|(-)/, "1-10,20,,,");
95is($cnt, scalar(@ary));
084811a7 96
97# do subpatterns generate additional fields (with a limit)?
98$_ = join '|', split(/,|(-)/, "1-10,20,,,", 10);
3a2263fe 99is($_, "1|-|10||20||||||");
c1a7495a
BB
100@ary = split(/,|(-)/, "1-10,20,,,", 10);
101$cnt = split(/,|(-)/, "1-10,20,,,", 10);
102is($cnt, scalar(@ary));
e1fa4fd3
HS
103
104# is the 'two undefs' bug fixed?
105(undef, $a, undef, $b) = qw(1 2 3 4);
3a2263fe 106is("$a|$b", "2|4");
e1fa4fd3
HS
107
108# .. even for locals?
109{
110 local(undef, $a, undef, $b) = qw(1 2 3 4);
3a2263fe 111 is("$a|$b", "2|4");
e1fa4fd3 112}
fb73857a 113
114# check splitting of null string
115$_ = join('|', split(/x/, '',-1), 'Z');
3a2263fe 116is($_, "Z");
c1a7495a
BB
117@ary = split(/x/, '',-1);
118$cnt = split(/x/, '',-1);
119is($cnt, scalar(@ary));
fb73857a 120
121$_ = join('|', split(/x/, '', 1), 'Z');
3a2263fe 122is($_, "Z");
c1a7495a
BB
123@ary = split(/x/, '', 1);
124$cnt = split(/x/, '', 1);
125is($cnt, scalar(@ary));
fb73857a 126
127$_ = join('|', split(/(p+)/,'',-1), 'Z');
3a2263fe 128is($_, "Z");
c1a7495a
BB
129@ary = split(/(p+)/,'',-1);
130$cnt = split(/(p+)/,'',-1);
131is($cnt, scalar(@ary));
fb73857a 132
133$_ = join('|', split(/.?/, '',-1), 'Z');
3a2263fe 134is($_, "Z");
c1a7495a
BB
135@ary = split(/.?/, '',-1);
136$cnt = split(/.?/, '',-1);
137is($cnt, scalar(@ary));
fb73857a 138
c277df42
IZ
139
140# Are /^/m patterns scanned?
141$_ = join '|', split(/^a/m, "a b a\na d a", 20);
3a2263fe 142is($_, "| b a\n| d a");
c1a7495a
BB
143@ary = split(/^a/m, "a b a\na d a", 20);
144$cnt = split(/^a/m, "a b a\na d a", 20);
145is($cnt, scalar(@ary));
c277df42
IZ
146
147# Are /$/m patterns scanned?
148$_ = join '|', split(/a$/m, "a b a\na d a", 20);
3a2263fe 149is($_, "a b |\na d |");
c1a7495a
BB
150@ary = split(/a$/m, "a b a\na d a", 20);
151$cnt = split(/a$/m, "a b a\na d a", 20);
152is($cnt, scalar(@ary));
c277df42
IZ
153
154# Are /^/m patterns scanned?
155$_ = join '|', split(/^aa/m, "aa b aa\naa d aa", 20);
3a2263fe 156is($_, "| b aa\n| d aa");
c1a7495a
BB
157@ary = split(/^aa/m, "aa b aa\naa d aa", 20);
158$cnt = split(/^aa/m, "aa b aa\naa d aa", 20);
159is($cnt, scalar(@ary));
c277df42
IZ
160
161# Are /$/m patterns scanned?
162$_ = join '|', split(/aa$/m, "aa b aa\naa d aa", 20);
3a2263fe 163is($_, "aa b |\naa d |");
c1a7495a
BB
164@ary = split(/aa$/m, "aa b aa\naa d aa", 20);
165$cnt = split(/aa$/m, "aa b aa\naa d aa", 20);
166is($cnt, scalar(@ary));
c277df42
IZ
167
168# Greedyness:
169$_ = "a : b :c: d";
170@ary = split(/\s*:\s*/);
c1a7495a 171$cnt = split(/\s*:\s*/);
3a2263fe 172is(($res = join(".",@ary)), "a.b.c.d", $res);
c1a7495a 173is($cnt, scalar(@ary));
815d35b9
MG
174
175# use of match result as pattern (!)
3a2263fe 176is('p:q:r:s', join ':', split('abc' =~ /b/, 'p1q1r1s'));
c1a7495a
BB
177@ary = split('abc' =~ /b/, 'p1q1r1s');
178$cnt = split('abc' =~ /b/, 'p1q1r1s');
179is($cnt, scalar(@ary));
1ec94568
MG
180
181# /^/ treated as /^/m
182$_ = join ':', split /^/, "ab\ncd\nef\n";
3a2263fe 183is($_, "ab\n:cd\n:ef\n");
b3f5893f
GS
184
185# see if @a = @b = split(...) optimization works
186@list1 = @list2 = split ('p',"a p b c p");
3a2263fe
RGS
187ok(@list1 == @list2 &&
188 "@list1" eq "@list2" &&
189 @list1 == 2 &&
190 "@list1" eq "a b c ");
0156e0fd
RB
191
192# zero-width assertion
193$_ = join ':', split /(?=\w)/, "rm b";
3a2263fe 194is($_, "r:m :b");
c1a7495a
BB
195@ary = split /(?=\w)/, "rm b";
196$cnt = split /(?=\w)/, "rm b";
197is($cnt, scalar(@ary));
5a2d9fa2
JH
198
199# unicode splittage
974f237a 200
5a2d9fa2 201@ary = map {ord} split //, v1.20.300.4000.50000.4000.300.20.1;
c1a7495a 202$cnt = split //, v1.20.300.4000.50000.4000.300.20.1;
3a2263fe 203is("@ary", "1 20 300 4000 50000 4000 300 20 1");
c1a7495a 204is($cnt, scalar(@ary));
974f237a
JH
205
206@ary = split(/\x{FE}/, "\x{FF}\x{FE}\x{FD}"); # bug id 20010105.016
c1a7495a 207$cnt = split(/\x{FE}/, "\x{FF}\x{FE}\x{FD}"); # bug id 20010105.016
3a2263fe
RGS
208ok(@ary == 2 &&
209 $ary[0] eq "\xFF" && $ary[1] eq "\xFD" &&
210 $ary[0] eq "\x{FF}" && $ary[1] eq "\x{FD}");
c1a7495a 211is($cnt, scalar(@ary));
974f237a
JH
212
213@ary = split(/(\x{FE}\xFE)/, "\xFF\x{FF}\xFE\x{FE}\xFD\x{FD}"); # variant of 31
c1a7495a 214$cnt = split(/(\x{FE}\xFE)/, "\xFF\x{FF}\xFE\x{FE}\xFD\x{FD}"); # variant of 31
3a2263fe
RGS
215ok(@ary == 3 &&
216 $ary[0] eq "\xFF\xFF" &&
217 $ary[0] eq "\x{FF}\xFF" &&
218 $ary[0] eq "\x{FF}\x{FF}" &&
219 $ary[1] eq "\xFE\xFE" &&
220 $ary[1] eq "\x{FE}\xFE" &&
221 $ary[1] eq "\x{FE}\x{FE}" &&
222 $ary[2] eq "\xFD\xFD" &&
223 $ary[2] eq "\x{FD}\xFD" &&
224 $ary[2] eq "\x{FD}\x{FD}");
c1a7495a 225is($cnt, scalar(@ary));
4765795a
JH
226
227{
228 my @a = map ord, split(//, join("", map chr, (1234, 123, 2345)));
c1a7495a 229 my $c = split(//, join("", map chr, (1234, 123, 2345)));
3a2263fe 230 is("@a", "1234 123 2345");
c1a7495a 231 is($c, scalar(@a));
4765795a
JH
232}
233
234{
31e261c7
JH
235 my $x = 'A';
236 my @a = map ord, split(/$x/, join("", map chr, (1234, ord($x), 2345)));
c1a7495a 237 my $c = split(/$x/, join("", map chr, (1234, ord($x), 2345)));
3a2263fe 238 is("@a", "1234 2345");
c1a7495a 239 is($c, scalar(@a));
4765795a
JH
240}
241
242{
243 # bug id 20000427.003
244
245 use warnings;
246 use strict;
247
248 my $sushi = "\x{b36c}\x{5a8c}\x{ff5b}\x{5079}\x{505b}";
249
250 my @charlist = split //, $sushi;
c1a7495a
BB
251 my $charnum = split //, $sushi;
252 is($charnum, scalar(@charlist));
4765795a
JH
253 my $r = '';
254 foreach my $ch (@charlist) {
255 $r = $r . " " . sprintf "U+%04X", ord($ch);
256 }
257
3a2263fe 258 is($r, " U+B36C U+5A8C U+FF5B U+5079 U+505B");
4765795a
JH
259}
260
261{
dd83d948
DD
262 my $s = "\x20\x40\x{80}\x{100}\x{80}\x40\x20";
263
3a2263fe 264 SKIP: {
31e261c7 265 if (ord('A') == 193) {
3a2263fe 266 skip("EBCDIC", 1);
31e261c7
JH
267 } else {
268 # bug id 20000426.003
4765795a 269
31e261c7 270 my ($a, $b, $c) = split(/\x40/, $s);
3a2263fe 271 ok($a eq "\x20" && $b eq "\x{80}\x{100}\x{80}" && $c eq $a);
31e261c7 272 }
3a2263fe 273 }
4765795a
JH
274
275 my ($a, $b) = split(/\x{100}/, $s);
3a2263fe 276 ok($a eq "\x20\x40\x{80}" && $b eq "\x{80}\x40\x20");
4765795a
JH
277
278 my ($a, $b) = split(/\x{80}\x{100}\x{80}/, $s);
3a2263fe 279 ok($a eq "\x20\x40" && $b eq "\x40\x20");
4765795a 280
3a2263fe 281 SKIP: {
31e261c7 282 if (ord('A') == 193) {
3a2263fe 283 skip("EBCDIC", 1);
31e261c7
JH
284 } else {
285 my ($a, $b) = split(/\x40\x{80}/, $s);
3a2263fe 286 ok($a eq "\x20" && $b eq "\x{100}\x{80}\x40\x20");
31e261c7 287 }
3a2263fe 288 }
4765795a
JH
289
290 my ($a, $b, $c) = split(/[\x40\x{80}]+/, $s);
3a2263fe 291 ok($a eq "\x20" && $b eq "\x{100}" && $c eq "\x20");
4765795a
JH
292}
293
294{
295 # 20001205.014
296
297 my $a = "ABC\x{263A}";
298
299 my @b = split( //, $a );
c1a7495a
BB
300 my $c = split( //, $a );
301 is($c, scalar(@b));
4765795a 302
3a2263fe 303 is(scalar @b, 4);
4765795a 304
3a2263fe 305 ok(length($b[3]) == 1 && $b[3] eq "\x{263A}");
4765795a
JH
306
307 $a =~ s/^A/Z/;
3a2263fe 308 ok(length($a) == 4 && $a eq "ZBC\x{263A}");
4765795a
JH
309}
310
311{
312 my @a = split(/\xFE/, "\xFF\xFE\xFD");
c1a7495a 313 my $b = split(/\xFE/, "\xFF\xFE\xFD");
4765795a 314
3a2263fe 315 ok(@a == 2 && $a[0] eq "\xFF" && $a[1] eq "\xFD");
c1a7495a 316 is($b, scalar(@a));
4765795a
JH
317}
318
16bdb4ac
RG
319{
320 # check that PMf_WHITE is cleared after \s+ is used
321 # reported in <20010627113312.RWGY6087.viemta06@localhost>
322 my $r;
323 foreach my $pat ( qr/\s+/, qr/ll/ ) {
324 $r = join ':' => split($pat, "hello cruel world");
325 }
3a2263fe 326 is($r, "he:o cruel world");
16bdb4ac 327}
6de67870
JP
328
329
330{
331 # split /(A)|B/, "1B2" should return (1, undef, 2)
332 my @x = split /(A)|B/, "1B2";
c1a7495a
BB
333 my $y = split /(A)|B/, "1B2";
334 is($y, scalar(@x));
3a2263fe 335 ok($x[0] eq '1' and (not defined $x[1]) and $x[2] eq '2');
6de67870 336}
1d86a7f9
HS
337
338{
339 # [perl #17064]
340 my $warn;
341 local $SIG{__WARN__} = sub { $warn = join '', @_; chomp $warn };
342 my $char = "\x{10f1ff}";
343 my @a = split /\r?\n/, "$char\n";
c1a7495a
BB
344 my $b = split /\r?\n/, "$char\n";
345 is($b, scalar(@a));
3a2263fe
RGS
346 ok(@a == 1 && $a[0] eq $char && !defined($warn));
347}
348
349{
350 # [perl #18195]
e1c3fb40
RGS
351 for my $u (0, 1) {
352 for my $a (0, 1) {
353 $_ = 'readin,database,readout';
354 utf8::upgrade $_ if $u;
355 /(.+)/;
356 my @d = split /[,]/,$1;
c1a7495a
BB
357 my $e = split /[,]/,$1;
358 is($e, scalar(@d));
e1c3fb40 359 is(join (':',@d), 'readin:database:readout', "[perl #18195]");
3a2263fe 360 }
1d86a7f9
HS
361 }
362}
3b0d546b
AE
363
364{
365 $p="a,b";
366 utf8::upgrade $p;
7f18b612 367 eval { @a=split(/[, ]+/,$p) };
c1a7495a
BB
368 eval { $b=split(/[, ]+/,$p) };
369 is($b, scalar(@a));
3b0d546b
AE
370 is ("$@-@a-", '-a b-', '#20912 - split() to array with /[]+/ and utf8');
371}
7f18b612
YST
372
373{
374 is (\@a, \@{"a"}, '@a must be global for following test');
375 $p="";
376 $n = @a = split /,/,$p;
377 is ($n, 0, '#21765 - pmreplroot hack used to return undef for 0 iters');
378}
e3a8873f
DM
379
380{
381 # [perl #28938]
382 # assigning off the end of the array after a split could leave garbage
383 # in the inner elements
384
385 my $x;
386 @a = split /,/, ',,,,,';
387 $a[3]=1;
388 $x = \$a[2];
389 is (ref $x, 'SCALAR', '#28938 - garbage after extend');
390}
ede8ac17
TS
391
392{
393 my $src = "ABC \0 FOO \0 XYZ";
394 my @s = split(" \0 ", $src);
395 my @r = split(/ \0 /, $src);
c1a7495a
BB
396 my $cs = split(" \0 ", $src);
397 my $cr = split(/ \0 /, $src);
ede8ac17 398 is(scalar(@s), 3);
c1a7495a
BB
399 is($cs, 3);
400 is($cr, 3);
ede8ac17
TS
401 is($s[0], "ABC");
402 is($s[1], "FOO");
403 is($s[2]," XYZ");
404 is(join(':',@s), join(':',@r));
405}
b8de32d5
AV
406
407{
408 use constant BANG => {};
409 () = split m/,/, "", BANG;
410 ok(1);
411}
941446f6
FC
412
413{
2f7a9718 414 # Bug #69875
941446f6
FC
415 # 'Hybrid' scalar-and-array context
416 scalar(our @PATH = split /::/, "Font::GlyphNames");
417 # 'my' doesn't trigger the bug
418 is "@PATH", "Font GlyphNames", "hybrid scalar-and-array context";
419}
5255171e 420
cd346b28
JK
421{
422 my @results;
dbc200c5
YO
423 my $expr= "foo bar";
424 my $cond;
425
426 @results= split(0||" ", $expr);
427 is @results, 2, 'split(0||" ") is treated like split(" ")'; #'
428
429 $cond= 0;
430 @results= split $cond ? " " : qr/ /, $expr;
431 is @results, 3, 'split($cond ? " " : qr/ /, $expr) works as expected (like qr/ /)';
432 $cond= 1;
433 @results= split $cond ? " " : qr/ /, $expr;
434 is @results, 2, 'split($cond ? " " : qr/ /, $expr) works as expected (like " ")';
cd346b28 435
dbc200c5 436 $expr = ' a b c ';
cd346b28
JK
437 @results = split /\s/, $expr;
438 is @results, 4,
439 "split on regex of single space metacharacter: captured 4 elements";
440 is $results[0], '',
441 "split on regex of single space metacharacter: first element is empty string";
442
443 @results = split / /, $expr;
444 is @results, 4,
445 "split on regex of single whitespace: captured 4 elements";
446 is $results[0], '',
447 "split on regex of single whitespace: first element is empty string";
448
449 @results = split " ", $expr;
450 is @results, 3,
451 "split on string of single whitespace: captured 3 elements";
452 is $results[0], 'a',
453 "split on string of single whitespace: first element is non-empty";
454
455 $expr = " a \tb c ";
456 @results = split " ", $expr;
457 is @results, 3,
458 "split on string of single whitespace: captured 3 elements";
459 is $results[0], 'a',
460 "split on string of single whitespace: first element is non-empty; multiple contiguous space characters";
dbc200c5
YO
461
462 my @seq;
463 for my $cond (0,1,0,1,0) {
464 $expr = " foo ";
465 @results = split $cond ? qr/ / : " ", $expr;
466 push @seq, scalar(@results) . ":" . $results[-1];
467 }
468 is join(" ", @seq), "1:foo 3:foo 1:foo 3:foo 1:foo",
469 qq{split(\$cond ? qr/ / : " ", "$exp") behaves as expected over repeated similar patterns};
cd346b28
JK
470}
471
dbc200c5
YO
472{
473 # 'RT #116086: split "\x20" does not work as documented';
cd346b28
JK
474 my @results;
475 my $expr;
476 $expr = ' a b c ';
477 @results = split "\x20", $expr;
478 is @results, 3,
479 "RT #116086: split on string of single hex-20: captured 3 elements";
480 is $results[0], 'a',
481 "RT #116086: split on string of single hex-20: first element is non-empty";
482
483 $expr = " a \tb c ";
484 @results = split "\x20", $expr;
485 is @results, 3,
486 "RT #116086: split on string of single hex-20: captured 3 elements";
487 is $results[0], 'a',
488 "RT #116086: split on string of single hex-20: first element is non-empty; multiple contiguous space characters";
489}
490
60041a09
FC
491# Nasty interaction between split and use constant
492use constant nought => 0;
493($a,$b,$c) = split //, $foo, nought;
494is nought, 0, 'split does not mangle 0 constants';