1*eac174f2Safresh1# -*- mode: perl; -*- 2b8851fccSafresh1 3b8851fccSafresh1use strict; 4b8851fccSafresh1use warnings; 5b8851fccSafresh1 69f11ffb7Safresh1use Test::More; 7b8851fccSafresh1 89f11ffb7Safresh1my $count = 128; 9b8851fccSafresh1 109f11ffb7Safresh1plan(($^O eq 'os390') ? (skip_all => 'takes too long on os390') 119f11ffb7Safresh1 : (tests => $count*4)); 12b8851fccSafresh1 139f11ffb7Safresh1use Math::BigInt only => 'Calc'; 14b8851fccSafresh1 15b8851fccSafresh1my $length = 128; 16b8851fccSafresh1 17b8851fccSafresh1# If you get a failure here, please re-run the test with the printed seed 18b8851fccSafresh1# value as input "perl t/mbi_rand.t seed" and send me the output 19b8851fccSafresh1 20b8851fccSafresh1my $seed = @ARGV == 1 ? $ARGV[0] : int(rand(1165537)); 21b8851fccSafresh1#diag(" seed: $seed\n"); 22b8851fccSafresh1srand($seed); 23b8851fccSafresh1 24b8851fccSafresh1my $_base_len; 25b8851fccSafresh1my @_base_len; 26b8851fccSafresh1 279f11ffb7Safresh1#diag(" lib: ", Math::BigInt->config('lib')); 289f11ffb7Safresh1if (Math::BigInt->config('lib') =~ /::Calc/) { 29b8851fccSafresh1 $_base_len = Math::BigInt::Calc->_base_len(); 30b8851fccSafresh1 @_base_len = Math::BigInt::Calc->_base_len(); 31b8851fccSafresh1 #diag("base len: $_base_len (scalar context)"); 32b8851fccSafresh1 #diag("base len: @_base_len (list contex)"); 33b8851fccSafresh1} 34b8851fccSafresh1 35b8851fccSafresh1my ($A, $B, $A_str, $B_str, $AdivB, $AmodB, $A_len, $B_len); 36b8851fccSafresh1my $two = Math::BigInt->new(2); 37b8851fccSafresh1for (my $i = 0; $i < $count; $i++) { 38b8851fccSafresh1 #diag(""); 39b8851fccSafresh1 40b8851fccSafresh1 # length of A and B 41b8851fccSafresh1 $A_len = int(rand($length) + 1); 42b8851fccSafresh1 $B_len = int(rand($length) + 1); 43b8851fccSafresh1 $A_str = ''; 44b8851fccSafresh1 $B_str = ''; 45b8851fccSafresh1 46b8851fccSafresh1 # 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 49b8851fccSafresh1 # together digits, we would end up with "1272398823211223" etc. It also 50b8851fccSafresh1 # means that we get more frequently equal numbers or other special cases. 51b8851fccSafresh1 52b8851fccSafresh1 while (length($A_str) < $A_len) { 53b8851fccSafresh1 $A_str .= int(rand(100)) x int(rand(16)); 54b8851fccSafresh1 } 55b8851fccSafresh1 while (length($B_str) < $B_len) { 56b8851fccSafresh1 $B_str .= int(rand(100)) x int(rand(16)); 57b8851fccSafresh1 } 58b8851fccSafresh1 59b8851fccSafresh1 $A_str =~ s/^0+(?=\d)//; 60b8851fccSafresh1 $B_str =~ s/^0+(?=\d)//; 61b8851fccSafresh1 #diag(" As: $A_str"); 62b8851fccSafresh1 #diag(" Bs: $B_str"); 639f11ffb7Safresh1 $A = Math::BigInt->new($A_str); 649f11ffb7Safresh1 $B = Math::BigInt->new($B_str); 65b8851fccSafresh1 #diag(" A: $A"); 66b8851fccSafresh1 #diag(" B: $B"); 67b8851fccSafresh1 68b8851fccSafresh1 SKIP: { 69b8851fccSafresh1 skip '$A and/or $B are zero.', 4 if $A->is_zero() || $B->is_zero(); 70b8851fccSafresh1 71b8851fccSafresh1 # check that int(A / B) * B + A % B == A holds for all inputs 72b8851fccSafresh1 73b8851fccSafresh1 # $X = ($A / $B) * $B + 2 * ($A % $B) - ($A % $B); 74b8851fccSafresh1 75b8851fccSafresh1 ($AdivB, $AmodB) = $A->copy()->bdiv($B); 76b8851fccSafresh1 77b8851fccSafresh1 #diag(" A / B: $AdivB"); 78b8851fccSafresh1 #diag(" A % B: $AmodB"); 79b8851fccSafresh1 80b8851fccSafresh1 is($AdivB * $B + $two * $AmodB - $AmodB, $A_str, 81b8851fccSafresh1 "AdivB * B + 2 * AmodB - AmodB == A"); 82b8851fccSafresh1 83b8851fccSafresh1 if (is($AdivB * $B / $B, $AdivB, "AdivB * B / B == AdivB")) { 849f11ffb7Safresh1 if (Math::BigInt->config('lib') =~ /::Calc/) { 85b8851fccSafresh1 #diag("AdivB->[-1]: ", $AdivB->{value}->[-1]); 86b8851fccSafresh1 #diag(" B->[-1]: ", $B->{value}->[-1]); 87b8851fccSafresh1 } 88b8851fccSafresh1 } 89b8851fccSafresh1 90b8851fccSafresh1 # swap 'em and try this, too 91b8851fccSafresh1 # $X = ($B/$A)*$A + $B % $A; 92b8851fccSafresh1 ($AdivB, $AmodB) = $B->copy()->bdiv($A); 93b8851fccSafresh1 # print "check: $AdivB $AmodB"; 94b8851fccSafresh1 95b8851fccSafresh1 is($AdivB * $A + $two * $AmodB - $AmodB, $B_str, 96b8851fccSafresh1 "AdivB * A + 2 * AmodB - AmodB == B"); 97b8851fccSafresh1 98b8851fccSafresh1 is($AdivB * $A / $A, $AdivB, "AdivB * A / A == AdivB"); 99b8851fccSafresh1 } 100b8851fccSafresh1} 101