1*eac174f2Safresh1# -*- mode: perl; -*-
2b8851fccSafresh1
3b8851fccSafresh1use strict;
49f11ffb7Safresh1use warnings;
59f11ffb7Safresh1
6b8851fccSafresh1use Test::More;
7b8851fccSafresh1
8b8851fccSafresh1my $count = 128;
9b8851fccSafresh1
109f11ffb7Safresh1plan(($^O eq 'os390') ? (skip_all => 'takes too long on os390')
119f11ffb7Safresh1                      : (tests => $count*4));
12b8851fccSafresh1
139f11ffb7Safresh1use Math::BigInt only => 'FastCalc';
14b8851fccSafresh1
15b8851fccSafresh1my $length = 128;
16b8851fccSafresh1
17b8851fccSafresh1# If you get a failure here, please re-run the test with the printed seed
189f11ffb7Safresh1# value as input "perl t/mbi_rand.t seed" and send me the output
19b8851fccSafresh1
209f11ffb7Safresh1my $seed = @ARGV == 1 ? $ARGV[0] : int(rand(1165537));
219f11ffb7Safresh1#diag("    seed: $seed\n");
229f11ffb7Safresh1srand($seed);
23b8851fccSafresh1
249f11ffb7Safresh1my $_base_len;
259f11ffb7Safresh1my @_base_len;
269f11ffb7Safresh1
279f11ffb7Safresh1#diag("     lib: ", Math::BigInt->config('lib'));
289f11ffb7Safresh1if (Math::BigInt->config('lib') =~ /::Calc/) {
299f11ffb7Safresh1    $_base_len = Math::BigInt::Calc->_base_len();
309f11ffb7Safresh1    @_base_len = Math::BigInt::Calc->_base_len();
319f11ffb7Safresh1    #diag("base len: $_base_len (scalar context)");
329f11ffb7Safresh1    #diag("base len: @_base_len (list contex)");
339f11ffb7Safresh1}
349f11ffb7Safresh1
359f11ffb7Safresh1my ($A, $B, $A_str, $B_str, $AdivB, $AmodB, $A_len, $B_len);
36b8851fccSafresh1my $two = Math::BigInt->new(2);
379f11ffb7Safresh1for (my $i = 0; $i < $count; $i++) {
389f11ffb7Safresh1    #diag("");
399f11ffb7Safresh1
40b8851fccSafresh1    # length of A and B
419f11ffb7Safresh1    $A_len = int(rand($length) + 1);
429f11ffb7Safresh1    $B_len = int(rand($length) + 1);
439f11ffb7Safresh1    $A_str = '';
449f11ffb7Safresh1    $B_str = '';
459f11ffb7Safresh1
469f11ffb7Safresh1    # We create the numbers from "patterns", e.g. get a random number and a
47b8851fccSafresh1    # random count and string them together. This means things like
48b8851fccSafresh1    # "100000999999999999911122222222" are much more likely. If we just strung
499f11ffb7Safresh1    # together digits, we would end up with "1272398823211223" etc. It also
509f11ffb7Safresh1    # means that we get more frequently equal numbers or other special cases.
519f11ffb7Safresh1
529f11ffb7Safresh1    while (length($A_str) < $A_len) {
539f11ffb7Safresh1        $A_str .= int(rand(100)) x int(rand(16));
54b8851fccSafresh1    }
559f11ffb7Safresh1    while (length($B_str) < $B_len) {
569f11ffb7Safresh1        $B_str .= int(rand(100)) x int(rand(16));
57b8851fccSafresh1    }
58b8851fccSafresh1
599f11ffb7Safresh1    $A_str =~ s/^0+(?=\d)//;
609f11ffb7Safresh1    $B_str =~ s/^0+(?=\d)//;
619f11ffb7Safresh1    #diag("      As: $A_str");
629f11ffb7Safresh1    #diag("      Bs: $B_str");
639f11ffb7Safresh1    $A = Math::BigInt->new($A_str);
649f11ffb7Safresh1    $B = Math::BigInt->new($B_str);
659f11ffb7Safresh1    #diag("       A: $A");
669f11ffb7Safresh1    #diag("       B: $B");
679f11ffb7Safresh1
689f11ffb7Safresh1  SKIP: {
699f11ffb7Safresh1        skip '$A and/or $B are zero.', 4 if $A->is_zero() || $B->is_zero();
709f11ffb7Safresh1
719f11ffb7Safresh1        # check that int(A / B) * B + A % B == A holds for all inputs
729f11ffb7Safresh1
739f11ffb7Safresh1        # $X = ($A / $B) * $B + 2 * ($A % $B) - ($A % $B);
749f11ffb7Safresh1
759f11ffb7Safresh1        ($AdivB, $AmodB) = $A->copy()->bdiv($B);
769f11ffb7Safresh1
779f11ffb7Safresh1        #diag("   A / B: $AdivB");
789f11ffb7Safresh1        #diag("   A % B: $AmodB");
799f11ffb7Safresh1
809f11ffb7Safresh1        is($AdivB * $B + $two * $AmodB - $AmodB, $A_str,
819f11ffb7Safresh1           "AdivB * B + 2 * AmodB - AmodB == A");
829f11ffb7Safresh1
839f11ffb7Safresh1        if (is($AdivB * $B / $B, $AdivB, "AdivB * B / B == AdivB")) {
849f11ffb7Safresh1            if (Math::BigInt->config('lib') =~ /::Calc/) {
859f11ffb7Safresh1                #diag("AdivB->[-1]: ", $AdivB->{value}->[-1]);
869f11ffb7Safresh1                #diag("    B->[-1]: ", $B->{value}->[-1]);
879f11ffb7Safresh1            }
889f11ffb7Safresh1        }
899f11ffb7Safresh1
909f11ffb7Safresh1        # swap 'em and try this, too
919f11ffb7Safresh1        # $X = ($B/$A)*$A + $B % $A;
929f11ffb7Safresh1        ($AdivB, $AmodB) = $B->copy()->bdiv($A);
939f11ffb7Safresh1        # print "check: $AdivB $AmodB";
949f11ffb7Safresh1
959f11ffb7Safresh1        is($AdivB * $A + $two * $AmodB - $AmodB, $B_str,
969f11ffb7Safresh1           "AdivB * A + 2 * AmodB - AmodB == B");
979f11ffb7Safresh1
989f11ffb7Safresh1        is($AdivB * $A / $A, $AdivB, "AdivB * A / A == AdivB");
999f11ffb7Safresh1    }
1009f11ffb7Safresh1}
101