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