Upgrade to Time-HiRes-1.9707
[p5sagit/p5-mst-13.2.git] / ext / IO / Compress / Zlib / t / 050interop-gzip.t
CommitLineData
25f0751f 1BEGIN {
2 if ($ENV{PERL_CORE}) {
3 chdir 't' if -d 't';
4 @INC = ("../lib", "lib/compress");
5 }
6}
7
8use lib qw(t t/compress);
9use strict;
10use warnings;
11use bytes;
12
13use Test::More ;
25139ca2 14use CompTestUtils;
25f0751f 15
16my $GZIP ;
17
25f0751f 18
25139ca2 19sub ExternalGzipWorks
20{
21 my $lex = new LexFile my $outfile;
22 my $content = qq {
23Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut tempus odio id
24 dolor. Camelus perlus. Larrius in lumen numen. Dolor en quiquum filia
25 est. Quintus cenum parat.
26};
25f0751f 27
25139ca2 28 writeWithGzip($outfile, $content)
29 or return 0;
25f0751f 30
25139ca2 31 my $got ;
32 readWithGzip($outfile, $got)
33 or return 0;
25f0751f 34
25139ca2 35 if ($content ne $got)
36 {
37 diag "Uncompressed content is wrong";
38 return 0 ;
39 }
25f0751f 40
25139ca2 41 return 1 ;
25f0751f 42}
43
25f0751f 44sub readWithGzip
45{
46 my $file = shift ;
47
cb7abd7f 48 my $lex = new LexFile my $outfile;
49
25f0751f 50 my $comp = "$GZIP -dc" ;
51
25139ca2 52 if ( system("$comp $file >$outfile") == 0 )
53 {
54 $_[0] = readFile($outfile);
55 return 1
56 }
25f0751f 57
25139ca2 58 diag "'$comp' failed: $?";
59 return 0 ;
25f0751f 60}
61
62sub getGzipInfo
63{
64 my $file = shift ;
65}
66
67sub writeWithGzip
68{
69 my $file = shift ;
70 my $content = shift ;
71 my $options = shift || '';
72
cb7abd7f 73 my $lex = new LexFile my $infile;
74 writeFile($infile, $content);
75
25f0751f 76 unlink $file ;
25139ca2 77 my $comp = "$GZIP -c $options $infile >$file" ;
25f0751f 78
25139ca2 79 return 1
80 if system($comp) == 0 ;
81
82 diag "'$comp' failed: $?";
83 return 0 ;
84}
85
86BEGIN {
87
88 # Check external gzip is available
89 my $name = 'gzip';
90 for my $dir (reverse split ":", $ENV{PATH})
91 {
92 $GZIP = "$dir/$name"
93 if -x "$dir/$name" ;
94 }
95
96 plan(skip_all => "Cannot find $name")
97 if ! $GZIP ;
98
99 plan(skip_all => "$name doesn't work as expected")
100 if ! ExternalGzipWorks();
101
102
103 # use Test::NoWarnings, if available
104 my $extra = 0 ;
105 $extra = 1
106 if eval { require Test::NoWarnings ; import Test::NoWarnings; 1 };
107
108 plan tests => 7 + $extra ;
109
110 use_ok('IO::Compress::Gzip', ':all') ;
111 use_ok('IO::Uncompress::Gunzip', ':all') ;
25f0751f 112
25f0751f 113}
114
115
116{
117 title "Test interop with $GZIP" ;
118
119 my $file;
120 my $file1;
121 my $lex = new LexFile $file, $file1;
44033ed4 122 my $content = qq {
123Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut tempus odio id
124 dolor. Camelus perlus. Larrius in lumen numen. Dolor en quiquum filia
125 est. Quintus cenum parat.
126};
25f0751f 127 my $got;
128
25139ca2 129 ok writeWithGzip($file, $content), "writeWithGzip ok";
25f0751f 130
131 gunzip $file => \$got ;
cb7abd7f 132 is $got, $content, "got content";
25f0751f 133
134
135 gzip \$content => $file1;
136 $got = '';
25139ca2 137 ok readWithGzip($file1, $got), "readWithGzip ok";
25f0751f 138 is $got, $content, "got content";
139}
140
141