6 require './test.pl'; require './charset_tools.pl';
10 $tests_count -= 2 if $::IS_EBCDIC;
11 plan tests => $tests_count;
15 is ($c . $_, 'cab', 'optimized');
19 is ($c . $_ , 'cab', 'unoptimized');
25 @foo = ("hi \n","there\n","!\n");
28 is (join('',@bar), 'hi there!', 'chop list of strings');
32 is (join('',$foo,@foo), 'hi there!', 'chop on list reduces one-character element to an empty string');
36 is($got, 1, 'check return value when chomp string ending with two newlines; $/ is set to default of one newline');
37 is ($_, "foo\n", 'chomp string ending with two newlines while $/ is set to one newline' );
41 is($got, 1, 'check return value chomp string ending with one newline while $/ is set to a newline');
42 is ($_, "foo", 'test typical use of chomp; chomp a string ending in a single newline while $/ is set to default of one newline');
46 is($got, 0, 'check return value when chomp a string that does not end with current value of $/, 0 should be returned');
47 is ($_, "foo", 'chomp a string that does not end with the current value of $/');
52 is ($got, "2", 'check return value when chomp string with $/ consisting of more than one character, and with the ending of the string matching $/');
53 is ($_, "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');
58 is($got, "0", 'check return value when call chomp with $/ consisting of more than one character, and with the ending of the string NOT matching $/');
59 is ($_, "bar", 'chomp a string when $/ consists of two characters that are NOT at the end of the string');
64 is ($got, 5, 'check return value when chomp in paragraph mode on string ending with 5 newlines');
65 is ($_, "f", 'chomp in paragraph mode on string ending with 5 newlines');
70 is ($got, 2, 'check return value when chomp in paragraph mode on string ending with 2 newlines');
71 is ($_, "f", 'chomp in paragraph mode on string ending with 2 newlines');
76 is ($got, 1, 'check return value when chomp in paragraph mode on string ending with 1 newline');
77 is ($_, "f", 'chomp in paragraph mode on string ending with 1 newlines');
82 is ($got, 0, 'check return value when chomp in paragraph mode on string ending with no newlines');
83 is ($_, "f", 'chomp in paragraph mode on string lacking trailing newlines');
88 is ($got, 2, 'check return value when chomp string that consists solely of current value of $/');
89 is ($_, "", 'chomp on string that consists solely of current value of $/; check that empty string remains');
94 is ($got, 2, 'check return value when chomp string that ends with current value of $/. $/ contains two characters');
95 is ($_, "a", 'check that when chomp string that ends with currnt value of $/, the part of original string that wasn\'t in $/ remains');
100 is ($got, 0, 'check return value when chomp string that does not end with $/');
101 is ($_, "axx", 'chomp a string that does not end with $/, the entire string should remain intact');
103 # This case once mistakenly behaved like paragraph mode.
107 is ($got, 0, 'check return value when call chomp with $_ = "ab\\n", $/ = \3' );
108 is ($_, "ab\n", 'chomp with $_ = "ab\\n", $/ = \3' );
114 is ($_, "abc", 'Go Unicode');
118 is ($_, "abc\x{1234}");
120 $_ = "\x{1234}\x{2345}";
124 my @stuff = qw(this that);
125 is (chop(@stuff[0,1]), 't');
127 # bug id 20010305.012
128 @stuff = qw(ab cd ef);
129 is (chop(@stuff = @stuff), 'f');
131 @stuff = qw(ab cd ef);
132 is (chop(@stuff[0, 2]), 'f');
135 is (chop(@stuff{1, 3}), '4');
137 # chomp should not stringify references unless it decides to modify them
141 ok ($got == 0) or print "# got $got\n";
142 is (ref($_), "ARRAY", "chomp ref (modify)");
144 $/ = ")"; # the last char of something like "ARRAY(0x80ff6e4)"
146 ok ($got == 1) or print "# got $got\n";
147 ok (!ref($_), "chomp ref (no modify)");
151 %chomp = ("One" => "One", "Two\n" => "Two", "" => "");
152 %chop = ("One" => "On", "Two\n" => "Two", "" => "");
154 foreach (keys %chomp) {
160 fail ("\$\@ = \"$err\"");
162 is ($_, $chomp{$key}, "chomp hash key");
166 foreach (keys %chop) {
172 fail ("\$\@ = \"$err\"");
174 is ($_, $chop{$key}, "chop hash key");
178 # chop and chomp can't be lvalues
179 eval 'chop($x) = 1;';
180 ok($@ =~ /Can\'t modify.*chop.*in.*assignment/);
181 eval 'chomp($x) = 1;';
182 ok($@ =~ /Can\'t modify.*chom?p.*in.*assignment/);
183 eval 'chop($x, $y) = (1, 2);';
184 ok($@ =~ /Can\'t modify.*chop.*in.*assignment/);
185 eval 'chomp($x, $y) = (1, 2);';
186 ok($@ =~ /Can\'t modify.*chom?p.*in.*assignment/);
189 uni_to_native("\xd3"),
190 substr (uni_to_native("\xd4") . "\x{100}", 0, 1),
192 foreach my $start (@chars) {
193 foreach my $end (@chars) {
195 my $message = "start=" . ord ($start) . " end=" . ord $end;
196 my $string = $start . $end;
197 is (chomp ($string), 1, "$message [returns 1]");
198 is ($string, $start, $message);
201 utf8::encode ($end_utf8);
202 next if $end_utf8 eq $end;
204 # $end ne $end_utf8, so these should not chomp.
205 $string = $start . $end_utf8;
206 my $chomped = $string;
207 is (chomp ($chomped), 0, "$message (end as bytes) [returns 0]");
208 is ($chomped, $string, "$message (end as bytes)");
211 $string = $start . $end;
213 is (chomp ($chomped), 0, "$message (\$/ as bytes) [returns 0]");
214 is ($chomped, $string, "$message (\$/ as bytes)");
219 # returns length in characters, but not in bytes.
225 $/ = "\x{100}\x{101}";
232 # [perl #36569] chop fails on decoded string with trailing nul
234 my $utf = "perl".pack('U',0); # marked as utf8
235 is(chop($asc), "\0", "chopping ascii NUL");
236 is(chop($utf), "\0", "chopping utf8 NUL");
237 is($asc, "perl", "chopped ascii NUL");
238 is($utf, "perl", "chopped utf8 NUL");
242 # Change 26011: Re: A surprising segfault
243 # to make sure only that these obfuscated sentences will not crash.
245 map chop(+()), ('')x68;
246 ok(1, "extend sp in pp_chop");
248 map chomp(+()), ('')x68;
249 ok(1, "extend sp in pp_chomp");
253 # [perl #73246] chop doesn't support utf8
254 # the problem was UTF8_IS_START() didn't handle perl's extended UTF8
255 skip("Not representable in EBCDIC", 2) if $::IS_EBCDIC;
257 # We use hex constants instead of literal chars to avoid compilation
259 my $first_char = 0x80000001;
260 my $second_char = 0x80000000;
261 my $utf = chr($first_char) . chr($second_char);
262 my $result = chop($utf);
263 is($utf, chr $first_char, "chopping high 'unicode'- remnant");
264 is($result, chr $second_char, "chopping high 'unicode' - result");
267 no warnings 'overflow'; # avoid compile-time warnings below on 32-bit architectures
270 or skip("this build can't handle very large characters", 2);
271 my $first_char = 0xffffffffffffffff;
272 my $second_char = 0xfffffffffffffffe;
273 my $utf = chr($first_char) . chr($second_char);
274 my $result = chop $utf;
275 is($utf, chr $first_char, "chop even higher 'unicode' - remnant");
276 is($result, chr $second_char, "chop even higher 'unicode' - result");
282 my $expected = 99999;
283 my $input = "UserID\talpha $expected\n";
285 chomp(my @line = split (/ |\t/,$input));
288 "RT #123057: chomp works as expected on split");
292 my $a = local $/ = 7;
294 is $a, 1, 'lexical $a = chomp $a when $a eq $/ eq 7';
297 is $a, 1, 'lexical $a = chomp $a when $a eq $/ eq 0';
303 'lexical $b = chomp @a when $b eq $/ eq 7 and \$a[0] == \$b';
307 'lexical $b = chomp @a when $b eq $/ eq 0 and \$a[0] == \$b';