blib/lib/Backup/Hanoi.pm

filter: Statementstmt 100% 33 / 33 Branchbran 80.0% 8 / 10 Conditioncond 50.0% 2 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 5 / 5 Podpod 100% 2 / 2 totaltotal 89.2% 50 / 56 CC 8 SCAR 11.0
CC
8
Cov
91%
get_device_for_cycle
4.0
new
3.0
_get_right_zeros_from_digital_representation
3.0
CRAP 3.0
↑ Summary bin/backup-hanoi »
1 package Backup::Hanoi;
2 # ABSTRACT: select backup according to algo
3 4 $Backup::Hanoi::VERSION = '0.003';
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 sub new {
8 7 ▶ my $class = shift;
Subroutine new: called CC=3 SCAR=11.1
Pod documented
9 7 ▶ my $devices = shift // [];
Condition50%1/2
shift() // []
l!l
70
MC/DC0%0/1
shift() // []
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
10  
11 7 ▶ die "You need at least three devices, for this to work.\n"
Branch: 1 branch
BranchTrueFalseTotal
if $devices < 3 0 7 7
12 if ($devices < 3);
13  
14 # the number of devices predicts the size of the cycles
15 7 my $device_count = scalar @{$devices};
16  
17 # half a hanoi cycle is just what we need for backup
18 7 my $hanoi_cycles_half = (2**$device_count) / 2;
19  
20 7 my $self = { devices => $devices,
21 hanoi_cycles_half => $hanoi_cycles_half,
22 };
23  
24 7 bless $self, $class;
25  
26 7 return $self;
27 }
28  
29 sub get_device_for_cycle {
30  
31 96 ▶ my $self = shift;
Subroutine get_device_for_cycle: called CC=4 SCAR=13.9
Pod documented
32 96 ▶ my $cycle = shift // 0;
Condition50%1/2
shift() // 0
l!l
960
MC/DC0%0/1
shift() // 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
33  
34 # treat negative numbers as normal FIFO
35 # e.g. index -1 gives the second last element
36 return $self->{devices}
37 96 ▶ ->[(scalar @{$self->{devices}}) + $cycle -1]
Branch: 1 branch
BranchTrueFalseTotal
if $cycle < 0 5 91 96
38 if ($cycle < 0);
39  
40 # allow cycle to cross hanoi limit, map it to first half
41 91 my $modulo_cycle = $cycle % $self->{hanoi_cycles_half};
42  
43 # change zero to maximum, so that zero gets highest device
44 $modulo_cycle = $self->{hanoi_cycles_half}
45 91 ▶ if ($modulo_cycle == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $modulo_cycle == 0 18 73 91
46  
47 # calculate which device is used for given cycle
48 91 my $hanoi_number =
49 _get_right_zeros_from_digital_representation(
50 $modulo_cycle);
51  
52 # select and return device
53 91 return $self->{devices}->[$hanoi_number];
54  
55 }
56  
57 sub _get_right_zeros_from_digital_representation {
58  
59 91 ▶ my $number = shift;
Subroutine _get_right_zeros_from_digital_representation: called CC=3 SCAR=11.0
60  
61 # convert to binary format: e.g. number 5 -> 101
62 91 my $binary = sprintf "%b", $number;
63  
64 # represent number zero as empty string
65 91 ▶ $binary = '' if ($binary == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $binary == 0 0 91 91
66  
67  
68 # count the zeros, at the right from the binary number
69  
70 91 my $zeros_from_the_right = 0;
71  
72 91 ▶ if ( $binary =~ /(0+)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($binary =~ /(0+)$/) 50 41 91
73 50 my $zero_capture = $1;
74  
75 50 $zeros_from_the_right = length $zero_capture;
76 }
77  
78 91 return $zeros_from_the_right;
79 }
80  
81 4 1;
82  
83 __END__
↑ Summary bin/backup-hanoi »