blib/lib/Geometry/Formula.pm

filter: Statementstmt 99.3% 148 / 149 Branchbran 97.7% 43 / 44 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 27 / 27 Podpod 100% 21 / 21 totaltotal 99.1% 239 / 241 CC 25 SCAR 10.6
CC
25
Cov
99%
_param_check
8.0
circle
3.0
frustum_of_right_circular_cone
3.0
CRAP 2.9
↑ Summary
1 package Geometry::Formula;
2  
3 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 22 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 22 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 22 my $PI = 3.1415926;
7  
8 22 our $VERSION = 0.02;
9  
10 20 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
11 20 my $class = shift;
12 20 my $self = {};
13 20 bless $self, $class;
14 20 return $self;
15 }
16  
17 6 ▶sub annulus {
Subroutine annulus: called CC=1 SCAR=0.0
Pod documented
18 6 my ( $self, %param, $x ) = @_;
19 6 _param_check( 'annulus', %param );
20  
21 $x = $PI *
22 1 ( $self->_squared( $param{'outer_radius'} ) - $self->_squared( $param{'inner_radius'} ) );
23  
24 1 return $x;
25 }
26  
27 10 ▶sub circle {
Subroutine circle: called CC=3 SCAR=11.0
Pod documented
28 10 my ( $self, %param, $x ) = @_;
29 10 _param_check( 'circle', %param );
30  
31 3 ▶ if ( $param{'formula'} eq 'area' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($param{'formula'} eq 'area') { } 1 2 3
elsif ($param{'formula'} eq 'circumference') { } 1 1 2
32 1 $x = $PI * $self->_squared( $param{'radius'} );
33 }
34 elsif ( $param{'formula'} eq 'circumference' ) {
35 1 $x = ( 2 * $PI ) * $param{'radius'};
36 }
37 else {
38 1 $x = $param{'radius'} * 2;
39 }
40  
41 3 return $x;
42 }
43  
44 6 ▶sub cone {
Subroutine cone: called CC=1 SCAR=0.0
Pod documented
45 6 my ( $self, %param, $x ) = @_;
46 6 _param_check( 'cone', %param );
47  
48 1 $x = ( 1 / 3 ) * ( $param{'base'} * $param{'height'} );
49  
50 1 return $x;
51 }
52  
53 7 ▶sub cube {
Subroutine cube: called CC=2 SCAR=6.9
Pod documented
54 7 my ( $self, %param, $x ) = @_;
55 7 _param_check( 'cube', %param );
56  
57 2 ▶ if ( $param{'formula'} eq 'surface_area' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param{'formula'} eq 'surface_area') { } 1 1 2
58 1 $x = 6 * ( $param{'a'} * 2 );
59 }
60 else {
61 1 $x = $self->_cubed( $param{'a'} );
62 }
63  
64 2 return $x;
65 }
66  
67 11 ▶sub ellipse {
Subroutine ellipse: called CC=2 SCAR=6.9
Pod documented
68 11 my ( $self, %param, $x ) = @_;
69 11 _param_check( 'ellipse', %param );
70  
71 2 ▶ if ( $param{'formula'} eq 'area' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param{'formula'} eq 'area') { } 1 1 2
72 1 $x = $PI * ( $param{'a'} * $param{'b'} );
73 }
74 else {
75 $x = 2 * $PI *
76 1 sqrt( ( $self->_squared( $param{'a'} ) + $self->_squared( $param{'b'} ) ) / 2 );
77 }
78  
79 2 return $x;
80 }
81  
82 8 ▶sub ellipsoid {
Subroutine ellipsoid: called CC=1 SCAR=0.0
Pod documented
83 8 my ( $self, %param, $x ) = @_;
84 8 _param_check( 'ellipsoid', %param );
85  
86 1 $x = ( 4 / 3 ) * $PI * $param{'a'} * $param{'b'} * $param{'c'};
87  
88 1 return $x;
89 }
90  
91 4 ▶sub equilateral_triangle {
Subroutine equilateral_triangle: called CC=1 SCAR=0.0
Pod documented
92 4 my ( $self, %param, $x ) = @_;
93 4 _param_check( 'equilateral_triangle', %param );
94  
95 1 $x = $self->_squared( $param{'side'} ) * ( sqrt(3) / 4 );
96  
97 1 return $x;
98 }
99  
100 22 ▶sub frustum_of_right_circular_cone {
Subroutine frustum_of_right_circular_cone: called CC=3 SCAR=11.0
Pod documented
101 22 my ( $self, %param, $x ) = @_;
102 22 _param_check( 'frustum_of_right_circular_cone', %param );
103  
104 3 ▶ if ( $param{'formula'} eq 'lateral_surface_area' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($param{'formula'} eq 'lateral_surface_area') { } 1 2 3
elsif ($param{'formula'} eq 'total_surface_area') { } 1 1 2
105 $x =
106 $PI *
107 ( $param{'large_radius'} + $param{'small_radius'} ) *
108 sqrt( $self->_squared( $param{'large_radius'} - $param{'small_radius'} ) +
109 1 $self->_squared( $param{'slant_height'} ) );
110 }
111 elsif ( $param{'formula'} eq 'total_surface_area' ) {
112 my $slant_height =
113 sqrt( $self->_squared( $param{'large_radius'} - $param{'small_radius'} ) +
114 1 $self->_squared( $param{'height'} ) );
115  
116 $x =
117 $PI *
118 ( $param{'small_radius'} *
119 ( $param{'small_radius'} + $slant_height )
120 + $param{'large_radius'} *
121 1 ( $param{'large_radius'} + $slant_height ) );
122  
123 }
124 else {
125 $x = (
126 $PI * (
127 $self->_squared( $param{'small_radius'} ) +
128 ( $param{'small_radius'} * $param{'large_radius'} ) +
129 $self->_squared( $param{'large_radius'} )
130 1 ) * $param{'height'}
131 ) / 3;
132 }
133  
134 3 return $x;
135 }
136  
137 11 ▶sub parallelogram {
Subroutine parallelogram: called CC=2 SCAR=6.9
Pod documented
138 11 my ( $self, %param, $x ) = @_;
139 11 _param_check( 'parallelogram', %param );
140  
141 2 ▶ if ( $param{'formula'} eq 'area' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param{'formula'} eq 'area') { } 1 1 2
142 1 $x = $param{'base'} * $param{'height'};
143 }
144 else {
145 1 $x = ( 2 * $param{'a'} ) + ( 2 * $param{'b'} );
146 }
147  
148 2 return $x;
149 }
150  
151 11 ▶sub rectangle {
Subroutine rectangle: called CC=2 SCAR=6.9
Pod documented
152 11 my ( $self, %param, $x ) = @_;
153 11 _param_check( 'rectangle', %param );
154  
155 2 ▶ if ( $param{'formula'} eq 'area' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param{'formula'} eq 'area') { } 1 1 2
156 1 $x = $param{'length'} * $param{'width'};
157 }
158 else {
159 1 $x = ( 2 * $param{'length'} ) + ( 2 * $param{'width'} );
160 }
161  
162 2 return $x;
163 }
164  
165 15 ▶sub rectangular_solid {
Subroutine rectangular_solid: called CC=2 SCAR=6.9
Pod documented
166 15 my ( $self, %param, $x ) = @_;
167 15 _param_check( 'rectangular_solid', %param );
168  
169 2 ▶ if ( $param{'formula'} eq 'volume' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param{'formula'} eq 'volume') { } 1 1 2
170 1 $x = $param{'length'} * $param{'width'} * $param{'height'};
171 }
172 else {
173 $x =
174 2 *
175 ( ( $param{'length'} * $param{'width'} ) +
176 ( $param{'width'} * $param{'height'} ) +
177 1 ( $param{'length'} * $param{'height'} ) );
178 }
179  
180 2 return $x;
181 }
182  
183 6 ▶sub rhombus {
Subroutine rhombus: called CC=1 SCAR=0.0
Pod documented
184 6 my ( $self, %param, $x ) = @_;
185 6 _param_check( 'rhombus', %param );
186  
187 1 $x = ( $param{'a'} * $param{'b'} ) / 2;
188  
189 1 return $x;
190 }
191  
192 11 ▶sub right_circular_cone {
Subroutine right_circular_cone: called CC=2 SCAR=6.9
Pod documented
193 11 my ( $self, %param, $x ) = @_;
194 11 _param_check( 'right_circular_cone', %param );
195  
196 2 ▶ if ( $param{'formula'} eq 'lateral_surface_area' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param{'formula'} eq 'lateral_surface_area') { } 1 1 2
197 $x =
198 $PI *
199 $param{'radius'} *
200 1 ( sqrt( $self->_squared( $param{'radius'} ) + $self->_squared( $param{'height'} ) ) );
201 }
202 else {
203 1 $x = ( 1 / 3 ) * $PI * $self->_squared( $param{'radius'} ) * $param{'height'};
204 }
205  
206 2 return $x;
207 }
208  
209 16 ▶sub right_circular_cylinder {
Subroutine right_circular_cylinder: called CC=3 SCAR=11.0
Pod documented
210 16 my ( $self, %param, $x ) = @_;
211 16 _param_check( 'right_circular_cylinder', %param );
212  
213 3 ▶ if ( $param{'formula'} eq 'lateral_surface_area' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($param{'formula'} eq 'lateral_surface_area') { } 1 2 3
elsif ($param{'formula'} eq 'total_surface_area') { } 1 1 2
214 1 $x = 2 * $PI * $param{'radius'} * $param{'height'};
215 }
216 elsif ( $param{'formula'} eq 'total_surface_area' ) {
217 $x =
218 1 2 * $PI * $param{'radius'} * ( $param{'radius'} + $param{'height'} );
219 }
220 else {
221 1 $x = $PI * ( $self->_squared( $param{'radius'} ) * $param{'height'} );
222 }
223  
224 3 return $x;
225 }
226  
227 6 ▶sub sector_of_circle {
Subroutine sector_of_circle: called CC=1 SCAR=0.0
Pod documented
228 6 my ( $self, %param, $x ) = @_;
229 6 _param_check( 'sector_of_circle', %param );
230  
231 1 $x = ( $param{'theta'} / 360 ) * $PI * $self->_squared( $param{'radius'} );
232  
233 1 return $x;
234 }
235  
236 7 ▶sub sphere {
Subroutine sphere: called CC=2 SCAR=6.9
Pod documented
237 7 my ( $self, %param, $x ) = @_;
238 7 _param_check( 'sphere', %param );
239  
240 2 ▶ if ( $param{'formula'} eq 'surface_area' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param{'formula'} eq 'surface_area') { } 1 1 2
241 1 $x = 4 * $PI * $self->_squared( $param{'radius'} );
242 }
243 else {
244 1 $x = ( 4 / 3 ) * $PI * $self->_cubed( $param{'radius'} );
245 }
246  
247 2 return $x;
248 }
249  
250 7 ▶sub square {
Subroutine square: called CC=2 SCAR=6.9
Pod documented
251 7 my ( $self, %param, $x ) = @_;
252 7 _param_check( 'square', %param );
253  
254 2 ▶ if ( $param{'formula'} eq 'area' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param{'formula'} eq 'area') { } 1 1 2
255 1 $x = $self->_squared( $param{'side'} );
256 }
257 else {
258 1 $x = $param{'side'} * 4;
259 }
260  
261 2 return $x;
262 }
263  
264 11 ▶sub torus {
Subroutine torus: called CC=2 SCAR=6.9
Pod documented
265 11 my ( $self, %param, $x ) = @_;
266 11 _param_check( 'torus', %param );
267  
268 2 ▶ if ( $param{'formula'} eq 'surface_area' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param{'formula'} eq 'surface_area') { } 1 1 2
269 1 $x = 4 * $self->_squared($PI) * $param{'a'} * $param{'b'};
270 }
271 else {
272 1 $x = 2 * $self->_squared($PI) * $self->_squared( $param{'a'} ) * $param{'b'};
273 }
274  
275 2 return $x;
276 }
277  
278 17 ▶sub trapezoid {
Subroutine trapezoid: called CC=2 SCAR=6.9
Pod documented
279 17 my ( $self, %param, $x ) = @_;
280 17 _param_check( 'trapezoid', %param );
281  
282 2 ▶ if ( $param{'formula'} eq 'area' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param{'formula'} eq 'area') { } 1 1 2
283 1 $x = ( ( $param{'a'} + $param{'b'} ) / 2 ) * $param{'height'};
284 }
285 else {
286 1 $x = $param{'a'} + $param{'b'} + $param{'c'} + $param{'d'};
287 }
288  
289 2 return $x;
290 }
291  
292 13 ▶sub triangle {
Subroutine triangle: called CC=2 SCAR=6.9
Pod documented
293 13 my ( $self, %param, $x ) = @_;
294 13 _param_check( 'triangle', %param );
295  
296 2 ▶ if ( $param{'formula'} eq 'area' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param{'formula'} eq 'area') { } 1 1 2
297 1 $x = .5 * $param{'base'} * $param{'height'};
298 }
299 else {
300 1 $x = $param{'a'} + $param{'b'} + $param{'c'};
301 }
302  
303 2 return $x;
304 }
305  
306 22 ▶sub _squared {
Subroutine _squared: called CC=1 SCAR=0.0
307 22 my ( $self, $num ) = @_;
308  
309 22 return $num ** 2;
310 }
311  
312 2 ▶sub _cubed {
Subroutine _cubed: called CC=1 SCAR=0.0
313 2 my ( $self, $num ) = @_;
314  
315 2 return $num ** 3;
316 }
317  
318 205 ▶sub _param_check {
Subroutine _param_check: called CC=8 SCAR=20.8
319 205 my ( $method, %param ) = @_;
320  
321 205 my %valid_params = (
322 annulus => { area => [ 'inner_radius', 'outer_radius' ] },
323 circle => {
324 area => ['radius'],
325 circumference => ['radius'],
326 diameter => ['radius']
327 },
328 cone => { volume => [ 'base', 'height' ] },
329 cube => {
330 surface_area => ['a'],
331 volume => ['a']
332 },
333 ellipse => {
334 area => [ 'a', 'b' ],
335 perimeter => [ 'a', 'b' ]
336 },
337 ellipsoid => { volume => [ 'a', 'b', 'c' ] },
338 equilateral_triangle => { area => ['side'] },
339 frustum_of_right_circular_cone => {
340 lateral_surface_area =>
341 [ 'slant_height', 'small_radius', 'large_radius' ],
342 total_surface_area => [ 'height', 'small_radius', 'large_radius' ],
343 volume => [ 'height', 'small_radius', 'large_radius' ]
344 },
345 parallelogram => {
346 area => [ 'base', 'height' ],
347 perimeter => [ 'a', 'b' ]
348 },
349 rectangle => {
350 area => [ 'length', 'width' ],
351 perimeter => [ 'length', 'width' ]
352 },
353 rectangular_solid => {
354 surface_area => [ 'length', 'width', 'height' ],
355 volume => [ 'length', 'width', 'height' ]
356 },
357 rhombus => { area => [ 'a', 'b' ] },
358 right_circular_cone => {
359 lateral_surface_area => [ 'radius', 'height' ],
360 volume => [ 'radius', 'height' ]
361 },
362 right_circular_cylinder => {
363 total_surface_area => [ 'radius', 'height' ],
364 lateral_surface_area => [ 'radius', 'height' ],
365 volume => [ 'radius', 'height' ]
366 },
367 sector_of_circle => { area => [ 'theta', 'radius' ] },
368 sphere => {
369 surface_area => ['radius'],
370 volume => ['radius']
371 },
372 square => {
373 area => ['side'],
374 perimeter => ['side']
375 },
376 torus => {
377 surface_area => [ 'a', 'b' ],
378 volume => [ 'a', 'b' ]
379 },
380 trapezoid => {
381 area => [ 'a', 'b', 'height' ],
382 perimeter => [ 'a', 'b', 'c', 'd' ]
383 },
384 triangle => {
385 area => [ 'base', 'height' ],
386 perimeter => [ 'a', 'b', 'c' ]
387 },
388 );
389  
390 # validate that parameter values are defined and numeric
391 205 foreach ( @{ $valid_params{$method}{ $param{'formula'} } } ) {
392 croak "required parameter '$_' not defined"
393 317 ▶ if !$param{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless $param{$_} 74 243 317
394  
395 croak "parameter '$_' requires a numeric value"
396 243 ▶ if $param{$_} !~ m/^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $param{$_} =~ /^\d+$/ 74 169 243
397 }
398  
399 # validate parameter is a valid constructor/component of formula
400 57 foreach my $param ( keys %param ) {
401 137 ▶ next if $param eq 'formula';
Branch: 1 branch
BranchTrueFalseTotal
if $param eq "formula" 43 94 137
402  
403 94 my @constructors = @{ $valid_params{$method}{ $param{'formula'} } };
404  
405 94 ▶ if ( !@constructors ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@constructors) 20 74 94
406 20 croak "invalid formula name: $param{'formula'} specified";
407 }
408  
409 74 ▶ if ( grep { $_ eq $param } @constructors ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq $param;} @constructors) { } 74 0 74
410 74 next;
411 }
412 else {
413 0 croak "invalid parameter '$param' specified for $method";
414 }
415 }
416  
417 37 return 0;
418 }
419  
420 22 1;
421  
422 __END__
↑ Summary