Upgrade to Math::BigInt 1.57.
[p5sagit/p5-mst-13.2.git] / lib / Math / BigInt / t / mbi_rand.t
CommitLineData
56b9c951 1#!/usr/bin/perl -w
2
3use Test;
4use strict;
5
6my $count;
7
8BEGIN
9 {
11b9455c 10 if ($^O eq 'os390') { print "1..0\n"; exit(0) }
56b9c951 11 $| = 1;
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
15 chdir 't' if -d 't';
d614cd8b 16 $count = 128;
56b9c951 17 plan tests => $count*2;
18 }
19
20use Math::BigInt;
21my $c = 'Math::BigInt';
22
d614cd8b 23my $length = 128;
56b9c951 24
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
27
28my $seed = int(rand(65537)); print "# seed: $seed\n"; srand($seed);
29
d614cd8b 30my ($A,$B,$As,$Bs,$ADB,$AMB,$la,$lb);
31my $two = Math::BigInt->new(2);
56b9c951 32for (my $i = 0; $i < $count; $i++)
33 {
34 # length of A and B
35 $la = int(rand($length)+1); $lb = int(rand($length)+1);
d614cd8b 36 $As = ''; $Bs = '';
56b9c951 37 # we create the numbers from "patterns", e.g. get a random number and a
38 # random count and string them together. This means things like
39 # "100000999999999999911122222222" are much more likely. If we just strung
40 # together digits, we would end up with "1272398823211223" etc.
d614cd8b 41 while (length($As) < $la) { $As .= int(rand(100)) x int(rand(16)); }
42 while (length($Bs) < $lb) { $Bs .= int(rand(100)) x int(rand(16)); }
43 $As =~ s/^0+//; $Bs =~ s/^0+//;
44 $A = $c->new($As); $B = $c->new($Bs);
8f675a64 45 # print "# A $A\n# B $B\n";
56b9c951 46 if ($A->is_zero() || $B->is_zero())
47 {
48 ok (1,1); ok (1,1); next;
49 }
50 # check that int(A/B)*B + A % B == A holds for all inputs
51 # $X = ($A/$B)*$B + 2 * ($A % $B) - ($A % $B);
52 ($ADB,$AMB) = $A->copy()->bdiv($B);
8f675a64 53 print "# ". join(' ',Math::BigInt::Calc->_base_len()),"\n"
d614cd8b 54 unless ok ($ADB*$B+$two*$AMB-$AMB,$As);
56b9c951 55 # swap 'em and try this, too
56 # $X = ($B/$A)*$A + $B % $A;
57 ($ADB,$AMB) = $B->copy()->bdiv($A);
8f675a64 58 print "# ". join(' ',Math::BigInt::Calc->_base_len()),"\n"
d614cd8b 59 unless ok ($ADB*$A+$two*$AMB-$AMB,$Bs);
56b9c951 60 }
61