PlanePath/File.pm

filter: Statementstmt 40.1% 45 / 112 Branchbran 8.7% 4 / 46 Conditioncond 5.5% 1 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 80.0% 12 / 15 Podpod 100% 7 / 7 totaltotal 33.0% 69 / 209 CC 30 SCAR 46.2
CC
30
Cov
28%
_read
196.8
xy_to_n
90.0
n_to_xy
20.0
CRAP 101.1
« PlanePath/AR2W2Curve.pm ↑ Summary PlanePath/OctagramSpiral.pm »
1 # Copyright 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 Kevin Ryde
2  
3 # This file is part of Math-PlanePath.
4 #
5 # Math-PlanePath is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the Free
7 # Software Foundation; either version 3, or (at your option) any later
8 # version.
9 #
10 # Math-PlanePath is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 # for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with Math-PlanePath. If not, see <http://www.gnu.org/licenses/>.
17  
18  
19 # maybe file type for sequence of turns instead of x,y
20  
21  
22  
23 package Math::PlanePath::File;
24 1 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 1 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 $VERSION = 128;
30 1 ▶use Math::PlanePath;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 @ISA = ('Math::PlanePath');
32  
33 use Math::PlanePath::Base::Generic
34 1 'round_nearest',
35 1 ▶ 'is_infinite';
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 # uncomment this to run the ### lines
38 #use Devel::Comments;
39  
40  
41 sub n_start {
42 1 ▶ my ($self) = @_;
Subroutine n_start: called CC=2 SCAR=7.0
Pod documented
43 1 ▶ if (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) 1 0 1
44 1 _read($self);
45 }
46 1 return $self->SUPER::n_start;
47 }
48 1 ▶sub x_negative { return _read($_[0])->{'x_negative'} }
Subroutine x_negative: called CC=1 SCAR=0.0
Pod documented
49 1 ▶sub y_negative { return _read($_[0])->{'y_negative'} }
Subroutine y_negative: called CC=1 SCAR=0.0
Pod documented
50 0 ▶sub figure { return _read($_[0])->{'figure'} }
Subroutine figure: not called CC=1 SCAR=6.9
Pod documented
51  
52 1 use constant parameter_info_array =>
53 [ { name => 'filename',
54 display => 'Filename',
55 type => 'filename',
56 width => 40,
57 default => '',
58 description => 'File name to read.',
59 1 ▶ } ];
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 sub n_to_xy {
62 0 ▶ my ($self, $n) = @_;
Subroutine n_to_xy: not called CC=4 SCAR=30.0
Pod documented
63 0 ▶ if ($n < $self->{'n_start'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < $self->{"n_start"}) 0 0 0
64 0 return;
65 }
66 0 ▶ if (is_infinite($n)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 0 0
67 0 return;
68 }
69 0 ▶ if (defined (my $x = _read($self)->{'x_array'}->[$n])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $x = _read($self)->{"x_array"}[$n])) 0 0 0
70 0 return ($x, $self->{'y_array'}->[$n]);
71 }
72 0 return;
73 }
74  
75 sub xy_to_n {
76 0 ▶ my ($self, $x, $y) = @_;
Subroutine xy_to_n: not called CC=9 SCAR=45.0
Pod documented
77  
78 # lazy xy_hash creation
79 0 ▶ if (! defined $self->{'xy_hash'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"xy_hash"}) 0 0 0
80 0 my %xy_hash;
81 0 _read($self)->{'xy_hash'} = \%xy_hash;
82 0 my $x_array = $self->{'x_array'};
83 0 my $y_array = $self->{'y_array'};
84 0 ▶ for (my $n = 0; $n <= $#$x_array; $n++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n <= $x_array->$#*) 0 0 0
85 0 ▶ if (defined (my $nx = $x_array->[$n])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $nx = $x_array->[$n])) 0 0 0
86 0 $xy_hash{"$nx,$y_array->[$n]"} = $n;
87  
88 # && $nx == int($nx)
89 # if ($ny == int($ny)) {
90 # }
91 }
92 }
93 }
94  
95 {
96 0 ▶ my $key = ($self->{'figure'} eq 'square'
Branch: 1 branch
BranchTrueFalseTotal
$self->{'figure'} eq 'square' ? : 0 0 0
97 ? round_nearest($x).','.round_nearest($y)
98 : "$x,$y");
99 0 ▶ if (defined (my $n = _read($self)->{'xy_hash'}->{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $n = _read($self)->{"xy_hash"}{$key})) 0 0 0
100 0 return $n;
101 }
102 }
103  
104 0 my $x_array = $self->{'x_array'};
105 0 my $y_array = $self->{'y_array'};
106 0 ▶ for (my $n = 0; $n <= $#$x_array; $n++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n <= $x_array->$#*) 0 0 0
107 0 ▶ defined (my $nx = $x_array->[$n]) or next;
Branch: 1 branch
BranchTrueFalseTotal
defined(my $nx = $x_array->[$n]) or next 0 0 0
108 0 my $ny = $y_array->[$n];
109 0 ▶ if (($x-$nx)**2 + ($y-$ny)**2 <= .25) {
Branch: 1 branch
BranchTrueFalseTotal
if (($x - $nx) ** 2 + ($y - $ny) ** 2 <= "0.25") 0 0 0
110 0 return $n;
111 }
112 }
113 0 return undef;
114 }
115  
116 # exact
117 sub rect_to_n_range {
118 1 ▶ my ($self) = @_;
Subroutine rect_to_n_range: called CC=1 SCAR=0.0
Pod documented
119 1 _read($self);
120 1 return ($self->{'n_start'}, $self->{'n_last'});
121 }
122  
123 1 my $num = "-?(?:\\.[0-9]+|[0-9]+(?:\\.[0-9]*)?)(?:[eE]-?[0-9]+)?";
124  
125 sub _read {
126 4 ▶ my ($self) = @_;
Subroutine _read: called CC=18 SCAR=52.8
127 4 ▶ if (defined $self->{'n_start'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"n_start"}) 3 1 4
128 3 return $self;
129 }
130  
131 1 my $n = 1;
132 1 $self->{'n_start'} = $n;
133 1 $self->{'n_last'} = $n-1; # default no range
134 1 $self->{'x_negative'} = 0;
135 1 $self->{'y_negative'} = 0;
136 1 $self->{'figure'} = 'square';
137  
138 1 my $filename = $self->{'filename'};
139 1 ▶ if (! defined $filename || $filename =~ /^\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $filename or $filename =~ /^\s*$/) 1 0 1
Condition33%1/3
not defined $filename or $filename =~ /^\s*$/
l!l&&r!l&&!r
100
MC/DC0%0/2
not defined $filename or $filename =~ /^\s*$/
not defined $filename $filename =~ /^\s*$/
Truth table33%1/3
not(A) or B
A= not defined $filenameB= $filename =~ /^\s*$/
ABresult
1X1
011
000
140 1 return $self;
141 }
142 0 my $fh;
143 0 ▶ ($] >= 5.006
Branch: 2 branches
BranchTrueFalseTotal
$] >= "5.006" ? open($fh, "<", $filename) : open($fh, "< $filename") or croak("Cannot open ", $filename, ": ", $!) 0 0 0
$] >= 5.006 ? : 0 0 0
144 ? open $fh, '<', $filename
145 : open $fh, "< $filename")
146 or croak "Cannot open ",$filename,": ",$!;
147  
148 0 my $n_start;
149 my @x_array;
150 0 my @y_array;
151 0 my $x_negative = 0;
152 0 my $y_negative = 0;
153 0 my $any_frac = 0;
154 0 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 0 0 0
155 0 ▶ $line =~ /^\s*-?\.?[0-9]/
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /^\s*-?\.?[0-9]/ or next 0 0 0
156 or next;
157 $line =~ /^\s*($num)[ \t,]+($num)([ \t,]+($num))?/o
158 0 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /^\s*($num)[ \t,]+($num)([ \t,]+($num))?/o or do { warn $filename, ":", $., ": File unrecognised line: ", $line; next } 0 0 0
159 0 warn $filename,':',$.,": File unrecognised line: ",$line;
160 0 next;
161 };
162 0 my ($x,$y);
163 0 ▶ if (defined $4) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $4) { } 0 0 0
164 0 $n = $1;
165 0 $x = $2;
166 0 $y = $4;
167 } else {
168 0 $x = $1;
169 0 $y = $2;
170 }
171 0 $x_array[$n] = $x;
172 0 $y_array[$n] = $y;
173 0 ▶ $x_negative ||= ($x < 0);
Condition0%0/3
$x_negative ||= $x < 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$x_negative ||= $x < 0
$x_negative $x < 0
Truth table0%0/3
A ||= B
A= $x_negativeB= $x < 0
ABresult
1X1
011
000
174 0 ▶ $y_negative ||= ($y < 0);
Condition0%0/3
$y_negative ||= $y < 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$y_negative ||= $y < 0
$y_negative $y < 0
Truth table0%0/3
A ||= B
A= $y_negativeB= $y < 0
ABresult
1X1
011
000
175 0 ▶ $any_frac ||= ($x != int($x) || $y != int($y));
Condition0%0/6
$any_frac ||= $x != int $x || $y != int $y
l!l&&r!l&&!r
000
$x != int $x || $y != int $y
l!l&&r!l&&!r
000
MC/DC0%0/3
$any_frac ||= $x != int $x || $y != int $y
$any_frac $x != int $x $y != int $y
Truth table0%0/4
A ||= B || C
A= $any_fracB= $x != int $xC= $y != int $y
ABCresult
1XX1
01X1
0011
0000
176 0 ▶ if (! defined $n_start || $n < $n_start) { $n_start = $n; }
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $n_start or $n < $n_start) 0 0 0
Condition0%0/3
not defined $n_start or $n < $n_start
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $n_start or $n < $n_start
not defined $n_start $n < $n_start
Truth table0%0/3
not(A) or B
A= not defined $n_startB= $n < $n_start
ABresult
1X1
011
000
177 ### $x
178 ### $y
179 0 $n++;
180 }
181  
182 0 ▶ close $fh
Branch: 1 branch
BranchTrueFalseTotal
close $fh or croak("Error closing ", $filename, ": ", $!) 0 0 0
183 or croak "Error closing ",$filename,": ",$!;
184  
185 0 $self->{'x_array'} = \@x_array;
186 0 $self->{'y_array'} = \@y_array;
187 0 $self->{'x_negative'} = $x_negative;
188 0 $self->{'y_negative'} = $y_negative;
189 0 $self->{'n_start'} = $n_start;
190 0 $self->{'n_last'} = $#x_array; # last n index
191 0 ▶ if ($any_frac) { $self->{'figure'} = 'circle' }
Branch: 1 branch
BranchTrueFalseTotal
if ($any_frac) 0 0 0
192 0 return $self;
193 }
194  
195 1 1;
196 __END__
« PlanePath/AR2W2Curve.pm ↑ Summary PlanePath/OctagramSpiral.pm »