File: mbi_rand.t

package info (click to toggle)
perl 5.24.1-3%2Bdeb9u7
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 107,108 kB
  • sloc: perl: 559,649; ansic: 293,918; sh: 67,316; pascal: 7,632; cpp: 3,895; makefile: 2,436; xml: 2,410; yacc: 989; sed: 6; lisp: 1
file content (55 lines) | stat: -rw-r--r-- 1,806 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/perl -w

use strict;
use Test::More;

my $count = 128;

plan(($^O eq 'os390')
     ? (skip_all => 'takes too long on os390') : (tests => $count*2));

use Math::BigInt lib => 'FastCalc';
my $c = 'Math::BigInt';

my $length = 128;

# If you get a failure here, please re-run the test with the printed seed
# value as input: perl t/mbi_rand.t seed

my $seed = ($#ARGV == 0) ? $ARGV[0] : int(rand(65537));
print "# seed: $seed\n"; srand($seed);

my ($A,$B,$As,$Bs,$ADB,$AMB,$la,$lb);
my $two = Math::BigInt->new(2);
for (my $i = 0; $i < $count; $i++)
  {
  # length of A and B
  $la = int(rand($length)+1); $lb = int(rand($length)+1);
  $As = ''; $Bs = '';
  # we create the numbers from "patterns", e.g. get a random number and a
  # random count and string them together. This means things like
  # "100000999999999999911122222222" are much more likely. If we just strung
  # together digits, we would end up with "1272398823211223" etc.
  while (length($As) < $la) { $As .= int(rand(100)) x int(rand(16)); }
  while (length($Bs) < $lb) { $Bs .= int(rand(100)) x int(rand(16)); }
  $As =~ s/^0+//; $Bs =~ s/^0+//; 
  $As = $As || '0'; $Bs = $Bs || '0';
  # print "# As $As\n# Bs $Bs\n";
  $A = $c->new($As); $B = $c->new($Bs);
  # print "# A $A\n# B $B\n";
  if ($A->is_zero() || $B->is_zero())
    {
    is (1,1); is (1,1); next;
    }
  # check that int(A/B)*B + A % B == A holds for all inputs
  # $X = ($A/$B)*$B + 2 * ($A % $B) - ($A % $B);
  ($ADB,$AMB) = $A->copy()->bdiv($B);
  print "# ". join(' ',Math::BigInt::Calc->_base_len()),"\n"
   unless is ($ADB*$B+$two*$AMB-$AMB,$As);
  # swap 'em and try this, too
  # $X = ($B/$A)*$A + $B % $A;
  ($ADB,$AMB) = $B->copy()->bdiv($A);
  print "# ". join(' ',Math::BigInt::Calc->_base_len()),"\n"
   unless is ($ADB*$A+$two*$AMB-$AMB,$Bs);
  }