blib/lib/Zuzu/Value/Equality.pm

filter: Statementstmt 94.5% 104 / 110 Branchbran 66.2% 49 / 74 Conditioncond 47.2% 17 / 36 MC/DCmcdc 9.5% 2 / 21 Subroutinesub 90.4% 19 / 21 Podpod 0.0% 0 / 3 totaltotal 72.0% 191 / 265 CC 60 SCAR 21.6
CC
60
Cov
77%
_scalar_type
20.2
value_equal
12.5
_pairlist_equal
11.0
CRAP 8.7
« blib/lib/Zuzu/Module/IO.pm ↑ Summary blib/lib/Zuzu/Web/PSGI.pm »
1 package Zuzu::Value::Equality;
2  
3 79 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 79 our $VERSION = '0.007002';
6  
7 79 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 79 our @EXPORT_OK = qw(
10 equality_type
11 value_equal
12 stable_value_key
13 );
14  
15 79 ▶use B qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
16 SVf_IOK
17 SVf_NOK
18 SVf_POK
19 svref_2object
20 79 );
21 79 ▶use Scalar::Util qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
22 blessed
23 refaddr
24 79 );
25  
26 252708 ▶sub _scalar_type {
Subroutine _scalar_type: called CC=10 SCAR=30.0
27 252708 my ( $value ) = @_;
28 252708 my $flags = svref_2object( \$value )->FLAGS;
29 252708 ▶ my $is_numeric = ( $flags & ( SVf_IOK | SVf_NOK ) ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$flags & 768 ? : 207024 45684 252708
30 252708 ▶ my $is_stringy = ( $flags & SVf_POK ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$flags & 1024 ? : 45684 207024 252708
31  
32 252708 ▶ return 'Number' if $is_numeric and ! $is_stringy;
Branch: 1 branch
BranchTrueFalseTotal
if $is_numeric and not $is_stringy 207024 45684 252708
Condition66%2/3
$is_numeric and not $is_stringy
!ll&&!rl&&r
456840207024
MC/DC50%1/2
$is_numeric and not $is_stringy
$is_numeric not $is_stringy
Truth table66%2/3
A and not(B)
A= $is_numericB= not $is_stringy
ABresult
0X0
100
111
33 45684 ▶ return 'String' if ! $is_numeric and $is_stringy;
Branch: 1 branch
BranchTrueFalseTotal
if not $is_numeric and $is_stringy 45684 0 45684
Condition33%1/3
not $is_numeric and $is_stringy
!ll&&!rl&&r
0045684
MC/DC0%0/2
not $is_numeric and $is_stringy
not $is_numeric $is_stringy
Truth table33%1/3
not(A) and B
A= not $is_numericB= $is_stringy
ABresult
0X0
100
111
34  
35 0 ▶ if ( $is_numeric and $is_stringy ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_numeric and $is_stringy) 0 0 0
Condition0%0/3
$is_numeric and $is_stringy
!ll&&!rl&&r
000
MC/DC0%0/2
$is_numeric and $is_stringy
$is_numeric $is_stringy
Truth table0%0/3
A and B
A= $is_numericB= $is_stringy
ABresult
0X0
100
111
36 0 ▶ return ( $value =~ /\A-?(?:\d+(?:\.\d+)?)\z/ ) ? 'Number' : 'String';
Branch: 1 branch
BranchTrueFalseTotal
$value =~ /\A-?(?:\d+(?:\.\d+)?)\z/ ? : 0 0 0
37 }
38  
39 0 return 'String';
40 }
41  
42 260857 ▶sub equality_type {
Subroutine equality_type: called CC=5 SCAR=16.4
Pod undocumented
43 260857 my ( $value ) = @_;
44  
45 260857 ▶ return 'Null' if ! defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 7353 253504 260857
46 253504 ▶ if ( blessed($value) and $value->isa('Zuzu::Value::Boolean') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value) and $value->isa("Zuzu::Value::Boolean")) 0 253504 253504
Condition66%2/3
builtin::blessed($value) and $value->isa("Zuzu::Value::Boolean")
!ll&&!rl&&r
2527087960
MC/DC0%0/2
builtin::blessed($value) and $value->isa("Zuzu::Value::Boolean")
builtin::blessed($value) $value->isa("Zuzu::Value::Boolean")
Truth table66%2/3
A and B
A= builtin::blessed($value)B= $value->isa("Zuzu::Value::Boolean")
ABresult
0X0
100
111
47 0 return 'Boolean';
48 }
49 253504 ▶ if ( ref $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) 796 252708 253504
50 796 return ref $value;
51 }
52  
53 252708 return _scalar_type( $value );
54 }
55  
56 260 ▶sub stable_value_key {
Subroutine stable_value_key: called CC=5 SCAR=16.6
Pod undocumented
57 260 my ( $value ) = @_;
58 260 my $type = equality_type( $value );
59 my %encoders = (
60 1 ▶ Null => sub { return 'Null:null'; },
Subroutine __ANON__: called CC=1 SCAR=0.0
61 0 ▶ Boolean => sub { return 'Boolean:' . ( $value->value ? '1' : '0' ); },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$value->value ? : 0 0 0
62 129 ▶ Number => sub { return 'Number:' . ( 0 + $value ); },
Subroutine __ANON__: called CC=1 SCAR=0.0
63 91 ▶ String => sub { return 'String:' . $value; },
Subroutine __ANON__: called CC=1 SCAR=0.0
64 260 );
65  
66 260 ▶ if ( exists $encoders{$type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $encoders{$type}) 221 39 260
67 221 return $encoders{$type}->();
68 }
69  
70 39 ▶ if ( blessed($value) and $value->can('_stable_key') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value) and $value->can("_stable_key")) 6 33 39
Condition66%2/3
builtin::blessed($value) and $value->can("_stable_key")
!ll&&!rl&&r
0336
MC/DC50%1/2
builtin::blessed($value) and $value->can("_stable_key")
builtin::blessed($value) $value->can("_stable_key")
Truth table66%2/3
A and B
A= builtin::blessed($value)B= $value->can("_stable_key")
ABresult
0X0
100
111
71 6 return $type . ':' . $value->_stable_key;
72 }
73  
74 33 ▶ return $type . ':' . ( refaddr( $value ) // "$value" );
Condition50%1/2
builtin::refaddr($value) // "$value"
l!l
330
MC/DC0%0/1
builtin::refaddr($value) // "$value"
builtin::refaddr($value)
Truth table50%1/2
A
A= builtin::refaddr($value)
Aresult
11
00
75 }
76  
77 18244 ▶sub value_equal {
Subroutine value_equal: called CC=12 SCAR=25.3
Pod undocumented
78 18244 my ( $left, $right ) = @_;
79 18244 my $left_type = equality_type( $left );
80 18244 my $right_type = equality_type( $right );
81  
82 18244 ▶ return 0 if $left_type ne $right_type;
Branch: 1 branch
BranchTrueFalseTotal
if $left_type ne $right_type 1211 17033 18244
83  
84 my %comparators = (
85 3078 ▶ Null => sub { return 1; },
Subroutine __ANON__: called CC=1 SCAR=0.0
86 0 ▶ Boolean => sub { return $left->value == $right->value ? 1 : 0; },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$left->value == $right->value ? : 0 0 0
87 261 ▶ Number => sub { return ( 0 + $left ) == ( 0 + $right ) ? 1 : 0; },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
0 + $left == 0 + $right ? : 141 120 261
88 13663 ▶ String => sub { return $left eq $right ? 1 : 0; },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$left eq $right ? : 757 12906 13663
89 17033 );
90  
91 17033 ▶ if ( exists $comparators{$left_type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $comparators{$left_type}) 17002 31 17033
92 17002 return $comparators{$left_type}->();
93 }
94  
95 31 ▶ if ( blessed($left) and blessed($right) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($left) and builtin::blessed($right)) 31 0 31
Condition33%1/3
builtin::blessed($left) and builtin::blessed($right)
!ll&&!rl&&r
0031
MC/DC0%0/2
builtin::blessed($left) and builtin::blessed($right)
builtin::blessed($left) builtin::blessed($right)
Truth table33%1/3
A and B
A= builtin::blessed($left)B= builtin::blessed($right)
ABresult
0X0
100
111
96 31 ▶ return 0 if ref($left) ne ref($right);
Branch: 1 branch
BranchTrueFalseTotal
if ref $left ne ref $right 0 31 31
97  
98 31 ▶ if ( $left->isa('Zuzu::Value::Array') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($left->isa("Zuzu::Value::Array")) 7 24 31
99 7 return _array_equal( $left, $right );
100 }
101 24 ▶ if ( $left->isa('Zuzu::Value::Bag') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($left->isa("Zuzu::Value::Bag")) 1 23 24
102 1 return _bag_equal( $left, $right );
103 }
104 23 ▶ if ( $left->isa('Zuzu::Value::Set') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($left->isa("Zuzu::Value::Set")) 1 22 23
105 1 return _set_equal( $left, $right );
106 }
107 22 ▶ if ( $left->isa('Zuzu::Value::Dict') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($left->isa("Zuzu::Value::Dict")) 4 18 22
108 4 return _dict_equal( $left, $right );
109 }
110 18 ▶ if ( $left->isa('Zuzu::Value::PairList') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($left->isa("Zuzu::Value::PairList")) 2 16 18
111 2 return _pairlist_equal( $left, $right );
112 }
113 }
114  
115 16 ▶ return stable_value_key( $left ) eq stable_value_key( $right ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
stable_value_key($left) eq stable_value_key($right) ? : 16 0 16
116 }
117  
118 7 ▶sub _array_equal {
Subroutine _array_equal: called CC=6 SCAR=19.0
119 7 my ( $left, $right ) = @_;
120  
121 7 ▶ my $left_items = $left->items // [];
Condition50%1/2
$left->items // []
l!l
70
MC/DC0%0/1
$left->items // []
$left->items
Truth table50%1/2
A
A= $left->items
Aresult
11
00
122 7 ▶ my $right_items = $right->items // [];
Condition50%1/2
$right->items // []
l!l
70
MC/DC0%0/1
$right->items // []
$right->items
Truth table50%1/2
A
A= $right->items
Aresult
11
00
123  
124 7 ▶ return 0 if @$left_items != @$right_items;
Branch: 1 branch
BranchTrueFalseTotal
if @$left_items != @$right_items 0 7 7
125  
126 7 for my $i ( 0 .. $#$left_items ) {
127 12 ▶ return 0 if !value_equal(
Branch: 1 branch
BranchTrueFalseTotal
unless value_equal($left->_value_at($i), $right->_value_at($i)) 0 12 12
128 $left->_value_at($i),
129 $right->_value_at($i),
130 );
131 }
132  
133 7 return 1;
134 }
135  
136 1 ▶sub _bag_equal {
Subroutine _bag_equal: called CC=4 SCAR=14.0
137 1 my ( $left, $right ) = @_;
138  
139 my @left_items = sort {
140 1 stable_value_key( $a ) cmp stable_value_key( $b )
141 } $left->resolved_items;
142 my @right_items = sort {
143 1 stable_value_key( $a ) cmp stable_value_key( $b )
144 } $right->resolved_items;
145  
146 1 ▶ return 0 if @left_items != @right_items;
Branch: 1 branch
BranchTrueFalseTotal
if @left_items != @right_items 0 1 1
147  
148 1 for my $i ( 0 .. $#left_items ) {
149 2 ▶ return 0 if !value_equal( $left_items[$i], $right_items[$i] );
Branch: 1 branch
BranchTrueFalseTotal
unless value_equal($left_items[$i], $right_items[$i]) 0 2 2
150 }
151  
152 1 return 1;
153 }
154  
155 1 ▶sub _set_equal {
Subroutine _set_equal: called CC=5 SCAR=16.5
156 1 my ( $left, $right ) = @_;
157  
158 1 for my $left_item ( $left->resolved_items ) {
159 2 ▶ return 0 if !$right->contains( $left_item );
Branch: 1 branch
BranchTrueFalseTotal
unless $right->contains($left_item) 0 2 2
160 }
161 1 for my $right_item ( $right->resolved_items ) {
162 2 ▶ return 0 if !$left->contains( $right_item );
Branch: 1 branch
BranchTrueFalseTotal
unless $left->contains($right_item) 0 2 2
163 }
164  
165 1 return 1;
166 }
167  
168 4 ▶sub _dict_equal {
Subroutine _dict_equal: called CC=8 SCAR=21.8
169 4 my ( $left, $right ) = @_;
170  
171 4 ▶ my @left_keys = sort CORE::keys %{ $left->map // {} };
Condition50%1/2
$left->map // {}
l!l
40
MC/DC0%0/1
$left->map // {}
$left->map
Truth table50%1/2
A
A= $left->map
Aresult
11
00
172 4 ▶ my @right_keys = sort CORE::keys %{ $right->map // {} };
Condition50%1/2
$right->map // {}
l!l
40
MC/DC0%0/1
$right->map // {}
$right->map
Truth table50%1/2
A
A= $right->map
Aresult
11
00
173  
174 4 ▶ return 0 if @left_keys != @right_keys;
Branch: 1 branch
BranchTrueFalseTotal
if @left_keys != @right_keys 0 4 4
175  
176 4 for my $i ( 0 .. $#left_keys ) {
177 5 ▶ return 0 if !value_equal( $left_keys[$i], $right_keys[$i] );
Branch: 1 branch
BranchTrueFalseTotal
unless value_equal($left_keys[$i], $right_keys[$i]) 0 5 5
178 }
179 4 for my $key ( @left_keys ) {
180 5 ▶ return 0 if !value_equal(
Branch: 1 branch
BranchTrueFalseTotal
unless value_equal($left->_value_for_key($key), $right->_value_for_key($key)) 0 5 5
181 $left->_value_for_key($key),
182 $right->_value_for_key($key),
183 );
184 }
185  
186 4 return 1;
187 }
188  
189 2 ▶sub _pairlist_equal {
Subroutine _pairlist_equal: called CC=9 SCAR=24.0
190 2 my ( $left, $right ) = @_;
191  
192 2 ▶ my $left_pairs = $left->list // [];
Condition50%1/2
$left->list // []
l!l
20
MC/DC0%0/1
$left->list // []
$left->list
Truth table50%1/2
A
A= $left->list
Aresult
11
00
193 2 ▶ my $right_pairs = $right->list // [];
Condition50%1/2
$right->list // []
l!l
20
MC/DC0%0/1
$right->list // []
$right->list
Truth table50%1/2
A
A= $right->list
Aresult
11
00
194  
195 2 ▶ return 0 if @$left_pairs != @$right_pairs;
Branch: 1 branch
BranchTrueFalseTotal
if @$left_pairs != @$right_pairs 0 2 2
196  
197 2 for my $i ( 0 .. $#$left_pairs ) {
198 2 ▶ my $left_pair = $left_pairs->[$i] // [];
Condition50%1/2
$left_pairs->[$i] // []
l!l
20
MC/DC0%0/1
$left_pairs->[$i] // []
$left_pairs->[$i]
Truth table50%1/2
A
A= $left_pairs->[$i]
Aresult
11
00
199 2 ▶ my $right_pair = $right_pairs->[$i] // [];
Condition50%1/2
$right_pairs->[$i] // []
l!l
20
MC/DC0%0/1
$right_pairs->[$i] // []
$right_pairs->[$i]
Truth table50%1/2
A
A= $right_pairs->[$i]
Aresult
11
00
200  
201 2 ▶ return 0 if !value_equal( $left_pair->[0], $right_pair->[0] );
Branch: 1 branch
BranchTrueFalseTotal
unless value_equal($left_pair->[0], $right_pair->[0]) 0 2 2
202 2 ▶ return 0 if !value_equal(
Branch: 1 branch
BranchTrueFalseTotal
unless value_equal($left->_value_at($i), $right->_value_at($i)) 0 2 2
203 $left->_value_at($i),
204 $right->_value_at($i),
205 );
206 }
207  
208 2 return 1;
209 }
210  
211 79 1;
212  
213 =pod
214  
215 =head1 COPYRIGHT AND LICENCE
216  
217 B<< Zuzu::Value::Equality >> is copyright Toby Inkster.
218  
219 It is free software; you may redistribute it and/or modify it under
220 the terms of either the Artistic License 1.0 or the GNU General Public
221 License version 2.
222  
223 =cut
« blib/lib/Zuzu/Module/IO.pm ↑ Summary blib/lib/Zuzu/Web/PSGI.pm »