Compress-BGZF

 view release on metacpan or  search on metacpan

t/bgzf_fh.t  view on Meta::CPAN

#!/usr/bin/perl

use strict;
use warnings;

use Test::Fatal;
use Test::More qw/no_plan/;
use FindBin;
use Compress::BGZF;
use Compress::BGZF::Reader;
use Compress::BGZF::Writer;
use File::Temp qw/tempfile/;
use IO::Handle;

chdir $FindBin::Bin;

require_ok ("Compress::BGZF");

# check that compressed and uncompressed FHs return identical results
my @files = (
    'corpus/long_chunks',
    'corpus/lorem',
    'corpus/lorem_long',
);

for my $i (0..$#files) {

    my $file = $files[$i];

    # create a compressed version
    my ($tmp_fh, $tmp_fn) = tempfile('bgzfXXXXXXXX', SUFFIX => '.gz',
        TMPDIR => 1, UNLINK => 1);
    my $fh_w = Compress::BGZF::Writer->new_filehandle("$tmp_fn");
    open my $fh_u, '<:raw', $file;
    print {$fh_w} $_ while (<$fh_u>);
    close $fh_w;
    seek $fh_u, 0, 0;

    # initialize read pair
    my $fh_c = Compress::BGZF::Reader->new_filehandle($tmp_fn);
    ok (ref($fh_c) eq 'FileHandle', "returned filehandle FileHandle");
    my $buf_c;
    my $buf_u;
    my @inputs = ( [$fh_c, $buf_c], [$fh_u, $buf_u] );

    # perform as many possible types of seek/read as we can

    seek( $_->[0], 72000, 0 )   for @inputs;
    read( $_->[0], $_->[1], 9 ) for @inputs;
    ok ($inputs[0]->[1] eq $inputs[1]->[1], "seek/read check $i.1");

    ok (eof($inputs[0]->[0]) == eof($inputs[1]->[0]), "eof check $i.1");

    seek( $_->[0], 18, 1 )   for @inputs;
    read( $_->[0], $_->[1], 623, 5) for @inputs;;
    ok ($inputs[0]->[1] eq $inputs[1]->[1], "seek/read check $i.2");

    seek( $_->[0], -18, 2 )   for @inputs;
    read( $_->[0], $_->[1], 82, -2) for @inputs;;
    ok ($inputs[0]->[1] eq $inputs[1]->[1], "seek/read check $i.3");

    seek( $_->[0], 18, 2 )   for @inputs;
    read( $_->[0], $_->[1], 82, 0) for @inputs;;
    ok ($inputs[0]->[1] eq $inputs[1]->[1], "seek/read check $i.4");

    seek( $_->[0], 130000, 0 )   for @inputs;



( run in 0.847 second using v1.01-cache-2.11-cpan-54e63673c56 )