File Coverage

lib/Crypt/Perl/Math.pm
Criterion Covered Total %
statement 23 23 100.0
branch 3 4 75.0
condition 2 3 66.6
subroutine 6 6 100.0
pod 0 2 0.0
total 34 38 89.4


line stmt bran cond sub pod time code
1             package Crypt::Perl::Math;
2              
3 8     8   112373 use strict;
  8         18  
  8         182  
4 8     8   29 use warnings;
  8         16  
  8         156  
5              
6 8     8   328 use Crypt::Perl::BigInt ();
  8         14  
  8         113  
7 8     8   2480 use Crypt::Perl::RNG ();
  8         17  
  8         1026  
8              
9             sub ceil {
10 66 50   66 0 53722 return int($_[0]) if $_[0] <= 0;
11              
12 66         322 return int($_[0]) + int( !!($_[0] - int $_[0]) );
13             }
14              
15             #limit is inclusive
16             #cf. Python’s random.randint()
17             sub randint {
18 324     324 0 17637 my ($limit) = @_;
19              
20 324         688 my $limit_bit_count;
21 324 100 66     4225 if (ref($limit) && (ref $limit)->isa('Math::BigInt')) {
22 304         1647 $limit_bit_count = length($limit->as_bin()) - 2;
23             }
24              
25             #Is this ever needed??
26             else {
27 20         63 $limit_bit_count = length sprintf '%b', $limit;
28             }
29              
30 324         266794 my $rand;
31 324         1298 do {
32 440         441483 $rand = Crypt::Perl::BigInt->from_bin( Crypt::Perl::RNG::bit_string($limit_bit_count) );
33             } while $rand > $limit;
34              
35 324         1384691 return $rand;
36             }
37              
38             1;