| 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
Condition66%4/6
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 and ref $_[0] eq "ARRAY"
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"
Truth table66%2/3
A and B
A= @_ == 1B= ref $_[0] eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 2 | my (@items, @weights); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 2 | ▶ | while (@_) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | my $val= shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | ▶ | delete $self->{_odds_table} if $#{$self->{items}} != $#$val; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 141 | my $mid= int(($max+$min)/2); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 141 | ▶ | if ($r < $tbl->[$mid]) { $max= $mid-1; } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 81 | else { $min= $mid; } | |||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 101 | ▶ | $pick= ($max > $min && $tbl->[$max] <= $r)? $max : $min; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$max > $min and $tbl->[$max] <= $r
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
ref $cmp_item and ref($cmp_item)->can("generate")
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")
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC0%0/1
$weights->[$_] ||= 1
$weights->[$_]
Truth table50%1/2
A
A= $weights->[$_]
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
ref $_ and ref($_)->can("generate")
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")
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
ref $_ and ref($_)->can("generate")
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")
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 2 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 5 | ▶ | if (!$self->weights) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 5 | ▶ | }; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | my $mid= int(($max+$min)/2); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | if ($r < $odds_table->[$mid]) { $max= $mid-1; } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | else { $min= $mid; } | |||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | ▶ | my $pick= ($max > $min && $odds_table->[$max] <= $r)? $max : $min; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$max > $min and $odds_table->[$max] <= $r
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | ▶ | $compiled[$pick]? $compiled[$pick]->(@_) : $items->[$pick]; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 1 | my $peer_items= $peer->items; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 1 | my $peer_weights= $peer->weights; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 1 | ▶ | if ($weights || $peer_weights) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$weights or $peer_weights
MC/DC0%0/2
$weights or $peer_weights
$weights
$peer_weights
Truth table33%1/3
A or B
A= $weightsB= $peer_weights
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | ▶ | $weights= [ | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
$weights and @$weights
MC/DC0%0/2
$weights and @$weights
$weights
@$weights
Truth table0%0/3
A and B
A= $weightsB= @$weights
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||||||||||