blib/lib/Math/MagicSquare/Generator.pm

filter: Statementstmt 70.5% 48 / 68 Branchbran 55.0% 11 / 20 Conditioncond 41.6% 5 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 58.3% 7 / 12 Podpod 100% 8 / 8 totaltotal 62.7% 79 / 126 CC 25 SCAR 21.9
CC
25
Cov
64%
new
12.3
as_string
12.0
check
11.4
CRAP 8.9
↑ Summary
1 package Math::MagicSquare::Generator;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 $VERSION = '0.01';
7  
8 3948 ▶sub _sum {
Subroutine _sum: called CC=2 SCAR=6.9
9 3948 my $sum = 0;
10 3948 $sum += $_ for @_;
11 3948 return $sum
12 }
13  
14 20 ▶sub new {
Subroutine new: called CC=11 SCAR=25.1
Pod documented
15 20 my ($class, %opt) = @_;
16 20 ▶ $opt{size} ||= 5;
Condition50%1/2
$opt{"size"} ||= 5
l!l
200
MC/DC0%0/1
$opt{"size"} ||= 5
$opt{"size"}
Truth table50%1/2
A
A= $opt{"size"}
Aresult
11
00
17 20 ▶ $opt{start} ||= 1;
Condition50%1/2
$opt{"start"} ||= 1
l!l
020
MC/DC0%0/1
$opt{"start"} ||= 1
$opt{"start"}
Truth table50%1/2
A
A= $opt{"start"}
Aresult
11
00
18 20 ▶ $opt{step} ||= 1;
Condition50%1/2
$opt{"step"} ||= 1
l!l
020
MC/DC0%0/1
$opt{"step"} ||= 1
$opt{"step"}
Truth table50%1/2
A
A= $opt{"step"}
Aresult
11
00
19 croak "Size needs to be a positive, odd integer"
20 unless $opt{size} > 0 and
21 $opt{size} % 2 and
22 20 ▶ $opt{size} == int($opt{size});
Branch: 1 branch
BranchTrueFalseTotal
unless $opt{"size"} > 0 and $opt{"size"} % 2 and $opt{"size"} == int $opt{"size"} 0 20 20
Condition33%2/6
$opt{"size"} > 0 and $opt{"size"} % 2 and $opt{"size"} == int $opt{"size"}
!ll&&!rl&&r
0020
$opt{"size"} > 0 and $opt{"size"} % 2
!ll&&!rl&&r
0020
MC/DC0%0/3
$opt{"size"} > 0 and $opt{"size"} % 2 and $opt{"size"} == int $opt{"size"}
$opt{"size"} > 0 $opt{"size"} % 2 $opt{"size"} == int $opt{"size"}
Truth table25%1/4
A and B and C
A= $opt{"size"} > 0B= $opt{"size"} % 2C= $opt{"size"} == int $opt{"size"}
ABCresult
0XX0
10X0
1100
1111
23 my $self = [
24 map {
25 1944 [ (undef) x $opt{size} ]
26 } 1..$opt{size}
27 20 ];
28 20 my $value = $opt{start};
29 20 my $halv = int(@$self / 2);
30 20 for my $start_x (-$halv..$halv) {
31 1944 my $x = $start_x - 1;
32 1944 my $y = $x + @$self + 1;
33 1944 for (1 .. @$self) {
34 254772 ▶ $x = $x - @$self if ++$x > $#$self;
Branch: 1 branch
BranchTrueFalseTotal
if ++$x > $self->$#* 962 253810 254772
35 254772 ▶ $y = $y - @$self if --$y > $#$self;
Branch: 1 branch
BranchTrueFalseTotal
if --$y > $self->$#* 962 253810 254772
36 254772 $self->[$y][$x] = $value;
37 254772 $value += $opt{step};
38 }
39 }
40 20 return bless $self, $class;
41 }
42  
43 0 ▶sub hflip {
Subroutine hflip: not called CC=2 SCAR=17.9
Pod documented
44 0 my ($self) = @_;
45 0 my $clone;
46 0 push @$clone, [ reverse @$_ ] for @$self;
47 0 return bless $clone, ref $self;
48 }
49  
50 0 ▶sub vflip {
Subroutine vflip: not called CC=2 SCAR=17.9
Pod documented
51 0 my ($self) = @_;
52 0 my $clone;
53 0 push @$clone, [ @$_ ] for reverse @$self;
54 0 return bless $clone, ref $self;
55 }
56  
57 20 ▶sub sum {
Subroutine sum: called CC=1 SCAR=0.0
Pod documented
58 20 my ($self) = @_;
59 20 return _sum( @{ $self->[0] } );
60 }
61  
62 20 ▶sub check {
Subroutine check: called CC=10 SCAR=24.3
Pod documented
63 20 my ($self) = @_;
64 20 my $sum = $self->sum;
65 # Horizontals
66 20 for (@$self[1..$#$self]) {
67 1924 ▶ return undef if @$_ > @$self; # undef if not square
Branch: 1 branch
BranchTrueFalseTotal
if @$_ > @$self 0 1924 1924
68 1924 ▶ return undef if _sum(@$_) != $sum;
Branch: 1 branch
BranchTrueFalseTotal
if _sum(@$_) != $sum 0 1924 1924
69 }
70 # Verticals
71 20 for my $x (0..$#$self) {
72 1944 ▶ return undef if _sum(map $self->[$_][$x], 0..$#$self) != $sum;
Branch: 1 branch
BranchTrueFalseTotal
if _sum(map($self->[$_][$x], 0 .. $self->$#*)) != $sum 0 1944 1944
73 }
74 # Diagonals
75 20 ▶ return undef if _sum(map $self->[$_][$_], 0..$#$self) != $sum;
Branch: 1 branch
BranchTrueFalseTotal
if _sum(map($self->[$_][$_], 0 .. $self->$#*)) != $sum 0 20 20
76 20 ▶ return undef if _sum(map $self->[$#$self - $_][$_], 0..$#$self) != $sum;
Branch: 1 branch
BranchTrueFalseTotal
if _sum(map($$self[$self->$#* - $_][$_], 0 .. $self->$#*)) != $sum 0 20 20
77 # Duplicates
78 20 my %seen;
79 20 $seen{$_}++ for map @$_, @$self;
80 20 ▶ return undef if _sum(values %seen) != keys %seen;
Branch: 1 branch
BranchTrueFalseTotal
if _sum(values %seen) != keys %seen 0 20 20
81 # Passed all tests!
82 20 return $sum;
83 }
84  
85 0 ▶sub as_string {
Subroutine as_string: not called CC=3 SCAR=24.8
Pod documented
86 0 my ($self) = @_;
87 0 my $max = 0;
88 0 ▶ length > $max and $max = length for map @$_, @$self;
Branch: 1 branch
BranchTrueFalseTotal
length $_ > $max and $max = length $_ 0 0 0
89 0 return map { join(' ', map {' 'x($max - length) . $_} @$_) . "\n" } @$self;
90 }
91  
92 0 ▶sub as_html {
Subroutine as_html: not called CC=1 SCAR=6.9
Pod documented
93 0 my ($self) = @_;
94 return "<table>\n" . join("\n",
95 0 map { '<tr><td>' . join('</td><td>', @$_) . '</td></tr>' } @$self) .
96 "\n</table>\n";
97 }
98  
99 0 ▶sub as_csv {
Subroutine as_csv: not called CC=1 SCAR=6.9
Pod documented
100 0 my ($self) = @_;
101 0 return join("\n", map { join ',', @$_ } @$self) . "\n";
102 }
103  
104 1 1;
105  
106 __END__
↑ Summary