Subject: | strange behavior with to_Dec() and to_Hex() |
I am not sure, if the problem might just be a misunderstanding of mine.
But maybe even then, a more consistent error handling would help.
On perl 5.16.2 with Bit::Vector-7.2 I noticed the following:
$v = Bit::Vector->new_Bin(2, "10");
$d = $v->to_Dec();
$h = $v->to_Hex();
$b = $v->to_Bin();
In this test, $d == -2 and $h == 2 and $b eq "10". $h and $b are as I
would expect, $d is different. This problem stays the same at any bit
length that I tested. If the first bit is set to "1", to_Dec() returns a
wrong value.
In case that the bit size is calculated different, e.g. starting with
"0" for a (naturally counted) length of 1, there should be a consistent
error across all to_*-calls.
Attached you will find a test failing with 6/36 subtests. From my
understanding, all should pass.
Subject: | 41___conversion.t |
#!perl -w
use strict;
no strict "vars";
use Bit::Vector;
my @l = ("0", "1", "00", "01", "10", "11");
print "1..", @l * 6, "\n";
my ($v, $w, $x, $y);
my $n = 1;
foreach my $b (@l){
$x = sprintf("Bit::Vector->new_Bin(%d, 0b%s)", length($b), $b);
eval{$v = Bit::Vector->new_Bin(length($b), $b);};
unless($@)
{print "ok $n\n";} else {print "not ok $n\n"; warn("Could not create " . $x);}
$n++;
$y = sprintf("to_Dec() == %d", bin2dec($b));
if($v->to_Dec == bin2dec($b))
{print "ok $n\n";} else {print "not ok $n\n"; warn($x, "; ", $y);}
$n++;
$y = sprintf("to_Hex() == %s", bin2hex($b));
if($v->to_Hex eq bin2hex($b))
{print "ok $n\n";} else {print "not ok $n\n"; warn($x, "; ", $y);}
$n++;
$x = sprintf("Bit::Vector->new_Dec(%d, %d)", length($b), bin2dec($b));
eval{$v = Bit::Vector->new_Dec(length($b), bin2dec($b));};
unless($@)
{print "ok $n\n";} else {print "not ok $n\n"; warn("Could not create " . $x);}
$n++;
$y = sprintf("to_Dec() == %d", bin2dec($b));
if($v->to_Dec == bin2dec($b))
{print "ok $n\n";} else {print "not ok $n\n"; warn($x, "; ", $y);}
$n++;
$y = sprintf("to_Hex() == %s", bin2hex($b));
if($v->to_Hex eq bin2hex($b))
{print "ok $n\n";} else {print "not ok $n\n"; warn($x, "; ", $y);}
$n++;
}
sub bin2dec {
return unpack("N", pack("B32", substr("0" x 32 . shift, -32)));
}
sub bin2hex {
return sprintf("%x", bin2dec(shift));
}