1N/A#!/usr/bin/perl -w
1N/A
1N/Ause Test;
1N/Ause strict;
1N/A
1N/Amy $count;
1N/A
1N/ABEGIN
1N/A {
1N/A $| = 1;
1N/A if ($^O eq 'os390') { print "1..0\n"; exit(0) } # test takes too long there
1N/A unshift @INC, '../lib'; # for running manually
1N/A my $location = $0; $location =~ s/mbi_rand.t//;
1N/A unshift @INC, $location; # to locate the testing files
1N/A chdir 't' if -d 't';
1N/A $count = 128;
1N/A plan tests => $count*4;
1N/A }
1N/A
1N/Ause Math::BigInt;
1N/Amy $c = 'Math::BigInt';
1N/A
1N/Amy $length = 128;
1N/A
1N/A# If you get a failure here, please re-run the test with the printed seed
1N/A# value as input "perl t/mbi_rand.t seed" and send me the output
1N/A
1N/Amy $seed = ($#ARGV == 0) ? $ARGV[0] : int(rand(1165537));
1N/Aprint "# seed: $seed\n"; srand($seed);
1N/A
1N/Amy ($A,$B,$As,$Bs,$ADB,$AMB,$la,$lb);
1N/Amy $two = Math::BigInt->new(2);
1N/Afor (my $i = 0; $i < $count; $i++)
1N/A {
1N/A # length of A and B
1N/A $la = int(rand($length)+1); $lb = int(rand($length)+1);
1N/A $As = ''; $Bs = '';
1N/A
1N/A # we create the numbers from "patterns", e.g. get a random number and a
1N/A # random count and string them together. This means things like
1N/A # "100000999999999999911122222222" are much more likely. If we just strung
1N/A # together digits, we would end up with "1272398823211223" etc. It also means
1N/A # that we get more frequently equal numbers or other special cases.
1N/A while (length($As) < $la) { $As .= int(rand(100)) x int(rand(16)); }
1N/A while (length($Bs) < $lb) { $Bs .= int(rand(100)) x int(rand(16)); }
1N/A
1N/A $As =~ s/^0+//; $Bs =~ s/^0+//;
1N/A $As = $As || '0'; $Bs = $Bs || '0';
1N/A # print "# As $As\n# Bs $Bs\n";
1N/A $A = $c->new($As); $B = $c->new($Bs);
1N/A # print "# A $A\n# B $B\n";
1N/A if ($A->is_zero() || $B->is_zero())
1N/A {
1N/A for (1..4) { ok (1,1); } next;
1N/A }
1N/A
1N/A # check that int(A/B)*B + A % B == A holds for all inputs
1N/A
1N/A # $X = ($A/$B)*$B + 2 * ($A % $B) - ($A % $B);
1N/A
1N/A ($ADB,$AMB) = $A->copy()->bdiv($B);
1N/A# print "# ($A / $B, $A % $B ) = $ADB $AMB\n";
1N/A
1N/A print "# seed $seed, ". join(' ',Math::BigInt::Calc->_base_len()),"\n".
1N/A "# tried $ADB * $B + $two*$AMB - $AMB\n"
1N/A unless ok ($ADB*$B+$two*$AMB-$AMB,$As);
1N/A print "# seed: $seed, \$ADB * \$B / \$B = ", $ADB * $B / $B, " != $ADB (\$B=$B)\n"
1N/A unless ok ($ADB*$B/$B,$ADB);
1N/A # swap 'em and try this, too
1N/A # $X = ($B/$A)*$A + $B % $A;
1N/A ($ADB,$AMB) = $B->copy()->bdiv($A);
1N/A #print "check: $ADB $AMB";
1N/A print "# seed $seed, ". join(' ',Math::BigInt::Calc->_base_len()),"\n".
1N/A "# tried $ADB * $A + $two*$AMB - $AMB\n"
1N/A unless ok ($ADB*$A+$two*$AMB-$AMB,$Bs);
1N/A# print " +$two * $AMB = ",$ADB * $A + $two * $AMB,"\n";
1N/A# print " -$AMB = ",$ADB * $A + $two * $AMB - $AMB,"\n";
1N/A print "# seed $seed, \$ADB * \$A / \$A = ", $ADB * $A / $A, " != $ADB (\$A=$A)\n"
1N/A unless ok ($ADB*$A/$A,$ADB);
1N/A }
1N/A