This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Use concat overloading for "foo$_->$*"
[perl5.git] / t / op / chop.t
CommitLineData
8d063cd8
LW
1#!./perl
2
991e6d41
NC
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
173ee337 6 require './test.pl'; require './charset_tools.pl';
991e6d41 7}
8d063cd8 8
e53ec980 9my $tests_count = 148;
e53ec980 10plan tests => $tests_count;
8d063cd8
LW
11
12$_ = 'abc';
bb19cdcd 13$c = foo();
991e6d41 14is ($c . $_, 'cab', 'optimized');
8d063cd8
LW
15
16$_ = 'abc';
17$c = chop($_);
991e6d41 18is ($c . $_ , 'cab', 'unoptimized');
8d063cd8
LW
19
20sub foo {
21 chop;
22}
a687059c
LW
23
24@foo = ("hi \n","there\n","!\n");
25@bar = @foo;
26chop(@bar);
ff64f327 27is (join('',@bar), 'hi there!', 'chop list of strings');
a687059c
LW
28
29$foo = "\n";
30chop($foo,@foo);
ff64f327 31is (join('',$foo,@foo), 'hi there!', 'chop on list reduces one-character element to an empty string');
a0d0e21e
LW
32
33$_ = "foo\n\n";
991e6d41 34$got = chomp();
ff64f327
JA
35is($got, 1, 'check return value when chomp string ending with two newlines; $/ is set to default of one newline');
36is ($_, "foo\n", 'chomp string ending with two newlines while $/ is set to one newline' );
a0d0e21e
LW
37
38$_ = "foo\n";
991e6d41 39$got = chomp();
ff64f327
JA
40is($got, 1, 'check return value chomp string ending with one newline while $/ is set to a newline');
41is ($_, "foo", 'test typical use of chomp; chomp a string ending in a single newline while $/ is set to default of one newline');
a0d0e21e
LW
42
43$_ = "foo";
991e6d41 44$got = chomp();
ff64f327
JA
45is($got, 0, 'check return value when chomp a string that does not end with current value of $/, 0 should be returned');
46is ($_, "foo", 'chomp a string that does not end with the current value of $/');
a0d0e21e
LW
47
48$_ = "foo";
49$/ = "oo";
991e6d41 50$got = chomp();
ff64f327
JA
51is ($got, "2", 'check return value when chomp string with $/ consisting of more than one character, and with the ending of the string matching $/');
52is ($_, "f", 'chomp a string when $/ consists of two characters that are at the end of the string, check that chomped string contains remnant of original string');
a0d0e21e
LW
53
54$_ = "bar";
55$/ = "oo";
991e6d41 56$got = chomp();
ff64f327
JA
57is($got, "0", 'check return value when call chomp with $/ consisting of more than one character, and with the ending of the string NOT matching $/');
58is ($_, "bar", 'chomp a string when $/ consists of two characters that are NOT at the end of the string');
a0d0e21e
LW
59
60$_ = "f\n\n\n\n\n";
61$/ = "";
991e6d41 62$got = chomp();
ff64f327
JA
63is ($got, 5, 'check return value when chomp in paragraph mode on string ending with 5 newlines');
64is ($_, "f", 'chomp in paragraph mode on string ending with 5 newlines');
a0d0e21e
LW
65
66$_ = "f\n\n";
67$/ = "";
991e6d41 68$got = chomp();
ff64f327
JA
69is ($got, 2, 'check return value when chomp in paragraph mode on string ending with 2 newlines');
70is ($_, "f", 'chomp in paragraph mode on string ending with 2 newlines');
a0d0e21e
LW
71
72$_ = "f\n";
73$/ = "";
991e6d41 74$got = chomp();
ff64f327
JA
75is ($got, 1, 'check return value when chomp in paragraph mode on string ending with 1 newline');
76is ($_, "f", 'chomp in paragraph mode on string ending with 1 newlines');
a0d0e21e
LW
77
78$_ = "f";
79$/ = "";
991e6d41 80$got = chomp();
ff64f327
JA
81is ($got, 0, 'check return value when chomp in paragraph mode on string ending with no newlines');
82is ($_, "f", 'chomp in paragraph mode on string lacking trailing newlines');
c85b29f8 83
84$_ = "xx";
85$/ = "xx";
991e6d41 86$got = chomp();
ff64f327
JA
87is ($got, 2, 'check return value when chomp string that consists solely of current value of $/');
88is ($_, "", 'chomp on string that consists solely of current value of $/; check that empty string remains');
c85b29f8 89
90$_ = "axx";
91$/ = "xx";
991e6d41 92$got = chomp();
ff64f327
JA
93is ($got, 2, 'check return value when chomp string that ends with current value of $/. $/ contains two characters');
94is ($_, "a", 'check that when chomp string that ends with currnt value of $/, the part of original string that wasn\'t in $/ remains');
c85b29f8 95
96$_ = "axx";
97$/ = "yy";
991e6d41 98$got = chomp();
ff64f327
JA
99is ($got, 0, 'check return value when chomp string that does not end with $/');
100is ($_, "axx", 'chomp a string that does not end with $/, the entire string should remain intact');
4c5a6083
GS
101
102# This case once mistakenly behaved like paragraph mode.
103$_ = "ab\n";
104$/ = \3;
991e6d41 105$got = chomp();
ff64f327
JA
106is ($got, 0, 'check return value when call chomp with $_ = "ab\\n", $/ = \3' );
107is ($_, "ab\n", 'chomp with $_ = "ab\\n", $/ = \3' );
9b83640a
JH
108
109# Go Unicode.
110
111$_ = "abc\x{1234}";
112chop;
ff64f327 113is ($_, "abc", 'Go Unicode');
9b83640a
JH
114
115$_ = "abc\x{1234}d";
116chop;
991e6d41 117is ($_, "abc\x{1234}");
9b83640a
JH
118
119$_ = "\x{1234}\x{2345}";
120chop;
991e6d41 121is ($_, "\x{1234}");
37ce32a7 122
37ce32a7 123my @stuff = qw(this that);
991e6d41 124is (chop(@stuff[0,1]), 't');
2ec6af5f
RG
125
126# bug id 20010305.012
127@stuff = qw(ab cd ef);
991e6d41 128is (chop(@stuff = @stuff), 'f');
2ec6af5f
RG
129
130@stuff = qw(ab cd ef);
991e6d41 131is (chop(@stuff[0, 2]), 'f');
2ec6af5f
RG
132
133my %stuff = (1..4);
991e6d41 134is (chop(@stuff{1, 3}), '4');
9b33ce3b
GA
135
136# chomp should not stringify references unless it decides to modify them
137$_ = [];
138$/ = "\n";
991e6d41
NC
139$got = chomp();
140ok ($got == 0) or print "# got $got\n";
141is (ref($_), "ARRAY", "chomp ref (modify)");
9b33ce3b
GA
142
143$/ = ")"; # the last char of something like "ARRAY(0x80ff6e4)"
991e6d41
NC
144$got = chomp();
145ok ($got == 1) or print "# got $got\n";
146ok (!ref($_), "chomp ref (no modify)");
147
148$/ = "\n";
149
150%chomp = ("One" => "One", "Two\n" => "Two", "" => "");
151%chop = ("One" => "On", "Two\n" => "Two", "" => "");
152
153foreach (keys %chomp) {
154 my $key = $_;
155 eval {chomp $_};
156 if ($@) {
157 my $err = $@;
158 $err =~ s/\n$//s;
159 fail ("\$\@ = \"$err\"");
160 } else {
161 is ($_, $chomp{$key}, "chomp hash key");
162 }
163}
164
165foreach (keys %chop) {
166 my $key = $_;
167 eval {chop $_};
168 if ($@) {
169 my $err = $@;
170 $err =~ s/\n$//s;
171 fail ("\$\@ = \"$err\"");
172 } else {
173 is ($_, $chop{$key}, "chop hash key");
174 }
175}
75ea820e
SM
176
177# chop and chomp can't be lvalues
178eval 'chop($x) = 1;';
30ebab2d 179ok($@ =~ /Can\'t modify.*chop.*in.*assignment/);
75ea820e 180eval 'chomp($x) = 1;';
30ebab2d 181ok($@ =~ /Can\'t modify.*chom?p.*in.*assignment/);
75ea820e 182eval 'chop($x, $y) = (1, 2);';
30ebab2d 183ok($@ =~ /Can\'t modify.*chop.*in.*assignment/);
75ea820e 184eval 'chomp($x, $y) = (1, 2);';
30ebab2d 185ok($@ =~ /Can\'t modify.*chom?p.*in.*assignment/);
75ea820e 186
e53ec980
KW
187my @chars = ("N",
188 uni_to_native("\xd3"),
189 substr (uni_to_native("\xd4") . "\x{100}", 0, 1),
190 chr 1296);
c4c87a06
NC
191foreach my $start (@chars) {
192 foreach my $end (@chars) {
193 local $/ = $end;
194 my $message = "start=" . ord ($start) . " end=" . ord $end;
195 my $string = $start . $end;
132d8bbf 196 is (chomp ($string), 1, "$message [returns 1]");
c4c87a06
NC
197 is ($string, $start, $message);
198
199 my $end_utf8 = $end;
200 utf8::encode ($end_utf8);
201 next if $end_utf8 eq $end;
202
203 # $end ne $end_utf8, so these should not chomp.
204 $string = $start . $end_utf8;
205 my $chomped = $string;
132d8bbf 206 is (chomp ($chomped), 0, "$message (end as bytes) [returns 0]");
c4c87a06
NC
207 is ($chomped, $string, "$message (end as bytes)");
208
209 $/ = $end_utf8;
210 $string = $start . $end;
211 $chomped = $string;
132d8bbf 212 is (chomp ($chomped), 0, "$message (\$/ as bytes) [returns 0]");
c4c87a06
NC
213 is ($chomped, $string, "$message (\$/ as bytes)");
214 }
215}
a6aa349d
TS
216
217{
218 # returns length in characters, but not in bytes.
219 $/ = "\x{100}";
220 $a = "A$/";
221 $b = chomp $a;
222 is ($b, 1);
223
224 $/ = "\x{100}\x{101}";
225 $a = "A$/";
226 $b = chomp $a;
227 is ($b, 2);
228}
ace7757b
TS
229
230{
231 # [perl #36569] chop fails on decoded string with trailing nul
232 my $asc = "perl\0";
233 my $utf = "perl".pack('U',0); # marked as utf8
234 is(chop($asc), "\0", "chopping ascii NUL");
235 is(chop($utf), "\0", "chopping utf8 NUL");
236 is($asc, "perl", "chopped ascii NUL");
237 is($utf, "perl", "chopped utf8 NUL");
238}
1937c63e
TS
239
240{
241 # Change 26011: Re: A surprising segfault
242 # to make sure only that these obfuscated sentences will not crash.
243
244 map chop(+()), ('')x68;
245 ok(1, "extend sp in pp_chop");
246
247 map chomp(+()), ('')x68;
248 ok(1, "extend sp in pp_chomp");
249}
65ab9279 250
c0236afe 251{
65ab9279
TC
252 # [perl #73246] chop doesn't support utf8
253 # the problem was UTF8_IS_START() didn't handle perl's extended UTF8
e53ec980 254
760c7c2f 255 no warnings 'deprecated'; # This is above IV_MAX on 32 bit machines
c0236afe 256 my $utf = "\x{80000001}\x{80000000}";
65ab9279 257 my $result = chop($utf);
c0236afe
KW
258 is($utf, "\x{80000001}", "chopping high 'unicode'- remnant");
259 is($result, "\x{80000000}", "chopping high 'unicode' - result");
65ab9279
TC
260
261 SKIP: {
dfae3023 262 no warnings 'overflow'; # avoid compile-time warnings below on 32-bit architectures
65ab9279
TC
263 use Config;
264 $Config{ivsize} >= 8
265 or skip("this build can't handle very large characters", 2);
c0236afe 266 my $utf = "\x{ffffffffffffffff}\x{fffffffffffffffe}";
65ab9279 267 my $result = chop $utf;
c0236afe
KW
268 is($utf, "\x{ffffffffffffffff}", "chop even higher 'unicode' - remnant");
269 is($result, "\x{fffffffffffffffe}", "chop even higher 'unicode' - result");
65ab9279
TC
270 }
271}
6c7f10b5 272
e4e95921 273$/ = "\n";
6c7f10b5
JK
274{
275 my $expected = 99999;
276 my $input = "UserID\talpha $expected\n";
277 my $uid = '';
278 chomp(my @line = split (/ |\t/,$input));
279 $uid = $line[-1];
280 is($uid, $expected,
281 "RT #123057: chomp works as expected on split");
282}
f595e19f
FC
283
284{
285 my $a = local $/ = 7;
286 $a = chomp $a;
287 is $a, 1, 'lexical $a = chomp $a when $a eq $/ eq 7';
288 $a = $/ = 0;
289 $a = chomp $a;
290 is $a, 1, 'lexical $a = chomp $a when $a eq $/ eq 0';
291 my @a = "7";
292 for my $b($a[0]) {
293 $/ = 7;
294 $b = chomp @a;
295 is $b, 1,
296 'lexical $b = chomp @a when $b eq $/ eq 7 and \$a[0] == \$b';
297 $b = $/ = 0;
298 $b = chomp @a;
299 is $b, 1,
300 'lexical $b = chomp @a when $b eq $/ eq 0 and \$a[0] == \$b';
301 }
302}