This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re: [ID 20020422.003] Suggestion in Perl 5.6.1 installation on AIX
[perl5.git] / lib / open.t
CommitLineData
e8c9ad1b 1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
bbd5c0f5 6 require Config; import Config;
e8c9ad1b 7}
8
99ef548b 9use Test::More tests => 16;
e8c9ad1b 10
11# open::import expects 'open' as its first argument, but it clashes with open()
12sub import {
13 open::import( 'open', @_ );
14}
15
16# can't use require_ok() here, with a name like 'open'
217f68ed 17ok( require 'open.pm', 'requiring open' );
e8c9ad1b 18
19# this should fail
20eval { import() };
217f68ed 21like( $@, qr/needs explicit list of disciplines/,
22 'import should fail without args' );
e8c9ad1b 23
24# the hint bits shouldn't be set yet
217f68ed 25is( $^H & $open::hint_bits, 0,
26 'hint bits should not be set in $^H before open import' );
e8c9ad1b 27
28# prevent it from loading I18N::Langinfo, so we can test encoding failures
e8c9ad1b 29my $warn;
30local $SIG{__WARN__} = sub {
31 $warn .= shift;
32};
33
34# and it shouldn't be able to find this discipline
99ef548b
PM
35$warn = '';
36eval q{ no warnings 'layer'; use open IN => ':macguffin' ; };
37is( $warn, '',
38 'should not warn about unknown discipline with bad discipline provided' );
39
40$warn = '';
41eval q{ use warnings 'layer'; use open IN => ':macguffin' ; };
217f68ed 42like( $warn, qr/Unknown discipline layer/,
43 'should warn about unknown discipline with bad discipline provided' );
e8c9ad1b 44
c129b0bd
JH
45SKIP: {
46 skip("no perlio, no :utf8", 1) unless $Config{useperlio};
47 # now load a real-looking locale
48 $ENV{LC_ALL} = ' .utf8';
49 import( 'IN', 'locale' );
d928c1f3 50 like( ${^OPEN}, qr/^(:utf8)?:utf8\0/,
c129b0bd
JH
51 'should set a valid locale layer' );
52}
e8c9ad1b 53
54# and see if it sets the magic variables appropriately
55import( 'IN', ':crlf' );
217f68ed 56ok( $^H & $open::hint_bits,
57 'hint bits should be set in $^H after open import' );
58is( $^H{'open_IN'}, 'crlf', 'should have set crlf layer' );
e8c9ad1b 59
60# it should reset them appropriately, too
61import( 'IN', ':raw' );
217f68ed 62is( $^H{'open_IN'}, 'raw', 'should have reset to raw layer' );
e8c9ad1b 63
1e616cf5 64# it dies if you don't set IN, OUT, or IO
e8c9ad1b 65eval { import( 'sideways', ':raw' ) };
217f68ed 66like( $@, qr/Unknown discipline class/, 'should croak with unknown class' );
e8c9ad1b 67
68# but it handles them all so well together
1e616cf5
JH
69import( 'IO', ':raw :crlf' );
70is( ${^OPEN}, ":raw :crlf\0:raw :crlf",
217f68ed 71 'should set multi types, multi disciplines' );
1e616cf5 72is( $^H{'open_IO'}, 'crlf', 'should record last layer set in %^H' );
e8c9ad1b 73
bbd5c0f5 74SKIP: {
820c63ad
JH
75 skip("no perlio, no :utf8", 4) unless $Config{'useperlio'};
76
24c43532 77 eval <<EOE;
bbd5c0f5
JH
78 use open ':utf8';
79 open(O, ">utf8");
80 print O chr(0x100);
81 close O;
82 open(I, "<utf8");
e111333b 83 is(ord(<I>), 0x100, ":utf8 single wide character round-trip");
bbd5c0f5 84 close I;
24c43532 85EOE
bbd5c0f5 86
820c63ad
JH
87 open F, ">a";
88 @a = map { chr(1 << ($_ << 2)) } 0..5; # 0x1, 0x10, .., 0x100000
89 unshift @a, chr(0); # ... and a null byte in front just for fun
90 print F @a;
91 close F;
e111333b 92
820c63ad
JH
93 sub systell {
94 use Fcntl 'SEEK_CUR';
95 sysseek($_[0], 0, SEEK_CUR);
96 }
e111333b 97
820c63ad
JH
98 require bytes; # not use
99
100 my $ok;
101
102 open F, "<:utf8", "a";
103 $ok = $a = 0;
104 for (@a) {
105 unless (
106 ($c = sysread(F, $b, 1)) == 1 &&
107 length($b) == 1 &&
108 ord($b) == ord($_) &&
109 systell(F) == ($a += bytes::length($b))
110 ) {
111 print '# ord($_) == ', ord($_), "\n";
112 print '# ord($b) == ', ord($b), "\n";
113 print '# length($b) == ', length($b), "\n";
114 print '# bytes::length($b) == ', bytes::length($b), "\n";
115 print '# systell(F) == ', systell(F), "\n";
116 print '# $a == ', $a, "\n";
117 print '# $c == ', $c, "\n";
118 last;
119 }
120 $ok++;
e111333b 121 }
820c63ad
JH
122 close F;
123 ok($ok == @a,
124 "on :utf8 streams sysread() should work on characters, not bytes");
125
126 # syswrite() on should work on characters, not bytes
127 open G, ">:utf8", "b";
128 $ok = $a = 0;
129 for (@a) {
130 unless (
131 ($c = syswrite(G, $_, 1)) == 1 &&
132 systell(G) == ($a += bytes::length($_))
133 ) {
134 print '# ord($_) == ', ord($_), "\n";
135 print '# bytes::length($_) == ', bytes::length($_), "\n";
136 print '# systell(G) == ', systell(G), "\n";
137 print '# $a == ', $a, "\n";
138 print '# $c == ', $c, "\n";
139 print "not ";
140 last;
141 }
142 $ok++;
e111333b 143 }
820c63ad
JH
144 close G;
145 ok($ok == @a,
146 "on :utf8 streams syswrite() should work on characters, not bytes");
147
148 open G, "<:utf8", "b";
149 $ok = $a = 0;
150 for (@a) {
151 unless (
152 ($c = sysread(G, $b, 1)) == 1 &&
153 length($b) == 1 &&
154 ord($b) == ord($_) &&
155 systell(G) == ($a += bytes::length($_))
156 ) {
157 print '# ord($_) == ', ord($_), "\n";
158 print '# ord($b) == ', ord($b), "\n";
159 print '# length($b) == ', length($b), "\n";
160 print '# bytes::length($b) == ', bytes::length($b), "\n";
161 print '# systell(G) == ', systell(G), "\n";
162 print '# $a == ', $a, "\n";
163 print '# $c == ', $c, "\n";
164 last;
165 }
166 $ok++;
e111333b 167 }
820c63ad
JH
168 close G;
169 ok($ok == @a,
170 "checking syswrite() output on :utf8 streams by reading it back in");
e111333b 171}
e111333b 172
bbd5c0f5
JH
173END {
174 1 while unlink "utf8";
e111333b
JH
175 1 while unlink "a";
176 1 while unlink "b";
bbd5c0f5 177}
1e616cf5
JH
178
179# the test cases beyond __DATA__ need to be executed separately
180
181__DATA__
e8c9ad1b 182$ENV{LC_ALL} = 'nonexistent.euc';
183eval { open::_get_locale_encoding() };
217f68ed 184like( $@, qr/too ambiguous/, 'should die with ambiguous locale encoding' );
1e616cf5
JH
185%%%
186# the special :locale layer
b429a72e 187$ENV{LC_ALL} = $ENV{LANG} = 'ru_RU.KOI8-R';
dbd62f41
JH
188# the :locale will probe the locale environment variables like LANG
189use open OUT => ':locale';
1e616cf5 190open(O, ">koi8");
23bcb45a 191print O chr(0x430); # Unicode CYRILLIC SMALL LETTER A = KOI8-R 0xc1
1e616cf5
JH
192close O;
193open(I, "<koi8");
23bcb45a 194printf "%#x\n", ord(<I>), "\n"; # this should print 0xc1
1e616cf5
JH
195close I;
196%%%