This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Skip one test that was failing with DEBUGGING and threads,
[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
7541f296
RGS
9use Config;
10
ede8ac17 11plan tests => 135;
8d063cd8
LW
12
13$FS = ':';
14
15$_ = 'a:b:c';
16
17($a,$b,$c) = split($FS,$_);
18
3a2263fe 19is(join(';',$a,$b,$c), 'a;b;c');
8d063cd8
LW
20
21@ary = split(/:b:/);
3a2263fe 22is(join("$_",@ary), 'aa:b:cc');
8d063cd8
LW
23
24$_ = "abc\n";
4765795a 25my @xyz = (@ary = split(//));
3a2263fe 26is(join(".",@ary), "a.b.c.\n");
8d063cd8
LW
27
28$_ = "a:b:c::::";
29@ary = split(/:/);
3a2263fe 30is(join(".",@ary), "a.b.c");
2e1b3b7e 31
378cc40b 32$_ = join(':',split(' '," a b\tc \t d "));
3a2263fe 33is($_, 'a:b:c:d');
2e1b3b7e
KK
34
35$_ = join(':',split(/ */,"foo bar bie\tdoll"));
3a2263fe 36is($_ , "f:o:o:b:a:r:b:i:e:\t:d:o:l:l");
378cc40b
LW
37
38$_ = join(':', 'foo', split(/ /,'a b c'), 'bar');
3a2263fe 39is($_, "foo:a:b::c:bar");
378cc40b 40
a687059c
LW
41# Can we say how many fields to split to?
42$_ = join(':', split(' ','1 2 3 4 5 6', 3));
3a2263fe 43is($_, '1:2:3 4 5 6');
a687059c
LW
44
45# Can we do it as a variable?
46$x = 4;
47$_ = join(':', split(' ','1 2 3 4 5 6', $x));
3a2263fe 48is($_, '1:2:3:4 5 6');
a687059c
LW
49
50# Does the 999 suppress null field chopping?
51$_ = join(':', split(/:/,'1:2:3:4:5:6:::', 999));
3a2263fe 52is($_ , '1:2:3:4:5:6:::');
a687059c
LW
53
54# Does assignment to a list imply split to one more field than that?
7541f296
RGS
55SKIP: {
56 if ($Config{useithreads}) {
57 skip("No IV value dump with threads", 1);
58 }
59 else {
60 $foo = runperl( switches => ['-Dt'], stderr => 1, prog => '($a,$b)=split;' );
61 ok($foo =~ /DEBUGGING/ || $foo =~ /const\n?\Q(IV(3))\E/);
62 }
63}
a687059c
LW
64
65# Can we say how many fields to split to when assigning to a list?
66($a,$b) = split(' ','1 2 3 4 5 6', 2);
67$_ = join(':',$a,$b);
3a2263fe 68is($_, '1:2 3 4 5 6');
a687059c 69
084811a7 70# do subpatterns generate additional fields (without trailing nulls)?
71$_ = join '|', split(/,|(-)/, "1-10,20,,,");
3a2263fe 72is($_, "1|-|10||20");
084811a7 73
74# do subpatterns generate additional fields (with a limit)?
75$_ = join '|', split(/,|(-)/, "1-10,20,,,", 10);
3a2263fe 76is($_, "1|-|10||20||||||");
e1fa4fd3
HS
77
78# is the 'two undefs' bug fixed?
79(undef, $a, undef, $b) = qw(1 2 3 4);
3a2263fe 80is("$a|$b", "2|4");
e1fa4fd3
HS
81
82# .. even for locals?
83{
84 local(undef, $a, undef, $b) = qw(1 2 3 4);
3a2263fe 85 is("$a|$b", "2|4");
e1fa4fd3 86}
fb73857a 87
88# check splitting of null string
89$_ = join('|', split(/x/, '',-1), 'Z');
3a2263fe 90is($_, "Z");
fb73857a 91
92$_ = join('|', split(/x/, '', 1), 'Z');
3a2263fe 93is($_, "Z");
fb73857a 94
95$_ = join('|', split(/(p+)/,'',-1), 'Z');
3a2263fe 96is($_, "Z");
fb73857a 97
98$_ = join('|', split(/.?/, '',-1), 'Z');
3a2263fe 99is($_, "Z");
fb73857a 100
c277df42
IZ
101
102# Are /^/m patterns scanned?
103$_ = join '|', split(/^a/m, "a b a\na d a", 20);
3a2263fe 104is($_, "| b a\n| d a");
c277df42
IZ
105
106# Are /$/m patterns scanned?
107$_ = join '|', split(/a$/m, "a b a\na d a", 20);
3a2263fe 108is($_, "a b |\na d |");
c277df42
IZ
109
110# Are /^/m patterns scanned?
111$_ = join '|', split(/^aa/m, "aa b aa\naa d aa", 20);
3a2263fe 112is($_, "| b aa\n| d aa");
c277df42
IZ
113
114# Are /$/m patterns scanned?
115$_ = join '|', split(/aa$/m, "aa b aa\naa d aa", 20);
3a2263fe 116is($_, "aa b |\naa d |");
c277df42
IZ
117
118# Greedyness:
119$_ = "a : b :c: d";
120@ary = split(/\s*:\s*/);
3a2263fe 121is(($res = join(".",@ary)), "a.b.c.d", $res);
815d35b9
MG
122
123# use of match result as pattern (!)
3a2263fe 124is('p:q:r:s', join ':', split('abc' =~ /b/, 'p1q1r1s'));
1ec94568
MG
125
126# /^/ treated as /^/m
127$_ = join ':', split /^/, "ab\ncd\nef\n";
3a2263fe 128is($_, "ab\n:cd\n:ef\n");
b3f5893f
GS
129
130# see if @a = @b = split(...) optimization works
131@list1 = @list2 = split ('p',"a p b c p");
3a2263fe
RGS
132ok(@list1 == @list2 &&
133 "@list1" eq "@list2" &&
134 @list1 == 2 &&
135 "@list1" eq "a b c ");
0156e0fd
RB
136
137# zero-width assertion
138$_ = join ':', split /(?=\w)/, "rm b";
3a2263fe 139is($_, "r:m :b");
5a2d9fa2
JH
140
141# unicode splittage
974f237a 142
5a2d9fa2 143@ary = map {ord} split //, v1.20.300.4000.50000.4000.300.20.1;
3a2263fe 144is("@ary", "1 20 300 4000 50000 4000 300 20 1");
974f237a
JH
145
146@ary = split(/\x{FE}/, "\x{FF}\x{FE}\x{FD}"); # bug id 20010105.016
3a2263fe
RGS
147ok(@ary == 2 &&
148 $ary[0] eq "\xFF" && $ary[1] eq "\xFD" &&
149 $ary[0] eq "\x{FF}" && $ary[1] eq "\x{FD}");
974f237a
JH
150
151@ary = split(/(\x{FE}\xFE)/, "\xFF\x{FF}\xFE\x{FE}\xFD\x{FD}"); # variant of 31
3a2263fe
RGS
152ok(@ary == 3 &&
153 $ary[0] eq "\xFF\xFF" &&
154 $ary[0] eq "\x{FF}\xFF" &&
155 $ary[0] eq "\x{FF}\x{FF}" &&
156 $ary[1] eq "\xFE\xFE" &&
157 $ary[1] eq "\x{FE}\xFE" &&
158 $ary[1] eq "\x{FE}\x{FE}" &&
159 $ary[2] eq "\xFD\xFD" &&
160 $ary[2] eq "\x{FD}\xFD" &&
161 $ary[2] eq "\x{FD}\x{FD}");
4765795a
JH
162
163{
164 my @a = map ord, split(//, join("", map chr, (1234, 123, 2345)));
3a2263fe 165 is("@a", "1234 123 2345");
4765795a
JH
166}
167
168{
31e261c7
JH
169 my $x = 'A';
170 my @a = map ord, split(/$x/, join("", map chr, (1234, ord($x), 2345)));
3a2263fe 171 is("@a", "1234 2345");
4765795a
JH
172}
173
174{
175 # bug id 20000427.003
176
177 use warnings;
178 use strict;
179
180 my $sushi = "\x{b36c}\x{5a8c}\x{ff5b}\x{5079}\x{505b}";
181
182 my @charlist = split //, $sushi;
183 my $r = '';
184 foreach my $ch (@charlist) {
185 $r = $r . " " . sprintf "U+%04X", ord($ch);
186 }
187
3a2263fe 188 is($r, " U+B36C U+5A8C U+FF5B U+5079 U+505B");
4765795a
JH
189}
190
191{
dd83d948
DD
192 my $s = "\x20\x40\x{80}\x{100}\x{80}\x40\x20";
193
3a2263fe 194 SKIP: {
31e261c7 195 if (ord('A') == 193) {
3a2263fe 196 skip("EBCDIC", 1);
31e261c7
JH
197 } else {
198 # bug id 20000426.003
4765795a 199
31e261c7 200 my ($a, $b, $c) = split(/\x40/, $s);
3a2263fe 201 ok($a eq "\x20" && $b eq "\x{80}\x{100}\x{80}" && $c eq $a);
31e261c7 202 }
3a2263fe 203 }
4765795a
JH
204
205 my ($a, $b) = split(/\x{100}/, $s);
3a2263fe 206 ok($a eq "\x20\x40\x{80}" && $b eq "\x{80}\x40\x20");
4765795a
JH
207
208 my ($a, $b) = split(/\x{80}\x{100}\x{80}/, $s);
3a2263fe 209 ok($a eq "\x20\x40" && $b eq "\x40\x20");
4765795a 210
3a2263fe 211 SKIP: {
31e261c7 212 if (ord('A') == 193) {
3a2263fe 213 skip("EBCDIC", 1);
31e261c7
JH
214 } else {
215 my ($a, $b) = split(/\x40\x{80}/, $s);
3a2263fe 216 ok($a eq "\x20" && $b eq "\x{100}\x{80}\x40\x20");
31e261c7 217 }
3a2263fe 218 }
4765795a
JH
219
220 my ($a, $b, $c) = split(/[\x40\x{80}]+/, $s);
3a2263fe 221 ok($a eq "\x20" && $b eq "\x{100}" && $c eq "\x20");
4765795a
JH
222}
223
224{
225 # 20001205.014
226
227 my $a = "ABC\x{263A}";
228
229 my @b = split( //, $a );
230
3a2263fe 231 is(scalar @b, 4);
4765795a 232
3a2263fe 233 ok(length($b[3]) == 1 && $b[3] eq "\x{263A}");
4765795a
JH
234
235 $a =~ s/^A/Z/;
3a2263fe 236 ok(length($a) == 4 && $a eq "ZBC\x{263A}");
4765795a
JH
237}
238
239{
240 my @a = split(/\xFE/, "\xFF\xFE\xFD");
241
3a2263fe 242 ok(@a == 2 && $a[0] eq "\xFF" && $a[1] eq "\xFD");
4765795a
JH
243}
244
16bdb4ac
RG
245{
246 # check that PMf_WHITE is cleared after \s+ is used
247 # reported in <20010627113312.RWGY6087.viemta06@localhost>
248 my $r;
249 foreach my $pat ( qr/\s+/, qr/ll/ ) {
250 $r = join ':' => split($pat, "hello cruel world");
251 }
3a2263fe 252 is($r, "he:o cruel world");
16bdb4ac 253}
6de67870
JP
254
255
256{
257 # split /(A)|B/, "1B2" should return (1, undef, 2)
258 my @x = split /(A)|B/, "1B2";
3a2263fe 259 ok($x[0] eq '1' and (not defined $x[1]) and $x[2] eq '2');
6de67870 260}
1d86a7f9
HS
261
262{
263 # [perl #17064]
264 my $warn;
265 local $SIG{__WARN__} = sub { $warn = join '', @_; chomp $warn };
266 my $char = "\x{10f1ff}";
267 my @a = split /\r?\n/, "$char\n";
3a2263fe
RGS
268 ok(@a == 1 && $a[0] eq $char && !defined($warn));
269}
270
271{
272 # [perl #18195]
e1c3fb40
RGS
273 for my $u (0, 1) {
274 for my $a (0, 1) {
275 $_ = 'readin,database,readout';
276 utf8::upgrade $_ if $u;
277 /(.+)/;
278 my @d = split /[,]/,$1;
279 is(join (':',@d), 'readin:database:readout', "[perl #18195]");
3a2263fe 280 }
1d86a7f9
HS
281 }
282}
3b0d546b
AE
283
284{
285 $p="a,b";
286 utf8::upgrade $p;
7f18b612 287 eval { @a=split(/[, ]+/,$p) };
3b0d546b
AE
288 is ("$@-@a-", '-a b-', '#20912 - split() to array with /[]+/ and utf8');
289}
7f18b612
YST
290
291{
292 is (\@a, \@{"a"}, '@a must be global for following test');
293 $p="";
294 $n = @a = split /,/,$p;
295 is ($n, 0, '#21765 - pmreplroot hack used to return undef for 0 iters');
296}
e3a8873f
DM
297
298{
299 # [perl #28938]
300 # assigning off the end of the array after a split could leave garbage
301 # in the inner elements
302
303 my $x;
304 @a = split /,/, ',,,,,';
305 $a[3]=1;
306 $x = \$a[2];
307 is (ref $x, 'SCALAR', '#28938 - garbage after extend');
308}
8727f688
YO
309{
310 # check the special casing of split /\s/ and unicode
311 use charnames qw(:full);
312 # below test data is extracted from
313 # PropList-5.0.0.txt
314 # Date: 2006-06-07, 23:22:52 GMT [MD]
315 #
316 # Unicode Character Database
317 # Copyright (c) 1991-2006 Unicode, Inc.
318 # For terms of use, see http://www.unicode.org/terms_of_use.html
319 # For documentation, see UCD.html
320 my @spaces=(
613f191e
TS
321 ord("\t"), # Cc <control-0009>
322 ord("\n"), # Cc <control-000A>
323 # not PerlSpace # Cc <control-000B>
324 ord("\f"), # Cc <control-000C>
325 ord("\r"), # Cc <control-000D>
326 ord(" "), # Zs SPACE
327 ord("\N{NEL}"), # Cc <control-0085>
328 ord("\N{NO-BREAK SPACE}"),
329 # Zs NO-BREAK SPACE
8727f688
YO
330 0x1680, # Zs OGHAM SPACE MARK
331 0x180E, # Zs MONGOLIAN VOWEL SEPARATOR
332 0x2000..0x200A, # Zs [11] EN QUAD..HAIR SPACE
333 0x2028, # Zl LINE SEPARATOR
334 0x2029, # Zp PARAGRAPH SEPARATOR
335 0x202F, # Zs NARROW NO-BREAK SPACE
336 0x205F, # Zs MEDIUM MATHEMATICAL SPACE
337 0x3000 # Zs IDEOGRAPHIC SPACE
338 );
339 #diag "Have @{[0+@spaces]} to test\n";
340 foreach my $cp (@spaces) {
613f191e 341 my $msg = sprintf "Space: U+%04x", $cp;
8727f688 342 my $space = chr($cp);
613f191e 343 my $str="A:$space:B\x{FFFD}";
8727f688 344 chop $str;
613f191e 345
8727f688 346 my @res=split(/\s+/,$str);
613f191e
TS
347 ok(@res == 2 && join('-',@res) eq "A:-:B", "$msg - /\\s+/");
348
349 my $s2 = "$space$space:A:$space$space:B\x{FFFD}";
350 chop $s2;
351
352 my @r2 = split(' ',$s2);
353 ok(@r2 == 2 && join('-', @r2) eq ":A:-:B", "$msg - ' '");
354
355 my @r3 = split(/\s+/, $s2);
356 ok(@r3 == 3 && join('-', @r3) eq "-:A:-:B", "$msg - /\\s+/ No.2");
8727f688
YO
357 }
358}
ede8ac17
TS
359
360{
361 my $src = "ABC \0 FOO \0 XYZ";
362 my @s = split(" \0 ", $src);
363 my @r = split(/ \0 /, $src);
364 is(scalar(@s), 3);
365 is($s[0], "ABC");
366 is($s[1], "FOO");
367 is($s[2]," XYZ");
368 is(join(':',@s), join(':',@r));
369}