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