Blame t/19nonpv.t

Packit d03632
BEGIN {
Packit d03632
    if ($ENV{PERL_CORE}) {
Packit d03632
	chdir 't' if -d 't';
Packit d03632
	@INC = ("../lib", "lib/compress");
Packit d03632
    }
Packit d03632
}
Packit d03632
Packit d03632
use lib qw(t t/compress);
Packit d03632
use strict;
Packit d03632
use warnings;
Packit d03632
Packit d03632
use Test::More ;
Packit d03632
use CompTestUtils;
Packit d03632
Packit d03632
BEGIN 
Packit d03632
{ 
Packit d03632
    # use Test::NoWarnings, if available
Packit d03632
    my $extra = 0 ;
Packit d03632
    $extra = 1
Packit d03632
        if eval { require Test::NoWarnings ;  import Test::NoWarnings; 1 };
Packit d03632
Packit d03632
    plan tests => 38 + $extra ;
Packit d03632
Packit d03632
    use_ok('Compress::Raw::Zlib', 2) ;
Packit d03632
}
Packit d03632
 
Packit d03632
Packit d03632
Packit d03632
my $hello = <
Packit d03632
hello world
Packit d03632
this is a test
Packit d03632
EOM
Packit d03632
Packit d03632
my $len   = length $hello ;
Packit d03632
Packit d03632
# Check zlib_version and ZLIB_VERSION are the same.
Packit d03632
SKIP: {
Packit d03632
    skip "TEST_SKIP_VERSION_CHECK is set", 1 
Packit d03632
        if $ENV{TEST_SKIP_VERSION_CHECK};
Packit d03632
    is Compress::Raw::Zlib::zlib_version, ZLIB_VERSION,
Packit d03632
        "ZLIB_VERSION matches Compress::Raw::Zlib::zlib_version" ;
Packit d03632
}
Packit d03632
Packit d03632
Packit d03632
{
Packit d03632
    title 'non-PV dictionary';
Packit d03632
    # ==============================
Packit d03632
Packit d03632
    my $dictionary = *hello ;
Packit d03632
Packit d03632
    ok my $x = new Compress::Raw::Zlib::Deflate({-Level => Z_BEST_COMPRESSION,
Packit d03632
			     -Dictionary => $dictionary}) ;
Packit d03632
 
Packit d03632
    my $dictID = $x->dict_adler() ;
Packit d03632
Packit d03632
    my ($X, $Y, $Z);
Packit d03632
    cmp_ok $x->deflate($hello, $X), '==', Z_OK;
Packit d03632
    cmp_ok $x->flush($Y), '==', Z_OK;
Packit d03632
    $X .= $Y ;
Packit d03632
 
Packit d03632
    ok my $k = new Compress::Raw::Zlib::Inflate(-Dictionary => $dictionary) ;
Packit d03632
 
Packit d03632
    cmp_ok $k->inflate($X, $Z), '==', Z_STREAM_END;
Packit d03632
    is $k->dict_adler(), $dictID;
Packit d03632
    is $hello, $Z ;
Packit d03632
Packit d03632
}
Packit d03632
Packit d03632
{
Packit d03632
Packit d03632
    title  "deflate/inflate - non-PV buffers";
Packit d03632
    # ==============================
Packit d03632
Packit d03632
    my $hello = *hello ;
Packit d03632
    my ($err, $x, $X, $status); 
Packit d03632
 
Packit d03632
    ok( ($x, $err) = new Compress::Raw::Zlib::Deflate, "Create deflate object" );
Packit d03632
    ok $x, "Compress::Raw::Zlib::Deflate ok" ;
Packit d03632
    cmp_ok $err, '==', Z_OK, "status is Z_OK" ;
Packit d03632
 
Packit d03632
    ok ! defined $x->msg() ;
Packit d03632
    is $x->total_in(), 0, "total_in() == 0" ;
Packit d03632
    is $x->total_out(), 0, "total_out() == 0" ;
Packit d03632
Packit d03632
    $X = *X;
Packit d03632
    my $Answer = '';
Packit d03632
    $status = $x->deflate($hello, $X) ;
Packit d03632
    $Answer .= $X ;
Packit d03632
     
Packit d03632
    cmp_ok $status, '==', Z_OK, "deflate returned Z_OK" ;
Packit d03632
    
Packit d03632
    $X = *X;
Packit d03632
    cmp_ok  $x->flush($X), '==', Z_OK, "flush returned Z_OK" ;
Packit d03632
    $Answer .= $X ;
Packit d03632
     
Packit d03632
    ok ! defined $x->msg()  ;
Packit d03632
    is $x->total_in(), length $hello, "total_in ok" ;
Packit d03632
    is $x->total_out(), length $Answer, "total_out ok" ;
Packit d03632
     
Packit d03632
    my $k;
Packit d03632
    ok(($k, $err) = new Compress::Raw::Zlib::Inflate);
Packit d03632
    ok $k, "Compress::Raw::Zlib::Inflate ok" ;
Packit d03632
    cmp_ok $err, '==', Z_OK, "status is Z_OK" ;
Packit d03632
 
Packit d03632
    ok ! defined $k->msg(), "No error messages" ;
Packit d03632
    is $k->total_in(), 0, "total_in() == 0" ;
Packit d03632
    is $k->total_out(), 0, "total_out() == 0" ;
Packit d03632
    my $GOT = '';
Packit d03632
    my $Z;
Packit d03632
    $Z = *Z;
Packit d03632
    my $Alen = length $Answer;
Packit d03632
    $status = $k->inflate($Answer, $Z) ;
Packit d03632
    $GOT .= $Z ;
Packit d03632
     
Packit d03632
    cmp_ok $status, '==', Z_STREAM_END, "Got Z_STREAM_END" ;
Packit d03632
    is $GOT, $hello, "uncompressed data matches ok" ;
Packit d03632
    ok ! defined $k->msg(), "No error messages" ;
Packit d03632
    is $k->total_in(), $Alen, "total_in ok" ;
Packit d03632
    is $k->total_out(), length $hello , "total_out ok";
Packit d03632
Packit d03632
Packit d03632
    ok(($k, $err) = new Compress::Raw::Zlib::Inflate);
Packit d03632
    ok $k, "Compress::Raw::Zlib::Inflate ok" ;
Packit d03632
    cmp_ok $err, '==', Z_OK, "status is Z_OK" ;
Packit d03632
Packit d03632
    $Z = *Z;
Packit d03632
    $status = $k->inflate($hello, $Z);
Packit d03632
    is $Z, "", 'inflating *hello does not crash';
Packit d03632
Packit d03632
    $hello = *hello;
Packit d03632
    $status = $k->inflateSync($hello);
Packit d03632
    cmp_ok $status, "!=", Z_OK,
Packit d03632
       "inflateSync on *hello returns error (and does not crash)";
Packit d03632
}
Packit d03632