xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/t/op/chop.t (revision 0:68f95e015346)
1*0Sstevel@tonic-gate#!./perl
2*0Sstevel@tonic-gate
3*0Sstevel@tonic-gateBEGIN {
4*0Sstevel@tonic-gate    chdir 't' if -d 't';
5*0Sstevel@tonic-gate    @INC = '../lib';
6*0Sstevel@tonic-gate    require './test.pl';
7*0Sstevel@tonic-gate}
8*0Sstevel@tonic-gate
9*0Sstevel@tonic-gateplan tests => 133;
10*0Sstevel@tonic-gate
11*0Sstevel@tonic-gate$_ = 'abc';
12*0Sstevel@tonic-gate$c = do foo();
13*0Sstevel@tonic-gateis ($c . $_, 'cab', 'optimized');
14*0Sstevel@tonic-gate
15*0Sstevel@tonic-gate$_ = 'abc';
16*0Sstevel@tonic-gate$c = chop($_);
17*0Sstevel@tonic-gateis ($c . $_ , 'cab', 'unoptimized');
18*0Sstevel@tonic-gate
19*0Sstevel@tonic-gatesub foo {
20*0Sstevel@tonic-gate    chop;
21*0Sstevel@tonic-gate}
22*0Sstevel@tonic-gate
23*0Sstevel@tonic-gate@foo = ("hi \n","there\n","!\n");
24*0Sstevel@tonic-gate@bar = @foo;
25*0Sstevel@tonic-gatechop(@bar);
26*0Sstevel@tonic-gateis (join('',@bar), 'hi there!');
27*0Sstevel@tonic-gate
28*0Sstevel@tonic-gate$foo = "\n";
29*0Sstevel@tonic-gatechop($foo,@foo);
30*0Sstevel@tonic-gateis (join('',$foo,@foo), 'hi there!');
31*0Sstevel@tonic-gate
32*0Sstevel@tonic-gate$_ = "foo\n\n";
33*0Sstevel@tonic-gate$got = chomp();
34*0Sstevel@tonic-gateok ($got == 1) or print "# got $got\n";
35*0Sstevel@tonic-gateis ($_, "foo\n");
36*0Sstevel@tonic-gate
37*0Sstevel@tonic-gate$_ = "foo\n";
38*0Sstevel@tonic-gate$got = chomp();
39*0Sstevel@tonic-gateok ($got == 1) or print "# got $got\n";
40*0Sstevel@tonic-gateis ($_, "foo");
41*0Sstevel@tonic-gate
42*0Sstevel@tonic-gate$_ = "foo";
43*0Sstevel@tonic-gate$got = chomp();
44*0Sstevel@tonic-gateok ($got == 0) or print "# got $got\n";
45*0Sstevel@tonic-gateis ($_, "foo");
46*0Sstevel@tonic-gate
47*0Sstevel@tonic-gate$_ = "foo";
48*0Sstevel@tonic-gate$/ = "oo";
49*0Sstevel@tonic-gate$got = chomp();
50*0Sstevel@tonic-gateok ($got == 2) or print "# got $got\n";
51*0Sstevel@tonic-gateis ($_, "f");
52*0Sstevel@tonic-gate
53*0Sstevel@tonic-gate$_ = "bar";
54*0Sstevel@tonic-gate$/ = "oo";
55*0Sstevel@tonic-gate$got = chomp();
56*0Sstevel@tonic-gateok ($got == 0) or print "# got $got\n";
57*0Sstevel@tonic-gateis ($_, "bar");
58*0Sstevel@tonic-gate
59*0Sstevel@tonic-gate$_ = "f\n\n\n\n\n";
60*0Sstevel@tonic-gate$/ = "";
61*0Sstevel@tonic-gate$got = chomp();
62*0Sstevel@tonic-gateok ($got == 5) or print "# got $got\n";
63*0Sstevel@tonic-gateis ($_, "f");
64*0Sstevel@tonic-gate
65*0Sstevel@tonic-gate$_ = "f\n\n";
66*0Sstevel@tonic-gate$/ = "";
67*0Sstevel@tonic-gate$got = chomp();
68*0Sstevel@tonic-gateok ($got == 2) or print "# got $got\n";
69*0Sstevel@tonic-gateis ($_, "f");
70*0Sstevel@tonic-gate
71*0Sstevel@tonic-gate$_ = "f\n";
72*0Sstevel@tonic-gate$/ = "";
73*0Sstevel@tonic-gate$got = chomp();
74*0Sstevel@tonic-gateok ($got == 1) or print "# got $got\n";
75*0Sstevel@tonic-gateis ($_, "f");
76*0Sstevel@tonic-gate
77*0Sstevel@tonic-gate$_ = "f";
78*0Sstevel@tonic-gate$/ = "";
79*0Sstevel@tonic-gate$got = chomp();
80*0Sstevel@tonic-gateok ($got == 0) or print "# got $got\n";
81*0Sstevel@tonic-gateis ($_, "f");
82*0Sstevel@tonic-gate
83*0Sstevel@tonic-gate$_ = "xx";
84*0Sstevel@tonic-gate$/ = "xx";
85*0Sstevel@tonic-gate$got = chomp();
86*0Sstevel@tonic-gateok ($got == 2) or print "# got $got\n";
87*0Sstevel@tonic-gateis ($_, "");
88*0Sstevel@tonic-gate
89*0Sstevel@tonic-gate$_ = "axx";
90*0Sstevel@tonic-gate$/ = "xx";
91*0Sstevel@tonic-gate$got = chomp();
92*0Sstevel@tonic-gateok ($got == 2) or print "# got $got\n";
93*0Sstevel@tonic-gateis ($_, "a");
94*0Sstevel@tonic-gate
95*0Sstevel@tonic-gate$_ = "axx";
96*0Sstevel@tonic-gate$/ = "yy";
97*0Sstevel@tonic-gate$got = chomp();
98*0Sstevel@tonic-gateok ($got == 0) or print "# got $got\n";
99*0Sstevel@tonic-gateis ($_, "axx");
100*0Sstevel@tonic-gate
101*0Sstevel@tonic-gate# This case once mistakenly behaved like paragraph mode.
102*0Sstevel@tonic-gate$_ = "ab\n";
103*0Sstevel@tonic-gate$/ = \3;
104*0Sstevel@tonic-gate$got = chomp();
105*0Sstevel@tonic-gateok ($got == 0) or print "# got $got\n";
106*0Sstevel@tonic-gateis ($_, "ab\n");
107*0Sstevel@tonic-gate
108*0Sstevel@tonic-gate# Go Unicode.
109*0Sstevel@tonic-gate
110*0Sstevel@tonic-gate$_ = "abc\x{1234}";
111*0Sstevel@tonic-gatechop;
112*0Sstevel@tonic-gateis ($_, "abc", "Go Unicode");
113*0Sstevel@tonic-gate
114*0Sstevel@tonic-gate$_ = "abc\x{1234}d";
115*0Sstevel@tonic-gatechop;
116*0Sstevel@tonic-gateis ($_, "abc\x{1234}");
117*0Sstevel@tonic-gate
118*0Sstevel@tonic-gate$_ = "\x{1234}\x{2345}";
119*0Sstevel@tonic-gatechop;
120*0Sstevel@tonic-gateis ($_, "\x{1234}");
121*0Sstevel@tonic-gate
122*0Sstevel@tonic-gatemy @stuff = qw(this that);
123*0Sstevel@tonic-gateis (chop(@stuff[0,1]), 't');
124*0Sstevel@tonic-gate
125*0Sstevel@tonic-gate# bug id 20010305.012
126*0Sstevel@tonic-gate@stuff = qw(ab cd ef);
127*0Sstevel@tonic-gateis (chop(@stuff = @stuff), 'f');
128*0Sstevel@tonic-gate
129*0Sstevel@tonic-gate@stuff = qw(ab cd ef);
130*0Sstevel@tonic-gateis (chop(@stuff[0, 2]), 'f');
131*0Sstevel@tonic-gate
132*0Sstevel@tonic-gatemy %stuff = (1..4);
133*0Sstevel@tonic-gateis (chop(@stuff{1, 3}), '4');
134*0Sstevel@tonic-gate
135*0Sstevel@tonic-gate# chomp should not stringify references unless it decides to modify them
136*0Sstevel@tonic-gate$_ = [];
137*0Sstevel@tonic-gate$/ = "\n";
138*0Sstevel@tonic-gate$got = chomp();
139*0Sstevel@tonic-gateok ($got == 0) or print "# got $got\n";
140*0Sstevel@tonic-gateis (ref($_), "ARRAY", "chomp ref (modify)");
141*0Sstevel@tonic-gate
142*0Sstevel@tonic-gate$/ = ")";  # the last char of something like "ARRAY(0x80ff6e4)"
143*0Sstevel@tonic-gate$got = chomp();
144*0Sstevel@tonic-gateok ($got == 1) or print "# got $got\n";
145*0Sstevel@tonic-gateok (!ref($_), "chomp ref (no modify)");
146*0Sstevel@tonic-gate
147*0Sstevel@tonic-gate$/ = "\n";
148*0Sstevel@tonic-gate
149*0Sstevel@tonic-gate%chomp = ("One" => "One", "Two\n" => "Two", "" => "");
150*0Sstevel@tonic-gate%chop = ("One" => "On", "Two\n" => "Two", "" => "");
151*0Sstevel@tonic-gate
152*0Sstevel@tonic-gateforeach (keys %chomp) {
153*0Sstevel@tonic-gate  my $key = $_;
154*0Sstevel@tonic-gate  eval {chomp $_};
155*0Sstevel@tonic-gate  if ($@) {
156*0Sstevel@tonic-gate    my $err = $@;
157*0Sstevel@tonic-gate    $err =~ s/\n$//s;
158*0Sstevel@tonic-gate    fail ("\$\@ = \"$err\"");
159*0Sstevel@tonic-gate  } else {
160*0Sstevel@tonic-gate    is ($_, $chomp{$key}, "chomp hash key");
161*0Sstevel@tonic-gate  }
162*0Sstevel@tonic-gate}
163*0Sstevel@tonic-gate
164*0Sstevel@tonic-gateforeach (keys %chop) {
165*0Sstevel@tonic-gate  my $key = $_;
166*0Sstevel@tonic-gate  eval {chop $_};
167*0Sstevel@tonic-gate  if ($@) {
168*0Sstevel@tonic-gate    my $err = $@;
169*0Sstevel@tonic-gate    $err =~ s/\n$//s;
170*0Sstevel@tonic-gate    fail ("\$\@ = \"$err\"");
171*0Sstevel@tonic-gate  } else {
172*0Sstevel@tonic-gate    is ($_, $chop{$key}, "chop hash key");
173*0Sstevel@tonic-gate  }
174*0Sstevel@tonic-gate}
175*0Sstevel@tonic-gate
176*0Sstevel@tonic-gate# chop and chomp can't be lvalues
177*0Sstevel@tonic-gateeval 'chop($x) = 1;';
178*0Sstevel@tonic-gateok($@ =~ /Can\'t modify.*chop.*in.*assignment/);
179*0Sstevel@tonic-gateeval 'chomp($x) = 1;';
180*0Sstevel@tonic-gateok($@ =~ /Can\'t modify.*chom?p.*in.*assignment/);
181*0Sstevel@tonic-gateeval 'chop($x, $y) = (1, 2);';
182*0Sstevel@tonic-gateok($@ =~ /Can\'t modify.*chop.*in.*assignment/);
183*0Sstevel@tonic-gateeval 'chomp($x, $y) = (1, 2);';
184*0Sstevel@tonic-gateok($@ =~ /Can\'t modify.*chom?p.*in.*assignment/);
185*0Sstevel@tonic-gate
186*0Sstevel@tonic-gatemy @chars = ("N", "\xd3", substr ("\xd4\x{100}", 0, 1), chr 1296);
187*0Sstevel@tonic-gateforeach my $start (@chars) {
188*0Sstevel@tonic-gate  foreach my $end (@chars) {
189*0Sstevel@tonic-gate    local $/ = $end;
190*0Sstevel@tonic-gate    my $message = "start=" . ord ($start) . " end=" . ord $end;
191*0Sstevel@tonic-gate    my $string = $start . $end;
192*0Sstevel@tonic-gate    is (chomp ($string), 1, "$message [returns 1]");
193*0Sstevel@tonic-gate    is ($string, $start, $message);
194*0Sstevel@tonic-gate
195*0Sstevel@tonic-gate    my $end_utf8 = $end;
196*0Sstevel@tonic-gate    utf8::encode ($end_utf8);
197*0Sstevel@tonic-gate    next if $end_utf8 eq $end;
198*0Sstevel@tonic-gate
199*0Sstevel@tonic-gate    # $end ne $end_utf8, so these should not chomp.
200*0Sstevel@tonic-gate    $string = $start . $end_utf8;
201*0Sstevel@tonic-gate    my $chomped = $string;
202*0Sstevel@tonic-gate    is (chomp ($chomped), 0, "$message (end as bytes) [returns 0]");
203*0Sstevel@tonic-gate    is ($chomped, $string, "$message (end as bytes)");
204*0Sstevel@tonic-gate
205*0Sstevel@tonic-gate    $/ = $end_utf8;
206*0Sstevel@tonic-gate    $string = $start . $end;
207*0Sstevel@tonic-gate    $chomped = $string;
208*0Sstevel@tonic-gate    is (chomp ($chomped), 0, "$message (\$/ as bytes) [returns 0]");
209*0Sstevel@tonic-gate    is ($chomped, $string, "$message (\$/ as bytes)");
210*0Sstevel@tonic-gate  }
211*0Sstevel@tonic-gate}
212*0Sstevel@tonic-gate
213*0Sstevel@tonic-gate{
214*0Sstevel@tonic-gate    # returns length in characters, but not in bytes.
215*0Sstevel@tonic-gate    $/ = "\x{100}";
216*0Sstevel@tonic-gate    $a = "A$/";
217*0Sstevel@tonic-gate    $b = chomp $a;
218*0Sstevel@tonic-gate    is ($b, 1);
219*0Sstevel@tonic-gate
220*0Sstevel@tonic-gate    $/ = "\x{100}\x{101}";
221*0Sstevel@tonic-gate    $a = "A$/";
222*0Sstevel@tonic-gate    $b = chomp $a;
223*0Sstevel@tonic-gate    is ($b, 2);
224*0Sstevel@tonic-gate}
225