source: trunk/essentials/dev-lang/perl/t/op/split.t

Last change on this file was 3181, checked in by bird, 19 years ago

perl 5.8.8

File size: 6.7 KB
Line 
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