Data/Set.pm

filter: Statementstmt 78.4% 73 / 93 Branchbran 51.9% 27 / 52 Conditioncond 50.0% 16 / 32 MC/DCmcdc 23.8% 5 / 21 Subroutinesub 92.8% 13 / 14 Podpod 100% 7 / 7 totaltotal 64.3% 141 / 219 CC 40 SCAR 25.8
CC
40
Cov
66%
__ANON__
42.0
combine_generator
18.7
compile
18.7
CRAP 13.1
« Data/Plugin/Text.pm ↑ Summary Data/Plugin/Number.pm »
1 package Mock::Data::Set;
2 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 require Mock::Data::Generator;
5 12 our @ISA= qw( Mock::Data::Generator );
6  
7 # ABSTRACT: Generator which returns one item from a set
8 12 our $VERSION = '0.04'; # VERSION
9  
10  
11 19 ▶sub new {
Subroutine new: called CC=5 SCAR=17.2
Pod documented
12 19 my $class= shift;
13 0 ▶ my %args= @_ == 1 && ref $_[0] eq 'HASH'? %{$_[0]}
Branch: 2 branches
BranchTrueFalseTotal
@_ == 1 && ref $_[0] eq 'HASH' ? : 0 19 19
@_ == 1 && ref $_[0] eq 'ARRAY' ? : 2 17 19
Condition66%4/6
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
1720
@_ == 1 and ref $_[0] eq "ARRAY"
!ll&&!rl&&r
1702
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
MC/DC50%1/2
@_ == 1 and ref $_[0] eq "ARRAY"
@_ == 1 ref $_[0] eq "ARRAY"
Truth table66%2/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= @_ == 1B= ref $_[0] eq "ARRAY"
ABresult
0X0
100
111
14 : @_ == 1 && ref $_[0] eq 'ARRAY'? ( items => $_[0] )
15 : @_;
16 19 bless \%args, $class;
17 }
18  
19 2 ▶sub new_weighted {
Subroutine new_weighted: called CC=3 SCAR=11.0
Pod documented
20 2 my $class= shift;
21 2 ▶ @_ & 1 and Carp::croak("Odd number of elements given to new_weighted, need (value => weight) pairs");
Branch: 1 branch
BranchTrueFalseTotal
@_ & 1 and Carp::croak("Odd number of elements given to new_weighted, need (value => weight) pairs") 0 2 2
22 2 my (@items, @weights);
23 2 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 7 2 9
24 7 push @items, shift;
25 7 push @weights, shift;
26 }
27 2 $class->new(items => \@items, weights => \@weights);
28 }
29  
30  
31 131 ▶sub items {
Subroutine items: called CC=3 SCAR=18.1
Pod documented
32 131 my $self= shift;
33 131 ▶ return $self->{items} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 131 0 131
34 0 my $val= shift;
35 0 ▶ delete $self->{_odds_table} if $#{$self->{items}} != $#$val;
Branch: 1 branch
BranchTrueFalseTotal
if $#{$$self{"items"};} != $val->$#* 0 0 0
36 0 return $self->{items}= $val;
37 }
38  
39 118 ▶sub weights {
Subroutine weights: called CC=2 SCAR=9.2
Pod documented
40 118 my $self= shift;
41 118 ▶ return $self->{weights} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 118 0 118
42 0 delete $self->{_odds_table};
43 0 return $self->{weights}= shift;
44 }
45  
46  
47 108 ▶sub generate {
Subroutine generate: called CC=8 SCAR=20.8
Pod documented
48 108 my $self= shift;
49 108 my $items= $self->items;
50 108 my $pick;
51 108 ▶ if (!$self->weights) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->weights) { } 7 101 108
52 7 $pick= rand( scalar @$items );
53 } else {
54 # binary search for the random number
55 101 my $tbl= $self->_odds_table;
56 101 my ($min, $max, $r)= (0, $#$items, rand);
57 101 ▶ while ($min+1 < $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min + 1 < $max) 141 101 242
58 141 my $mid= int(($max+$min)/2);
59 141 ▶ if ($r < $tbl->[$mid]) { $max= $mid-1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($r < $tbl->[$mid]) { } 60 81 141
60 81 else { $min= $mid; }
61 }
62 101 ▶ $pick= ($max > $min && $tbl->[$max] <= $r)? $max : $min;
Branch: 1 branch
BranchTrueFalseTotal
$max > $min && $tbl->[$max] <= $r ? : 69 32 101
Condition100%3/3
$max > $min and $tbl->[$max] <= $r
!ll&&!rl&&r
13169
MC/DC100%2/2
$max > $min and $tbl->[$max] <= $r
$max > $min $tbl->[$max] <= $r
Truth table100%3/3
A and B
A= $max > $minB= $tbl->[$max] <= $r
ABresult
0X0
100
111
63 }
64 108 my $cmp_item= $items->[$pick];
65 108 ▶ return $cmp_item unless ref $cmp_item && ref($cmp_item)->can('generate');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cmp_item and ref($cmp_item)->can("generate") 104 4 108
Condition66%2/3
ref $cmp_item and ref($cmp_item)->can("generate")
!ll&&!rl&&r
10404
MC/DC50%1/2
ref $cmp_item and ref($cmp_item)->can("generate")
ref $cmp_item ref($cmp_item)->can("generate")
Truth table66%2/3
A and B
A= ref $cmp_itemB= ref($cmp_item)->can("generate")
ABresult
0X0
100
111
66 4 $cmp_item->generate(@_);
67 }
68  
69 101 ▶sub _odds_table {
Subroutine _odds_table: called CC=2 SCAR=7.2
70 101 ▶ $_[0]{_odds_table} ||= $_[0]->_build__odds_table;
Condition66%2/3
$_[0]{"_odds_table"} ||= $_[0]->_build__odds_table
l!l&&r!l&&!r
9920
MC/DC0%0/2
$_[0]{"_odds_table"} ||= $_[0]->_build__odds_table
$_[0]{"_odds_table"} $_[0]->_build__odds_table
Truth table66%2/3
A ||= B
A= $_[0]{"_odds_table"}B= $_[0]->_build__odds_table
ABresult
1X1
011
000
71 }
72  
73 2 ▶sub _build__odds_table {
Subroutine _build__odds_table: called CC=3 SCAR=11.0
74 2 my $self= shift;
75 2 my $items= $self->items;
76 2 my $weights= $self->weights;
77 2 my $total= 0;
78 $total += ($weights->[$_] ||= 1)
79 2 ▶ for 0..$#$items;
Condition50%1/2
$weights->[$_] ||= 1
l!l
70
MC/DC0%0/1
$weights->[$_] ||= 1
$weights->[$_]
Truth table50%1/2
A
A= $weights->[$_]
Aresult
11
00
80 2 my $sum= 0;
81 2 return [ map { my $x= $sum; $sum += $_; $x/$total } @$weights ]
82 }
83  
84  
85 5 ▶sub compile {
Subroutine compile: called CC=8 SCAR=29.3
Pod documented
86 5 my $self= shift;
87 5 my $items= $self->items;
88 5 my @compiled;
89 5 for (@$items) {
90 5 ▶ if (ref && ref->can('generate')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_ and ref($_)->can("generate")) 2 3 5
Condition66%2/3
ref $_ and ref($_)->can("generate")
!ll&&!rl&&r
302
MC/DC50%1/2
ref $_ and ref($_)->can("generate")
ref $_ ref($_)->can("generate")
Truth table66%2/3
A and B
A= ref $_B= ref($_)->can("generate")
ABresult
0X0
100
111
91 # Some items are generators. Compile a list of them, but only the generators.
92 2 ▶ @compiled= map +(ref && ref->can('generate')? $_->compile : undef), @$items;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ && ref($_)->can('generate') ? : 2 0 2
Condition33%1/3
ref $_ and ref($_)->can("generate")
!ll&&!rl&&r
002
MC/DC0%0/2
ref $_ and ref($_)->can("generate")
ref $_ ref($_)->can("generate")
Truth table33%1/3
A and B
A= ref $_B= ref($_)->can("generate")
ABresult
0X0
100
111
93 2 last;
94 }
95 }
96
97 5 ▶ if (!$self->weights) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->weights) { } 5 0 5
98 return !@compiled
99 2 ▶ ? sub { $items->[int rand scalar @$items] }
Subroutine __ANON__: called CC=1 SCAR=0.0
100 2 ▶ : sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
101 2 my $pick= int rand scalar @$items;
102 2 ▶ $compiled[$pick]? $compiled[$pick]->(@_) : $items->[$pick];
Branch: 1 branch
BranchTrueFalseTotal
$compiled[$pick] ? : 2 0 2
103 5 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
!@compiled ? : 3 2 5
104 }
105 else {
106 0 my $odds_table= $self->_odds_table;
107 0 ▶ return sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
108 # binary search for the random number
109 0 my ($min, $max, $r)= (0, $#$items, rand);
110 0 ▶ while ($min+1 < $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min + 1 < $max) 0 0 0
111 0 my $mid= int(($max+$min)/2);
112 0 ▶ if ($r < $odds_table->[$mid]) { $max= $mid-1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($r < $odds_table->[$mid]) { } 0 0 0
113 0 else { $min= $mid; }
114 }
115 0 ▶ my $pick= ($max > $min && $odds_table->[$max] <= $r)? $max : $min;
Branch: 1 branch
BranchTrueFalseTotal
$max > $min && $odds_table->[$max] <= $r ? : 0 0 0
Condition0%0/3
$max > $min and $odds_table->[$max] <= $r
!ll&&!rl&&r
000
MC/DC0%0/2
$max > $min and $odds_table->[$max] <= $r
$max > $min $odds_table->[$max] <= $r
Truth table0%0/3
A and B
A= $max > $minB= $odds_table->[$max] <= $r
ABresult
0X0
100
111
116 0 ▶ $compiled[$pick]? $compiled[$pick]->(@_) : $items->[$pick];
Branch: 1 branch
BranchTrueFalseTotal
$compiled[$pick] ? : 0 0 0
117 }
118 0 }
119 }
120  
121  
122 1 ▶sub combine_generator {
Subroutine combine_generator: called CC=8 SCAR=29.3
Pod documented
123 1 my ($self, $peer)= @_;
124 1 my @items= @{$self->items};
125 1 my $weights= $self->weights;
126 1 ▶ if ($peer->isa('Mock::Data::Set')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($peer->isa('Mock::Data::Set')) { } 1 0 1
127 1 my $peer_items= $peer->items;
128 1 my $peer_weights= $peer->weights;
129 1 ▶ if ($weights || $peer_weights) {
Branch: 1 branch
BranchTrueFalseTotal
if ($weights or $peer_weights) 0 1 1
Condition33%1/3
$weights or $peer_weights
l!l&&r!l&&!r
001
MC/DC0%0/2
$weights or $peer_weights
$weights $peer_weights
Truth table33%1/3
A or B
A= $weightsB= $peer_weights
ABresult
1X1
011
000
130 0 ▶ $weights= [
Branch: 2 branches
BranchTrueFalseTotal
$weights ? : 0 0 0
$peer_weights ? : 0 0 0
131 $weights? @$weights : (map 1, @items),
132 $peer_weights? @$peer_weights : (map 1, @$peer_items),
133 ];
134 }
135 1 push @items, @$peer_items;
136 } else {
137 0 push @items, $peer;
138 0 ▶ $weights= $weights && @$weights? [ @$weights, List::Util::sum0(@$weights)/@$weights ] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$weights && @$weights ? : 0 0 0
Condition0%0/3
$weights and @$weights
!ll&&!rl&&r
000
MC/DC0%0/2
$weights and @$weights
$weights @$weights
Truth table0%0/3
A and B
A= $weightsB= @$weights
ABresult
0X0
100
111
139 }
140 1 return Mock::Data::Set->new(
141 items => \@items,
142 weights => $weights,
143 );
144 }
145  
146 12 require Mock::Data::Util;
147  
148 __END__
« Data/Plugin/Text.pm ↑ Summary Data/Plugin/Number.pm »