lib/FP/Abstract/Sequence/t.pm

filter: Statementstmt 97.6% 247 / 253 Branchbran 70.0% 7 / 10 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 97.1% 101 / 104 Podpod 0.0% 0 / 2 totaltotal 96.2% 360 / 374 CC 9 SCAR 4.4
CC
9
Cov
97%
__ANON__
8.0
is_pair_purearray
2.0
__ANON__
2.0
CRAP 1.6
« lib/Chj/Linux/LmSensors.pm ↑ Summary examples/logic »
1 #
2 # Copyright (c) 2015-2021 Christian Jaeger, copying@christianjaeger.ch
3 #
4 # This is free software, offered under either the same terms as perl 5
5 # or the terms of the Artistic License version 2 or the terms of the
6 # MIT License (Expat version). See the file COPYING.md that came
7 # bundled with this file.
8 #
9  
10 =head1 NAME
11  
12 FP::Abstract::Sequence::t -- tests for FP::Abstract::Sequence
13  
14 =head1 SYNOPSIS
15  
16 # is tested by `t/require_and_run_tests`
17  
18 =head1 DESCRIPTION
19  
20  
21 =head1 NOTE
22  
23 This is alpha software! Read the status section in the package README
24 or on the L<website|http://functional-perl.org/>.
25  
26 =cut
27  
28 package FP::Abstract::Sequence::t;
29  
30 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 ▶use warnings FATAL => 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 1 ▶use FP::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 # from SYNOPSIS:
37  
38 1 ▶use FP::Predicates; # qw(is_sequence is_seq);
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 # ^ since we can't have those in FP::Abstract::Sequence
41 1 ▶use FP::PureArray;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 1 ▶use FP::StrictList;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 1 ▶use FP::List;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 1 ▶use FP::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 1 ▶use FP::Array 'array';
Subroutine BEGIN: called CC=1 SCAR=0.0
46 1 ▶use FP::Ops qw(the_method string_cmp);
Subroutine BEGIN: called CC=1 SCAR=0.0
47 1 ▶use FP::Div qw(identity);
Subroutine BEGIN: called CC=1 SCAR=0.0
48 1 ▶use Chj::TEST;
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 1 my $t_vals = list(
51 purearray(3, 4), strictlist(3, 4), list(3, 4), stream(3, 4),
52  
53 purearray(), strictlist(), list(), stream(),
54  
55 cons(3, 4), # ok this can't really count as a sequence,
56 # what to do about it?
57 array(3, 4), # Could `autobox` change this?
58 array(), 3, "character sequence", { 3 => 4 },
59 );
60  
61 3 ▶sub t_fn {
Subroutine t_fn: called CC=1 SCAR=0.0
Pod undocumented
62 3 my ($fn) = @_;
63 3 $t_vals->map($fn)->array
64 }
65  
66 1 ▶TEST { t_fn \&is_sequence }
Subroutine __ANON__: called CC=1 SCAR=0.0
67 1 [1, 1, 1, 1, 1, 1, 1, 1, 1, undef, undef, undef, undef, undef];
68  
69 1 ▶TEST { t_fn \&is_proper_sequence }
Subroutine __ANON__: called CC=1 SCAR=0.0
70 1 [1, 1, 1, 1, 1, 1, 1, 1, 0, undef, undef, undef, undef, undef];
71  
72 1 ▶TEST { t_fn \&is_seq }
Subroutine __ANON__: called CC=1 SCAR=0.0
73 1 [1, 1, 1, 1, 0, 0, 0, 0, 1, undef, undef, undef, undef, undef];
74  
75 1 my $t_seqs = $t_vals->filter(\&is_proper_sequence);
76 1 ▶TEST { $t_seqs->map(the_method "stream")->map(the_method "list") }
Subroutine __ANON__: called CC=1 SCAR=0.0
77 1 ▶GIVES {
Subroutine __ANON__: called CC=1 SCAR=0.0
78 1 list(
79 list(3, 4), list(3, 4), list(3, 4), list(3, 4),
80  
81 list(), list(), list(), list(),
82 )
83 1 };
84  
85 # more tests:
86  
87 1 ▶use FP::List ":all";
Subroutine BEGIN: called CC=1 SCAR=0.0
88 1 ▶use FP::Ops qw(the_method number_eq);
Subroutine BEGIN: called CC=1 SCAR=0.0
89 1 ▶use FP::Array_sort qw(on); # XX should really be in another place.
Subroutine BEGIN: called CC=1 SCAR=0.0
90 1 ▶use FP::Array qw(array_first array_second);
Subroutine BEGIN: called CC=1 SCAR=0.0
91 1 ▶use FP::Stream qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
92  
93 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
94 1 list(list(3), list(4, 5), list(6))->flatten
95 }
96 1 list(3, 4, 5, 6);
97  
98 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
99 1 list(list(6))->flatten(list 9)
100 }
101 1 list(6, 9);
102  
103 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
104 1 list(list(6))->flatten(undef)
105 }
106 1 improper_list(6, undef); # OK?
107  
108 1 ▶TEST { list(3, 4, 5)->max } 5;
Subroutine __ANON__: called CC=1 SCAR=0.0
109 1 ▶TEST { list(3, 4, 5, -1)->min } -1;
Subroutine __ANON__: called CC=1 SCAR=0.0
110 1 ▶TEST { purearray(3, -2, 5)->min } -2;
Subroutine __ANON__: called CC=1 SCAR=0.0
111 1 ▶TEST { [stream(3, 4, 5, -1)->minmax] } [-1, 5];
Subroutine __ANON__: called CC=1 SCAR=0.0
112  
113 1 ▶TEST { [list([3, "a"], [4, "b"], [5, "c"], [-1, "d"])->minmax(\&array_first)] }
Subroutine __ANON__: called CC=1 SCAR=0.0
114 1 [[-1, "d"], [5, "c"]];
115  
116 1 ▶TEST { F list(qw())->intersperse("-") }
Subroutine __ANON__: called CC=1 SCAR=0.0
117 1 null;
118 1 ▶TEST { F list(qw(a))->intersperse("-") }
Subroutine __ANON__: called CC=1 SCAR=0.0
119 1 list('a');
120 1 ▶TEST { F list(qw(a b))->intersperse("-") }
Subroutine __ANON__: called CC=1 SCAR=0.0
121 1 list('a', '-', 'b');
122 1 ▶TEST { F list(qw(a b c))->intersperse("-") }
Subroutine __ANON__: called CC=1 SCAR=0.0
123 1 list('a', '-', 'b', '-', 'c');
124  
125 1 ▶TEST { stream(1, 44, 2)->join("-") }
Subroutine __ANON__: called CC=1 SCAR=0.0
126 1 '1-44-2';
127  
128 4 ▶sub is_pair_purearray {
Subroutine is_pair_purearray: called CC=2 SCAR=7.1
Pod undocumented
129 4 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 4 0 4
130 4 my ($v) = @_;
131 4 [is_pair($v), is_purearray($v)]
132 }
133  
134 # consing onto a purearray just builds an improper list:
135 1 my $a;
136 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
137 1 $a = cons 0, purearray(1, 2, 3);
138 1 is_pair_purearray($a)
139 }
140 1 [1, ''];
141 1 ▶TEST { is_pair_purearray($a->rest) }
Subroutine __ANON__: called CC=1 SCAR=0.0
142 1 ['', 1];
143  
144 # but purearrays also have a cons method that does the same:
145 1 my $b;
146 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
147 1 $b = purearray(4, 5)->cons(3);
148 1 is_pair_purearray($b)
149 }
150 1 [1, ''];
151 1 ▶TEST { is_pair_purearray($b->rest) }
Subroutine __ANON__: called CC=1 SCAR=0.0
152 1 ['', 1];
153  
154 # improper-list-but-sequence ops:
155 1 ▶TEST { $a->length }
Subroutine __ANON__: called CC=1 SCAR=0.0
156 1 4;
157 1 ▶TEST { $b->ref(2) }
Subroutine __ANON__: called CC=1 SCAR=0.0
158 1 5;
159  
160 # XX add more interesting tests
161 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
162 1 my $s = purearray(3, 4, 4, 5, 6, 8, 5, 5)->map_with_index(\&array)->stream;
163 1 my $r = $s->group(on \&array_second, \&number_eq)->array;
164 1 [$s, $r]
165 }
166 [
167 undef,
168 [
169 1 list([0, 3]),
170 list([2, 4], [1, 4]),
171 list([3, 5]),
172 list([4, 6]),
173 list([5, 8]),
174 list([7, 5], [6, 5])
175 ]
176 ];
177  
178 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
179 [
180 1 list(50, 40, -10)->sum,
181 list(50, 40, -10)->product,
182 stream(50, 40, -10)->product,
183 stream(50, 40, -10)->sum
184 ]
185 }
186 1 [80, -20000, -20000, 80];
187  
188 1 ▶TEST { [list(qw(a b c d e f g))->split_at(3)] }
Subroutine __ANON__: called CC=1 SCAR=0.0
189 1 [list('a', 'b', 'c'), list('d', 'e', 'f', 'g')];
190  
191 1 ▶TEST { purearray(qw(a b c d e f))->chunks_of(4)->array }
Subroutine __ANON__: called CC=1 SCAR=0.0
192 1 [purearray('a', 'b', 'c', 'd'), purearray('e', 'f')];
193  
194 # XX chunks_of returns a stream in this case; fine? Make up some rules
195 # about this...
196  
197 1 ▶TEST_EXCEPTION { purearray(qw(a b c d e f))->strictly_chunks_of(4)->array }
Subroutine __ANON__: called CC=1 SCAR=0.0
198 1 'premature end of input';
199  
200 # XX TODO change most of the tests in this file to test ~all sequences.
201 1 ▶TEST { purearray(qw(a bc d e))->string } 'abcde';
Subroutine __ANON__: called CC=1 SCAR=0.0
202  
203 # Test across all sequence types
204 1 our @sequencetypes = qw(
205 purearray
206 mutablearray
207 list
208 stream
209 strictlist
210 );
211  
212 1 ▶use FP::Equal;
Subroutine BEGIN: called CC=1 SCAR=0.0
213 1 ▶use FP::Show;
Subroutine BEGIN: called CC=1 SCAR=0.0
214  
215 # use FP::List;
216 # use FP::Stream;
217 1 ▶use FP::PureArray;
Subroutine BEGIN: called CC=1 SCAR=0.0
218 1 ▶use FP::MutableArray;
Subroutine BEGIN: called CC=1 SCAR=0.0
219 1 ▶use FP::StrictList;
Subroutine BEGIN: called CC=1 SCAR=0.0
220 1 ▶use Chj::TEST;
Subroutine BEGIN: called CC=1 SCAR=0.0
221  
222 1 ▶TEST {
Subroutine __ANON__: called CC=8 SCAR=20.9
223 1 for my $orig (@sequencetypes) {
224 5 my $constructor = eval '\&' . $orig;
225 5 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 5 5
226 5 for my $target (@sequencetypes) {
227 25 ▶ next if $orig eq $target; #XX TODO: make it always valid
Branch: 1 branch
BranchTrueFalseTotal
if $orig eq $target 5 20 25
228 20 ▶ next if ($orig eq "mutablearray" and $target eq "purearray"); #XXX
Branch: 1 branch
BranchTrueFalseTotal
if $orig eq "mutablearray" and $target eq "purearray" 1 19 20
Condition100%3/3
$orig eq "mutablearray" and $target eq "purearray"
!ll&&!rl&&r
1631
MC/DC100%2/2
$orig eq "mutablearray" and $target eq "purearray"
$orig eq "mutablearray" $target eq "purearray"
Truth table100%3/3
A and B
A= $orig eq "mutablearray"B= $target eq "purearray"
ABresult
0X0
100
111
229 19 my $d1 = $constructor->(qw(a b c d e));
230 19 my $d2 = Keep($d1)->$target;
231 19 my $d3 = $d2->$orig;
232 19 ▶ equal $d1, $d3
Branch: 1 branch
BranchTrueFalseTotal
equal($d1, $d3) or die "not equal (from $orig to $target and back): " . show($d1) . ' vs. ' . show($d3) 19 0 19
233  
234 # XX what is the recommended way to make/format
235 # exceptions?
236 or die("not equal (from $orig to $target and back): "
237 . show($d1) . " vs. "
238 . show($d3));
239 }
240 }
241 1
242 1 }
243 1 1;
244  
245 1 ▶use FP::autobox;
Subroutine BEGIN: called CC=1 SCAR=0.0
246  
247 1 ▶TEST { []->group(\&number_eq) } purearray();
Subroutine __ANON__: called CC=1 SCAR=0.0
248 1 ▶TEST { [1]->group(\&number_eq) } purearray(purearray(1));
Subroutine __ANON__: called CC=1 SCAR=0.0
249 1 ▶TEST { [1, 3]->group(\&number_eq) } purearray(purearray(1), purearray(3));
Subroutine __ANON__: called CC=1 SCAR=0.0
250 1 ▶TEST { [1, 1, 3]->group(\&number_eq) } purearray(purearray(1, 1), purearray(3));
Subroutine __ANON__: called CC=1 SCAR=0.0
251 1 ▶TEST { [1, 1, 3, 3]->group(\&number_eq) }
Subroutine __ANON__: called CC=1 SCAR=0.0
252 1 purearray(purearray(1, 1), purearray(3, 3));
253 1 ▶TEST { [1, 1, 3, 3, 4]->group(\&number_eq) }
Subroutine __ANON__: called CC=1 SCAR=0.0
254 1 purearray(purearray(1, 1), purearray(3, 3), purearray(4));
255 1 ▶TEST { [[3, "a"], [3, "b"], [4, "c"]]->group(on(\&array_first, \&number_eq)) }
Subroutine __ANON__: called CC=1 SCAR=0.0
256 1 purearray(purearray([3, 'a'], [3, 'b']), purearray([4, 'c']));
257 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
258 1 [[3, "a"], [3, "b"], [4, "c"]]->list->group(on(\&array_first, \&number_eq))
259 }
260 1 list(list([3, 'b'], [3, 'a']), list([4, 'c']));
261  
262 # ^ XXX should give same order as in purearray case. Really implement
263 # rlist?
264  
265 1 ▶TEST { ["b", "a", "x", "c"]->max(\&string_cmp) } 'x';
Subroutine __ANON__: called CC=1 SCAR=0.0
266 1 ▶TEST { ["b", "a", "x", "c"]->min(\&string_cmp) } 'a';
Subroutine __ANON__: called CC=1 SCAR=0.0
267  
268 # The false-returning behaviour of `any`: (TODO: try this
269 # *automatically* with all sequence types instead of this lame
270 # copy-paste)
271  
272 1 ▶TEST { list(1, 3, 5)->any(\&is_even) } 0;
Subroutine __ANON__: called CC=1 SCAR=0.0
273 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
274 1 ▶ list(1, 3, 5)->any(sub { my ($v) = @_; defined $v })
Subroutine __ANON__: called CC=1 SCAR=0.0
275 1 }
276 1 1;
277 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
278 3 ▶ list(1, 3, 5)->any(sub { my ($v) = @_; not defined $v })
Subroutine __ANON__: called CC=1 SCAR=0.0
279 1 }
280 1 '';
281 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=2.6
282 0 ▶ list()->any(sub { my ($v) = @_; defined $v })
Subroutine __ANON__: not called CC=1 SCAR=6.9
283 1 }
284 1 undef;
285 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
286 1 ▶ list(undef)->any(sub { my ($v) = @_; defined $v })
Subroutine __ANON__: called CC=1 SCAR=0.0
287 1 }
288 1 '';
289 1 ▶TEST { list(undef)->any(\&identity) } undef;
Subroutine __ANON__: called CC=1 SCAR=0.0
290 1 ▶TEST { list('', undef)->any(\&identity) } undef;
Subroutine __ANON__: called CC=1 SCAR=0.0
291 1 ▶TEST { list('')->any(\&identity) } '';
Subroutine __ANON__: called CC=1 SCAR=0.0
292  
293 1 ▶TEST { purearray(1, 3, 5)->any(\&is_even) } 0;
Subroutine __ANON__: called CC=1 SCAR=0.0
294 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
295 1 ▶ purearray(1, 3, 5)->any(sub { my ($v) = @_; defined $v })
Subroutine __ANON__: called CC=1 SCAR=0.0
296 1 }
297 1 1;
298 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
299 3 ▶ purearray(1, 3, 5)->any(sub { my ($v) = @_; not defined $v })
Subroutine __ANON__: called CC=1 SCAR=0.0
300 1 }
301 1 '';
302 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=2.6
303 0 ▶ purearray()->any(sub { my ($v) = @_; defined $v })
Subroutine __ANON__: not called CC=1 SCAR=6.9
304 1 }
305 1 undef;
306 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
307 1 ▶ purearray(undef)->any(sub { my ($v) = @_; defined $v })
Subroutine __ANON__: called CC=1 SCAR=0.0
308 1 }
309 1 '';
310 1 ▶TEST { purearray(undef)->any(\&identity) } undef;
Subroutine __ANON__: called CC=1 SCAR=0.0
311 1 ▶TEST { purearray('', undef)->any(\&identity) } undef;
Subroutine __ANON__: called CC=1 SCAR=0.0
312 1 ▶TEST { purearray('')->any(\&identity) } '';
Subroutine __ANON__: called CC=1 SCAR=0.0
313  
314 1 ▶TEST { stream(1, 3, 5)->any(\&is_even) } 0;
Subroutine __ANON__: called CC=1 SCAR=0.0
315 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
316 1 ▶ stream(1, 3, 5)->any(sub { my ($v) = @_; defined $v })
Subroutine __ANON__: called CC=1 SCAR=0.0
317 1 }
318 1 1;
319 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
320 3 ▶ stream(1, 3, 5)->any(sub { my ($v) = @_; not defined $v })
Subroutine __ANON__: called CC=1 SCAR=0.0
321 1 }
322 1 '';
323 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=2.6
324 0 ▶ stream()->any(sub { my ($v) = @_; defined $v })
Subroutine __ANON__: not called CC=1 SCAR=6.9
325 1 }
326 1 undef;
327 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
328 1 ▶ stream(undef)->any(sub { my ($v) = @_; defined $v })
Subroutine __ANON__: called CC=1 SCAR=0.0
329 1 }
330 1 '';
331 1 ▶TEST { stream(undef)->any(\&identity) } undef;
Subroutine __ANON__: called CC=1 SCAR=0.0
332 1 ▶TEST { stream('', undef)->any(\&identity) } undef;
Subroutine __ANON__: called CC=1 SCAR=0.0
333 1 ▶TEST { stream('')->any(\&identity) } '';
Subroutine __ANON__: called CC=1 SCAR=0.0
334  
335 1 1
« lib/Chj/Linux/LmSensors.pm ↑ Summary examples/logic »