1BEGIN { 2 $tests = 21; 3 if ($ENV{'WITH_CRC64'}) { 4 $tests=$tests+2; 5 } 6 $| = 1; 7 8 eval "use Test::More tests => $tests"; 9 10 $@ and eval <<'ENDEV'; 11$ok = 1; 12 13print "1..$tests\n"; 14 15sub ok { 16 my($res,$comment) = @_; 17 defined $comment and print "# $comment\n"; 18 $res or print "not "; 19 print "ok ", $ok++, "\n"; 20} 21ENDEV 22} 23 24use Digest::CRC qw(crc64 crc32 crc16 crcccitt crc8 crcopenpgparmor); 25ok(1, 'use'); 26 27my $input = "123456789"; 28my ($crc32,$crc16,$crcccitt,$crc8) = (crc32($input),crc16($input),crcccitt($input),crc8($input)); 29 30if ($ENV{'WITH_CRC64'}) { 31 my $crc64 = crc64($input); 32 ok($crc64 == 5090661014116757502, 'crc64 '.$crc64); 33 $ctx = Digest::CRC->new(type=>"crc64"); 34 $ctx->add($input); 35 $crc64 = $ctx->digest; 36 ok($crc64 == 5090661014116757502, 'OO crc64 '.$crc64); 37} 38 39ok($crc32 == 3421780262, 'crc32'); 40$crc32=$crc32^0xffffffff; 41ok(crc32($input.join('', 42 map {chr(($crc32>>(8*$_))&0xff)} (0,1,2,3))) == 0xffffffff, 43 'crc32 Nulltest'); 44ok($crcccitt == 10673, 'crcccitt'); 45ok($crc16 == 47933, 'crc16'); 46ok($crc8 == 244, 'crc8'); 47ok(($crc8=crc8($input.chr($crc8))) == 0, 'crc8 Nulltest'); 48my $ctx; $ctx = Digest::CRC->new(); 49$ctx->add($input); 50ok($ctx->digest == 3421780262, 'OO crc32'); 51 52$crc32=$crc32^0xffffffff; 53 54 55# addfile 56open(F,"<README")||die "Cannot open README"; 57$ctx->addfile(F); 58close(F); 59my $y = $ctx->digest; 60ok($y == 1662879226, 'OO crc32 with addfile '.$y); 61 62# start at offset >0 with previous checksum result 63$ctx = Digest::CRC->new(type=>"crc32",cont=>1,init=>460478609); 64open(F,"<README")||die "Cannot open README"; 65use Fcntl qw(:seek); 66seek(F,989,Fcntl::SEEK_SET); 67$ctx->addfile(F); 68close(F); 69$y = $ctx->digest; 70ok($y == 2371909219, 'OO crc32 with init and addfile '.$y); 71 72$ctx = Digest::CRC->new(type=>"crcccitt"); 73$ctx->add($input); 74ok($ctx->digest == 10673, 'OO crcccitt'); 75 76$ctx = Digest::CRC->new(type=>"crc16"); 77$ctx->add($input); 78ok($ctx->digest == 47933, 'OO crc16'); 79 80$ctx = Digest::CRC->new(width=>16,init=>0,xorout=>0,refout=>1,poly=>0x3456, 81 refin=>1,cont=>0); 82$ctx->add($input); 83ok($ctx->digest == 12803, 'OO crc16 poly 3456'); 84 85$ctx = Digest::CRC->new(type=>"crc8"); 86$ctx->add($input); 87ok($ctx->digest == 244, 'OO crc8'); 88 89# crc8 test from Mathis Moder <mathis@pixelconcepts.de> 90$ctx = Digest::CRC->new(width=>8, init=>0xab, xorout=>0x00, refout=>0, poly=>0x07, 91 refin=>0, cont=>0); 92$ctx->add($input); 93ok($ctx->digest == 135, 'OO crc8 init=ab'); 94 95$ctx = Digest::CRC->new(width=>8, init=>0xab, xorout=>0xff, refout=>1, poly=>0x07, 96 refin=>1, cont=>0); 97$ctx->add("f1"); 98ok($ctx->digest == 106, 'OO crc8 init=ab, refout'); 99 100$input = join '', 'aa'..'zz'; 101($crc32,$crc16,$crcccitt,$crc8) = (crc32($input),crc16($input),crcccitt($input),crc8($input)); 102 103# some more large messages 104ok($crc32 == 0xCDA63E54, 'crc32'); 105ok($crcccitt == 0x9702, 'crcccitt'); 106ok($crc16 == 0x0220, 'crc16'); 107ok($crc8 == 0x82, 'crc8'); 108 109# openpgparmor 110my $openpgparmor = crcopenpgparmor($input); 111ok($openpgparmor == 4874579, 'openpgparmor '.$openpgparmor); 112