blib/lib/Search/Binary.pm

filter: Statementstmt 95.2% 40 / 42 Branchbran 81.2% 13 / 16 Conditioncond 60.0% 3 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 5 / 5 Podpod 0.0% 0 / 1 totaltotal 86.1% 62 / 72 SCAR 24.1
CC
11
Cov
89%
binary_search
11.4
BEGIN
1.0
BEGIN
1.0
CRAP 11.2
↑ Summary
1 package Search::Binary;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 our @EXPORT = qw(binary_search);
8  
9 3 our $VERSION = '0.96_02';
10  
11 sub binary_search {
12 24 ▶ my ($posmin, $posmax, $target, $readfn, $handle, $smallblock) = @_;
Subroutine binary_search: called CC=11 SCAR=24.3
Pod undocumented
13 24 ▶ $smallblock ||= 0;
Condition50%1/2
$smallblock ||= 0
l!l
024
MC/DC0%0/1
$smallblock ||= 0
$smallblock
Truth table50%1/2
A
A= $smallblock
Aresult
11
00
14 24 ▶ if ($posmin > $posmax) {
Branch: 1 branch
BranchTrueFalseTotal
if ($posmin > $posmax) 1 23 24
15 1 carp 'First argument must be less then or equal to second argument'
16 . " (min: $posmin, max: $posmax)";
17 1 return 0; # some libraries rely on this behavior
18 }
19  
20 23 my ($x, $compare, $mid);
21  
22 # assert $posmin <= $posmax
23  
24 23 my $seeks = my $reads = 0;
25 23 my $lastmid = int($posmin + (($posmax - $posmin) / 2)) - 1;
26 23 ▶ while ($posmax - $posmin > $smallblock) {
Branch: 1 branch
BranchTrueFalseTotal
if ($posmax - $posmin > $smallblock) 97 4 101
27  
28 # assert: $posmin is the beginning of a record
29 # and $target >= index value for that record
30  
31 97 $seeks++;
32 97 $x = int($posmin + (($posmax - $posmin) / 2));
33 97 ($compare, $mid) = $readfn->($handle, $target, $x);
34  
35 97 ▶ unless (defined($compare)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $compare) 0 97 97
36 0 $posmax = $mid;
37 0 next;
38 }
39 97 ▶ last if ($mid == $lastmid);
Branch: 1 branch
BranchTrueFalseTotal
if $mid == $lastmid 19 78 97
40 78 ▶ if ($compare > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($compare > 0) { } 37 41 78
41 37 $posmin = $mid;
42 } else {
43 41 $posmax = $mid;
44 }
45 78 $lastmid = $mid;
46 }
47  
48 # Switch to sequential search.
49  
50 23 $x = $posmin;
51 23 ▶ while ($posmin <= $posmax) {
Branch: 1 branch
BranchTrueFalseTotal
if ($posmin <= $posmax) 44 0 44
52  
53 # same loop invarient as above applies here
54  
55 44 $reads++;
56 44 ($compare, $posmin) = $readfn->($handle, $target, $x);
57 44 ▶ last unless (defined($compare) && $compare > 0);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $compare and $compare > 0 23 21 44
Condition66%2/3
defined $compare and $compare > 0
!ll&&!rl&&r
02321
MC/DC50%1/2
defined $compare and $compare > 0
defined $compare $compare > 0
Truth table66%2/3
A and B
A= defined $compareB= $compare > 0
ABresult
0X0
100
111
58 21 $x = undef;
59 }
60 23 ▶ return wantarray ? ($posmin, $seeks, $reads) : $posmin;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 23 23
61 }
62  
63 3 1;
64 __END__
↑ Summary