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