Data-BitStream-XS
view release on metacpan or search on metacpan
examples/integercoding.pl view on Meta::CPAN
string of 0 and 1 characters representing the bit encoding of the integer
using that code.
$str = encode_unary(8); # die unless $str eq '000000001';
$str = encode_gamma(8); # die unless $str eq '0001000';
$str = encode_delta(8); # die unless $str eq '00100000';
$str = encode_omega(8); # die unless $str eq '1110000';
$str = encode_fib(8); # die unless $str eq '000011';
The C<decode_> methods take a single binary string as input and produce an
unsigned integer output decoded from the bit encoding.
$n = decode_unary('000000000000001'); # die unless $n == 14;
$n = decode_gamma('0001110'); # die unless $n == 14;
$n = decode_delta('00100110'); # die unless $n == 14;
$n = decode_omega('1111100'); # die unless $n == 14;
$n = decode_fib( '1000011'); # die unless $n == 14;
=head1 SEE ALSO
The CPAN module L<Data::BitStream> includes these codes and more.
t/72-rand-bad-gets.t view on Meta::CPAN
$s->write(3, 2); # 010 ends most codes
$s->put_string( '1' x (9*16*$nshorts+9) ); # Lots of 1s to end Rice/Golomb
$s->write_close;
foreach my $code (@encodings) {
# Set position to a little way in
$s->rewind; $s->skip(3); die "Position error" unless $s->pos == 3;
my $v;
eval { $v = $s->code_get($code); };
if ($@ eq '') {
# The random data is decoded as a value. Good for us.
isnt($v, undef, "Read a value with $code");
cmp_ok($s->pos, '>', 3, "Good $code read at least one bit");
} else {
# The only error we should see is a code error, and we expect the
# stream position to remain unchanged after the trapped read.
like($@, qr/code error/i, "$code trapped bad read");
is($s->pos, 3, "Bad $code read left position unchanged");
}
}
}
( run in 1.083 second using v1.01-cache-2.11-cpan-a9ef4e587e4 )