xref: /openbsd/gnu/usr.bin/perl/t/op/split.t (revision 7b36286a)
1#!./perl
2
3BEGIN {
4    chdir 't' if -d 't';
5    @INC = '../lib';
6    require './test.pl';
7}
8
9plan tests => 55;
10
11$FS = ':';
12
13$_ = 'a:b:c';
14
15($a,$b,$c) = split($FS,$_);
16
17is(join(';',$a,$b,$c), 'a;b;c');
18
19@ary = split(/:b:/);
20is(join("$_",@ary), 'aa:b:cc');
21
22$_ = "abc\n";
23my @xyz = (@ary = split(//));
24is(join(".",@ary), "a.b.c.\n");
25
26$_ = "a:b:c::::";
27@ary = split(/:/);
28is(join(".",@ary), "a.b.c");
29
30$_ = join(':',split(' ',"    a b\tc \t d "));
31is($_, 'a:b:c:d');
32
33$_ = join(':',split(/ */,"foo  bar bie\tdoll"));
34is($_ , "f:o:o:b:a:r:b:i:e:\t:d:o:l:l");
35
36$_ = join(':', 'foo', split(/ /,'a b  c'), 'bar');
37is($_, "foo:a:b::c:bar");
38
39# Can we say how many fields to split to?
40$_ = join(':', split(' ','1 2 3 4 5 6', 3));
41is($_, '1:2:3 4 5 6');
42
43# Can we do it as a variable?
44$x = 4;
45$_ = join(':', split(' ','1 2 3 4 5 6', $x));
46is($_, '1:2:3:4 5 6');
47
48# Does the 999 suppress null field chopping?
49$_ = join(':', split(/:/,'1:2:3:4:5:6:::', 999));
50is($_ , '1:2:3:4:5:6:::');
51
52# Does assignment to a list imply split to one more field than that?
53$foo = runperl( switches => ['-Dt'], stderr => 1, prog => '($a,$b)=split;' );
54ok($foo =~ /DEBUGGING/ || $foo =~ /const\n?\Q(IV(3))\E/);
55
56# Can we say how many fields to split to when assigning to a list?
57($a,$b) = split(' ','1 2 3 4 5 6', 2);
58$_ = join(':',$a,$b);
59is($_, '1:2 3 4 5 6');
60
61# do subpatterns generate additional fields (without trailing nulls)?
62$_ = join '|', split(/,|(-)/, "1-10,20,,,");
63is($_, "1|-|10||20");
64
65# do subpatterns generate additional fields (with a limit)?
66$_ = join '|', split(/,|(-)/, "1-10,20,,,", 10);
67is($_, "1|-|10||20||||||");
68
69# is the 'two undefs' bug fixed?
70(undef, $a, undef, $b) = qw(1 2 3 4);
71is("$a|$b", "2|4");
72
73# .. even for locals?
74{
75  local(undef, $a, undef, $b) = qw(1 2 3 4);
76  is("$a|$b", "2|4");
77}
78
79# check splitting of null string
80$_ = join('|', split(/x/,   '',-1), 'Z');
81is($_, "Z");
82
83$_ = join('|', split(/x/,   '', 1), 'Z');
84is($_, "Z");
85
86$_ = join('|', split(/(p+)/,'',-1), 'Z');
87is($_, "Z");
88
89$_ = join('|', split(/.?/,  '',-1), 'Z');
90is($_, "Z");
91
92
93# Are /^/m patterns scanned?
94$_ = join '|', split(/^a/m, "a b a\na d a", 20);
95is($_, "| b a\n| d a");
96
97# Are /$/m patterns scanned?
98$_ = join '|', split(/a$/m, "a b a\na d a", 20);
99is($_, "a b |\na d |");
100
101# Are /^/m patterns scanned?
102$_ = join '|', split(/^aa/m, "aa b aa\naa d aa", 20);
103is($_, "| b aa\n| d aa");
104
105# Are /$/m patterns scanned?
106$_ = join '|', split(/aa$/m, "aa b aa\naa d aa", 20);
107is($_, "aa b |\naa d |");
108
109# Greedyness:
110$_ = "a : b :c: d";
111@ary = split(/\s*:\s*/);
112is(($res = join(".",@ary)), "a.b.c.d", $res);
113
114# use of match result as pattern (!)
115is('p:q:r:s', join ':', split('abc' =~ /b/, 'p1q1r1s'));
116
117# /^/ treated as /^/m
118$_ = join ':', split /^/, "ab\ncd\nef\n";
119is($_, "ab\n:cd\n:ef\n");
120
121# see if @a = @b = split(...) optimization works
122@list1 = @list2 = split ('p',"a p b c p");
123ok(@list1 == @list2 &&
124   "@list1" eq "@list2" &&
125   @list1 == 2 &&
126   "@list1" eq "a   b c ");
127
128# zero-width assertion
129$_ = join ':', split /(?=\w)/, "rm b";
130is($_, "r:m :b");
131
132# unicode splittage
133
134@ary = map {ord} split //, v1.20.300.4000.50000.4000.300.20.1;
135is("@ary", "1 20 300 4000 50000 4000 300 20 1");
136
137@ary = split(/\x{FE}/, "\x{FF}\x{FE}\x{FD}"); # bug id 20010105.016
138ok(@ary == 2 &&
139   $ary[0] eq "\xFF"   && $ary[1] eq "\xFD" &&
140   $ary[0] eq "\x{FF}" && $ary[1] eq "\x{FD}");
141
142@ary = split(/(\x{FE}\xFE)/, "\xFF\x{FF}\xFE\x{FE}\xFD\x{FD}"); # variant of 31
143ok(@ary == 3 &&
144   $ary[0] eq "\xFF\xFF"     &&
145   $ary[0] eq "\x{FF}\xFF"   &&
146   $ary[0] eq "\x{FF}\x{FF}" &&
147   $ary[1] eq "\xFE\xFE"     &&
148   $ary[1] eq "\x{FE}\xFE"   &&
149   $ary[1] eq "\x{FE}\x{FE}" &&
150   $ary[2] eq "\xFD\xFD"     &&
151   $ary[2] eq "\x{FD}\xFD"   &&
152   $ary[2] eq "\x{FD}\x{FD}");
153
154{
155    my @a = map ord, split(//, join("", map chr, (1234, 123, 2345)));
156    is("@a", "1234 123 2345");
157}
158
159{
160    my $x = 'A';
161    my @a = map ord, split(/$x/, join("", map chr, (1234, ord($x), 2345)));
162    is("@a", "1234 2345");
163}
164
165{
166    # bug id 20000427.003
167
168    use warnings;
169    use strict;
170
171    my $sushi = "\x{b36c}\x{5a8c}\x{ff5b}\x{5079}\x{505b}";
172
173    my @charlist = split //, $sushi;
174    my $r = '';
175    foreach my $ch (@charlist) {
176	$r = $r . " " . sprintf "U+%04X", ord($ch);
177    }
178
179    is($r, " U+B36C U+5A8C U+FF5B U+5079 U+505B");
180}
181
182{
183    my $s = "\x20\x40\x{80}\x{100}\x{80}\x40\x20";
184
185  SKIP: {
186    if (ord('A') == 193) {
187	skip("EBCDIC", 1);
188    } else {
189	# bug id 20000426.003
190
191	my ($a, $b, $c) = split(/\x40/, $s);
192	ok($a eq "\x20" && $b eq "\x{80}\x{100}\x{80}" && $c eq $a);
193    }
194  }
195
196    my ($a, $b) = split(/\x{100}/, $s);
197    ok($a eq "\x20\x40\x{80}" && $b eq "\x{80}\x40\x20");
198
199    my ($a, $b) = split(/\x{80}\x{100}\x{80}/, $s);
200    ok($a eq "\x20\x40" && $b eq "\x40\x20");
201
202  SKIP: {
203    if (ord('A') == 193) {
204	skip("EBCDIC", 1);
205    }  else {
206	my ($a, $b) = split(/\x40\x{80}/, $s);
207	ok($a eq "\x20" && $b eq "\x{100}\x{80}\x40\x20");
208    }
209  }
210
211    my ($a, $b, $c) = split(/[\x40\x{80}]+/, $s);
212    ok($a eq "\x20" && $b eq "\x{100}" && $c eq "\x20");
213}
214
215{
216    # 20001205.014
217
218    my $a = "ABC\x{263A}";
219
220    my @b = split( //, $a );
221
222    is(scalar @b, 4);
223
224    ok(length($b[3]) == 1 && $b[3] eq "\x{263A}");
225
226    $a =~ s/^A/Z/;
227    ok(length($a) == 4 && $a eq "ZBC\x{263A}");
228}
229
230{
231    my @a = split(/\xFE/, "\xFF\xFE\xFD");
232
233    ok(@a == 2 && $a[0] eq "\xFF" && $a[1] eq "\xFD");
234}
235
236{
237    # check that PMf_WHITE is cleared after \s+ is used
238    # reported in <20010627113312.RWGY6087.viemta06@localhost>
239    my $r;
240    foreach my $pat ( qr/\s+/, qr/ll/ ) {
241	$r = join ':' => split($pat, "hello cruel world");
242    }
243    is($r, "he:o cruel world");
244}
245
246
247{
248    # split /(A)|B/, "1B2" should return (1, undef, 2)
249    my @x = split /(A)|B/, "1B2";
250    ok($x[0] eq '1' and (not defined $x[1]) and $x[2] eq '2');
251}
252
253{
254    # [perl #17064]
255    my $warn;
256    local $SIG{__WARN__} = sub { $warn = join '', @_; chomp $warn };
257    my $char = "\x{10f1ff}";
258    my @a = split /\r?\n/, "$char\n";
259    ok(@a == 1 && $a[0] eq $char && !defined($warn));
260}
261
262{
263    # [perl #18195]
264    for my $u (0, 1) {
265	for my $a (0, 1) {
266	    $_ = 'readin,database,readout';
267	    utf8::upgrade $_ if $u;
268	    /(.+)/;
269	    my @d = split /[,]/,$1;
270	    is(join (':',@d), 'readin:database:readout', "[perl #18195]");
271	}
272    }
273}
274
275{
276    $p="a,b";
277    utf8::upgrade $p;
278    eval { @a=split(/[, ]+/,$p) };
279    is ("$@-@a-", '-a b-', '#20912 - split() to array with /[]+/ and utf8');
280}
281
282{
283    is (\@a, \@{"a"}, '@a must be global for following test');
284    $p="";
285    $n = @a = split /,/,$p;
286    is ($n, 0, '#21765 - pmreplroot hack used to return undef for 0 iters');
287}
288
289{
290    # [perl #28938]
291    # assigning off the end of the array after a split could leave garbage
292    # in the inner elements
293
294    my $x;
295    @a = split /,/, ',,,,,';
296    $a[3]=1;
297    $x = \$a[2];
298    is (ref $x, 'SCALAR', '#28938 - garbage after extend');
299}
300
301