|
Packit |
cf0d07 |
BEGIN {
|
|
Packit |
cf0d07 |
$tests = 31;
|
|
Packit |
cf0d07 |
if ($ENV{'WITH_CRC64'}) {
|
|
Packit |
cf0d07 |
$tests=$tests+2;
|
|
Packit |
cf0d07 |
}
|
|
Packit |
cf0d07 |
$| = 1;
|
|
Packit |
cf0d07 |
|
|
Packit |
cf0d07 |
eval "use Test::More tests => $tests";
|
|
Packit |
cf0d07 |
|
|
Packit |
cf0d07 |
$@ and eval <<'ENDEV';
|
|
Packit |
cf0d07 |
$ok = 1;
|
|
Packit |
cf0d07 |
|
|
Packit |
cf0d07 |
print "1..$tests\n";
|
|
Packit |
cf0d07 |
|
|
Packit |
cf0d07 |
sub ok {
|
|
Packit |
cf0d07 |
my($res,$comment) = @_;
|
|
Packit |
cf0d07 |
defined $comment and print "# $comment\n";
|
|
Packit |
cf0d07 |
$res or print "not ";
|
|
Packit |
cf0d07 |
print "ok ", $ok++, "\n";
|
|
Packit |
cf0d07 |
}
|
|
Packit |
cf0d07 |
ENDEV
|
|
Packit |
cf0d07 |
}
|
|
Packit |
cf0d07 |
|
|
Packit |
cf0d07 |
use Digest::CRC qw(crc64 crc32 crc16 crcccitt crc8 crcopenpgparmor
|
|
Packit |
cf0d07 |
crc64_hex crc32_hex crc16_hex crcccitt_hex crc8_hex crcopenpgparmor_hex);
|
|
Packit |
cf0d07 |
ok(1, 'use');
|
|
Packit |
cf0d07 |
|
|
Packit |
cf0d07 |
my $input = "123456789";
|
|
Packit |
cf0d07 |
my ($crc32,$crc16,$crcccitt,$crc8) = (crc32($input),crc16($input),crcccitt($input),crc8($input));
|
|
Packit |
cf0d07 |
|
|
Packit |
cf0d07 |
if ($ENV{'WITH_CRC64'}) {
|
|
Packit |
cf0d07 |
my $crc64 = crc64($input);
|
|
Packit |
cf0d07 |
ok($crc64 == 5090661014116757502, 'crc64 '.$crc64);
|
|
Packit |
cf0d07 |
$ctx = Digest::CRC->new(type=>"crc64");
|
|
Packit |
cf0d07 |
$ctx->add($input);
|
|
Packit |
cf0d07 |
$crc64 = $ctx->digest;
|
|
Packit |
cf0d07 |
ok($crc64 == 5090661014116757502, 'OO crc64 '.$crc64);
|
|
Packit |
cf0d07 |
}
|
|
Packit |
cf0d07 |
|
|
Packit |
cf0d07 |
ok($crc32 == 3421780262, 'crc32');
|
|
Packit |
cf0d07 |
$crc32=$crc32^0xffffffff;
|
|
Packit |
cf0d07 |
ok(crc32($input.join('',
|
|
Packit |
cf0d07 |
map {chr(($crc32>>(8*$_))&0xff)} (0,1,2,3))) == 0xffffffff,
|
|
Packit |
cf0d07 |
'crc32 Nulltest');
|
|
Packit |
cf0d07 |
ok($crcccitt == 10673, 'crcccitt');
|
|
Packit |
cf0d07 |
ok($crc16 == 47933, 'crc16');
|
|
Packit |
cf0d07 |
ok($crc8 == 244, 'crc8');
|
|
Packit |
cf0d07 |
ok(($crc8=crc8($input.chr($crc8))) == 0, 'crc8 Nulltest');
|
|
Packit |
cf0d07 |
my $ctx; $ctx = Digest::CRC->new();
|
|
Packit |
cf0d07 |
$ctx->add($input);
|
|
Packit |
cf0d07 |
ok($ctx->digest == 3421780262, 'OO crc32');
|
|
Packit |
cf0d07 |
|
|
Packit |
cf0d07 |
$crc32=$crc32^0xffffffff;
|
|
Packit |
cf0d07 |
|
|
Packit |
cf0d07 |
|
|
Packit |
cf0d07 |
# addfile
|
|
Packit |
cf0d07 |
open(F,"
|
|
Packit |
cf0d07 |
$ctx->addfile(F);
|
|
Packit |
cf0d07 |
close(F);
|
|
Packit |
cf0d07 |
my $y = $ctx->digest;
|
|
Packit |
cf0d07 |
ok($y == 4009136024, 'OO crc32 with addfile '.$y);
|
|
Packit |
cf0d07 |
|
|
Packit |
cf0d07 |
# start at offset >0 with previous checksum result
|
|
Packit |
cf0d07 |
$ctx = Digest::CRC->new(type=>"crc32",cont=>1,init=>460478609);
|
|
Packit |
cf0d07 |
open(F,"
|
|
Packit |
cf0d07 |
use Fcntl qw(:seek);
|
|
Packit |
cf0d07 |
seek(F,989,Fcntl::SEEK_SET);
|
|
Packit |
cf0d07 |
$ctx->addfile(F);
|
|
Packit |
cf0d07 |
close(F);
|
|
Packit |
cf0d07 |
$y = $ctx->digest;
|
|
Packit |
cf0d07 |
ok($y == 2371909219, 'OO crc32 with init and addfile '.$y);
|
|
Packit |
cf0d07 |
|
|
Packit |
cf0d07 |
$ctx = Digest::CRC->new(type=>"crcccitt");
|
|
Packit |
cf0d07 |
$ctx->add($input);
|
|
Packit |
cf0d07 |
ok($ctx->digest == 10673, 'OO crcccitt');
|
|
Packit |
cf0d07 |
|
|
Packit |
cf0d07 |
$ctx = Digest::CRC->new(type=>"crc16");
|
|
Packit |
cf0d07 |
$ctx->add($input);
|
|
Packit |
cf0d07 |
ok($ctx->digest == 47933, 'OO crc16');
|
|
Packit |
cf0d07 |
|
|
Packit |
cf0d07 |
$ctx = Digest::CRC->new(width=>16,init=>0,xorout=>0,refout=>1,poly=>0x3456,
|
|
Packit |
cf0d07 |
refin=>1,cont=>0);
|
|
Packit |
cf0d07 |
$ctx->add($input);
|
|
Packit |
cf0d07 |
ok($ctx->digest == 12803, 'OO crc16 poly 3456');
|
|
Packit |
cf0d07 |
|
|
Packit |
cf0d07 |
$ctx = Digest::CRC->new(type=>"crc8");
|
|
Packit |
cf0d07 |
$ctx->add($input);
|
|
Packit |
cf0d07 |
ok($ctx->digest == 244, 'OO crc8');
|
|
Packit |
cf0d07 |
|
|
Packit |
cf0d07 |
# crc8 test from Mathis Moder <mathis@pixelconcepts.de>
|
|
Packit |
cf0d07 |
$ctx = Digest::CRC->new(width=>8, init=>0xab, xorout=>0x00, refout=>0, poly=>0x07,
|
|
Packit |
cf0d07 |
refin=>0, cont=>0);
|
|
Packit |
cf0d07 |
$ctx->add($input);
|
|
Packit |
cf0d07 |
ok($ctx->digest == 135, 'OO crc8 init=ab');
|
|
Packit |
cf0d07 |
|
|
Packit |
cf0d07 |
$ctx = Digest::CRC->new(width=>8, init=>0xab, xorout=>0xff, refout=>1, poly=>0x07,
|
|
Packit |
cf0d07 |
refin=>1, cont=>0);
|
|
Packit |
cf0d07 |
$ctx->add("f1");
|
|
Packit |
cf0d07 |
ok($ctx->digest == 106, 'OO crc8 init=ab, refout');
|
|
Packit |
cf0d07 |
|
|
Packit |
cf0d07 |
$input = join '', 'aa'..'zz';
|
|
Packit |
cf0d07 |
($crc32,$crc16,$crcccitt,$crc8) = (crc32($input),crc16($input),crcccitt($input),crc8($input));
|
|
Packit |
cf0d07 |
|
|
Packit |
cf0d07 |
# some more large messages
|
|
Packit |
cf0d07 |
ok($crc32 == 0xCDA63E54, 'crc32');
|
|
Packit |
cf0d07 |
ok($crcccitt == 0x9702, 'crcccitt');
|
|
Packit |
cf0d07 |
ok($crc16 == 0x0220, 'crc16');
|
|
Packit |
cf0d07 |
ok($crc8 == 0x82, 'crc8');
|
|
Packit |
cf0d07 |
|
|
Packit |
cf0d07 |
# hex digest
|
|
Packit |
cf0d07 |
my $hexinput = "ae";
|
|
Packit |
cf0d07 |
($crc32,$crc16,$crcccitt,$crc8,$crcopenpgparmor) = (crc32_hex($hexinput),crc16_hex($hexinput),crcccitt_hex($hexinput),crc8_hex($hexinput),crcopenpgparmor_hex($hexinput));
|
|
Packit |
cf0d07 |
ok($crc32 == "00e7ddce", 'crc32_hex'); # width padding to 4 bytes
|
|
Packit |
cf0d07 |
ok($crcccitt == "1917", 'crcccitt_hex');
|
|
Packit |
cf0d07 |
ok($crc16 == "bbe9", 'crc16_hex');
|
|
Packit |
cf0d07 |
ok($crc8 == "dc", 'crc8_hex');
|
|
Packit |
cf0d07 |
ok($crcopenpgparmor == "3e653a", 'crcopenpgparmor_hex');
|
|
Packit |
cf0d07 |
|
|
Packit |
cf0d07 |
$ctx = Digest::CRC->new(type=>"crc8");
|
|
Packit |
cf0d07 |
$ctx->add($hexinput);
|
|
Packit |
cf0d07 |
ok($ctx->hexdigest == "dc", 'OO crc8 hex');
|
|
Packit |
cf0d07 |
$ctx = Digest::CRC->new(type=>"crc16");
|
|
Packit |
cf0d07 |
$ctx->add($hexinput);
|
|
Packit |
cf0d07 |
ok($ctx->hexdigest == "bbe9", 'OO crc16 hex');
|
|
Packit |
cf0d07 |
$ctx = Digest::CRC->new(type=>"crc32");
|
|
Packit |
cf0d07 |
$ctx->add($hexinput);
|
|
Packit |
cf0d07 |
ok($ctx->hexdigest == "00e7ddce", 'OO crc32 hex');
|
|
Packit |
cf0d07 |
$ctx = Digest::CRC->new(type=>"crcccitt");
|
|
Packit |
cf0d07 |
$ctx->add($hexinput);
|
|
Packit |
cf0d07 |
ok($ctx->hexdigest == "1917", 'OO crcccitt hex');
|
|
Packit |
cf0d07 |
$ctx = Digest::CRC->new(type=>"crcopenpgparmor");
|
|
Packit |
cf0d07 |
$ctx->add($hexinput);
|
|
Packit |
cf0d07 |
ok($ctx->hexdigest == "3e653a", 'OO crcopenpgparmor hex');
|
|
Packit |
cf0d07 |
|
|
Packit |
cf0d07 |
# openpgparmor
|
|
Packit |
cf0d07 |
my $openpgparmor = crcopenpgparmor($input);
|
|
Packit |
cf0d07 |
ok($openpgparmor == 4874579, 'openpgparmor '.$openpgparmor);
|