Upgrade to Math::BigInt 1.55, from Tels.
[p5sagit/p5-mst-13.2.git] / lib / Math / BigInt / t / mbi_rand.t
1 #!/usr/bin/perl -w
2
3 use Test;
4 use strict;
5
6 my $count;
7   
8 BEGIN
9   {
10   $| = 1;
11   unshift @INC, '../lib'; # for running manually
12   my $location = $0; $location =~ s/mbi_rand.t//;
13   unshift @INC, $location; # to locate the testing files
14   chdir 't' if -d 't';
15   $count = 500;
16   plan tests => $count*2;
17   }
18
19 use Math::BigInt;
20 my $c = 'Math::BigInt';
21
22 my $length = 200;
23
24 # If you get a failure here, please re-run the test with the printed seed
25 # value as input: perl t/mbi_rand.t seed
26
27 my $seed = int(rand(65537)); print "# seed: $seed\n"; srand($seed);
28
29 my ($A,$B,$ADB,$AMB,$la,$lb);
30 for (my $i = 0; $i < $count; $i++)
31   {
32   # length of A and B
33   $la = int(rand($length)+1); $lb = int(rand($length)+1);
34   $A = ''; $B = '';
35   # we create the numbers from "patterns", e.g. get a random number and a
36   # random count and string them together. This means things like
37   # "100000999999999999911122222222" are much more likely. If we just strung
38   # together digits, we would end up with "1272398823211223" etc.
39   while (length($A) < $la) { $A .= int(rand(100)) x int(rand(16)); }
40   while (length($B) < $lb) { $B .= int(rand(100)) x int(rand(16)); }
41   $A = $c->new($A); $B = $c->new($B);
42   print "# A $A\n# B $B\n";
43   if ($A->is_zero() || $B->is_zero())
44     {
45     ok (1,1); ok (1,1); next;
46     }
47   # check that int(A/B)*B + A % B == A holds for all inputs
48   # $X = ($A/$B)*$B + 2 * ($A % $B) - ($A % $B);
49   ($ADB,$AMB) = $A->copy()->bdiv($B);
50   ok ($A,$ADB*$B+2*$AMB-$AMB);
51   # swap 'em and try this, too
52   # $X = ($B/$A)*$A + $B % $A;
53   ($ADB,$AMB) = $B->copy()->bdiv($A);
54   ok ($B,$ADB*$A+2*$AMB-$AMB);
55   }
56