File: filter-lzwdecode.t

package info (click to toggle)
libpdf-builder-perl 3.027-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 23,992 kB
  • sloc: perl: 107,532; makefile: 10
file content (76 lines) | stat: -rw-r--r-- 2,895 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/perl
use warnings;
use strict;

use PDF::Builder::Basic::PDF::Utils;
use PDF::Builder::Basic::PDF::Filter::LZWDecode;
use Test::More tests => 9;
#-# 4 note() debugs presently commented out

my $filter = PDF::Builder::Basic::PDF::Filter::LZWDecode->new();
my $in = 'BT /F1 24 Tf 100 700 Td (Hello World)Tj ET';
my $out = $filter->outfilt($in);
is $filter->infilt($out), $in, 'LZWDecode test string round-tripped correctly';

###

my $repeat = 22;
#-#note( 'Test data size: ' . length($in)*$repeat );
$filter = PDF::Builder::Basic::PDF::Filter::LZWDecode->new();
$out = $filter->outfilt($in x $repeat);
#-#note( 'Final bits: '.$filter->{code_length} );
cmp_ok length($out), '<', length($in)*$repeat, "Data compresses smaller";

$filter = PDF::Builder::Basic::PDF::Filter::LZWDecode->new();
is $filter->infilt($out), $in x $repeat, 'Data decompresses unchanged at 10bit boundary';

###

$in = pack "H*", '8000000014040807050001e100f840fd00e0003fd00ff8a44e2b01';
my $expected = pack "H*", '00000180000003800000078000000f8000003f80e000ff80ffffff80ffffff80';
$filter = PDF::Builder::Basic::PDF::Filter::LZWDecode->new();
$out = $filter->infilt($in);
is $out, $expected, 'decompress binary data';

($in, $expected) = ($expected, $in);
$filter = PDF::Builder::Basic::PDF::Filter::LZWDecode->new();
$out = $filter->outfilt($in);
is $out, $expected, 'compress binary data';

###

$repeat = 30000;
$in = '';
for (0..$repeat) {$in .= chr(int(rand(256)))}
#-#note( 'Test data size: ' . length($in) );
$filter = PDF::Builder::Basic::PDF::Filter::LZWDecode->new();
$out = $filter->outfilt($in);
#-#note( 'Final bits: '.$filter->{'code_length'} );

$filter = PDF::Builder::Basic::PDF::Filter::LZWDecode->new();
is $filter->infilt($out), $in, 'Data decompresses unchanged after reaching max code length';

###

$in = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
my $height = 2;
$filter = PDF::Builder::Basic::PDF::Filter::LZWDecode->new({Predictor => PDFNum(2), Rows=>PDFNum($height), Columns=>PDFNum(length($in)/$height)});
$out = $filter->outfilt($in);
is $filter->infilt($out), $in, 'LZWDecode test string round-tripped correctly with horizontal predictor';

###

$in = '000FFF000000FFF000';
$height = 2;
my $colors = 3;
$filter = PDF::Builder::Basic::PDF::Filter::LZWDecode->new({Predictor => PDFNum(2), Rows=>PDFNum($height), Columns=>PDFNum(length($in)/$height/$colors), Colors=>PDFNum($colors)});
$out = $filter->outfilt($in);
is $filter->infilt($out), $in, 'LZWDecode test string round-tripped correctly with horizontal predictor + 3 color channels';

###

$in = pack "H*", '0000FFFFFFFF0000';
$expected = pack "H*", '0000FF00FF000100';
$filter = PDF::Builder::Basic::PDF::Filter::LZWDecode->new({Predictor => PDFNum(2), Rows=>PDFNum($height), Columns=>PDFNum(length($in)/$height)});
$out = $filter->_predict($in);
is $out, $expected, 'predict binary data with overflow';