NumSeq/File.pm

filter: Statementstmt 90.1% 55 / 61 Branchbran 55.0% 11 / 20 Conditioncond 36.3% 4 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 91.6% 11 / 12 Podpod 100% 2 / 2 totaltotal 74.1% 83 / 112 CC 14 SCAR 17.6
CC
14
Cov
76%
next
10.0
rewind
7.5
_seek
6.0
CRAP 5.8
« NumSeq/GolayRudinShapiroCumulative.pm ↑ Summary NumSeq/GoldbachCount.pm »
1 # Copyright 2011, 2012, 2013, 2014, 2016, 2019, 2020 Kevin Ryde
2  
3 # This file is part of Math-NumSeq.
4 #
5 # Math-NumSeq is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by the
7 # Free Software Foundation; either version 3, or (at your option) any later
8 # version.
9 #
10 # Math-NumSeq 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-NumSeq. If not, see <http://www.gnu.org/licenses/>.
17  
18  
19 # characteristic('increasing')
20 # characteristic('integer')
21 # only by reading the whole file
22 # assume seekable
23  
24  
25  
26 package Math::NumSeq::File;
27 1 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 1 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
33 1 $VERSION = 75;
34  
35 1 ▶use Math::NumSeq;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 @ISA = ('Math::NumSeq');
37 1 *_to_bigint = \&Math::NumSeq::_to_bigint;
38  
39 # uncomment this to run the ### lines
40 #use Smart::Comments;
41  
42  
43 # use constant name => Math::NumSeq::__('File');
44 1 ▶use constant i_start => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 1 ▶use constant description => Math::NumSeq::__('Numbers from a file');
Subroutine BEGIN: called CC=1 SCAR=0.0
46 1 ▶use constant parameter_info_array =>
Subroutine BEGIN: called CC=1 SCAR=0.0
47 [ { name => 'filename',
48 type => 'filename',
49 display => Math::NumSeq::__('Filename'),
50 width => 40,
51 default => '',
52 1 } ];
53  
54 1 my $max_value = POSIX::FLT_RADIX() ** (POSIX::DBL_MANT_DIG()-5);
55 1 ▶if ((~0 >> 1) > $max_value) {
Branch: 1 branch
BranchTrueFalseTotal
if ('9223372036854775807' > $max_value) 1 0 1
56 1 $max_value = (~0 >> 1);
57 }
58 1 my $min_value = -$max_value;
59  
60 1 ▶sub rewind {
Subroutine rewind: called CC=6 SCAR=20.2
Pod documented
61 1 my ($self) = @_;
62 ### Values-File rewind()
63  
64 1 ▶ if ($self->{'fh'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'fh'}) { } 0 1 1
65 0 _seek ($self, 0);
66 } else {
67 1 my $filename = $self->{'filename'};
68 1 ▶ if (defined $filename && $filename !~ /^\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filename and not $filename =~ /^\s*$/) 1 0 1
Condition33%1/3
defined $filename and not $filename =~ /^\s*$/
!ll&&!rl&&r
001
MC/DC0%0/2
defined $filename and not $filename =~ /^\s*$/
defined $filename not $filename =~ /^\s*$/
Truth table33%1/3
A and not(B)
A= defined $filenameB= not $filename =~ /^\s*$/
ABresult
0X0
100
111
69 1 my $fh;
70 1 ▶ ($] >= 5.006
Branch: 2 branches
BranchTrueFalseTotal
$] >= "5.006" ? open($fh, "<", $filename) : open($fh, "< $filename") or croak("Cannot open ", $filename, ": ", $!) 1 0 1
$] >= 5.006 ? : 1 0 1
71 ? open $fh, '<', $filename
72 : open $fh, "< $filename")
73 or croak "Cannot open ",$filename,": ",$!;
74 1 $self->{'fh'} = $fh;
75 }
76 }
77 1 $self->{'i'} = $self->i_start - 1;
78 }
79  
80 4 ▶sub next {
Subroutine next: called CC=8 SCAR=23.0
Pod documented
81 4 my ($self) = @_;
82 4 ▶ my $fh = $self->{'fh'} || return;
Condition50%1/2
$self->{'fh'} || (return)
l!l
40
MC/DC0%0/1
$self->{'fh'} || (return)
$self->{'fh'}
Truth table50%1/2
A
A= $self->{'fh'}
Aresult
11
00
83 4 for (;;) {
84 22 my $line = readline $fh;
85 22 ▶ if (! defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $line) 1 21 22
86 ### EOF ...
87 1 return;
88 }
89  
90 21 ▶ if ($line =~ /^\s*(-?\d+)(\s+(-?(\d+(\.\d*)?|\.\d+)))?/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*(-?\d+)(\s+(-?(\d+(\.\d*)?|\.\d+)))?/) 3 18 21
91 3 my ($i, $value);
92 3 ▶ if (defined $3) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $3) { } 3 0 3
93 3 $i = $self->{'i'} = $1;
94 3 $value = $3;
95 } else {
96 0 $i = ++$self->{'i'};
97 0 $value = $1;
98 }
99  
100 # large integer values returned as Math::BigInt
101 3 ▶ if (($value > $max_value || $value < $min_value)
Branch: 1 branch
BranchTrueFalseTotal
if ($value > $max_value || $value < $min_value and not $value =~ /\./) 0 3 3
Condition33%2/6
$value > $max_value || $value < $min_value and not $value =~ /\./
!ll&&!rl&&r
300
$value > $max_value or $value < $min_value
l!l&&r!l&&!r
003
MC/DC0%0/3
$value > $max_value || $value < $min_value and not $value =~ /\./
$value > $max_value $value < $min_value not $value =~ /\./
Truth table20%1/5
A or B and not(C)
A= $value > $max_valueB= $value < $min_valueC= not $value =~ /\./
ABCresult
00X0
1X00
0100
1X11
0111
102 && $value !~ /\./) {
103 0 $value = _to_bigint($value);
104 }
105  
106 3 return ($i, $value);
107 }
108 }
109 }
110  
111 0 ▶sub _seek {
Subroutine _seek: not called CC=2 SCAR=17.9
112 0 my ($self, $pos) = @_;
113 0 ▶ seek ($self->{'fh'}, $pos, 0)
Branch: 1 branch
BranchTrueFalseTotal
seek $self->{"fh"}, $pos, 0 or croak(qq[Cannot seek $self->{"filename"}: $!]) 0 0 0
114 or croak "Cannot seek $self->{'filename'}: $!";
115 }
116  
117 1 1;
118 __END__
« NumSeq/GolayRudinShapiroCumulative.pm ↑ Summary NumSeq/GoldbachCount.pm »