Moose/Sweep/Step.pm

filter: Statementstmt 91.8% 102 / 111 Branchbran 78.1% 25 / 32 Conditioncond 80.0% 12 / 15 MC/DCmcdc 55.5% 5 / 9 Subroutinesub 100% 14 / 14 Podpod 0.0% 0 / 5 totaltotal 84.9% 158 / 186 CC 22 SCAR 22.0
CC
22
Cov
88%
_build_points
17.3
sweep_finished
3.3
get_value
3.1
CRAP 9.0
« Moose/Sweep/Continuous/Time.pm ↑ Summary Moose/Sweep.pm »
1 package Lab::Moose::Sweep::Step;
2 2 $Lab::Moose::Sweep::Step::VERSION = '3.931';
3 #ABSTRACT: Base class for step/list sweeps
4  
5 2 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7  
8 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Moose::Util::TypeConstraints 'enum';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use MooseX::Params::Validate;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # Do not import all functions as they clash with the attribute methods.
14 2 ▶use Lab::Moose 'linspace';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 extends 'Lab::Moose::Sweep';
19  
20 #
21 # Public attributes set by the user
22 #
23  
24 2 has from => ( is => 'ro', isa => 'Num', predicate => 'has_from' );
25 2 has to => ( is => 'ro', isa => 'Num', predicate => 'has_to' );
26 2 has step =>
27 ( is => 'ro', isa => 'Lab::Moose::PosNum', predicate => 'has_step' );
28  
29 2 has points =>
30 ( is => 'ro', isa => 'ArrayRef[Num]', predicate => 'has_points' );
31 2 has steps => ( is => 'ro', isa => 'ArrayRef[Num]', predicate => 'has_steps' );
32  
33 2 has list => (
34 is => 'ro', isa => 'ArrayRef[Num]', predicate => 'has_list',
35 writer => '_list'
36 );
37 2 has backsweep => ( is => 'ro', isa => 'Bool', default => 0 );
38  
39 2 has both_directions => ( is => 'ro', isa => 'Bool', default => 0 );
40  
41 2 has setter => ( is => 'ro', isa => 'CodeRef', required => 1 );
42  
43 #
44 # Private attributes used internally
45 #
46  
47 2 has _points => (
48 is => 'ro', isa => 'ArrayRef[Num]', lazy => 1, init_arg => undef,
49 builder => '_build_points', traits => ['Array'],
50 handles => { get_point => 'get', num_points => 'count', points_array => 'elements' },
51 writer => 'write_points',
52 );
53  
54 2 has index => (
55 is => 'ro', isa => 'Int', default => 0, init_arg => undef,
56 writer => '_index'
57 );
58  
59 2 has current_value => (
60 is => 'ro', isa => 'Num', init_arg => undef,
61 writer => '_current_value'
62 );
63  
64 2 my $error_msg = <<"EOF";
65 give either (from => ..., to => ..., step => ...)
66 or (list => [...])
67 or (points => [...], steps => [....])
68 or (points => [...], step => )
69  
70 EOF
71  
72 18 ▶sub _build_points {
Subroutine _build_points: called CC=16 SCAR=28.5
73 18 my $self = shift;
74 18 ▶ my $has_from_to_step
Condition66%4/6
$self->has_from && $self->has_to && $self->has_step
!ll&&!rl&&r
4014
$self->has_from and $self->has_to
!ll&&!rl&&r
4014
MC/DC33%1/3
$self->has_from && $self->has_to && $self->has_step
$self->has_from $self->has_to $self->has_step
Truth table50%2/4
A and B && C
A= $self->has_fromB= $self->has_toC= $self->has_step
ABCresult
0XX0
10X0
1100
1111
75 = $self->has_from && $self->has_to && $self->has_step;
76 18 my $has_list = $self->has_list;
77 18 ▶ my $has_points_steps = $self->has_points && $self->has_steps;
Condition100%3/3
$self->has_points && $self->has_steps
!ll&&!rl&&r
1611
MC/DC100%2/2
$self->has_points && $self->has_steps
$self->has_points $self->has_steps
Truth table100%3/3
A && B
A= $self->has_pointsB= $self->has_steps
ABresult
0X0
100
111
78 18 ▶ my $has_points_step = $self->has_points && $self->has_step;
Condition100%3/3
$self->has_points && $self->has_step
!ll&&!rl&&r
1611
MC/DC100%2/2
$self->has_points && $self->has_step
$self->has_points $self->has_step
Truth table100%3/3
A && B
A= $self->has_pointsB= $self->has_step
ABresult
0X0
100
111
79 18 ▶ if ( $has_from_to_step + $has_list + $has_points_steps + $has_points_step
Branch: 1 branch
BranchTrueFalseTotal
if ($has_from_to_step + $has_list + $has_points_steps + $has_points_step != 1) 0 18 18
80 != 1 ) {
81 0 croak $error_msg;
82 }
83  
84 18 my @points;
85  
86 18 ▶ if ($has_list) {
Branch: 2 branches
BranchTrueFalseTotal
if ($has_list) { } 2 16 18
elsif ($has_from_to_step) { } 14 2 16
87 2 @points = @{ $self->list() };
88 2 ▶ if ( @points < 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@points < 1) 0 2 2
89 0 croak "list needs at least 1 point";
90 }
91 }
92 elsif ($has_from_to_step) {
93 14 @points = linspace(
94 from => $self->from,
95 to => $self->to,
96 step => $self->step
97 );
98 }
99 else {
100 # points_steps or points_step
101 2 my @steps;
102 2 my @corner_points = @{ $self->points };
103 2 ▶ if ( @corner_points < 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@corner_points < 2) 0 2 2
104 0 croak "points array needs at least two elements";
105 }
106  
107 2 ▶ if ($has_points_steps) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_points_steps) { } 1 1 2
108 1 @steps = @{ $self->steps };
109 }
110 else {
111 1 @steps = ( $self->step );
112 }
113 2 ▶ if ( @steps >= @corner_points ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@steps >= @corner_points) 0 2 2
114 0 croak "steps array exceeds points array";
115 }
116 2 push @steps, map { $steps[-1] } ( 1 .. @corner_points - @steps - 1 );
117 2 my ( $p1, $p2 );
118 2 $p1 = shift @corner_points;
119 2 ▶ while (@corner_points) {
Branch: 1 branch
BranchTrueFalseTotal
if (@corner_points) 4 2 6
120 4 my $p2 = shift @corner_points;
121 4 my $step = shift @steps;
122 4 push @points, linspace( from => $p1, to => $p2, step => $step );
123 4 $p1 = $p2;
124 }
125 }
126  
127 18 ▶ if ( $self->backsweep ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->backsweep) 1 17 18
128 1 my @backsweep_points = reverse @points;
129 1 push @points, @backsweep_points;
130 }
131 18 ▶ if ( $self->backsweep && $self->both_directions ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->backsweep and $self->both_directions) 0 18 18
Condition66%2/3
$self->backsweep and $self->both_directions
!ll&&!rl&&r
1710
MC/DC0%0/2
$self->backsweep and $self->both_directions
$self->backsweep $self->both_directions
Truth table66%2/3
A and B
A= $self->backsweepB= $self->both_directions
ABresult
0X0
100
111
132 0 croak "Can't use backsweep and both_directions together."
133 }
134  
135 18 return \@points;
136 }
137  
138 36 ▶sub start_sweep {
Subroutine start_sweep: called CC=1 SCAR=0.0
Pod undocumented
139  
140 # do nothing
141 }
142  
143 142 ▶sub go_to_next_point {
Subroutine go_to_next_point: called CC=1 SCAR=0.0
Pod undocumented
144 142 my $self = shift;
145 142 my $index = $self->index();
146 142 my $point = $self->get_point($index);
147 142 my $setter = $self->setter();
148 142 $self->$setter($point);
149 142 $self->_current_value($point);
150 142 $self->_index( ++$index );
151 }
152  
153 36 ▶sub go_to_sweep_start {
Subroutine go_to_sweep_start: called CC=1 SCAR=0.0
Pod undocumented
154 36 my $self = shift;
155 36 my $point = $self->get_point(0);
156 36 my $setter = $self->setter();
157 36 $self->$setter($point);
158 36 $self->_current_value($point);
159 36 $self->_index(0);
160 }
161  
162 178 ▶sub sweep_finished {
Subroutine sweep_finished: called CC=3 SCAR=11.8
Pod undocumented
163 178 my $self = shift;
164 178 my $index = $self->index();
165 178 ▶ if ( $index >= $self->num_points ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index >= $self->num_points) 36 142 178
166 36 ▶ if ( $self->both_directions ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->both_directions) 0 36 36
167 0 my @points = $self->points_array;
168 0 @points = reverse @points;
169 0 $self->write_points( \@points );
170 }
171 36 return 1;
172 }
173 142 return 0;
174 }
175  
176 36 ▶sub _in_backsweep {
Subroutine _in_backsweep: called CC=3 SCAR=11.0
177 36 my $self = shift;
178 36 ▶ if ( $self->backsweep ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->backsweep) { } 6 30 36
179 6 ▶ if ( $self->index > $self->num_points / 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->index > $self->num_points / 2) 3 3 6
180 3 return 1;
181 }
182 }
183 else {
184 30 return 0;
185 }
186 }
187  
188 36 ▶sub get_value {
Subroutine get_value: called CC=3 SCAR=11.2
Pod undocumented
189 36 my $self = shift;
190 36 ▶ if ( not defined $self->current_value() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->current_value) 0 36 36
191 0 croak "sweep not yet started";
192 }
193 36 my $value = sprintf( "%.14g", $self->current_value );
194 36 ▶ if ( $self->_in_backsweep ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_in_backsweep) 3 33 36
195 3 $value .= '_backsweep';
196 }
197 36 return $value;
198 }
199  
200 2 __PACKAGE__->meta->make_immutable();
201 2 1;
202  
203 __END__
« Moose/Sweep/Continuous/Time.pm ↑ Summary Moose/Sweep.pm »