This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
More tweakage on the Unicode character class descriptions.
[perl5.git] / t / op / split.t
CommitLineData
8d063cd8
LW
1#!./perl
2
974f237a 3print "1..32\n";
8d063cd8
LW
4
5$FS = ':';
6
7$_ = 'a:b:c';
8
9($a,$b,$c) = split($FS,$_);
10
11if (join(';',$a,$b,$c) eq 'a;b;c') {print "ok 1\n";} else {print "not ok 1\n";}
12
13@ary = split(/:b:/);
14if (join("$_",@ary) eq 'aa:b:cc') {print "ok 2\n";} else {print "not ok 2\n";}
15
16$_ = "abc\n";
a687059c 17@xyz = (@ary = split(//));
8d063cd8
LW
18if (join(".",@ary) eq "a.b.c.\n") {print "ok 3\n";} else {print "not ok 3\n";}
19
20$_ = "a:b:c::::";
21@ary = split(/:/);
22if (join(".",@ary) eq "a.b.c") {print "ok 4\n";} else {print "not ok 4\n";}
2e1b3b7e 23
378cc40b
LW
24$_ = join(':',split(' '," a b\tc \t d "));
25if ($_ eq 'a:b:c:d') {print "ok 5\n";} else {print "not ok 5 #$_#\n";}
2e1b3b7e
KK
26
27$_ = join(':',split(/ */,"foo bar bie\tdoll"));
28if ($_ eq "f:o:o:b:a:r:b:i:e:\t:d:o:l:l")
29 {print "ok 6\n";} else {print "not ok 6\n";}
378cc40b
LW
30
31$_ = join(':', 'foo', split(/ /,'a b c'), 'bar');
32if ($_ eq "foo:a:b::c:bar") {print "ok 7\n";} else {print "not ok 7 $_\n";}
33
a687059c
LW
34# Can we say how many fields to split to?
35$_ = join(':', split(' ','1 2 3 4 5 6', 3));
36print $_ eq '1:2:3 4 5 6' ? "ok 8\n" : "not ok 8 $_\n";
37
38# Can we do it as a variable?
39$x = 4;
40$_ = join(':', split(' ','1 2 3 4 5 6', $x));
41print $_ eq '1:2:3:4 5 6' ? "ok 9\n" : "not ok 9 $_\n";
42
43# Does the 999 suppress null field chopping?
44$_ = join(':', split(/:/,'1:2:3:4:5:6:::', 999));
45print $_ eq '1:2:3:4:5:6:::' ? "ok 10\n" : "not ok 10 $_\n";
46
47# Does assignment to a list imply split to one more field than that?
68dc0745 48if ($^O eq 'MSWin32') { $foo = `.\\perl -D1024 -e "(\$a,\$b) = split;" 2>&1` }
f0963acb 49elsif ($^O eq 'VMS') { $foo = `./perl "-D1024" -e "(\$a,\$b) = split;" 2>&1` }
68dc0745 50else { $foo = `./perl -D1024 -e '(\$a,\$b) = split;' 2>&1` }
bfe546ed 51print $foo =~ /DEBUGGING/ || $foo =~ /SV = (VOID|IV\(3\))/ ? "ok 11\n" : "not ok 11\n";
a687059c
LW
52
53# Can we say how many fields to split to when assigning to a list?
54($a,$b) = split(' ','1 2 3 4 5 6', 2);
55$_ = join(':',$a,$b);
56print $_ eq '1:2 3 4 5 6' ? "ok 12\n" : "not ok 12 $_\n";
57
084811a7
PP
58# do subpatterns generate additional fields (without trailing nulls)?
59$_ = join '|', split(/,|(-)/, "1-10,20,,,");
60print $_ eq "1|-|10||20" ? "ok 13\n" : "not ok 13\n";
61
62# do subpatterns generate additional fields (with a limit)?
63$_ = join '|', split(/,|(-)/, "1-10,20,,,", 10);
64print $_ eq "1|-|10||20||||||" ? "ok 14\n" : "not ok 14\n";
e1fa4fd3
HS
65
66# is the 'two undefs' bug fixed?
67(undef, $a, undef, $b) = qw(1 2 3 4);
68print "$a|$b" eq "2|4" ? "ok 15\n" : "not ok 15\n";
69
70# .. even for locals?
71{
72 local(undef, $a, undef, $b) = qw(1 2 3 4);
73 print "$a|$b" eq "2|4" ? "ok 16\n" : "not ok 16\n";
74}
fb73857a
PP
75
76# check splitting of null string
77$_ = join('|', split(/x/, '',-1), 'Z');
78print $_ eq "Z" ? "ok 17\n" : "#$_\nnot ok 17\n";
79
80$_ = join('|', split(/x/, '', 1), 'Z');
81print $_ eq "Z" ? "ok 18\n" : "#$_\nnot ok 18\n";
82
83$_ = join('|', split(/(p+)/,'',-1), 'Z');
84print $_ eq "Z" ? "ok 19\n" : "#$_\nnot ok 19\n";
85
86$_ = join('|', split(/.?/, '',-1), 'Z');
87print $_ eq "Z" ? "ok 20\n" : "#$_\nnot ok 20\n";
88
c277df42
IZ
89
90# Are /^/m patterns scanned?
91$_ = join '|', split(/^a/m, "a b a\na d a", 20);
92print $_ eq "| b a\n| d a" ? "ok 21\n" : "not ok 21\n# `$_'\n";
93
94# Are /$/m patterns scanned?
95$_ = join '|', split(/a$/m, "a b a\na d a", 20);
96print $_ eq "a b |\na d |" ? "ok 22\n" : "not ok 22\n# `$_'\n";
97
98# Are /^/m patterns scanned?
99$_ = join '|', split(/^aa/m, "aa b aa\naa d aa", 20);
100print $_ eq "| b aa\n| d aa" ? "ok 23\n" : "not ok 23\n# `$_'\n";
101
102# Are /$/m patterns scanned?
103$_ = join '|', split(/aa$/m, "aa b aa\naa d aa", 20);
104print $_ eq "aa b |\naa d |" ? "ok 24\n" : "not ok 24\n# `$_'\n";
105
106# Greedyness:
107$_ = "a : b :c: d";
108@ary = split(/\s*:\s*/);
109if (($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
110
111# use of match result as pattern (!)
cc50a203 112'p:q:r:s' eq join ':', split('abc' =~ /b/, 'p1q1r1s') or print "not ";
815d35b9 113print "ok 26\n";
1ec94568
G
114
115# /^/ treated as /^/m
116$_ = join ':', split /^/, "ab\ncd\nef\n";
117print "not " if $_ ne "ab\n:cd\n:ef\n";
118print "ok 27\n";
b3f5893f
GS
119
120# see if @a = @b = split(...) optimization works
121@list1 = @list2 = split ('p',"a p b c p");
122print "not " if @list1 != @list2 or "@list1" ne "@list2"
123 or @list1 != 2 or "@list1" ne "a b c ";
124print "ok 28\n";
0156e0fd
RB
125
126# zero-width assertion
127$_ = join ':', split /(?=\w)/, "rm b";
128print "not" if $_ ne "r:m :b";
129print "ok 29\n";
5a2d9fa2
JH
130
131# unicode splittage
974f237a 132
5a2d9fa2
JH
133@ary = map {ord} split //, v1.20.300.4000.50000.4000.300.20.1;
134print "not " unless "@ary" eq "1 20 300 4000 50000 4000 300 20 1";
135print "ok 30\n";
974f237a
JH
136
137@ary = split(/\x{FE}/, "\x{FF}\x{FE}\x{FD}"); # bug id 20010105.016
138print "not " unless @ary == 2 &&
139 $ary[0] eq "\xFF" && $ary[1] eq "\xFD" &&
140 $ary[0] eq "\x{FF}" && $ary[1] eq "\x{FD}";
141print "ok 31\n";
142
143@ary = split(/(\x{FE}\xFE)/, "\xFF\x{FF}\xFE\x{FE}\xFD\x{FD}"); # variant of 31
144print "not " unless @ary == 3 &&
145 $ary[0] eq "\xFF\xFF" &&
146 $ary[0] eq "\x{FF}\xFF" &&
147 $ary[0] eq "\x{FF}\x{FF}" &&
148 $ary[1] eq "\xFE\xFE" &&
149 $ary[1] eq "\x{FE}\xFE" &&
150 $ary[1] eq "\x{FE}\x{FE}" &&
151 $ary[2] eq "\xFD\xFD" &&
152 $ary[2] eq "\x{FD}\xFD" &&
153 $ary[2] eq "\x{FD}\x{FD}";
154
155print "ok 32\n";