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
|
#!/usr/bin/perl
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
use strict;
use warnings;
use Test::More tests => 13;
use Test::Dpkg qw(:paths);
use_ok('Dpkg::Compression');
use_ok('Dpkg::Compression::FileHandle');
my $tmpdir = test_get_temp_path();
my @lines = ("One\n", "Two\n", "Three\n");
my $fh;
sub test_write {
my ($filename, $check_result) = @_;
$fh = Dpkg::Compression::FileHandle->new();
open $fh, '>', $filename or die 'open failed';
print { $fh } $lines[0];
syswrite($fh, $lines[1]);
printf { $fh } '%s', $lines[2];
close $fh or die 'close failed';
$check_result->($filename, 'std functions');
unlink $filename or die "cannot unlink $filename";
$fh = Dpkg::Compression::FileHandle->new();
$fh->open($filename, 'w');
$fh->print($lines[0]);
$fh->write($lines[1], length($lines[1]));
$fh->printf('%s', $lines[2]);
$fh->close() or die 'close failed';
$check_result->($filename, 'IO::Handle methods');
}
sub check_uncompressed {
my ($filename, $method) = @_;
open(my $read_fh, '<', $filename) or die "cannot read $filename";
my @read = <$read_fh>;
close $read_fh or die 'cannot close';
is_deeply(\@lines, \@read, "$filename correctly written ($method)");
}
sub check_compressed {
my ($filename, $method) = @_;
open my $read_fh, '-|', 'zcat', "$tmpdir/myfile.gz"
or die 'cannot fork zcat';
my @read = <$read_fh>;
close $read_fh or die 'cannot close';
is_deeply(\@lines, \@read, "$filename correctly written ($method)");
}
sub test_read {
my $filename = shift;
$fh = Dpkg::Compression::FileHandle->new();
open($fh, '<', $filename) or die 'open failed';
my @read = <$fh>;
close $fh or die 'close failed';
is_deeply(\@lines, \@read, "$filename correctly read (std functions)");
@read = ();
$fh = Dpkg::Compression::FileHandle->new();
$fh->open($filename, 'r') or die 'open failed';
@read = $fh->getlines();
$fh->close() or die 'close failed';
is_deeply(\@lines, \@read, "$filename correctly read (IO::Handle methods)");
}
# Test changing the default compression levels
my $old_level = compression_get_default_level();
compression_set_default_level(1);
is(compression_get_default_level(), 1, 'change default compression level');
compression_set_default_level(5);
is(compression_get_default_level(), 5, 'change default compression level');
compression_set_default_level(undef);
is(compression_get_default_level(), $old_level, 'reset default compression level');
# Test write on uncompressed file
test_write("$tmpdir/myfile", \&check_uncompressed);
# Test write on compressed file
test_write("$tmpdir/myfile.gz", \&check_compressed);
# Test read on uncompressed file
test_read("$tmpdir/myfile");
# Test read on compressed file
test_read("$tmpdir/myfile.gz");
|