examples/skip

filter: Statementstmt 89.8% 133 / 148 Branchbran 60.0% 36 / 60 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 90.3% 28 / 31 Podpod n/a totaltotal 79.9% 199 / 249 CC 15 SCAR 8.3
CC
15
Cov
80%
usage
6.0
resource_rss
6.0
__ANON__
4.2
CRAP 2.3
« lib/FP/DumperEqual.pm ↑ Summary lib/FP/Repl/AutoTrap.pm »
1 #!/usr/bin/env perl
2  
3 # Copyright (c) 2015,2020 Christian Jaeger, copying@christianjaeger.ch
4 # This is free software. See the file COPYING.md that came bundled
5 # with this file.
6  
7 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 23 ▶use warnings FATAL => 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # find modules from functional-perl working directory (not installed)
12 23 ▶use Cwd 'abs_path';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 23 our ($mydir, $myname);
14  
15 23 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=11.7
16 23 ▶ my $location = (-l $0) ? abs_path($0) : $0;
Branch: 1 branch
BranchTrueFalseTotal
-l $0 ? : 0 23 23
17 23 ▶ $location =~ /(.*?)([^\/]+?)_?\z/s or die "?";
Branch: 1 branch
BranchTrueFalseTotal
$location =~ m[(.*?)([^/]+?)_?\z]s or die "?" 23 0 23
18 23 ($mydir, $myname) = ($1, $2);
19 }
20 23 ▶use lib "$mydir/../lib";
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 23 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 23 ▶use FP::Predicates 'is_natural0';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 23 ▶use Chj::xopen 'glob_to_fh';
Subroutine BEGIN: called CC=1 SCAR=0.0
25 23 ▶use FP::IOStream 'fh_to_chunks';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 23 ▶use FP::Stream ":all";
Subroutine BEGIN: called CC=1 SCAR=0.0
27 23 ▶use FP::List ":all";
Subroutine BEGIN: called CC=1 SCAR=0.0
28 23 ▶use FP::Lazy ":all";
Subroutine BEGIN: called CC=1 SCAR=0.0
29 23 ▶use POSIX 'ESPIPE';
Subroutine BEGIN: called CC=1 SCAR=0.0
30 23 ▶use Chj::TEST;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 23 ▶use FP::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 23 our $BUFSIZ = 1024 * 16;
34  
35 0 ▶sub usage {
Subroutine usage: not called CC=2 SCAR=17.9
36 0 ▶ print STDERR map {"$_\n"} @_ if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
37 0 print "$myname n [m]
38  
39 Skip n bytes of input, output the rest. If m is given, don't output
40 the last m bytes. Example: `echo -n 'Hi World' | skip 3 2` -> 'Wor'
41  
42 --bufsize n use n instead of default $BUFSIZ as buffer size
43 --silent don't give error if the input is too short
44 ";
45 0 exit @_ ? 1 : 0;
46 }
47  
48 0 ▶sub resource_rss {
Subroutine resource_rss: not called CC=2 SCAR=17.9
49 0 ▶ @_ == 0 or fp_croak_arity 0;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 0 or fp_croak_arity(0) 0 0 0
50 0 require BSD::Resource;
51 0 (BSD::Resource::getrusage(BSD::Resource::RUSAGE_SELF()))[2]
52 }
53  
54 # ---- sliding buffer ----------------------------------------
55  
56 # Handle a sliding buffer window to make sure we don't output the
57 # input too soon (so that when we hit EOF, we've got at least m bytes
58 # still buffered). Do this by using a lazy list of chunks as the input
59 # and keep two pointers into it as the window.
60  
61 # `chunks_change_tail` returns a stream of the unmodified chunks until
62 # passing through one more would make the sum of the remaining chunks
63 # till the end of the stream smaller than $minsize; pass the remainder
64 # to &$fn($tail, $remainingsize) and use its result as the tail of the
65 # output stream. (See test cases below for illustration.)
66  
67 23 ▶sub chunks_change_tail {
Subroutine chunks_change_tail: called CC=3 SCAR=11.0
68 23 ▶ @_ == 3 or fp_croak_arity 3;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or fp_croak_arity(3) 23 0 23
69 23 my ($chunks, $minsize, $fn) = @_;
70 23 weaken $_[0];
71  
72 23 ▶ is_null $chunks and die "got empty input";
Branch: 1 branch
BranchTrueFalseTotal
is_null($chunks) and die "got empty input" 0 23 23
73  
74 # start and rest are parts of the same stream of chunks,
75 # windowsize is the number of bytes between them
76 23 my $next;
77 53 ▶ $next = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
78 53 ▶ @_ == 3 or fp_croak_arity 3;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or fp_croak_arity(3) 53 0 53
79 53 my ($start, $rest, $windowsize) = @_;
80 53 my $next = $next;
81 53 ▶ lazy {
Subroutine __ANON__: called CC=3 SCAR=11.0
82 NEXT: {
83 53 FORCE $start, $rest; # optional (since is_null, first
84 # etc. are forcing promises
85 # anyway), but might reduce
86 # overhead slightly?
87 114 my $first = first $start;
88 114 my $lenfirst = length $first;
89 114 my $reserve = $windowsize - $minsize;
90 114 ▶ if ($lenfirst <= $reserve) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lenfirst <= $reserve) { } 30 84 114
91 30 cons($first,
92 &$next(rest($start), $rest, $windowsize - $lenfirst))
93 } else {
94 84 ▶ if (is_null $rest) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_null($rest)) { } 23 61 84
95 23 &$fn($start, $windowsize)
96 } else {
97  
98 #&$next($start, rest $rest, $windowsize + length first $rest)
99 61 $windowsize = $windowsize + length first $rest;
100 61 $rest = rest $rest;
101 61 redo NEXT;
102 }
103 }
104 }
105 }
106 23 };
107 23 Weakened($next)->($chunks, $chunks, 0);
108 }
109  
110 7 ▶sub test_with_size {
Subroutine test_with_size: called CC=2 SCAR=7.0
111 7 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 7 0 7
112 7 my ($size) = @_;
113 stream_to_array chunks_change_tail(array_to_stream(["foo", "bars", "baz"]),
114 7 ▶ $size, sub { my ($tail, $len) = @_; cons $len, $tail });
Subroutine __ANON__: called CC=1 SCAR=0.0
115 }
116  
117 23 ▶use FP::DumperEqual;
Subroutine BEGIN: called CC=1 SCAR=0.0
118  
119 1 ▶TEST { test_with_size 1 }
Subroutine __ANON__: called CC=1 SCAR=0.0
120 23 ['foo', 'bars', 3, 'baz'];
121  
122 1 ▶TEST {
Subroutine __ANON__: called CC=2 SCAR=7.6
123 1 ▶ dumperequal(test_with_size(2), test_with_size(1))
Branch: 1 branch
BranchTrueFalseTotal
dumperequal(test_with_size(2), test_with_size(1)) and dumperequal(test_with_size(3), test_with_size(1)) 1 0 1
124 and dumperequal(test_with_size(3), test_with_size(1))
125 }
126 23 1;
127  
128 1 ▶TEST { test_with_size 4 }
Subroutine __ANON__: called CC=1 SCAR=0.0
129 23 ['foo', 7, 'bars', 'baz'];
130  
131 1 ▶TEST { test_with_size 999 }
Subroutine __ANON__: called CC=1 SCAR=0.0
132 23 [10, 'foo', 'bars', 'baz'];
133  
134 # ------------------------------------------------------------
135  
136 23 our $verbose = 0;
137 23 our $opt_repl;
138 23 our $opt_leaktest;
139 23 our $opt_silent;
140 GetOptions(
141 "verbose" => \$verbose,
142 0 ▶ "help" => sub {usage},
Subroutine __ANON__: not called CC=1 SCAR=6.9
143 23 ▶ "bufsize=n" => \$BUFSIZ,
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("verbose", \$main::verbose, "help", sub { usage(); } , "bufsize=n", \$main::BUFSIZ, "repl", \$main::opt_repl, "leaktest", \$main::opt_leaktest, "silent", \$main::opt_silent) or exit 1 23 0 23
144 "repl" => \$opt_repl,
145 "leaktest" => \$opt_leaktest,
146 "silent" => \$opt_silent,
147 ) or exit 1;
148  
149 # called for testing or debugging:
150  
151 23 ▶if ($opt_repl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::opt_repl) 0 23 23
152 0 require FP::Repl;
153 0 FP::Repl::repl();
154 0 exit 0
155 }
156  
157 23 ▶if (perhaps_run_tests "main") {
Branch: 1 branch
BranchTrueFalseTotal
if (perhaps_run_tests("main")) 1 22 23
158 1 exit 0;
159 }
160  
161 # called as tool:
162  
163 22 ▶usage unless (@ARGV == 1 or @ARGV == 2);
Branch: 1 branch
BranchTrueFalseTotal
unless @ARGV == 1 or @ARGV == 2 0 22 22
Condition33%1/3
@ARGV == 1 or @ARGV == 2
l!l&&r!l&&!r
0220
MC/DC0%0/2
@ARGV == 1 or @ARGV == 2
@ARGV == 1 @ARGV == 2
Truth table33%1/3
A or B
A= @ARGV == 1B= @ARGV == 2
ABresult
1X1
011
000
164  
165 22 our ($n, $maybe_m) = @ARGV;
166  
167 22 ▶is_natural0 $n or usage "n must be a non-negative integer";
Branch: 1 branch
BranchTrueFalseTotal
is_natural0($main::n) or usage("n must be a non-negative integer") 22 0 22
168  
169 22 ▶if (defined $maybe_m) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $main::maybe_m) 22 0 22
170 22 ▶ is_natural0 $maybe_m or usage "m must be a non-negative integer";
Branch: 1 branch
BranchTrueFalseTotal
is_natural0($main::maybe_m) or usage("m must be a non-negative integer") 22 0 22
171 }
172  
173 22 ▶our $mem_start = resource_rss if $opt_leaktest;
Branch: 1 branch
BranchTrueFalseTotal
if $main::opt_leaktest 0 22 22
174 22 our $mem_end;
175  
176 22 our $in = glob_to_fh * STDIN;
177 22 our $out = glob_to_fh * STDOUT;
178  
179 22 ▶if ($n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::n) 19 3 22
180 19 ▶ $in->seek($n) || do {
Branch: 1 branch
BranchTrueFalseTotal
$main::in->seek($main::n) or do { if ($! == 29) { my $nbufs = int $main::n / $main::BUFSIZ; my $nrest = $main::n % $main::BUFSIZ; $nbufs * $main::BUFSIZ + $nrest == $main::n or die "Perl can't calculate?"; my $buf; foreach $_ (1 .. $nbufs) { $main::in->xsysreadcompletely($buf, $main::BUFSIZ); } ; $main::in->xsysreadcompletely($buf, $nrest); } else { die "seek: $!"; } } 18 1 19
181 1 ▶ if ($! == ESPIPE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($! == 29) { } 1 0 1
182  
183 # non-seekable device
184 1 my $nbufs = int($n / $BUFSIZ);
185 1 my $nrest = $n % $BUFSIZ;
186 1 ▶ $nbufs * $BUFSIZ + $nrest == $n or die "Perl can't calculate?";
Branch: 1 branch
BranchTrueFalseTotal
$nbufs * $main::BUFSIZ + $nrest == $main::n or die "Perl can't calculate?" 1 0 1
187 1 my $buf;
188 1 for (1 .. $nbufs) {
189 0 $in->xsysreadcompletely($buf, $BUFSIZ);
190 }
191 1 $in->xsysreadcompletely($buf, $nrest);
192  
193 # XX: seek does not complain when file is too short; this
194 # does. This is inconsistent. Also, check $opt_silent ?
195 } else {
196 0 die "seek: $!";
197 }
198 };
199 }
200  
201 22 ▶if ($maybe_m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::maybe_m) { } 19 3 22
202 19 my $chunks = force fh_to_chunks $in, $BUFSIZ;
203 19 ▶ if (is_null $chunks) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_null($chunks)) { } 3 16 19
204 3 ▶ die "$myname: no remainder left after skipping $n byte(s)\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $main::opt_silent 3 0 3
205 unless $opt_silent;
206 } else {
207 my $chunks2 = chunks_change_tail(
208 $chunks, $maybe_m,
209 16 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=14.2
210 16 my ($rest, $remainingsize) = @_;
211 16 ▶ $mem_end = resource_rss if $opt_leaktest;
Branch: 1 branch
BranchTrueFalseTotal
if $main::opt_leaktest 0 16 16
212  
213 16 ▶ if ($remainingsize < $maybe_m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remainingsize < $main::maybe_m) { } 3 13 16
214 3 ▶ if ($opt_silent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::opt_silent) { } 0 3 3
215 0 null
216 } else {
217 3 die "$myname: only $remainingsize byte(s) left "
218 . "after skipping $n byte(s)\n"
219 }
220 } else {
221 13 my $last = first $rest;
222 13 cons(substr($last, 0, $remainingsize - $maybe_m), null)
223 }
224 }
225 16 );
226 32 ▶ stream_for_each sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
227 32 $out->xprint($_[0])
228 16 }, $chunks2;
229 }
230 } else {
231 3 $in->xsendfile_to($out);
232 3 $in->xclose;
233 }
234  
235 16 $out->xclose;
236  
237 16 ▶if ($opt_leaktest and (($mem_end - $mem_start) / $mem_start > 1.5)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::opt_leaktest and ($main::mem_end - $main::mem_start) / $main::mem_start > "1.5") 0 16 16
Condition33%1/3
$main::opt_leaktest and ($main::mem_end - $main::mem_start) / $main::mem_start > "1.5"
!ll&&!rl&&r
1600
MC/DC0%0/2
$main::opt_leaktest and ($main::mem_end - $main::mem_start) / $main::mem_start > "1.5"
$main::opt_leaktest ($main::mem_end - $main::mem_start) / $main::mem_start > "1.5"
Truth table33%1/3
A and B
A= $main::opt_leaktestB= ($main::mem_end - $main::mem_start) / $main::mem_start > "1.5"
ABresult
0X0
100
111
238 0 die "there was a leak";
239 }
240  
« lib/FP/DumperEqual.pm ↑ Summary lib/FP/Repl/AutoTrap.pm »