From: Quim Rovira Date: Sun, 14 Aug 2016 09:38:41 +0000 (+0200) Subject: Add tests for compression, and a CLI tool for manual testing. X-Git-Tag: 0.002~27 X-Git-Url: http://git.ieval.ro/?a=commitdiff_plain;h=2786a68d54239318b1ca7d6045f8a5830a979142;p=io-compress-brotli.git Add tests for compression, and a CLI tool for manual testing. I changed the names of the tests, so that compress round-trip runs after the uncompress tests. Also added a cli tool that tries to follow the official /bro/ cli tool, but it's use is mostly for validation and testing, and is still missing support for many of the flags (eg. --repeat). For this, I moved the File::Slurp module to an actual dependency, not a tests-only dep. --- diff --git a/Makefile.PL b/Makefile.PL index e2649e7..79ea79a 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -9,10 +9,12 @@ WriteMakefile( MIN_PERL_VERSION => '5.14.0', LICENSE => 'perl', SIGN => 1, - PREREQ_PM => {}, - BUILD_REQUIRES => { - qw/File::Slurp 0/, + EXE_FILES => [ 'bin/bro-perl' ], + PREREQ_PM => { + 'File::Slurp' => '9999.19', + 'Getopt::Long' => '2.45', }, + BUILD_REQUIRES => {}, INC => '-Ibrotli', MYEXTLIB => 'brotli/libbrotli$(LIB_EXT)', clean => { FILES => 'brotli/libbrotli$(LIB_EXT)' }, diff --git a/README b/README index 858d0d5..17f6a76 100644 --- a/README +++ b/README @@ -24,7 +24,8 @@ DEPENDENCIES This module requires these other modules and libraries: -* File::Slurp (only used for testing) +* File::Slurp +* Getopt::Long Additionally, it requires a C compiler. diff --git a/bin/bro-perl b/bin/bro-perl new file mode 100755 index 0000000..89a4e00 --- /dev/null +++ b/bin/bro-perl @@ -0,0 +1,75 @@ +#!perl +# + +use warnings; +use strict; +use 5.014; + +use File::Slurp; +use Getopt::Long; + +use IO::Compress::Brotli; +use IO::Uncompress::Brotli; + +GetOptions( + 'c|custom-dictionary=s' => \(my $DICTIONARY), + 'd|decompress' => \(my $DECOMPRESS), + 'h|help' => \(my $HELP), + 'i|input=s' => \(my $INPUT), + 'o|output=s' => \(my $OUTPUT), + 'q|quality=i' => \(my $QUALITY = 11), + 'r|repeat=i' => \(my $REPEAT), + 's|stream=i' => \(my $STREAM), + 'v|verbose' => \(my $VERBOSE), + 'w|window=i' => \(my $WINDOW = 22), +); + +if( $HELP ) { + say "Usage: $0 [--force] [--quality n] [--decompress] [--input filename] [--output filename]". + " [--repeat iters] [--verbose] [--window n] [--custom-dictionary filename] [--stream size]"; + exit 1; +} + +my ($ifh, $ofh) = (\*STDIN, \*STDOUT); + +if( $INPUT ) { + open $ifh, "<", $INPUT + or die "Cannot open input file $INPUT.\n"; +} +binmode $ifh; + +if( $OUTPUT ) { + open $ofh, ">", $OUTPUT + or die "Cannot open output file $OUTPUT.\n"; +} +binmode $ofh; + +if( $DECOMPRESS ) { + if( $STREAM ) { + my $brotli = IO::Uncompress::Brotli->create(); + while( read $ifh, (my $buf), $STREAM ) { + print $ofh $brotli->decompress($buf); + } + } + else { + my $encoded = read_file( $ifh ); + my $decoded = unbro( $encoded ); + write_file( $ofh, $decoded ); + } +} +else { + if( $STREAM ) { + my $brotli = IO::Compress::Brotli->create(); + $brotli->quality( $QUALITY ); + $brotli->window( $WINDOW ); + while( read $ifh, (my $buf), $STREAM ) { + print $ofh $brotli->compress($buf); + } + } + else { + my $decoded = read_file( $ifh ); + my $encoded = bro( $decoded, $QUALITY, $WINDOW ); + write_file( $ofh, $encoded ); + } +} + diff --git a/t/00-load.t b/t/00-load.t new file mode 100644 index 0000000..a0a534c --- /dev/null +++ b/t/00-load.t @@ -0,0 +1,10 @@ +#!/usr/bin/perl +use v5.14; +use warnings; + +use Test::More tests => 2; + +BEGIN { + use_ok('IO::Compress::Brotli'); + use_ok('IO::Uncompress::Brotli'); +} diff --git a/t/01-uncompress.t b/t/01-uncompress.t new file mode 100644 index 0000000..fe2fe3a --- /dev/null +++ b/t/01-uncompress.t @@ -0,0 +1,29 @@ +#!/usr/bin/perl +use v5.14; +use warnings; + +use Test::More tests => 80; +use File::Slurp; + +use IO::Uncompress::Brotli; + +my $todo_re = qr/empty\.compressed\.(?:1[7-9]|2)|x\.compressed\.0[12]/; + +for my $test () { + my ($expected) = $test =~ s/\.compressed.*//r; + $expected = read_file $expected; + + if($test !~ $todo_re) { + my $decoded = unbro (scalar read_file $test); + is $decoded, $expected, "$test"; + } + + open FH, '<', $test; + my $unbro = IO::Uncompress::Brotli->create; + my ($buf, $out); + until (eof FH) { + read FH, $buf, 100; + $out .= $unbro->decompress($buf); + } + is $out, $expected, "$test (streaming)"; +} diff --git a/t/02-roundtrip.t b/t/02-roundtrip.t new file mode 100644 index 0000000..16407d1 --- /dev/null +++ b/t/02-roundtrip.t @@ -0,0 +1,33 @@ +#!/usr/bin/perl +use v5.14; +use warnings; + +use Test::More tests => 114; +use File::Slurp; + +use IO::Compress::Brotli; +use IO::Uncompress::Brotli; + +for my $test () { + my ($source) = $test =~ s/\.compressed$//r; + $source = read_file $source; + + for my $quality (9,11) { + my $encoded = bro($source, $quality); + my $decoded = unbro($encoded); + + is $decoded, $source, "$test - quality $quality"; + } + + for my $quality (1,5,9,11) { + my $enc = IO::Compress::Brotli->create; + $enc->quality($quality); + my $encoded = $enc->compress($source, 1); + $encoded .= $enc->finish(); + + my $dec = IO::Uncompress::Brotli->create; + my $decoded = $dec->decompress($encoded); + + is $decoded, $source, "$test - streaming / quality $quality"; + } +} diff --git a/t/IO-Compress-Brotli.t b/t/IO-Compress-Brotli.t deleted file mode 100644 index c732120..0000000 --- a/t/IO-Compress-Brotli.t +++ /dev/null @@ -1,18 +0,0 @@ -# Before 'make install' is performed this script should be runnable with -# 'make test'. After 'make install' it should work as 'perl IO-Compress-Brotli.t' - -######################### - -# change 'tests => 1' to 'tests => last_test_to_print'; - -use strict; -use warnings; - -use Test::More tests => 1; -BEGIN { use_ok('IO::Compress::Brotli') }; - -######################### - -# Insert your test code below, the Test::More module is use()ed here so read -# its man page ( perldoc Test::More ) for help writing this test script. - diff --git a/t/IO-Uncompress-Brotli.t b/t/IO-Uncompress-Brotli.t deleted file mode 100644 index 94141c1..0000000 --- a/t/IO-Uncompress-Brotli.t +++ /dev/null @@ -1,28 +0,0 @@ -#!/usr/bin/perl -use v5.14; -use warnings; - -use Test::More tests => 81; -use File::Slurp; -BEGIN{ use_ok('IO::Uncompress::Brotli'); } - -my $todo_re = qr/empty\.compressed\.(?:1[7-9]|2)|x\.compressed\.0[12]/; - -for my $test () { - my ($expected) = $test =~ s/\.compressed.*//r; - $expected = read_file $expected; - - if($test !~ $todo_re) { - my $decoded = unbro (scalar read_file $test); - is $decoded, $expected, "$test"; - } - - open FH, '<', $test; - my $unbro = IO::Uncompress::Brotli->create; - my ($buf, $out); - until (eof FH) { - read FH, $buf, 100; - $out .= $unbro->decompress($buf); - } - is $out, $expected, "$test (streaming)"; -}