Upgrade to Compress::Zlib 2.000_05
[p5sagit/p5-mst-13.2.git] / ext / Compress / Zlib / t / 05examples.t
CommitLineData
f4c6fd49 1
642e522c 2use lib 't';
f4c6fd49 3
642e522c 4use strict;
5use warnings;
6use bytes;
f4c6fd49 7
642e522c 8use Test::More ;
9use ZlibTestUtils;
10use Compress::Zlib;
667342e9 11
642e522c 12BEGIN
13{
14 plan(skip_all => "Examples needs Perl 5.005 or better - you have Perl $]" )
15 if $] < 5.005 ;
16
17 # use Test::NoWarnings, if available
18 my $extra = 0 ;
19 $extra = 1
20 if eval { require Test::NoWarnings ; import Test::NoWarnings; 1 };
21
22 plan tests => 30 + $extra ;
667342e9 23}
24
25
5993747c 26my $Inc = join " ", map qq["-I$_"] => @INC;
667342e9 27$Inc = '"-MExtUtils::testlib"'
642e522c 28 if ! $ENV{PERL_CORE} && eval " require ExtUtils::testlib; " ;
f4c6fd49 29
642e522c 30my $Perl = ($ENV{'FULLPERL'} or $^X or 'perl') ;
f4c6fd49 31$Perl = qq["$Perl"] if $^O eq 'MSWin32' ;
32
642e522c 33$Perl = "$Perl $Inc -w" ;
667342e9 34my $examples = $ENV{PERL_CORE} ? "../ext/Compress/Zlib/examples"
35 : "./examples";
f4c6fd49 36
37my $hello1 = <<EOM ;
38hello
39this is
40a test
41message
42x ttttt
43xuuuuuu
44the end
45EOM
46
47my @hello1 = grep(s/$/\n/, split(/\n/, $hello1)) ;
48
49my $hello2 = <<EOM;
50
51Howdy
52this is the
53second
54file
55x ppppp
56xuuuuuu
57really the end
58EOM
59
60my @hello2 = grep(s/$/\n/, split(/\n/, $hello2)) ;
61
f4c6fd49 62my $file1 = "hello1.gz" ;
63my $file2 = "hello2.gz" ;
667342e9 64my $stderr = "err.out" ;
65unlink $stderr ;
f4c6fd49 66
642e522c 67unlink $file1, $file2 ;
68
667342e9 69my $gz = gzopen($file1, "wb");
70$gz->gzwrite($hello1);
71$gz->gzclose();
f4c6fd49 72
667342e9 73$gz = gzopen($file2, "wb");
74$gz->gzwrite($hello2);
75$gz->gzclose();
f4c6fd49 76
642e522c 77sub check
78{
79 my $command = shift ;
80 my $expected = shift ;
81
82 my $stderr = 'err.out';
83 unlink $stderr;
84
85 my $cmd = "$command 2>$stderr";
86 my $stdout = `$cmd` ;
87
88 my $aok = 1 ;
f4c6fd49 89
642e522c 90 $aok &= is $?, 0, " exit status is 0" ;
f4c6fd49 91
642e522c 92 $aok &= is readFile($stderr), '', " no stderr" ;
93
94 $aok &= is $stdout, $expected, " expected content is ok"
95 if defined $expected ;
96
97 if (! $aok) {
98 diag "Command line: $cmd";
99 my ($file, $line) = (caller)[1,2];
100 diag "Test called from $file, line $line";
101 }
102
103 unlink $stderr;
104}
f4c6fd49 105
667342e9 106# gzcat
107# #####
108
642e522c 109title "gzcat.zlib" ;
110check "$Perl ${examples}/gzcat.zlib $file1 $file2 ", $hello1 . $hello2 ;
111
112title "gzcat - command line" ;
113check "$Perl ${examples}/gzcat $file1 $file2", $hello1 . $hello2;
114
115title "gzcat - stdin" ;
116check "$Perl ${examples}/gzcat <$file1 ", $hello1;
117
f4c6fd49 118
119# gzgrep
120# ######
121
642e522c 122title "gzgrep";
123check "$Perl ${examples}/gzgrep the $file1 $file2",
667342e9 124 join('', grep(/the/, @hello1, @hello2));
f4c6fd49 125
f4c6fd49 126unlink $file1, $file2 ;
127
128
129# filtdef/filtinf
130# ##############
131
132
f4c6fd49 133writeFile($file1, $hello1) ;
134writeFile($file2, $hello2) ;
135
642e522c 136title "filtdef" ;
f4c6fd49 137# there's no way to set binmode on backticks in Win32 so we won't use $a later
642e522c 138check "$Perl ${examples}/filtdef $file1 $file2" ;
f4c6fd49 139
642e522c 140title "filtdef | filtinf";
141check "$Perl ${examples}/filtdef $file1 $file2 | $Perl ${examples}/filtinf",
142 $hello1 . $hello2;
f4c6fd49 143# gzstream
144# ########
145
146{
642e522c 147 title "gzstream" ;
f4c6fd49 148 writeFile($file1, $hello1) ;
642e522c 149 check "$Perl ${examples}/gzstream <$file1 >$file2";
76e6f389 150
642e522c 151 title "gzcat" ;
152 check "$Perl ${examples}/gzcat $file2", $hello1 ;
f4c6fd49 153}
154
5993747c 155END
156{
157 for ($file1, $file2, $stderr) { 1 while unlink $_ } ;
158}
642e522c 159