This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix for perl #112316: Wrong behavior regarding labels with same prefix
[perl5.git] / t / op / chop.t
old mode 100755 (executable)
new mode 100644 (file)
index 1ac45c3..4aa8db3
@@ -6,10 +6,10 @@ BEGIN {
     require './test.pl';
 }
 
-plan tests => 51;
+plan tests => 143;
 
 $_ = 'abc';
-$c = do foo();
+$c = foo();
 is ($c . $_, 'cab', 'optimized');
 
 $_ = 'abc';
@@ -175,11 +175,91 @@ foreach (keys %chop) {
 
 # chop and chomp can't be lvalues
 eval 'chop($x) = 1;';
-print $@ =~ /Can\'t modify.*chop.*in.*assignment/ ? "ok 48\n" : "not ok 48\n";
+ok($@ =~ /Can\'t modify.*chop.*in.*assignment/);
 eval 'chomp($x) = 1;';
-print $@ =~ /Can\'t modify.*chom?p.*in.*assignment/ ? "ok 49\n" : "not ok 49\n";
+ok($@ =~ /Can\'t modify.*chom?p.*in.*assignment/);
 eval 'chop($x, $y) = (1, 2);';
-print $@ =~ /Can\'t modify.*chop.*in.*assignment/ ? "ok 50\n" : "not ok 50\n";
+ok($@ =~ /Can\'t modify.*chop.*in.*assignment/);
 eval 'chomp($x, $y) = (1, 2);';
-print $@ =~ /Can\'t modify.*chom?p.*in.*assignment/ ? "ok 51\n" : "not ok 51\n";
+ok($@ =~ /Can\'t modify.*chom?p.*in.*assignment/);
+
+my @chars = ("N", latin1_to_native("\xd3"), substr ("\xd4\x{100}", 0, 1), chr 1296);
+foreach my $start (@chars) {
+  foreach my $end (@chars) {
+    local $/ = $end;
+    my $message = "start=" . ord ($start) . " end=" . ord $end;
+    my $string = $start . $end;
+    is (chomp ($string), 1, "$message [returns 1]");
+    is ($string, $start, $message);
+
+    my $end_utf8 = $end;
+    utf8::encode ($end_utf8);
+    next if $end_utf8 eq $end;
+
+    # $end ne $end_utf8, so these should not chomp.
+    $string = $start . $end_utf8;
+    my $chomped = $string;
+    is (chomp ($chomped), 0, "$message (end as bytes) [returns 0]");
+    is ($chomped, $string, "$message (end as bytes)");
+
+    $/ = $end_utf8;
+    $string = $start . $end;
+    $chomped = $string;
+    is (chomp ($chomped), 0, "$message (\$/ as bytes) [returns 0]");
+    is ($chomped, $string, "$message (\$/ as bytes)");
+  }
+}
+
+{
+    # returns length in characters, but not in bytes.
+    $/ = "\x{100}";
+    $a = "A$/";
+    $b = chomp $a;
+    is ($b, 1);
+
+    $/ = "\x{100}\x{101}";
+    $a = "A$/";
+    $b = chomp $a;
+    is ($b, 2);
+}
+
+{
+    # [perl #36569] chop fails on decoded string with trailing nul
+    my $asc = "perl\0";
+    my $utf = "perl".pack('U',0); # marked as utf8
+    is(chop($asc), "\0", "chopping ascii NUL");
+    is(chop($utf), "\0", "chopping utf8 NUL");
+    is($asc, "perl", "chopped ascii NUL");
+    is($utf, "perl", "chopped utf8 NUL");
+}
 
+{
+    # Change 26011: Re: A surprising segfault
+    # to make sure only that these obfuscated sentences will not crash.
+
+    map chop(+()), ('')x68;
+    ok(1, "extend sp in pp_chop");
+
+    map chomp(+()), ('')x68;
+    ok(1, "extend sp in pp_chomp");
+}
+
+{
+    # [perl #73246] chop doesn't support utf8
+    # the problem was UTF8_IS_START() didn't handle perl's extended UTF8
+    my $utf = "\x{80000001}\x{80000000}";
+    my $result = chop($utf);
+    is($utf, "\x{80000001}", "chopping high 'unicode'- remnant");
+    is($result, "\x{80000000}", "chopping high 'unicode' - result");
+
+    SKIP: {
+        no warnings 'overflow'; # avoid compile-time warnings below on 32-bit architectures
+        use Config;
+        $Config{ivsize} >= 8
+         or skip("this build can't handle very large characters", 2);
+        my $utf = "\x{ffffffffffffffff}\x{fffffffffffffffe}";
+        my $result = chop $utf;
+        is($utf, "\x{ffffffffffffffff}", "chop even higher 'unicode' - remnant");
+        is($result, "\x{fffffffffffffffe}", "chop even higher 'unicode' - result");
+    }
+}