summaryrefslogtreecommitdiff
path: root/t/040-memory.t
blob: a89fc87a971a44f15e8b01af63d5cfaebc125e04 (plain)
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
# -*- mode: perl -*-

use Test::More tests => 15;

BEGIN {
  use_ok('Compress::Bzip2', qw(:utilities :bzip1));
};


my $string = q/
Twas brillig and the slithy toves
did gire and gimble in the wabe
All mimsey were the borogroves
and the Momewrathes outgrabe
    /;

my $compress = memBzip( $string );
my $uncompress = memBunzip( $compress );

ok( substr($compress,5,16) =~ /^BZh/, "compressed starts with bzip magic header" );

ok( $compress ne $string, "string was not inouted" );
ok( length($compress)-10 < length($string), "string compression - ".length($compress).' vs '.length($string) );
ok( $uncompress eq $string, "uncompressed is same as the original" );

my $string10 = $string x 10;
my $compress10 = memBzip( $string10 );
my $uncompress10 = memBunzip( $compress10 );

ok( $compress10 ne $string10, "x10 string was not inouted" );
ok( length($compress10) < length($string10), "x10 string compression - ".length($compress10).' vs '.length($string10) );
ok( $uncompress10 eq $string10, "x10 uncompressed is same as the original" );

$compress = compress( $string );
$uncompress = decompress( $compress );

ok( $compress ne $string, "bzip1 string was not inouted" );
ok( length($compress)-10 < length($string), "bzip1 string compression - ".length($compress).' vs '.length($string) );
ok( $uncompress eq $string, "bzip1 decompress is same as the original" );

do 't/lib.pl';

# allow plain BZh files with memBunzip also
my $INFILE = catfile( qw(bzlib-src sample0.bz2) );
local $/ = undef;
open( IN, "< $INFILE" ) or die "$INFILE: $!";
binmode IN;
my $sample0 = <IN>;
close( IN );

$uncompress = memBunzip( $sample0 );
ok( $uncompress, "sample0 uncompressed w/o header" );
like( $uncompress, qr/^That\'s great, it starts with an earthquake/ );

my $header = pack("C", 0xf0);
$header .= pack "N", $uncompress ? length($uncompress) : 2027;
$uncompress = memBunzip( $header . $sample0 );
ok( $uncompress, "sample0 uncompressed w/ header" );
like( $uncompress, qr/^That\'s great, it starts with an earthquake/ );