Data-CompactReadonly

 view release on metacpan or  search on metacpan

t/root-node-array.t  view on Meta::CPAN

use strict;
use warnings;
no warnings qw(portable);

use Test::More;
use Test::Differences;
use Test::Exception;

use File::Temp qw(tempfile);
use String::Binary::Interpolation;

use Data::CompactReadonly;

my $header_bytes = "CROD\x00"; # version 0, byte pointers

open(my $fh, '<', \"$header_bytes${b01000000}\x00");
isa_ok(
    my $array = Data::CompactReadonly->read($fh),
    "Data::CompactReadonly::V0::Array::Byte"
);
is($array->count(), 0, "empty array");
is($array->_ptr_size(), 1, "1 byte pointers");
eq_or_diff($array->indices(), [], "can list collection indices");
throws_ok { $array->element(-1) }
    qr/Invalid element: -1: negative/,
    "negative elements are illegal";
throws_ok { $array->element(3.7) }
    qr/Invalid element: 3.7: non-integer/,
    "non-integer elements are illegal";
throws_ok { $array->element('horse') }
    qr/Invalid element: horse: non-integer/,
    "non-numeric elements are illegal";
throws_ok { $array->element(0) }
    qr/Invalid element: 0: out of range/,
    "trying to read beyond end of empty array is fatal";

my $ARRAYBYTE   = $b01000000;
my $ARRAYSHORT  = $b01001000;
my $ARRAYMEDIUM = $b01010000;
my $ARRAYLONG   = $b01011000;
my $TEXTBYTE    = $b00000000;
my $DICTBYTE    = $b10000000;
my $BYTE        = $b11000000;
my $SHORT       = $b11001000;
my $MEDIUM      = $b11010000;
my $LONG        = $b11011000;
my $HUGE        = $b11100000;
my $NULL        = $b11101000;

open($fh, '<', \(
    "\x00\x00".               # these don't count, we'll seek past them before we start
    "$header_bytes".          # 0x00
    "$ARRAYBYTE\x02\x0c\x09". # 0x05 A::Byte, two pointers, dests in reverse order of index
    "$SHORT\x94\x45".         # 0x09 Short, 0x9445
    "$BYTE\x94"               # 0x0c Byte,  0x94
));
read($fh, my $blah, 2);
$array = Data::CompactReadonly->read($fh);
is($array->_db_base(), 2, "the fh was opened after having already been partially read");
isa_ok($array, 'Data::CompactReadonly::V0::Array::Byte');
is($array->count(), 2, "2 element array");
throws_ok { $array->element(94) }
    qr/Invalid element: 94: out of range/,
    "trying to read beyond end of non-empty array is fatal";
is($array->element(0), 0x94, "fetched a Byte element from the array");
is($array->element(1), 0x9445, "fetched a Short element from the array");



( run in 1.358 second using v1.01-cache-2.11-cpan-13bb782fe5a )