1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
use strict;
use warnings;
use bytes;
use Test::More ;
use CompTestUtils;
BEGIN
{
plan skip_all => "Encode is not available"
if $] < 5.006 ;
eval { require Encode; Encode->import(); };
plan skip_all => "Encode is not available"
if $@ ;
# use Test::NoWarnings, if available
my $extra = 0 ;
my $st = eval { require Test::NoWarnings ; import Test::NoWarnings; 1; };
$extra = 1
if $st ;
plan(tests => 29 + $extra) ;
}
sub run
{
my $CompressClass = identify();
my $UncompressClass = getInverse($CompressClass);
my $Error = getErrorRef($CompressClass);
my $UnError = getErrorRef($UncompressClass);
my $string = "\x{df}\x{100}\x80";
my $encString = Encode::encode_utf8($string);
my $buffer = $encString;
#for my $from ( qw(filename filehandle buffer) )
{
# my $input ;
# my $lex = new LexFile my $name ;
#
#
# if ($from eq 'buffer')
# { $input = \$buffer }
# elsif ($from eq 'filename')
# {
# $input = $name ;
# writeFile($name, $buffer);
# }
# elsif ($from eq 'filehandle')
# {
# $input = new IO::File "<$name" ;
# }
for my $to ( qw(filehandle buffer))
{
title "OO Mode: To $to, Encode by hand";
my $lex2 = new LexFile my $name2 ;
my $output;
my $buffer;
if ($to eq 'buffer')
{ $output = \$buffer }
elsif ($to eq 'filename')
{
$output = $name2 ;
}
elsif ($to eq 'filehandle')
{
$output = new IO::File ">$name2" ;
}
my $out ;
my $cs = new $CompressClass($output, AutoClose =>1);
$cs->print($encString);
$cs->close();
my $input;
if ($to eq 'buffer')
{ $input = \$buffer }
else
{
$input = $name2 ;
}
my $ucs = new $UncompressClass($input, Append => 1);
my $got;
1 while $ucs->read($got) > 0 ;
is $got, $encString, " Expected output";
my $decode = Encode::decode_utf8($got);
is $decode, $string, " Expected output";
}
}
{
title "Catch wide characters";
my $out;
my $cs = new $CompressClass(\$out);
my $a = "a\xFF\x{100}";
eval { $cs->syswrite($a) };
like($@, qr/Wide character in ${CompressClass}::write/,
" wide characters in ${CompressClass}::write");
}
{
title "Unknown encoding";
my $output;
eval { my $cs = new $CompressClass(\$output, Encode => 'fred'); } ;
like($@, qr/${CompressClass}: Encoding 'fred' is not available/,
" Encoding 'fred' is not available");
}
{
title "Encode option";
for my $to ( qw(filehandle filename buffer))
{
title "Encode: To $to, Encode option";
my $lex2 = new LexFile my $name2 ;
my $output;
my $buffer;
if ($to eq 'buffer')
{
$output = \$buffer
}
elsif ($to eq 'filename')
{
$output = $name2 ;
}
elsif ($to eq 'filehandle')
{
$output = new IO::File ">$name2" ;
}
my $out ;
my $cs = new $CompressClass($output, AutoClose =>1, Encode => 'utf8');
ok $cs->print($string);
ok $cs->close();
my $input;
if ($to eq 'buffer')
{
$input = \$buffer
}
elsif ($to eq 'filename')
{
$input = $name2 ;
}
else
{
$input = new IO::File "<$name2" ;
}
{
my $ucs = new $UncompressClass($input, AutoClose =>1, Append => 1);
my $got;
1 while $ucs->read($got) > 0 ;
ok length($got) > 0;
is $got, $encString, " Expected output";
my $decode = Encode::decode_utf8($got);
is $decode, $string, " Expected output";
}
# {
# my $ucs = new $UncompressClass($input, Append => 1, Decode => 'utf8');
# my $got;
# 1 while $ucs->read($got) > 0 ;
# ok length($got) > 0;
# is $got, $string, " Expected output";
# }
}
}
}
1;
|