11 if ($^O eq 'os390') { print "1..0\n"; exit(0) } # test takes too long there
12 unshift @INC, '../lib'; # for running manually
13 my $location = $0; $location =~ s/mbi_rand.t//;
14 unshift @INC, $location; # to locate the testing files
17 plan tests => $count*4;
21 my $c = 'Math::BigInt';
25 # If you get a failure here, please re-run the test with the printed seed
26 # value as input "perl t/mbi_rand.t seed" and send me the output
28 my $seed = ($#ARGV == 0) ? $ARGV[0] : int(rand(1165537));
29 print "# seed: $seed\n"; srand($seed);
31 print "# lib: ", Math::BigInt->config()->{lib},"\n";
32 if (Math::BigInt->config()->{lib} =~ /::Calc/)
34 print "# base len: ", scalar Math::BigInt::Calc->_base_len(),"\n";
37 my ($A,$B,$As,$Bs,$ADB,$AMB,$la,$lb);
38 my $two = Math::BigInt->new(2);
39 for (my $i = 0; $i < $count; $i++)
42 $la = int(rand($length)+1); $lb = int(rand($length)+1);
45 # we create the numbers from "patterns", e.g. get a random number and a
46 # random count and string them together. This means things like
47 # "100000999999999999911122222222" are much more likely. If we just strung
48 # together digits, we would end up with "1272398823211223" etc. It also means
49 # that we get more frequently equal numbers or other special cases.
50 while (length($As) < $la) { $As .= int(rand(100)) x int(rand(16)); }
51 while (length($Bs) < $lb) { $Bs .= int(rand(100)) x int(rand(16)); }
53 $As =~ s/^0+//; $Bs =~ s/^0+//;
54 $As = $As || '0'; $Bs = $Bs || '0';
55 # print "# As $As\n# Bs $Bs\n";
56 $A = $c->new($As); $B = $c->new($Bs);
57 print "# A $A\n# B $B\n";
58 if ($A->is_zero() || $B->is_zero())
60 for (1..4) { is (1,1, 'skipped this test'); } next;
63 # check that int(A/B)*B + A % B == A holds for all inputs
65 # $X = ($A/$B)*$B + 2 * ($A % $B) - ($A % $B);
67 ($ADB,$AMB) = $A->copy()->bdiv($B);
68 print "# ($A / $B, $A % $B ) = $ADB $AMB\n";
70 print "# seed $seed, ". join(' ',Math::BigInt::Calc->_base_len()),"\n".
71 "# tried $ADB * $B + $two*$AMB - $AMB\n"
72 unless is ($ADB*$B+$two*$AMB-$AMB,$As, "ADB * B + 2 * AMB - AMB == A");
73 if (is ($ADB*$B/$B,$ADB, "ADB * B / B == ADB"))
75 print "# seed: $seed, \$ADB * \$B / \$B = ", $ADB * $B / $B, " != $ADB (\$B=$B)\n";
76 if (Math::BigInt->config()->{lib} =~ /::Calc/)
78 print "# ADB->[-1]: ", $ADB->{value}->[-1], " B->[-1]: ", $B->{value}->[-1],"\n";
81 # swap 'em and try this, too
82 # $X = ($B/$A)*$A + $B % $A;
83 ($ADB,$AMB) = $B->copy()->bdiv($A);
84 # print "check: $ADB $AMB";
85 print "# seed $seed, ". join(' ',Math::BigInt::Calc->_base_len()),"\n".
86 "# tried $ADB * $A + $two*$AMB - $AMB\n"
87 unless is ($ADB*$A+$two*$AMB-$AMB,$Bs, "ADB * A + 2 * AMB - AMB == B");
88 print "# +$two * $AMB = ",$ADB * $A + $two * $AMB,"\n";
89 print "# -$AMB = ",$ADB * $A + $two * $AMB - $AMB,"\n";
90 print "# seed $seed, \$ADB * \$A / \$A = ", $ADB * $A / $A, " != $ADB (\$A=$A)\n"
91 unless is ($ADB*$A/$A,$ADB, "ADB * A/A == ADB");