This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
More updates to Module-CoreList for Perl 5.20.2
[perl5.git] / lib / bytes.t
CommitLineData
2f3efc97 1
4e002eb0
JH
2BEGIN {
3 chdir 't' if -d 't';
4 @INC = '../lib';
1ae0ae17 5 require './test.pl';
4e002eb0
JH
6}
7
ac993614 8plan tests => 24;
4e002eb0 9
1ae0ae17 10my $a = chr(0x100);
4e002eb0 11
1ae0ae17
JH
12is(ord($a), 0x100, "ord sanity check");
13is(length($a), 1, "length sanity check");
579f6b36
JH
14is(substr($a, 0, 1), "\x{100}", "substr sanity check");
15is(index($a, "\x{100}"), 0, "index sanity check");
16is(rindex($a, "\x{100}"), 0, "rindex sanity check");
1ae0ae17 17is(bytes::length($a), 2, "bytes::length sanity check");
579f6b36 18is(bytes::chr(0x100), chr(0), "bytes::chr sanity check");
4e002eb0
JH
19
20{
21 use bytes;
1ae0ae17
JH
22 my $b = chr(0x100); # affected by 'use bytes'
23 is(ord($b), 0, "chr truncates under use bytes");
24 is(length($b), 1, "length truncated under use bytes");
579f6b36 25 is(bytes::ord($b), 0, "bytes::ord truncated under use bytes");
1ae0ae17 26 is(bytes::length($b), 1, "bytes::length truncated under use bytes");
579f6b36 27 is(bytes::substr($b, 0, 1), "\0", "bytes::substr truncated under use bytes");
4e002eb0
JH
28}
29
1ae0ae17 30my $c = chr(0x100);
ac993614
TC
31my $c2 = chr(0x2c7); # a unicode character that doesn't fold
32utf8::encode(my $c2_utf8 = $c2);
4e002eb0
JH
33
34{
35 use bytes;
1ae0ae17
JH
36 if (ord('A') == 193) { # EBCDIC?
37 is(ord($c), 0x8c, "ord under use bytes looks at the 1st byte");
0ef00eb6 38 } else {
1ae0ae17 39 is(ord($c), 0xc4, "ord under use bytes looks at the 1st byte");
0ef00eb6 40 }
1ae0ae17
JH
41 is(length($c), 2, "length under use bytes looks at bytes");
42 is(bytes::length($c), 2, "bytes::length under use bytes looks at bytes");
579f6b36
JH
43 if (ord('A') == 193) { # EBCDIC?
44 is(bytes::ord($c), 0x8c, "bytes::ord under use bytes looks at the 1st byte");
45 } else {
46 is(bytes::ord($c), 0xc4, "bytes::ord under use bytes looks at the 1st byte");
47 }
2f3efc97
JH
48 # In z/OS \x41,\x8c are the codepoints corresponding to \x80,\xc4 respectively under ASCII platform
49 if (ord('A') == 193) { # EBCDIC?
50 is(bytes::substr($c, 0, 1), "\x8c", "bytes::substr under use bytes looks at bytes");
51 is(bytes::index($c, "\x41"), 1, "bytes::index under use bytes looks at bytes");
52 is(bytes::rindex($c, "\x8c"), 0, "bytes::rindex under use bytes looks at bytes");
53
54 }
55 else{
56 is(bytes::substr($c, 0, 1), "\xc4", "bytes::substr under use bytes looks at bytes");
57 is(bytes::index($c, "\x80"), 1, "bytes::index under use bytes looks at bytes");
58 is(bytes::rindex($c, "\xc4"), 0, "bytes::rindex under use bytes looks at bytes");
59 }
60
ac993614
TC
61 # [perl #117355] [lu]cfirst don't respect 'use bytes'
62 # and if there's other tests for lc/uc under bytes I didn't find them
63 is(lc($c2), $c2_utf8, "lc under use bytes returns bytes");
64 is(uc($c2), $c2_utf8, "uc under use bytes returns bytes");
ac993614
TC
65 is(lcfirst($c2), $c2_utf8, "lcfirst under use bytes returns bytes");
66 is(ucfirst($c2), $c2_utf8, "unfirst under use bytes returns bytes");
4e002eb0 67}
5b5a256a
TS
68
69{
70 fresh_perl_like ('use bytes; bytes::moo()',
71 qr/Undefined subroutine bytes::moo/, {stderr=>1},
72 "Check Carp is loaded for AUTOLOADing errors")
73}