1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
#!perl -w
use strict;
no strict "vars";
use Bit::Vector;
# ======================================================================
# $vector->increment();
# $vector->decrement();
# ======================================================================
print "1..5296\n";
$n = 1;
$bits = 10;
$limit = (1 << $bits) - 1;
$k = 0;
$test_vector = bitvector($bits,$k);
for ( $i = 0; $i <= $limit; $i++ )
{
if ($k++ == $limit) { $k = 0; }
$ref_carry = ($test_vector->Norm() == $bits);
$test_carry = $test_vector->increment();
if ($test_carry == $ref_carry)
{print "ok $n\n";} else {print "not ok $n\n";}
$n++;
$ref_vector = bitvector($bits,$k);
if ($test_vector->equal($ref_vector))
{print "ok $n\n";} else {print "not ok $n\n";}
$n++;
}
$k = $limit;
$test_vector = bitvector($bits,$k);
for ( $i = $limit; $i >= 0; $i-- )
{
if ($k-- == 0) { $k = $limit; }
$ref_carry = ($test_vector->Norm() == 0);
$test_carry = $test_vector->decrement();
if ($test_carry == $ref_carry)
{print "ok $n\n";} else {print "not ok $n\n";}
$n++;
$ref_vector = bitvector($bits,$k);
if ($test_vector->equal($ref_vector))
{print "ok $n\n";} else {print "not ok $n\n";}
$n++;
}
$bits = 2000;
$upper = 150;
$lower = -150;
$k = $lower;
$test_vector = bitvector($bits,$k);
while (++$k <= $upper)
{
$ref_carry = ($test_vector->Norm() == $bits);
$test_carry = $test_vector->increment();
if ($test_carry == $ref_carry)
{print "ok $n\n";} else {print "not ok $n\n";}
$n++;
$ref_vector = bitvector($bits,$k);
if ($test_vector->equal($ref_vector))
{print "ok $n\n";} else {print "not ok $n\n";}
$n++;
}
$k = $upper;
$test_vector = bitvector($bits,$k);
while (--$k >= $lower)
{
$ref_carry = ($test_vector->Norm() == 0);
$test_carry = $test_vector->decrement();
if ($test_carry == $ref_carry)
{print "ok $n\n";} else {print "not ok $n\n";}
$n++;
$ref_vector = bitvector($bits,$k);
if ($test_vector->equal($ref_vector))
{print "ok $n\n";} else {print "not ok $n\n";}
$n++;
}
exit;
sub bitvector
{
my($bits,$value) = @_;
my($vector,$bit);
$vector = Bit::Vector->new($bits);
if ($value < 0)
{
$value = -1 - $value;
$vector->Fill();
}
$bit = 0;
while ($value)
{
if ($value & 1) { $vector->bit_flip($bit); }
$value >>= 1;
$bit++;
}
return($vector);
}
__END__
|