blib/lib/Langertha/Holmgang/Random.pm

filter: Statementstmt 100% 67 / 67 Branchbran 81.2% 13 / 16 Conditioncond 73.3% 11 / 15 MC/DCmcdc 57.1% 4 / 7 Subroutinesub 100% 12 / 12 Podpod 83.3% 5 / 6 totaltotal 91.0% 112 / 123 CC 16 SCAR 14.7
CC
16
Cov
93%
int
8.5
BUILD
5.0
_build_s
3.0
CRAP 4.3
« blib/lib/Langertha/Holmgang/Sim/Uebersetzung.pm ↑ Summary blib/lib/Langertha/Holmgang/Error.pm »
1 package Langertha::Holmgang::Random;
2 # ABSTRACT: The seeded private random source for case generators
3  
4 37 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 37 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 37 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 37 our $VERSION = '0.001';
10  
11  
12 37 my $M32 = 0xFFFFFFFF;
13  
14 37 has seed => (
15 is => 'ro',
16 required => 1,
17 );
18  
19  
20 37 has _s => (
21 is => 'ro',
22 init_arg => undef,
23 lazy => 1,
24 builder => '_build_s',
25 );
26  
27 57 ▶sub BUILD {
Subroutine BUILD: called CC=5 SCAR=16.1
Pod undocumented
28 57 my ( $self ) = @_;
29 57 my $seed = $self->seed;
30 57 ▶ croak __PACKAGE__.': seed must be an integer from 0 to 4294967295'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seed and not ref $seed and $seed =~ /\A[0-9]{1,10}\z/ and $seed <= $M32 6 51 57
Condition88%8/9
defined $seed and not ref $seed and $seed =~ /\A[0-9]{1,10}\z/ and $seed <= $M32
!ll&&!rl&&r
5151
defined $seed and not ref $seed and $seed =~ /\A[0-9]{1,10}\z/
!ll&&!rl&&r
1452
defined $seed and not ref $seed
!ll&&!rl&&r
1056
MC/DC75%3/4
defined $seed and not ref $seed and $seed =~ /\A[0-9]{1,10}\z/ and $seed <= $M32
defined $seed not ref $seed $seed =~ /\A[0-9]{1,10}\z/ $seed <= $M32
Truth table80%4/5
A and not(B) and C and D
A= defined $seedB= not ref $seedC= $seed =~ /\A[0-9]{1,10}\z/D= $seed <= $M32
ABCDresult
0XXX0
10XX0
110X0
11100
11111
31 unless defined $seed && !ref $seed && $seed =~ /\A[0-9]{1,10}\z/ && $seed <= $M32;
32 51 $self->_s;
33 51 return;
34 }
35  
36 # a * b mod 2**32 without leaving 48 bits
37 5818 ▶sub _mul32 {
Subroutine _mul32: called CC=1 SCAR=0.0
38 5818 my ( $x, $y ) = @_;
39 5818 return ( ( $x * ( $y & 0xFFFF ) ) + ( ( ( $x * ( $y >> 16 ) ) & 0xFFFF ) << 16 ) ) & $M32;
40 }
41  
42 5410 ▶sub _rotl { my ( $x, $k ) = @_; return ( ( $x << $k ) | ( $x >> ( 32 - $k ) ) ) & $M32 }
Subroutine _rotl: called CC=1 SCAR=0.0
43  
44 51 ▶sub _build_s {
Subroutine _build_s: called CC=3 SCAR=11.0
45 51 my ( $self ) = @_;
46 51 my $x = $self->seed & $M32;
47 51 my @s;
48 51 for ( 1 .. 4 ) { # splitmix32
49 204 $x = ( $x + 0x9E3779B9 ) & $M32;
50 204 my $z = $x;
51 204 $z = _mul32( $z ^ ( $z >> 16 ), 0x85EBCA6B );
52 204 $z = _mul32( $z ^ ( $z >> 13 ), 0xC2B2AE35 );
53 204 push @s, $z ^ ( $z >> 16 );
54 }
55 51 ▶ $s[0] = 1 unless grep { $_ } @s;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_;} @s 0 51 51
56 51 return \@s;
57 }
58  
59 2705 ▶sub next_u32 {
Subroutine next_u32: called CC=1 SCAR=0.0
Pod documented
60 2705 my ( $self ) = @_;
61 2705 my $s = $self->_s;
62 2705 my $result = _mul32( _rotl( _mul32( $s->[1], 5 ), 7 ), 9 );
63 2705 my $t = ( $s->[1] << 9 ) & $M32;
64 2705 $s->[2] ^= $s->[0];
65 2705 $s->[3] ^= $s->[1];
66 2705 $s->[1] ^= $s->[2];
67 2705 $s->[0] ^= $s->[3];
68 2705 $s->[2] ^= $t;
69 2705 $s->[3] = _rotl( $s->[3], 11 );
70 2705 return $result;
71 }
72  
73  
74 2599 ▶sub int {
Subroutine int: called CC=8 SCAR=21.4
Pod documented
75 2599 my ( $self, $lo, $hi ) = @_;
76 2599 for ( $lo, $hi ) {
77 5197 ▶ croak __PACKAGE__.'::int: bounds must be integers' unless defined && !ref && /\A-?[0-9]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ and not ref $_ and /\A-?[0-9]+\z/ 1 5196 5197
Condition50%3/6
defined $_ and not ref $_ and /\A-?[0-9]+\z/
!ll&&!rl&&r
015196
defined $_ and not ref $_
!ll&&!rl&&r
005197
MC/DC33%1/3
defined $_ and not ref $_ and /\A-?[0-9]+\z/
defined $_ not ref $_ /\A-?[0-9]+\z/
Truth table50%2/4
A and not(B) and C
A= defined $_B= not ref $_C= /\A-?[0-9]+\z/
ABCresult
0XX0
10X0
1100
1111
78 }
79 2598 ▶ croak __PACKAGE__.'::int: lower bound above upper bound' if $lo > $hi;
Branch: 1 branch
BranchTrueFalseTotal
if $lo > $hi 1 2597 2598
80 2597 my $n = $hi - $lo + 1;
81 2597 ▶ croak __PACKAGE__.'::int: range wider than 2**32' if $n > 4294967296;
Branch: 1 branch
BranchTrueFalseTotal
if $n > 4294967296 0 2597 2597
82 # rejection keeps every value equally likely
83 2597 my $limit = 4294967296 - ( 4294967296 % $n );
84 2597 my $u;
85 2597 ▶ do { $u = $self->next_u32 } until $u < $limit;
Branch: 1 branch
BranchTrueFalseTotal
$u < $limit or do { do { $u = $self->next_u32 } } 2597 0 2597
86 2597 return $lo + $u % $n;
87 }
88  
89  
90 1 ▶sub float { $_[0]->next_u32 / 4294967296 }
Subroutine float: called CC=1 SCAR=0.0
Pod documented
91  
92  
93 401 ▶sub pick {
Subroutine pick: called CC=2 SCAR=6.9
Pod documented
94 401 my ( $self, @list ) = @_;
95 401 ▶ croak __PACKAGE__.'::pick: nothing to pick from' unless @list;
Branch: 1 branch
BranchTrueFalseTotal
unless @list 1 400 401
96 400 return $list[ $self->int( 0, $#list ) ];
97 }
98  
99  
100 29 ▶sub shuffle {
Subroutine shuffle: called CC=2 SCAR=6.9
Pod documented
101 29 my ( $self, @list ) = @_;
102 29 ▶ for ( my $i = $#list; $i > 0; $i-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0) 122 29 151
103 122 my $j = $self->int( 0, $i );
104 122 @list[ $i, $j ] = @list[ $j, $i ];
105 }
106 29 return @list;
107 }
108  
109  
110 37 __PACKAGE__->meta->make_immutable;
111  
112 37 1;
113  
114 __END__
« blib/lib/Langertha/Holmgang/Sim/Uebersetzung.pm ↑ Summary blib/lib/Langertha/Holmgang/Error.pm »