| 1 |
#!perl -w |
| 2 |
|
| 3 |
BEGIN { require 'charset_tools.pl'; } |
| 4 |
|
| 5 |
use Test::More tests => 43; |
| 6 |
|
| 7 |
use XS::APItest; |
| 8 |
|
| 9 |
for my $func ('SvPVbyte_nolen', 'SvPVutf8_nolen') { |
| 10 |
$g = *glob; |
| 11 |
$r = \1; |
| 12 |
is &$func($g), '*main::glob', "$func(\$glob_copy)"; |
| 13 |
is ref\$g, 'GLOB', "$func(\$glob_copy) does not flatten the glob"; |
| 14 |
is &$func($r), "$r", "$func(\$ref)"; |
| 15 |
is ref\$r, 'REF', "$func(\$ref) does not flatten the ref"; |
| 16 |
|
| 17 |
is &$func(*glob), '*main::glob', "$func(*glob)"; |
| 18 |
is ref\$::{glob}, 'GLOB', "$func(*glob) does not flatten the glob"; |
| 19 |
is &$func($^V), "$^V", "$func(\$ro_ref)"; |
| 20 |
is ref\$^V, 'REF', "$func(\$ro_ref) does not flatten the ref"; |
| 21 |
} |
| 22 |
|
| 23 |
my $B6 = byte_utf8a_to_utf8n("\xC2\xB6"); |
| 24 |
my $individual_B6_utf8_bytes = ($::IS_ASCII) |
| 25 |
? "\xC3\x82\xC2\xB6" |
| 26 |
: I8_to_native("\xC6\xB8\xC6\xA1"); |
| 27 |
my $data_bin = $B6; |
| 28 |
utf8::downgrade($data_bin); |
| 29 |
tie my $scalar_bin, 'TieScalarCounter', $data_bin; |
| 30 |
do { my $fetch = $scalar_bin }; |
| 31 |
is tied($scalar_bin)->{fetch}, 1; |
| 32 |
is tied($scalar_bin)->{store}, 0; |
| 33 |
my $len; |
| 34 |
is SvPVutf8_nomg($scalar_bin, $len), $individual_B6_utf8_bytes; |
| 35 |
is $len, length($individual_B6_utf8_bytes), "check len set by SvPVutf8_nomg"; |
| 36 |
is tied($scalar_bin)->{fetch}, 1; |
| 37 |
is tied($scalar_bin)->{store}, 0; |
| 38 |
undef $len; |
| 39 |
is SvPVbyte_nomg($scalar_bin, $len), $B6; |
| 40 |
is $len, length($B6), "check len set by SvPVbyte_nomg"; |
| 41 |
is tied($scalar_bin)->{fetch}, 1; |
| 42 |
is tied($scalar_bin)->{store}, 0; |
| 43 |
|
| 44 |
my $data_uni = $B6; |
| 45 |
utf8::upgrade($data_uni); |
| 46 |
tie my $scalar_uni, 'TieScalarCounter', $data_uni; |
| 47 |
do { my $fetch = $scalar_uni }; |
| 48 |
is tied($scalar_uni)->{fetch}, 1; |
| 49 |
is tied($scalar_uni)->{store}, 0; |
| 50 |
undef $len; |
| 51 |
is SvPVbyte_nomg($scalar_uni, $len), $B6; |
| 52 |
is $len, length($B6), "cheeck len set by SvPVbyte_nomg"; |
| 53 |
is tied($scalar_uni)->{fetch}, 1; |
| 54 |
is tied($scalar_uni)->{store}, 0; |
| 55 |
undef $len; |
| 56 |
is SvPVutf8_nomg($scalar_uni, $len), $individual_B6_utf8_bytes; |
| 57 |
is $len, length($individual_B6_utf8_bytes), "check len set by SvPVutf8_nomg"; |
| 58 |
is tied($scalar_uni)->{fetch}, 1; |
| 59 |
is tied($scalar_uni)->{store}, 0; |
| 60 |
|
| 61 |
undef $len; |
| 62 |
is SvPVutf8($scalar_bin, $len), $individual_B6_utf8_bytes; |
| 63 |
is $len, length $individual_B6_utf8_bytes; |
| 64 |
undef $len; |
| 65 |
is SvPVutf8($scalar_uni, $len), $individual_B6_utf8_bytes; |
| 66 |
is $len, length $individual_B6_utf8_bytes, "check len set by SvPVutf8"; |
| 67 |
|
| 68 |
eval 'SvPVbyte_nolen(*{chr 256})'; |
| 69 |
like $@, qr/^Wide character/, 'SvPVbyte_nolen fails on Unicode glob'; |
| 70 |
package r { use overload '""' => sub { substr "\x{100}\xff", -1 } } |
| 71 |
is SvPVbyte_nolen(bless [], r::), "\xff", |
| 72 |
'SvPVbyte_nolen on ref returning downgradable utf8 string'; |
| 73 |
|
| 74 |
sub TIESCALAR { bless \(my $thing = pop), shift } |
| 75 |
sub FETCH { ${ +shift } } |
| 76 |
tie $tyre, main => bless [], r::; |
| 77 |
is SvPVbyte_nolen($tyre), "\xff", |
| 78 |
'SvPVbyte on tie returning ref that returns downgradable utf8 string'; |
| 79 |
|
| 80 |
package TieScalarCounter; |
| 81 |
|
| 82 |
sub TIESCALAR { |
| 83 |
my ($class, $value) = @_; |
| 84 |
return bless { fetch => 0, store => 0, value => $value }, $class; |
| 85 |
} |
| 86 |
|
| 87 |
sub FETCH { |
| 88 |
my ($self) = @_; |
| 89 |
$self->{fetch}++; |
| 90 |
return $self->{value}; |
| 91 |
} |
| 92 |
|
| 93 |
sub STORE { |
| 94 |
my ($self, $value) = @_; |
| 95 |
$self->{store}++; |
| 96 |
$self->{value} = $value; |
| 97 |
} |