Differences.pm

filter: Statementstmt 95.5% 43 / 45 Branchbran 84.6% 22 / 26 Conditioncond 75.0% 18 / 24 MC/DCmcdc 41.6% 5 / 12 Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 6 totaltotal 79.1% 95 / 120 CC 24 SCAR 18.5
CC
24
Cov
87%
filter
10.4
is_empty
7.0
filter_hash
5.0
CRAP 6.4
« Comparator.pm ↑ Summary Utilities.pm »
1 #!/usr/bin/perl -w
2 #!/usr/bin/perl -d:ptkdb -w
3 #
4 # This is module is based on a module with the same name, implemented
5 # when working for Newtec Cy, located in Belgium,
6 # http://www.newtec.be/.
7 #
8  
9 package Data::Differences;
10  
11  
12 #
13 # Please read the comments of the constructor (new()) before using
14 # this package.
15 #
16 # How it should be done (at least this is what I think at the moment
17 # of writing) :
18 #
19 # 1. A set of differences is essentially a blessed hash which inherits
20 # from this package.
21 #
22 # 2. At the top level the hash contains entries describing meta
23 # information :
24 #
25 # 2.1. The fundamental types that have been compared.
26 #
27 # 3. Additionally the following items are present at the top level :
28 #
29 # 3.1. 'subtractor_operands' : operands for the subtract operation.
30 #
31 # 3.2. 'adder_operands' : operands for the add operation. This should
32 # be feed to Hash::Merge, possibly by first embedding the given
33 # arguments in a top level hash (e.g. if the arguments are arrays).
34 #
35  
36  
37 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39  
40 #
41 # is_empty()
42 #
43 # Return true if a differences describing data structure describes no
44 # differences.
45 #
46  
47 sub is_empty
48 364 ▶{
Subroutine is_empty: called CC=7 SCAR=19.5
Pod undocumented
49 364 my $self = shift;
50  
51 #t perhaps I should also return false for an array with only
52 #t undefs and likewise for a hash ?
53  
54 # for an empty hash
55  
56 364 ▶ if (("$self" =~ /HASH/ && !%$self)
Branch: 1 branch
BranchTrueFalseTotal
if ("$self" =~ /HASH/ and not %$self or "$self" =~ /ARRAY/ and not @$self or "$self" =~ /SCALAR/ and not defined $$self) { } 357 7 364
Condition100%15/15
"$self" =~ /HASH/ and not %$self or "$self" =~ /ARRAY/ and not @$self or "$self" =~ /SCALAR/ and not defined $$self
l!l&&r!l&&!r
1981597
"$self" =~ /HASH/ and not %$self or "$self" =~ /ARRAY/ and not @$self
l!l&&r!l&&!r
14058166
"$self" =~ /HASH/ and not %$self
!ll&&!rl&&r
2222140
"$self" =~ /ARRAY/ and not @$self
!ll&&!rl&&r
164258
"$self" =~ /SCALAR/ and not defined $$self
!ll&&!rl&&r
43159
MC/DC83%5/6
"$self" =~ /HASH/ and not %$self or "$self" =~ /ARRAY/ and not @$self or "$self" =~ /SCALAR/ and not defined $$self
"$self" =~ /HASH/ not %$self "$self" =~ /ARRAY/ not @$self "$self" =~ /SCALAR/ not defined $$self
Truth table40%6/15
A and not(B) or C and not(D) or E and not(F)
A= "$self" =~ /HASH/B= not %$selfC= "$self" =~ /ARRAY/D= not @$selfE= "$self" =~ /SCALAR/F= not defined $$self
ABCDEFresult
11XXXX1
0X11XX1
1011XX1
0X0X111
0X10111
100X111
1010111
0X0X0X0
0X0X100
0X100X0
0X10100
100X0X0
100X100
10100X0
1010100
57  
58 # or an empty array
59  
60 || ("$self" =~ /ARRAY/ && !@$self)
61  
62 # or an empty scalar
63  
64 || ("$self" =~ /SCALAR/ && !defined $$self))
65 {
66 # return empty
67  
68 357 return 1;
69 }
70  
71 # otherwise
72  
73 else
74 {
75 # return false
76  
77 7 return 0;
78 }
79 }
80  
81  
82 #
83 # filter()
84 #
85 # Filter the differences set by removing the undef entries. For
86 # reasons of consistency this sub must be called during construction.
87 #
88  
89 sub filter
90 399 ▶{
Subroutine filter: called CC=8 SCAR=23.4
Pod undocumented
91 399 my $self = shift;
92  
93 399 ▶ if ("$self" =~ /ARRAY/)
Branch: 5 branches
BranchTrueFalseTotal
if ("$self" =~ /ARRAY/) { } 66 333 399
elsif ("$self" =~ /HASH/) { } 148 185 333
elsif ("$self" =~ /SCALAR/) { } 181 4 185
elsif ("$self" =~ /REF/ and not defined $$self) { } 0 4 4
elsif ("$self" =~ /REF/ and UNIVERSAL::isa($$self, 'Data::Differences')) { } 0 4 4
94 {
95 66 return $self->filter_array();
96 }
97 elsif ("$self" =~ /HASH/)
98 {
99 148 return $self->filter_hash();
100 }
101 elsif ("$self" =~ /SCALAR/)
102 {
103 181 return $self->filter_scalar();
104 }
105 ▶ elsif ("$self" =~ /REF/
Condition33%1/3
"$self" =~ /REF/ and not defined $$self
!ll&&!rl&&r
040
MC/DC0%0/2
"$self" =~ /REF/ and not defined $$self
"$self" =~ /REF/ not defined $$self
Truth table33%1/3
A and not(B)
A= "$self" =~ /REF/B= not defined $$self
ABresult
0X0
100
111
106 && !defined $$self)
107 {
108 0 return \undef;
109 }
110 ▶ elsif ("$self" =~ /REF/
Condition33%1/3
"$self" =~ /REF/ and UNIVERSAL::isa($$self, "Data::Differences")
!ll&&!rl&&r
040
MC/DC0%0/2
"$self" =~ /REF/ and UNIVERSAL::isa($$self, "Data::Differences")
"$self" =~ /REF/ UNIVERSAL::isa($$self, "Data::Differences")
Truth table33%1/3
A and B
A= "$self" =~ /REF/B= UNIVERSAL::isa($$self, "Data::Differences")
ABresult
0X0
100
111
111 && UNIVERSAL::isa($$self,'Data::Differences'))
112 {
113 0 return $$self->filter();
114 }
115 else
116 {
117 # a structure that cannot be dissected any further : return it.
118  
119 4 return $self;
120 }
121 }
122  
123  
124 sub filter_array
125 66 ▶{
Subroutine filter_array: called CC=5 SCAR=16.1
Pod undocumented
126 66 my $self = shift;
127  
128 66 my $is_empty = 1;
129  
130 66 foreach my $entry (@$self)
131 {
132 #t see comments below on ->filter_hash().
133 #t perhaps this needs protection with an additional check, not sure.
134  
135 21 ▶ if (defined $entry)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $entry) 13 8 21
136 {
137 13 ▶ if ($entry->filter())
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->filter) 8 5 13
138 {
139 8 $is_empty = 0;
140 }
141 }
142 }
143  
144 66 ▶ if ($is_empty)
Branch: 1 branch
BranchTrueFalseTotal
if ($is_empty) 58 8 66
145 {
146 58 @$self = ();
147 }
148  
149 66 return $self;
150 }
151  
152  
153 sub filter_hash
154 148 ▶{
Subroutine filter_hash: called CC=5 SCAR=16.2
Pod undocumented
155 148 my $self = shift;
156  
157 148 my $is_empty = 1;
158  
159 148 foreach my $key (keys %$self)
160 {
161 #t
162 #t The first if condition is bogus I guess,
163 #t commented out but perhaps
164 #t it is right for certain scenarios, unresolved.
165 #t The rest of the TODO comments is about the
166 #t commented out condition only.
167 #t
168 #t we can get here with a reference to an empty hash.
169 #t without the eval below, we get the perl error
170 #t 'Not a SCALAR reference'. I simply added the
171 #t eval {} statement to allow to continue the other
172 #t developments. I inspected via the debugger the
173 #t correctness of the software under test,
174 #t I am not sure of the correctness of the testing
175 #t software. Given the fact that this eval {} statement
176 #t also hides a number of warnings, I suspect so far
177 #t unforeseen scenarios that might popup as bugs of
178 #t the testing software.
179  
180 eval
181 8 {
182 # if (defined ${$self->{$key}})
183 8 ▶ if (defined $self->{$key})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{$key}) 8 0 8
184 {
185 8 ▶ if ($self->{$key}->filter())
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{$key}->filter) 8 0 8
186 {
187 8 $is_empty = 0;
188 }
189 }
190 };
191 }
192  
193 148 ▶ if ($is_empty)
Branch: 1 branch
BranchTrueFalseTotal
if ($is_empty) 140 8 148
194 {
195 140 %$self = ();
196 }
197  
198 148 return $self;
199 }
200  
201  
202 sub filter_scalar
203 181 ▶{
Subroutine filter_scalar: called CC=2 SCAR=6.9
Pod undocumented
204 181 my $self = shift;
205  
206 181 ▶ if (defined $$self)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$self) { } 12 169 181
207 {
208 12 return 1;
209 }
210 else
211 {
212 169 return undef;
213 }
214 }
215  
216  
217 #
218 # new()
219 #
220 # Create a new structure from the given data structure.
221 #
222 # The given data structure must be compliant with the common
223 # conventions for this data structure, whatever they may be. They
224 # still need complete definition.
225 #
226 # Currently the differences structure only deals with new data (to be
227 # added to existing data), it does not deal with data to be removed.
228 #
229  
230 sub new
231 378 ▶{
Subroutine new: called CC=2 SCAR=7.1
Pod undocumented
232 378 my $proto = shift;
233  
234 378 ▶ my $class = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
03780
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
235  
236 378 my $differences = shift;
237  
238 378 bless $differences, $class;
239  
240 378 $differences->filter();
241  
242 378 return $differences;
243 }
244  
245  
246 7 1;
247  
248  
« Comparator.pm ↑ Summary Utilities.pm »