blib/lib/perl5i/2/equal.pm

filter: Statementstmt 97.4% 77 / 79 Branchbran 87.1% 61 / 70 Conditioncond 77.2% 34 / 44 MC/DCmcdc 57.6% 15 / 26 Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 1 totaltotal 85.6% 197 / 230 SCAR 40.2
CC
52
Cov
89%
are_equal
22.0
_equal_overload
17.6
_overload_type
9.1
CRAP 55.5
« blib/lib/perl5i/0/Meta.pm ↑ Summary blib/lib/perl5i/2/SCALAR.pm »
1 package perl5i::2::equal;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶no if $] >= 5.018000, warnings => 'experimental::smartmatch';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 ▶use perl5i::2::autobox;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 sub are_equal {
9 411 ▶ my ($r1, $r2) = @_;
Subroutine are_equal: called CC=22 SCAR=30.9
Pod undocumented
10  
11 # given two scalars, decide whether they are identical or not,
12 # recursing over deep data structures. Since it uses recursion,
13 # traversal is done depth-first.
14 # Warning: complex if-then-else decision tree ahead. It's ordered on
15 # my perceived and anecdotical take on the frequency of occurrence
16 # of each reftype: most popular on top, most rare on the bottom.
17 # This way we return as early as possible.
18  
19 # undef eq undef
20 411 ▶ return 1 if !defined $r1 and !defined $r2;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $r1 or defined $r2 1 410 411
Condition100%3/3
defined $r1 or defined $r2
l!l&&r!l&&!r
40191
MC/DC100%2/2
defined $r1 or defined $r2
defined $r1 defined $r2
Truth table100%3/3
A or B
A= defined $r1B= defined $r2
ABresult
1X1
011
000
21  
22 # One is defined, one isn't
23 410 ▶ return if defined $r1 xor defined $r2;
Branch: 1 branch
BranchTrueFalseTotal
if defined $r1 xor defined $r2 20 390 410
Condition75%3/4
defined $r1 xor defined $r2
l&&rl&&!r!l&&r!l&&!r
3901190
MC/DC100%2/2
defined $r1 xor defined $r2
defined $r1 defined $r2
Truth table75%3/4
A xor B
A= defined $r1B= defined $r2
ABresult
110
101
011
000
24  
25 390 my( $ref1, $ref2 ) = (ref $r1, ref $r2);
26  
27 390 ▶ if( !$ref1 and !$ref2 ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $ref1 || $ref2) { } 90 300 390
elsif ($ref1 eq $ref2) { } 77 223 300
elsif ($ref1 and $ref2) { } 116 107 223
Condition100%3/3
$ref1 || $ref2
l!l&&r!l&&!r
2663490
MC/DC100%2/2
$ref1 || $ref2
$ref1 $ref2
Truth table100%3/3
A || B
A= $ref1B= $ref2
ABresult
1X1
011
000
28 90 my $is_num1 = $r1->is_number;
29 90 my $is_num2 = $r2->is_number;
30 90 ▶ if( $is_num1 xor $is_num2 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_num1 xor $is_num2) { } 2 88 90
elsif ($is_num1) { } 53 35 88
Condition100%4/4
$is_num1 xor $is_num2
l&&rl&&!r!l&&r!l&&!r
531135
MC/DC100%2/2
$is_num1 xor $is_num2
$is_num1 $is_num2
Truth table100%4/4
A xor B
A= $is_num1B= $is_num2
ABresult
110
101
011
000
31 # One's looks like a number, the other doesn't.
32 # Can't be equal.
33 2 return 0;
34 }
35 elsif( $is_num1 ) {
36 # They're both numbers
37 53 return $r1 == $r2;
38 }
39 else {
40 # They're both strings
41 35 return $r1 eq $r2;
42 }
43 }
44 elsif( $ref1 eq $ref2 ) {
45 77 ▶ if ( $ref1 ~~ [qw(Regexp GLOB CODE)] ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($ref1 ~~ ['Regexp', 'GLOB', 'CODE']) { } 12 65 77
elsif ($ref1 eq 'ARRAY') { } 12 53 65
elsif ($ref1 eq 'HASH') { } 27 26 53
elsif ($ref1 ~~ ['SCALAR', 'REF']) { } 14 12 26
46 12 return $r1 eq $r2;
47 }
48 elsif ( $ref1 eq 'ARRAY' ) {
49 12 return _equal_arrays( $r1, $r2 );
50 }
51 elsif ( $ref1 eq 'HASH' ) {
52 27 return _equal_hashes( $r1, $r2 );
53 }
54 elsif ( $ref1 ~~ [qw(SCALAR REF)] ) {
55 14 return are_equal($$r1, $$r2);
56 }
57 else {
58 # Must be an object
59 12 return _equal_objects( $r1, $r2 );
60 }
61 }
62 ▶ elsif( $ref1 and $ref2 ) {
Condition100%3/3
$ref1 and $ref2
!ll&&!rl&&r
3473116
MC/DC100%2/2
$ref1 and $ref2
$ref1 $ref2
Truth table100%3/3
A and B
A= $ref1B= $ref2
ABresult
0X0
100
111
63 # They're both refs, but not of the same type
64 116 my $is_overloaded1 = overload::Overloaded($r1);
65 116 my $is_overloaded2 = overload::Overloaded($r2);
66  
67 116 ▶ if( $is_overloaded1 and $is_overloaded2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_overloaded1 and $is_overloaded2) { } 6 110 116
Condition100%3/3
$is_overloaded1 and $is_overloaded2
!ll&&!rl&&r
94166
MC/DC100%2/2
$is_overloaded1 and $is_overloaded2
$is_overloaded1 $is_overloaded2
Truth table100%3/3
A and B
A= $is_overloaded1B= $is_overloaded2
ABresult
0X0
100
111
68 # Two overloaded objects
69 6 return _equal_overload( $r1, $r2 );
70 }
71 else {
72 # One's an overloaded object, the other is not or
73 # Two plain refs different type or
74 # non-overloaded objects of different type.
75 110 return 0;
76 }
77 }
78 else {
79 # One is a ref, one is not
80 107 ▶ my $is_overloaded = $ref1 ? overload::Overloaded($r1)
Branch: 1 branch
BranchTrueFalseTotal
$ref1 ? : 73 34 107
81 : overload::Overloaded($r2);
82  
83 107 ▶ if( $is_overloaded ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_overloaded) { } 20 87 107
84 # One's an overloaded object, one's a plain scalar
85 20 ▶ return $ref1 ? _equal_overload_vs_scalar($r1, $r2)
Branch: 1 branch
BranchTrueFalseTotal
$ref1 ? : 20 0 20
86 : _equal_overload_vs_scalar($r2, $r1);
87 }
88 else {
89 # One's a plain ref or object, one's a plain scalar
90 87 return 0;
91 }
92 }
93 }
94  
95 sub _equal_arrays {
96 12 ▶ my ($r1, $r2) = @_;
Subroutine _equal_arrays: called CC=4 SCAR=13.9
97 # They can only be equal if they have the same nº of elements.
98 12 ▶ return if @$r1 != @$r2;
Branch: 1 branch
BranchTrueFalseTotal
if @$r1 != @$r2 3 9 12
99  
100 9 foreach my $i (0 .. @$r1 - 1) {
101 24 ▶ return unless are_equal($r1->[$i], $r2->[$i]);
Branch: 1 branch
BranchTrueFalseTotal
unless are_equal($$r1[$i], $$r2[$i]) 2 22 24
102 }
103  
104 7 return 1;
105 }
106  
107 sub _equal_hashes {
108 27 ▶ my ($r1, $r2) = @_;
Subroutine _equal_hashes: called CC=4 SCAR=13.9
109 # Hashes can't be equal unless their keys are equal.
110 27 ▶ return unless ( %$r1 ~~ %$r2 );
Branch: 1 branch
BranchTrueFalseTotal
unless \%$r1 ~~ \%$r2 4 23 27
111  
112 # Compare the equality of the values for each key.
113 23 foreach my $key (keys %$r1) {
114 23 ▶ return unless are_equal( $r1->{$key}, $r2->{$key} );
Branch: 1 branch
BranchTrueFalseTotal
unless are_equal($$r1{$key}, $$r2{$key}) 7 16 23
115 }
116  
117 16 return 1;
118 }
119  
120 # Returns the code which will run when the object is used as a string
121 6 require overload;
122 sub _overload_type {
123 48 ▶ return unless ref $_[0];
Subroutine _overload_type: called CC=8 SCAR=22.1
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] 0 48 48
124 48 my $str = overload::Method($_[0], q[""]);
125 48 my $num = overload::Method($_[0], "0+");
126 48 ▶ return "both" if $str and $num;
Branch: 1 branch
BranchTrueFalseTotal
if $str and $num 0 48 48
Condition66%2/3
$str and $num
!ll&&!rl&&r
27210
MC/DC0%0/2
$str and $num
$str $num
Truth table66%2/3
A and B
A= $strB= $num
ABresult
0X0
100
111
127 48 ▶ return "" if !$str and !$num;
Branch: 1 branch
BranchTrueFalseTotal
unless $str or $num 0 48 48
Condition66%2/3
$str or $num
l!l&&r!l&&!r
21270
MC/DC0%0/2
$str or $num
$str $num
Truth table66%2/3
A or B
A= $strB= $num
ABresult
1X1
011
000
128 48 ▶ return "str" if $str;
Branch: 1 branch
BranchTrueFalseTotal
if $str 21 27 48
129 27 ▶ return "num" if $num;
Branch: 1 branch
BranchTrueFalseTotal
if $num 27 0 27
130 }
131  
132 # Two objects, possibly different classes, both overloaded.
133 sub _equal_overload {
134 14 ▶ my($obj1, $obj2) = @_;
Subroutine _equal_overload: called CC=13 SCAR=28.7
135  
136 14 my $type1 = _overload_type($obj1);
137 14 my $type2 = _overload_type($obj2);
138  
139 # One of them is not overloaded
140 14 ▶ return if !$type1 or !$type2;
Branch: 1 branch
BranchTrueFalseTotal
unless $type1 and $type2 0 14 14
Condition33%1/3
$type1 and $type2
!ll&&!rl&&r
0014
MC/DC0%0/2
$type1 and $type2
$type1 $type2
Truth table33%1/3
A and B
A= $type1B= $type2
ABresult
0X0
100
111
141  
142 14 ▶ if( $type1 eq 'both' and $type2 eq 'both' ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($type1 eq 'both' and $type2 eq 'both') { } 0 14 14
elsif ($type1 eq 'num' and $type2 eq 'str' or $type1 eq 'str' and $type2 eq 'num') { } 6 8 14
elsif ('num' ~~ [$type1, $type2]) { } 5 3 8
elsif ('str' ~~ [$type1, $type2]) { } 3 0 3
Condition33%1/3
$type1 eq "both" and $type2 eq "both"
!ll&&!rl&&r
1400
MC/DC0%0/2
$type1 eq "both" and $type2 eq "both"
$type1 eq "both" $type2 eq "both"
Truth table33%1/3
A and B
A= $type1 eq "both"B= $type2 eq "both"
ABresult
0X0
100
111
143 0 ▶ return $obj1 == $obj2 || $obj1 eq $obj2;
Condition0%0/3
$obj1 == $obj2 || $obj1 eq $obj2
l!l&&r!l&&!r
000
MC/DC0%0/2
$obj1 == $obj2 || $obj1 eq $obj2
$obj1 == $obj2 $obj1 eq $obj2
Truth table0%0/3
A || B
A= $obj1 == $obj2B= $obj1 eq $obj2
ABresult
1X1
011
000
144 }
145 ▶ elsif(
Condition100%9/9
$type1 eq "num" and $type2 eq "str" or $type1 eq "str" and $type2 eq "num"
l!l&&r!l&&!r
248
$type1 eq "num" and $type2 eq "str"
!ll&&!rl&&r
752
$type1 eq "str" and $type2 eq "num"
!ll&&!rl&&r
534
MC/DC75%3/4
$type1 eq "num" and $type2 eq "str" or $type1 eq "str" and $type2 eq "num"
$type1 eq "num" $type2 eq "str" $type1 eq "str" $type2 eq "num"
Truth table57%4/7
A and B or C and D
A= $type1 eq "num"B= $type2 eq "str"C= $type1 eq "str"D= $type2 eq "num"
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
146 ($type1 eq 'num' and $type2 eq 'str') or
147 ($type1 eq 'str' and $type2 eq 'num')
148 )
149 {
150 # They're not both numbers, not both strings, and not both both
151 # Must be str vs num.
152 6 ▶ return $type1 eq 'num' ? $obj1+0 eq "$obj2"
Branch: 1 branch
BranchTrueFalseTotal
$type1 eq 'num' ? : 2 4 6
153 : $obj2+0 eq "$obj1";
154 }
155 elsif( 'num' ~~ [$type1, $type2] ) {
156 5 return $obj1 == $obj2;
157 }
158 elsif( 'str' ~~ [$type1, $type2] ) {
159 3 return $obj1 eq $obj2;
160 }
161 else {
162 0 die "Should never be reached";
163 }
164 }
165  
166 # Two objects, same class
167 sub _equal_objects {
168 12 ▶ my($r1, $r2) = @_;
Subroutine _equal_objects: called CC=2 SCAR=6.9
169  
170 # No need to check both, they're the same class
171 12 my $is_overloaded = overload::Overloaded($r1);
172  
173 12 ▶ if( !$is_overloaded ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_overloaded) { } 4 8 12
174 # Neither are overloaded, they're the same class, are they the same object?
175 4 return $r1 eq $r2;
176 }
177 else {
178 8 return _equal_overload( $r1, $r2 );
179 }
180 }
181  
182 # One overloaded object, one plain scalar
183 # STRING != OBJ
184 # NUMBER != OBJ
185 # STRING eq OBJeq
186 # STRING eq OBJboth
187 # STRING != OBJ== (using == will throw a warning)
188 # NUMBER == OBJ==
189 # NUMBER eq OBJeq
190 # NUMBER == OBJboth
191 sub _equal_overload_vs_scalar {
192 20 ▶ my($obj, $scalar) = @_;
Subroutine _equal_overload_vs_scalar: called CC=5 SCAR=16.1
193  
194 20 my $type = _overload_type($obj);
195 20 ▶ return unless $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 0 20 20
196  
197 20 ▶ if( $scalar->is_number ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scalar->is_number) { } 10 10 20
198 10 ▶ if( $type eq 'str' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'str') { } 4 6 10
199 4 $obj eq $scalar;
200 }
201 else {
202 6 $obj == $scalar;
203 }
204 }
205 else {
206 10 ▶ if( $type eq 'num' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'num') { } 5 5 10
207 # Can't reliably compare
208 5 return;
209 }
210 else {
211 5 $obj eq $scalar;
212 }
213 }
214 }
215  
216 6 1;
« blib/lib/perl5i/0/Meta.pm ↑ Summary blib/lib/perl5i/2/SCALAR.pm »