blib/lib/List/Insertion.pm

filter: Statementstmt 66.0% 140 / 212 Branchbran 31.1% 28 / 90 Conditioncond 71.4% 20 / 28 MC/DCmcdc 37.5% 6 / 16 Subroutinesub 75.6% 28 / 37 Podpod 100% 1 / 1 totaltotal 58.0% 223 / 384 CC 60 SCAR 21.8
CC
60
Cov
57%
make_search
30.4
import
10.1
__ANON__
6.9
CRAP 8.8
↑ Summary
1 package List::Insertion;
2  
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Template::Plex;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Data::Combination;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 1 our $VERSION = 'v0.2.1';
12  
13 sub make_search;
14  
15 8 ▶sub import {
Subroutine import: called CC=10 SCAR=23.1
16  
17 8 shift;
18 8 my @import=@_;
19  
20 # Generate subs based on import options
21 8 my ($package)=caller;
22  
23 # Import make search if requested
24 #
25 8 ▶ if(@import==1 and grep /make_search/, @import){
Branch: 1 branch
BranchTrueFalseTotal
if (@import == 1 and grep /make_search/, @import) 1 7 8
Condition100%3/3
@import == 1 and grep /make_search/, @import
!ll&&!rl&&r
161
MC/DC100%2/2
@import == 1 and grep /make_search/, @import
@import == 1 grep /make_search/, @import
Truth table100%3/3
A and B
A= @import == 1B= grep /make_search/, @import
ABresult
0X0
100
111
26 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 *{$package."::make_search"}=\&make_search;
28 1 return;
29 }
30
31 # Otherwise assume we have a list of specifications
32 7 my @spec;
33 7 push @spec, (Data::Combination::combinations $_)->@* for @import;
34
35 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
36 7 for my $spec(@spec){
37 13 ▶ $spec->{prefix}//="search";
Condition100%2/2
$spec->{"prefix"} //= "search"
l!l
103
MC/DC100%1/1
$spec->{"prefix"} //= "search"
$spec->{"prefix"}
Truth table100%2/2
A
A= $spec->{"prefix"}
Aresult
11
00
38 13 ▶ $spec->{type}//="string";
Condition50%1/2
$spec->{"type"} //= "string"
l!l
130
MC/DC0%0/1
$spec->{"type"} //= "string"
$spec->{"type"}
Truth table50%1/2
A
A= $spec->{"type"}
Aresult
11
00
39 13 ▶ $spec->{duplicate}//="left";
Condition100%2/2
$spec->{"duplicate"} //= "left"
l!l
121
MC/DC100%1/1
$spec->{"duplicate"} //= "left"
$spec->{"duplicate"}
Truth table100%2/2
A
A= $spec->{"duplicate"}
Aresult
11
00
40 13 ▶ $spec->{package}//=$package;
Condition33%1/3
$spec->{"package"} //= $package
l!l&&r!l&&!r
0130
MC/DC0%0/2
$spec->{"package"} //= $package
$spec->{"package"} $package
Truth table33%1/3
A //= B
A= $spec->{"package"}B= $package
ABresult
1X1
011
000
41  
42 13 my ($sub,$code)=make_search $spec;
43 13 ▶ *{$package."::".$spec->{name}}=$sub if $sub;
Branch: 1 branch
BranchTrueFalseTotal
if $sub 13 0 13
44 #say STDERR $code;
45 }
46 }
47  
48  
49  
50 1 my $template_base=
51 '
52 my \$middle;
53 my \$lower;
54 my \$upper;
55  
56 sub {
57 package $package;
58 my (\$key, \$array)=\@_;
59 \$lower = 0;
60 \$upper = \@\$array;
61 return 0 unless \$upper;
62  
63 use integer;
64 # TODO: Run in eval for accessor fall back
65 #
66 # local \$_;
67 while(\$lower<\$upper){
68 \$middle=(\$upper+\$lower)>>1;
69 (\$key $condition->{$fields{type}}{$fields{duplicate}} \$array->[\$middle]$accessor)
70 $update->{$fields{duplicate}}
71 }
72 \$lower;
73 }
74 ';
75  
76 1 my %condition=(
77 string=>{
78 left=>'le',
79 right=>'ge',
80 },
81 numeric=>{
82 left=>'<=',
83 right=>'>='
84 },
85  
86 );
87  
88  
89 1 my %update=(
90 left=>
91 '
92 ? ($upper=$middle)
93 : ($lower=$middle+1)
94 ',
95  
96 right=>
97  
98 '
99 ? ($lower=$middle+1)
100 : ($upper=$middle)
101 '
102 );
103
104  
105  
106 # Make a binary search optimised for types and avoid sub routine callbacks
107 #
108 16 ▶sub make_search {
Subroutine make_search: called CC=12 SCAR=34.1
Pod documented
109 16 my ($options)=@_;
110  
111 # Ensure at least a default value for the required fields
112 #
113 16 ▶ $options->{duplicate}//="left";
Condition50%1/2
$options->{"duplicate"} //= "left"
l!l
160
MC/DC0%0/1
$options->{"duplicate"} //= "left"
$options->{"duplicate"}
Truth table50%1/2
A
A= $options->{"duplicate"}
Aresult
11
00
114 16 ▶ $options->{type}//="string";
Condition50%1/2
$options->{"type"} //= "string"
l!l
160
MC/DC0%0/1
$options->{"type"} //= "string"
$options->{"type"}
Truth table50%1/2
A
A= $options->{"type"}
Aresult
11
00
115 16 ▶ $options->{accessor}//="";
Condition100%2/2
$options->{"accessor"} //= ""
l!l
79
MC/DC100%1/1
$options->{"accessor"} //= ""
$options->{"accessor"}
Truth table100%2/2
A
A= $options->{"accessor"}
Aresult
11
00
116 16 ▶ $options->{prefix}//="search";
Condition100%2/2
$options->{"prefix"} //= "search"
l!l
133
MC/DC100%1/1
$options->{"prefix"} //= "search"
$options->{"prefix"}
Truth table100%2/2
A
A= $options->{"prefix"}
Aresult
11
00
117 16 ▶ $options->{package}//=caller;
Condition66%2/3
$options->{"package"} //= caller
l!l&&r!l&&!r
1330
MC/DC0%0/2
$options->{"package"} //= caller
$options->{"package"} caller
Truth table66%2/3
A //= B
A= $options->{"package"}B= caller
ABresult
1X1
011
000
118  
119 # Attempt to normalise values
120 #
121 16 $options->{duplicate}=~s/lesser/left/;
122 16 $options->{duplicate}=~s/greater/right/;
123  
124 16 $options->{type}=~s/pv/string/i;
125 16 $options->{type}=~s/nv/numeric/i;
126 16 $options->{type}=~s/int/numeric/i;
127  
128 16 ▶ $options->{name}//="$options->{prefix}_$options->{type}_$options->{duplicate}";
Condition50%1/2
$options->{"name"} //= qq[$options->{"prefix"}_$options->{"type"}_$options->{"duplicate"}]
l!l
016
MC/DC0%0/1
$options->{"name"} //= qq[$options->{"prefix"}_$options->{"type"}_$options->{"duplicate"}]
$options->{"name"}
Truth table50%1/2
A
A= $options->{"name"}
Aresult
11
00
129  
130  
131 #Check fields values are supported
132  
133
134 die "Unsupported value for duplicate field: $options->{duplicate }. Must be left or right"
135 16 ▶ unless $options->{duplicate }=~/^(left|right)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $options->{"duplicate"} =~ /^(left|right)$/ 0 16 16
136 die "Unsupported value for type field: $options->{type}. Must be string, pv, nv or int"
137 16 ▶ unless $options->{type}=~/^(string|numeric)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $options->{"type"} =~ /^(string|numeric)$/ 0 16 16
138 die "Unsupported value for type field: $options->{accessor}. Must be post dereference/method call ->..."
139 16 ▶ unless $options->{accessor} eq "" or $options->{accessor}=~/^->/;
Branch: 1 branch
BranchTrueFalseTotal
unless $options->{"accessor"} eq "" or $options->{"accessor"} =~ /^->/ 0 16 16
Condition66%2/3
$options->{"accessor"} eq "" or $options->{"accessor"} =~ /^->/
l!l&&r!l&&!r
1240
MC/DC0%0/2
$options->{"accessor"} eq "" or $options->{"accessor"} =~ /^->/
$options->{"accessor"} eq "" $options->{"accessor"} =~ /^->/
Truth table66%2/3
A or B
A= $options->{"accessor"} eq ""B= $options->{"accessor"} =~ /^->/
ABresult
1X1
011
000
140  
141 16 my $template=Template::Plex->load( [$template_base], {condition=>\%condition, update=>\%update, accessor=>$options->{accessor}, package=>$options->{package}}, inject=>['use feature "signatures";']);
142 16 my $code_str=$template->render({duplicate =>$options->{duplicate}, type=>$options->{type}});
143 16 $template->cleanup;
144  
145 #use feature "say";
146 #use Error::Show;
147 0 ▶ my $sub=eval($code_str);
Subroutine BEGIN: called CC=1 SCAR=1.6
Subroutine BEGIN: called CC=1 SCAR=1.6
Subroutine BEGIN: called CC=1 SCAR=1.6
Subroutine BEGIN: called CC=1 SCAR=1.6
Subroutine BEGIN: called CC=1 SCAR=1.6
Subroutine BEGIN: called CC=1 SCAR=1.6
Subroutine BEGIN: called CC=1 SCAR=1.6
Subroutine BEGIN: called CC=1 SCAR=1.6
Subroutine BEGIN: called CC=1 SCAR=1.6
Subroutine BEGIN: called CC=1 SCAR=1.6
Subroutine BEGIN: called CC=1 SCAR=1.6
Subroutine BEGIN: called CC=1 SCAR=1.6
Subroutine BEGIN: called CC=1 SCAR=1.6
Subroutine BEGIN: called CC=1 SCAR=1.6
Subroutine BEGIN: called CC=1 SCAR=1.6
Subroutine BEGIN: called CC=1 SCAR=1.6
Subroutine __ANON__: not called CC=4 SCAR=19.2
Subroutine __ANON__: not called CC=4 SCAR=19.2
Subroutine __ANON__: not called CC=4 SCAR=19.2
Subroutine __ANON__: not called CC=4 SCAR=19.2
Subroutine __ANON__: not called CC=4 SCAR=19.2
Subroutine __ANON__: called CC=4 SCAR=19.2
Subroutine __ANON__: not called CC=4 SCAR=19.2
Subroutine __ANON__: called CC=4 SCAR=19.2
Subroutine __ANON__: not called CC=4 SCAR=19.2
Subroutine __ANON__: not called CC=4 SCAR=19.2
Subroutine __ANON__: called CC=4 SCAR=19.2
Subroutine __ANON__: called CC=4 SCAR=19.2
Subroutine __ANON__: not called CC=4 SCAR=19.2
Branch: 39 branches
BranchTrueFalseTotal
unless $upper 0 0 0
if ($lower < $upper) 0 0 0
$key le $array->[$middle] ? : 0 0 0
unless $upper 0 0 0
if ($lower < $upper) 0 0 0
$key >= $array->[$middle] ? : 0 0 0
unless $upper 0 0 0
if ($lower < $upper) 0 0 0
$key <= $array->[$middle] ? : 0 0 0
unless $upper 0 0 0
if ($lower < $upper) 0 0 0
$key >= $array->[$middle] ? : 0 0 0
unless $upper 0 0 0
if ($lower < $upper) 0 0 0
$key <= $array->[$middle] ? : 0 0 0
unless $upper 0 1 1
if ($lower < $upper) 2 1 3
$key >= $array->[$middle]{'key'} ? : 1 1 2
unless $upper 0 0 0
if ($lower < $upper) 0 0 0
$key <= $array->[$middle]{'key'} ? : 0 0 0
unless $upper 0 1 1
if ($lower < $upper) 2 1 3
$key >= $array->[$middle][0] ? : 1 1 2
unless $upper 0 0 0
if ($lower < $upper) 0 0 0
$key <= $array->[$middle][0] ? : 0 0 0
unless $upper 0 0 0
if ($lower < $upper) 0 0 0
$key ge $array->[$middle] ? : 0 0 0
unless $upper 0 3 3
if ($lower < $upper) 9 3 12
$key <= $array->[$middle] ? : 6 3 9
unless $upper 0 3 3
if ($lower < $upper) 9 3 12
$key >= $array->[$middle] ? : 3 6 9
unless $upper 0 0 0
if ($lower < $upper) 0 0 0
$key le $array->[$middle] ? : 0 0 0
148 #say STDERR Error::Show::context error=>$@, program=>$code_str if($@ or !$sub);
149 #say STDERR $code_str;
150 16 ▶ wantarray?($sub,$code_str):$sub;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 13 3 16
151 }
152  
153 1 1;
↑ Summary