lib/FP/Equal.pm

filter: Statementstmt 92.0% 138 / 150 Branchbran 76.4% 52 / 68 Conditioncond 50.0% 14 / 28 MC/DCmcdc 37.5% 6 / 16 Subroutinesub 92.8% 26 / 28 Podpod 0.0% 0 / 5 totaltotal 80.0% 236 / 295 CC 46 SCAR 24.6
CC
46
Cov
83%
boolean_equal
30.0
__ANON__
23.5
is_equal
9.8
CRAP 11.7
« lib/FP/Ops.pm ↑ Summary lib/FP/IOStream.pm »
1 #
2 # Copyright (c) 2015-2019 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::Equal - generic equality comparison
13  
14 =head1 SYNOPSIS
15  
16 1 ▶ use FP::Equal;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶ use FP::List;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶ use FP::Div qw(inc);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 ok equal [1, list(2, 3)], [1, list(1, 2)->map(\&inc)];
21 is equal( [1, list(2, 3)], [1, list(1, 2)] ),
22 ''; # false but defined since same type
23 is equal( [1, list(2, 3)], [1, list([], 3)] ),
24 undef; # to say it's not the same type
25  
26 # equal forces any promises that it encounters:
27 1 ▶ use FP::Lazy; use FP::List 'pair';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
28 ok equal lazy{pair 3, lazy{2 + 1}},
29 pair(3, 2 + 1);
30  
31 1 ▶ use FP::Stream 'string_to_stream';
Subroutine BEGIN: called CC=1 SCAR=0.0
32 is equal( string_to_stream("Hello"),
33 string_to_stream("Hello1") ),
34 undef; # not the same type at the end, null vs. pair;
35 # although this may be subject to change
36  
37 # n-ary variant of equal (not done in equal itself for performance
38 # reasons and to allow for prototype declaration):
39 1 ▶ use FP::Equal qw(equaln);
Subroutine BEGIN: called CC=1 SCAR=0.0
40 is equaln("a", "a", "a"), 1;
41 is equaln("a", "a", "b"), '';
42  
43 # For writing tests with Test::More--the same as `is` but uses
44 # `equal` for comparisons, and shows values in failures via
45 # `show`:
46 1 ▶ use FP::Equal qw(is_equal);
Subroutine BEGIN: called CC=1 SCAR=0.0
47 is_equal list(1+1), list(2);
48  
49 # `equal` is giving an exception if 2 objects of the (~)same class
50 # are given, but the class doesn't implement the protocol:
51 is_equal [ scalar eval {
52 equal bless([1+1], 'FP::Equal::EXAMPLE'),
53 bless([2], 'FP::Equal::EXAMPLE')
54 },
55 length("$@")>5 ],
56 [undef, 1];
57  
58 # `relaxedequal` does not throw exceptions, it falls back to
59 # FP::DumperEqual if a class doesn't implement the protocol:
60 1 ▶ use FP::Equal qw(relaxedequal);
Subroutine BEGIN: called CC=1 SCAR=0.0
61 is_equal [ scalar eval {
62 relaxedequal
63 bless([1+1], 'FP::Equal::EXAMPLE'),
64 bless([2], 'FP::Equal::EXAMPLE')
65 },
66 length("$@") ],
67 [1, 0];
68  
69 =head1 DESCRIPTION
70  
71 Deep, generic (class controlled) structure equality comparison.
72  
73 Non-objects are hard coded in this module. Objects are expected to
74 have an `FP_Equal_equal` method that is able to take an argument of
75 the same class as the object to compare. If it doesn't have such an
76 object, it simply can't be compared using the C<equal*> functions,
77 they will give "no such method" exceptions; C<relaxedequal> falls back
78 to using L<FP::DumperEqual> (which uses L<Data::Dumper>) in such
79 cases.
80  
81 This does *name based* type comparison: structurally equivalent
82 objects do not count as equal if they do not have the same class (or
83 more general, reference name); the `FP_Equal_equal` method is not even
84 called in this case, and the functions return undef. This might be
85 subject to change: certain pairs of types will be fine to compare; let
86 the classes provide a method that checks whether a type is ok to
87 compare?
88  
89 =head1 TODO
90  
91 - cycle detection
92  
93 - immutable version -> equals_now equals_forever
94  
95 - do we need the possibility for "context" dependent (and not by way
96 of subclassing and overriding equals_*) equality comparisons?
97  
98 =head1 SEE ALSO
99  
100 L<FP::Abstract::Equal> for the protocol definition
101  
102 L<FP::DumperEqual> used as fallback in C<relaxedequal>.
103  
104 L<FP::Show>
105  
106 =head1 NOTE
107  
108 This is alpha software! Read the status section in the package README
109 or on the L<website|http://functional-perl.org/>.
110  
111 =cut
112  
113 package FP::Equal;
114 86 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
115 86 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
116 86 ▶use warnings FATAL => 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
117 86 ▶use Exporter "import";
Subroutine BEGIN: called CC=1 SCAR=0.0
118  
119 86 our @EXPORT = qw(equal);
120 86 our @EXPORT_OK = qw(equaln is_equal relaxedequal pointer_eq boolean_equal);
121 86 our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]);
122  
123 86 ▶use Scalar::Util qw(refaddr blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
124 86 ▶use FP::Lazy;
Subroutine BEGIN: called CC=1 SCAR=0.0
125 86 ▶use FP::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
126  
127 # equal can easily recurse deeply into data structures.
128 86 ▶no warnings "recursion";
Subroutine BEGIN: called CC=1 SCAR=0.0
129  
130 # To be fair, it's also currently missing loop detection, TODO.
131  
132 # I find it odd that nobody did this before. But I can't find anything
133 # on CPAN.
134  
135 our $primitive_equals = +{
136 462 ▶ ARRAY => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
137 462 my ($a, $b, $equal) = @_;
138 462 ▶ @$a == @$b and do {
Branch: 1 branch
BranchTrueFalseTotal
@$a == @$b and do { my $i = 0; LP: { $i < @$a ? &$equal($a->[$i], $b->[$i]) && do { ++$i; redo LP } : 1; } } 462 0 462
139 462 my $i = 0;
140 LP: {
141 462 $i < @$a
142 ? (
143 &$equal($$a[$i], $$b[$i])
144 1701 ▶ and do { $i++; redo LP }
Branch: 1 branch
BranchTrueFalseTotal
$i < @$a ? : 1247 454 1701
Condition100%3/3
&$equal($a->[$i], $b->[$i]) and do { ++$i; redo LP }
!ll&&!rl&&r
851234
MC/DC50%1/2
&$equal($a->[$i], $b->[$i]) and do { ++$i; redo LP }
&$equal($a->[$i], $b->[$i]) do { ++$i; redo LP }
Truth table66%2/3
A and B
A= &$equal($a->[$i], $b->[$i])B= do { ++$i; redo LP }
ABresult
0X0
100
111
145 )
146 : 1
147 }
148 }
149 },
150 40 ▶ HASH => sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
151 40 my ($a, $b, $equal) = @_;
152 40 ▶ keys %$a == keys %$b and do {
Branch: 1 branch
BranchTrueFalseTotal
keys %$a == keys %$b and do { foreach $_ (keys %$a) { my $v; $v = exists $b->{$_} && &$equal($a->{$_}, $b->{$_}) or return $v; } ; 1 } 38 2 40
153 38 for (keys %$a) {
154 75 my $v;
155 75 ▶ $v = (exists $$b{$_} and &$equal($$a{$_}, $$b{$_}))
Branch: 1 branch
BranchTrueFalseTotal
$v = exists $b->{$_} && &$equal($a->{$_}, $b->{$_}) or return $v 70 5 75
Condition100%3/3
exists $b->{$_} && &$equal($a->{$_}, $b->{$_})
!ll&&!rl&&r
2370
MC/DC100%2/2
exists $b->{$_} && &$equal($a->{$_}, $b->{$_})
exists $b->{$_} &$equal($a->{$_}, $b->{$_})
Truth table100%3/3
A && B
A= exists $b->{$_}B= &$equal($a->{$_}, $b->{$_})
ABresult
0X0
100
111
156 or return $v;
157 }
158 1
159 33 }
160 },
161 0 ▶ REF => sub { # references to references
Subroutine __ANON__: not called CC=1 SCAR=6.9
162 0 my ($a, $b, $equal) = @_;
163 0 &$equal($$a, $$b)
164 },
165  
166 # *references* to globs; direct globs are compared in equal directly
167 2 ▶ GLOB => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
168  
169 # is it the same glob? If it's different ones, compare all of
170 # their contents? XX if so, then also change the direct
171 # comparison in equal
172 2 '' # since if they are the same, then pointer comparison
173 # already did it
174 },
175 8 ▶ SCALAR => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
176 8 my ($a, $b, $equal) = @_;
177 8 &$equal(${ $_[0] }, ${ $_[1] })
178 },
179  
180 # compare closures using XS? Existing module?
181 #CODE => sub {
182 #}
183 86 };
184  
185 1622 ▶sub pointer_eq {
Subroutine pointer_eq: called CC=4 SCAR=14.9
Pod undocumented
186 1622 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 1622 0 1622
187 1622 ▶ my $a = refaddr($_[0]) // return;
Condition50%1/2
builtin::refaddr($_[0]) // (return)
l!l
16220
MC/DC0%0/1
builtin::refaddr($_[0]) // (return)
builtin::refaddr($_[0])
Truth table50%1/2
A
A= builtin::refaddr($_[0])
Aresult
11
00
188 1622 ▶ my $b = refaddr($_[1]) // return;
Condition50%1/2
builtin::refaddr($_[1]) // (return)
l!l
16220
MC/DC0%0/1
builtin::refaddr($_[1]) // (return)
builtin::refaddr($_[1])
Truth table50%1/2
A
A= builtin::refaddr($_[1])
Aresult
11
00
189 1622 $a == $b
190 }
191  
192 172 ▶sub make_equal {
Subroutine make_equal: called CC=1 SCAR=0.0
Pod undocumented
193 172 my ($relaxed) = @_;
194 172 my $equal;
195 3892 ▶ $equal = sub {
Subroutine __ANON__: called CC=23 SCAR=31.6
196 3892 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 3892 0 3892
197 EQUAL: {
198 3892 my ($a, $b) = @_;
199 4088 ▶ if (!defined $a) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $a) { } 100 3988 4088
200 100 ▶ if (!defined $b) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $b) { } 97 3 100
201 97 1
202 } else {
203 3 ▶ if (length ref $b) {
Branch: 1 branch
BranchTrueFalseTotal
if (length ref $b) { } 0 3 3
204 0 ▶ if (is_promise $b) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_promise($b)) { } 0 0 0
205 0 @_ = ($a, force($b));
206 0 redo EQUAL;
207 } else {
208 undef
209 0 }
210 } else {
211 undef
212 3 }
213 }
214 } else {
215  
216 # $a is defined
217 3988 ▶ if (!defined $b) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $b) { } 10 3978 3988
218 10 ▶ if (length ref $a) {
Branch: 1 branch
BranchTrueFalseTotal
if (length ref $a) { } 5 5 10
219 5 ▶ if (is_promise $a) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_promise($a)) { } 4 1 5
220 4 @_ = (force($a), $b);
221 4 redo EQUAL;
222 } else {
223 undef
224 1 }
225 } else {
226 undef
227 5 }
228 } else {
229  
230 # both are defined
231 3978 ▶ if (length(my $ar = ref $a)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length(my $ar = ref $a)) { } 1634 2344 3978
232 1634 ▶ if (length(my $br = ref $b)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length(my $br = ref $b)) { } 1622 12 1634
233 1622 ▶ pointer_eq($a, $b) or do {
Branch: 1 branch
BranchTrueFalseTotal
pointer_eq($a, $b) or do { if (is_promise($a) or is_promise($b)) { @_ = (force($a), force($b)); redo EQUAL; } elsif ($ar eq $br) { if (my $cmp = $FP::Equal::primitive_equals->{$ar}) { &$cmp($a, $b, $equal); } else { if ($relaxed) { if (defined builtin::blessed($a) and my $m = $a->can("FP_Equal_equal")) { goto $m; } else { require FP::DumperEqual; FP::DumperEqual::dumperequal_utf8($a, $b); } ; } else { $a->FP_Equal_equal($b); } ; } ; } else { undef; } } 168 1454 1622
234 1454 ▶ if (is_promise $a or is_promise $b) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_promise($a) or is_promise($b)) { } 177 1277 1454
elsif ($ar eq $br) { } 1271 6 1277
Condition100%3/3
is_promise($a) or is_promise($b)
l!l&&r!l&&!r
17611277
MC/DC100%2/2
is_promise($a) or is_promise($b)
is_promise($a) is_promise($b)
Truth table100%3/3
A or B
A= is_promise($a)B= is_promise($b)
ABresult
1X1
011
000
235 177 @_ = (force($a), force($b));
236 177 redo EQUAL;
237 } elsif ($ar eq $br) {
238 1271 ▶ if (my $cmp = $$primitive_equals{$ar}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cmp = $FP::Equal::primitive_equals->{$ar}) { } 512 759 1271
239 512 &$cmp($a, $b, $equal)
240 } else {
241 759 ▶ if ($relaxed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($relaxed) { } 107 652 759
242 107 ▶ if (defined blessed($a)
Branch: 1 branch
BranchTrueFalseTotal
if (defined builtin::blessed($a) and my $m = $a->can('FP_Equal_equal')) { } 97 10 107
Condition66%2/3
defined builtin::blessed($a) and my $m = $a->can("FP_Equal_equal")
!ll&&!rl&&r
01097
MC/DC50%1/2
defined builtin::blessed($a) and my $m = $a->can("FP_Equal_equal")
defined builtin::blessed($a) my $m = $a->can("FP_Equal_equal")
Truth table66%2/3
A and B
A= defined builtin::blessed($a)B= my $m = $a->can("FP_Equal_equal")
ABresult
0X0
100
111
243 and my $m
244 = $a->can("FP_Equal_equal"))
245 {
246 #@_ $a and $b are still the original $a and $b
247 97 goto $m
248 } else {
249  
250 # costly require?, but slow anyway.
251 10 require FP::DumperEqual;
252  
253 # and, given that slow
254 # anyway, take the
255 # safe path via
256 # Useperl (_utf8
257 # variant)
258 10 FP::DumperEqual::dumperequal_utf8(
259 $a, $b)
260 }
261 } else {
262 652 $a->FP_Equal_equal($b)
263 }
264 }
265 } else {
266  
267 # XXX allow subclasses of same
268 # hierarchy? Check whether $br isa $ar
269 # or vica versa and then call
270 # FP_Equal_equal on the one that's more?
271 # (or the less?) specialized?
272 undef
273 6 }
274 };
275 } else {
276  
277 # $b is not a reference ($a is)
278 12 ▶ if (is_promise $a) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_promise($a)) { } 9 3 12
279 9 @_ = (force($a), $b);
280 9 redo EQUAL;
281 } else {
282 undef
283 3 }
284 }
285 } else {
286  
287 # $a is not a reference
288 2344 ▶ if (length ref $b) {
Branch: 1 branch
BranchTrueFalseTotal
if (length ref $b) { } 12 2332 2344
289 12 ▶ if (is_promise $b) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_promise($b)) { } 6 6 12
290 6 @_ = ($a, force($b));
291 6 redo EQUAL;
292 } else {
293 undef
294 6 }
295 } else {
296  
297 # $b is not a reference either
298 # make sure it's the same kind of non-reference values:
299 2332 ▶ if (ref(\$a) eq ref(\$b)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref \$a eq ref \$b) { } 2330 2 2332
300  
301 # XX number comparison could optimize the case where both
302 # values don't have string representations, compare using
303 # == then.
304  
305 # XXX Also, on a slightly independent note, and not just
306 # an optimization: in the other case (any of the
307 # arguments also has a string representation) compare
308 # both as string and as number?
309  
310 2330 $a eq $b
311 } else {
312 undef
313 2 }
314 }
315 }
316 }
317 }
318 }
319 }
320 172 }
321  
322 sub equal;
323 86 *equal = make_equal(0);
324 sub relaxedequal;
325 86 *relaxedequal = make_equal(1);
326  
327 72 ▶sub equaln {
Subroutine equaln: called CC=5 SCAR=16.2
Pod undocumented
328 72 ▶ if (@_ == 2) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 2) { } 70 2 72
elsif (@_ == 1) { } 0 2 2
329 70 goto \&equal
330 } elsif (@_ == 1) {
331 0 1
332 } else {
333 2 my $a = shift;
334 2 for (@_) {
335 4 my $v;
336 4 ▶ $v = equal($a, $_) or return $v;
Branch: 1 branch
BranchTrueFalseTotal
$v = equal($a, $_) or return $v 3 1 4
337 }
338 1
339 1 }
340 }
341  
342 75 ▶sub is_equal {
Subroutine is_equal: called CC=6 SCAR=22.9
Pod undocumented
343 75 ▶ @_ >= 2 and @_ <= 3 or fp_croak_arity "2-3";
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 2 and @_ <= 3 or fp_croak_arity("2-3") 75 0 75
Condition33%1/3
@_ >= 2 and @_ <= 3
!ll&&!rl&&r
0075
MC/DC0%0/2
@_ >= 2 and @_ <= 3
@_ >= 2 @_ <= 3
Truth table33%1/3
A and B
A= @_ >= 2B= @_ <= 3
ABresult
0X0
100
111
344 75 my ($a, $b, $maybe_name) = @_;
345 75 require Test::More;
346 75 my $tb = Test::More->builder;
347  
348 75 ▶ if (equal $a, $b) {
Branch: 1 branch
BranchTrueFalseTotal
if (equal($a, $b)) { } 75 0 75
349 75 ▶ $tb->ok(1, $maybe_name ? $maybe_name : ())
Branch: 1 branch
BranchTrueFalseTotal
$maybe_name ? : 0 75 75
350 } else {
351 0 require FP::Show;
352 0 ▶ $tb->is_eq(FP::Show::show($a), FP::Show::show($b),
Branch: 1 branch
BranchTrueFalseTotal
$maybe_name ? : 0 0 0
353 $maybe_name ? $maybe_name : ())
354 }
355 }
356  
357 # Currently, doesn't really fit here, but where else to put? A
358 # FP::Boolean module?
359 0 ▶sub boolean_equal {
Subroutine boolean_equal: not called CC=5 SCAR=34.0
Pod undocumented
360 0 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 0 0 0
361 0 my ($a, $b) = @_;
362 0 ▶ (($a and $b) or (!$a and !$b))
Branch: 1 branch
BranchTrueFalseTotal
$a and $b or not $a || $b 0 0 0
Condition0%0/9
$a && $b || !($a || $b)
l!l&&r!l&&!r
000
$a and $b
!ll&&!rl&&r
000
$a || $b
l!l&&r!l&&!r
000
MC/DC0%0/4
$a && $b || !($a || $b)
$a $b $a $b
Truth table0%0/7
A and B || not(C || D)
A= $aB= $bC= $aD= $b
ABCDresult
11XX1
0X001
10001
0X1X0
0X010
101X0
10010
363 }
364  
365 86 1
« lib/FP/Ops.pm ↑ Summary lib/FP/IOStream.pm »