SegmentTree/ValueLookup.pm

filter: Statementstmt 68.1% 105 / 154 Branchbran 48.7% 38 / 78 Conditioncond 41.6% 10 / 24 MC/DCmcdc 20.0% 3 / 15 Subroutinesub 75.0% 12 / 16 Podpod 0.0% 0 / 13 totaltotal 56.0% 168 / 300 SCAR 55.9
CC
57
Cov
60%
serialize_array
34.8
created
20.0
deserialize_array
11.4
CRAP 268.6
« SegmentTree/node.pm ↑ Summary SegmentTree/Builder.pm »
1 1 ▶use Set::SegmentTree::node;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package Set::SegmentTree::ValueLookup;
3 # table package auto-generated by Data::FlatTables
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 sub new {
8 4 ▶ my ($class, %args) = @_;
Subroutine new: called CC=6 SCAR=18.0
Pod undocumented
9 4 my $self = bless {}, $class;
10  
11 4 ▶ $self->{root} = $args{root} if defined $args{root} and $args{root} != 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"root"} and $args{"root"} != 0 2 2 4
Condition66%2/3
defined $args{"root"} and $args{"root"} != 0
!ll&&!rl&&r
202
MC/DC50%1/2
defined $args{"root"} and $args{"root"} != 0
defined $args{"root"} $args{"root"} != 0
Truth table66%2/3
A and B
A= defined $args{"root"}B= $args{"root"} != 0
ABresult
0X0
100
111
12 $self->{nodes} =
13 [ map {
14 58 Set::SegmentTree::node->new(%$_)
15 2 } @{$args{nodes}} ]
16 4 ▶ if exists $args{nodes};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"nodes"} 2 2 4
17 4 ▶ $self->{created} = $args{created} if defined $args{created} and $args{created} != 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"created"} and $args{"created"} != 0 2 2 4
Condition66%2/3
defined $args{"created"} and $args{"created"} != 0
!ll&&!rl&&r
202
MC/DC50%1/2
defined $args{"created"} and $args{"created"} != 0
defined $args{"created"} $args{"created"} != 0
Truth table66%2/3
A and B
A= defined $args{"created"}B= $args{"created"} != 0
ABresult
0X0
100
111
18  
19 4 return $self;
20 }
21  
22 0 ▶sub flatbuffers_type { 'table' }
Subroutine flatbuffers_type: not called CC=1 SCAR=6.9
Pod undocumented
23  
24 1 my %basic_types = (
25 bool => { format => 'C', length => 1 },
26 byte => { format => 'c', length => 1 },
27 ubyte => { format => 'C', length => 1 },
28 short => { format => 's<', length => 2 },
29 ushort => { format => 'S<', length => 2 },
30 int => { format => 'l<', length => 4 },
31 uint => { format => 'L<', length => 4 },
32 float => { format => 'f<', length => 4 },
33 long => { format => 'q<', length => 8 },
34 ulong => { format => 'Q<', length => 8 },
35 double => { format => 'd<', length => 8 },
36 );
37 28 ▶sub root { @_ > 1 ? $_[0]{root} = ( $_[1] == 0 ? undef : $_[1]) : $_[0]{root} // 0 }
Subroutine root: called CC=4 SCAR=19.4
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
@_ > 1 ? : 0 28 28
$_[1] == 0 ? : 0 0 0
Condition50%1/2
$_[0]{"root"} // 0
l!l
280
MC/DC0%0/1
$_[0]{"root"} // 0
$_[0]{"root"}
Truth table50%1/2
A
A= $_[0]{"root"}
Aresult
11
00
38 sub nodes {
39 @_ > 1 ? $_[0]{nodes} =
40 [ map {
41 0 ▶ (ref and not UNIVERSAL::can($_, 'can')) ? Set::SegmentTree::node->new(%$_) : $_
Branch: 1 branch
BranchTrueFalseTotal
ref $_ && !UNIVERSAL::can($_, 'can') ? : 0 0 0
Condition0%0/3
ref $_ and not UNIVERSAL::can($_, "can")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_ and not UNIVERSAL::can($_, "can")
ref $_ not UNIVERSAL::can($_, "can")
Truth table0%0/3
A and not(B)
A= ref $_B= not UNIVERSAL::can($_, "can")
ABresult
0X0
100
111
42 0 } @{$_[1]} ]
43 : $_[0]{nodes}
44 264 ▶}
Subroutine nodes: called CC=4 SCAR=15.2
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 264 264
45 0 ▶sub created { @_ > 1 ? $_[0]{created} = ( $_[1] == 0 ? undef : $_[1]) : $_[0]{created} // 0 }
Subroutine created: not called CC=4 SCAR=30.0
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
@_ > 1 ? : 0 0 0
$_[1] == 0 ? : 0 0 0
Condition0%0/2
$_[0]{"created"} // 0
l!l
00
MC/DC0%0/1
$_[0]{"created"} // 0
$_[0]{"created"}
Truth table0%0/2
A
A= $_[0]{"created"}
Aresult
11
00
46  
47 sub deserialize {
48 2 ▶ my ($self, $data, $offset) = @_;
Subroutine deserialize: called CC=8 SCAR=23.4
Pod undocumented
49 2 ▶ $offset //= 0;
Condition50%1/2
$offset //= 0
l!l
02
MC/DC0%0/1
$offset //= 0
$offset
Truth table50%1/2
A
A= $offset
Aresult
11
00
50 2 ▶ $self = $self->new unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 2 0 2
51  
52 # verify file identifier
53 2 ▶ if ($offset == 0 and 'RTRE' ne substr $data, 4, 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset == 0 and "RTRE" ne substr($data, 4, 4)) 0 2 2
Condition33%1/3
$offset == 0 and "RTRE" ne substr($data, 4, 4)
!ll&&!rl&&r
020
MC/DC0%0/2
$offset == 0 and "RTRE" ne substr($data, 4, 4)
$offset == 0 "RTRE" ne substr($data, 4, 4)
Truth table33%1/3
A and B
A= $offset == 0B= "RTRE" ne substr($data, 4, 4)
ABresult
0X0
100
111
54 0 die 'invalid fbs file identifier, "RTRE" expected';
55 }
56  
57 2 my $object_offset = $offset + unpack "L<", substr $data, $offset, 4;
58 2 my $vtable_offset = $object_offset - unpack "l<", substr $data, $object_offset, 4;
59 2 my @offsets = map unpack ("S<", $_), map substr ($data, $vtable_offset + $_ * 2, 2), 2 .. 4;
60  
61 2 ▶ $self->{root} = unpack 'l<', substr $data, $object_offset + $offsets[0], 4 if $offsets[0] != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $offsets[0] != 0 2 0 2
62 2 ▶ $self->{nodes} = $self->deserialize_array('[Set::SegmentTree::node]', $data, $object_offset + $offsets[1]) if $offsets[1] != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $offsets[1] != 0 2 0 2
63 2 ▶ $self->{created} = unpack 'l<', substr $data, $object_offset + $offsets[2], 4 if $offsets[2] != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $offsets[2] != 0 2 0 2
64  
65 2 return $self
66 }
67  
68  
69  
70 sub deserialize_string {
71 0 ▶ my ($self, $data, $offset) = @_;
Subroutine deserialize_string: not called CC=1 SCAR=6.9
Pod undocumented
72  
73 0 my $string_offset = $offset + unpack "L<", substr $data, $offset, 4; # dereference the string pointer
74 0 my $string_length = unpack "L<", substr $data, $string_offset, 4; # get the length
75 0 return substr $data, $string_offset + 4, $string_length # return a substring
76 }
77  
78 sub deserialize_array {
79 2 ▶ my ($self, $array_type, $data, $offset) = @_;
Subroutine deserialize_array: called CC=6 SCAR=24.3
Pod undocumented
80  
81 2 $array_type = $array_type =~ s/\A\[(.*)\]\Z/$1/sr;
82  
83 2 $offset = $offset + unpack "L<", substr $data, $offset, 4; # dereference the array pointer
84 2 my $array_length = unpack "L<", substr $data, $offset, 4; # get the length
85 2 $offset += 4;
86  
87 2 my @array;
88 2 ▶ if (exists $basic_types{$array_type}) { # if its an array of numerics
Branch: 3 branches
BranchTrueFalseTotal
if (exists $basic_types{$array_type}) { } 0 2 2
elsif ($array_type eq 'string') { } 0 2 2
elsif ($array_type =~ /\A\[/) { } 0 2 2
89 0 @array = map { unpack $basic_types{$array_type}{format}, $_ }
90 0 map { substr $data, $offset + $_, $basic_types{$array_type}{length} }
91 map $_ * $basic_types{$array_type}{length},
92 0 0 .. ($array_length - 1);
93
94 } elsif ($array_type eq "string") { # if its an array of strings
95 0 @array = map { $self->deserialize_string($data, $offset + $_) }
96 map $_ * 4,
97 0 .. ($array_length - 1);
98  
99 } elsif ($array_type =~ /\A\[/) { # if its an array of strings
100 0 @array = map { $self->deserialize_array($array_type, $data, $offset + $_) }
101 map $_ * 4,
102 0 .. ($array_length - 1);
103
104 } else { # if its an array of objects
105 2 ▶ if ($array_type->flatbuffers_type eq "table") {
Branch: 2 branches
BranchTrueFalseTotal
if ($array_type->flatbuffers_type eq 'table') { } 2 0 2
elsif ($array_type->flatbuffers_type eq 'struct') { } 0 0 0
106 2 @array = map { $array_type->deserialize($data, $offset + $_) }
107 map $_ * 4,
108 0 .. ($array_length - 1);
109 } elsif ($array_type->flatbuffers_type eq "struct") {
110 0 my $length = $array_type->flatbuffers_struct_length;
111 0 @array = map { $array_type->deserialize($data, $offset + $_) }
112 map $_ * $length,
113 0 .. ($array_length - 1);
114 } else {
115 ...
116 0 }
117 }
118  
119 return \@array
120 2 }
121  
122  
123 sub serialize {
124 4 ▶ my ($self, $cache) = @_;
Subroutine serialize: called CC=9 SCAR=22.5
Pod undocumented
125 4 ▶ if (not defined $cache) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $cache) { } 2 2 4
126 2 $cache = {};
127  
128 2 my @objects = $self->serialize($cache);
129 2 my $root = $objects[0]; # get the root data structure
130  
131 # insert file identifier
132 2 unshift @objects, { type => 'file_identifier', data => 'RTRE' };
133  
134 # header pointer to root data structure
135 2 unshift @objects, { type => "header", data => "\0\0\0\0", reloc => [{ offset => 0, item => $root, type => "unsigned delta" }] };
136  
137 2 return $self->serialize_objects(@objects);
138 } else {
139  
140 my $vtable = $self->serialize_vtable(
141 defined $self->{root} ? 4 : 0,
142 defined $self->{nodes} ? 4 : 0,
143 2 ▶ defined $self->{created} ? 4 : 0,
Branch: 3 branches
BranchTrueFalseTotal
defined $self->{'root'} ? : 2 0 2
defined $self->{'nodes'} ? : 2 0 2
defined $self->{'created'} ? : 2 0 2
144 );
145 2 my $data = "\0\0\0\0";
146  
147 2 my @reloc = ({ offset => 0, item => $vtable, type => "signed negative delta" });
148 # flatbuffers vtable offset is stored in negative form
149 2 my @objects = ($vtable);
150  
151 2 ▶ if (defined $self->{root}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"root"}) 2 0 2
152 2 $data .= pack 'l<', $self->{root};
153 }
154  
155 2 ▶ if (defined $self->{nodes}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"nodes"}) 2 0 2
156 2 my ($array_object, @array_objects) = $self->serialize_array('[Set::SegmentTree::node]', $self->{nodes}, $cache);
157 2 push @objects, $array_object, @array_objects;
158 2 push @reloc, { offset => length ($data), item => $array_object, type => 'unsigned delta'};
159 2 $data .= "\0\0\0\0";
160 }
161  
162 2 ▶ if (defined $self->{created}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"created"}) 2 0 2
163 2 $data .= pack 'l<', $self->{created};
164 }
165  
166 # pad to 4 byte boundary
167 2 ▶ $data .= pack "x" x (4 - (length ($data) % 4)) if length ($data) % 4;
Branch: 1 branch
BranchTrueFalseTotal
if length($data) % 4 0 2 2
168  
169 # return table data and other objects that we've created
170 2 return { type => "table", data => $data, reloc => \@reloc }, @objects
171 }
172 }
173
174  
175 sub serialize_objects {
176 2 ▶ my ($self, @objects) = @_;
Subroutine serialize_objects: called CC=10 SCAR=23.9
Pod undocumented
177  
178  
179 2 my $data = "";
180 2 my $offset = 0;
181  
182 # concatentate the data
183 2 for my $object (@objects) {
184 204 $object->{serialized_offset} = $offset;
185 204 $data .= $object->{data};
186 204 $offset += length $object->{data};
187 }
188  
189 # second pass for writing offsets to other parts
190 2 for my $object (@objects) {
191 204 ▶ if (defined $object->{reloc}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $object->{"reloc"}) 94 110 204
192 # perform address relocation
193 94 for my $reloc (@{$object->{reloc}}) {
194 200 my $value;
195 200 ▶ if (defined $reloc->{lambda}) { # allow the reloc to have a custom format
Branch: 3 branches
BranchTrueFalseTotal
if (defined $reloc->{'lambda'}) { } 0 200 200
elsif (defined $reloc->{'type'} and $reloc->{'type'} eq 'unsigned delta') { } 140 60 200
elsif (defined $reloc->{'type'} and $reloc->{'type'} eq 'signed negative delta') { } 60 0 60
196 0 $value = $reloc->{lambda}($object, $reloc);
197 ▶ } elsif (defined $reloc->{type} and $reloc->{type} eq "unsigned delta") {
Condition66%2/3
defined $reloc->{"type"} and $reloc->{"type"} eq "unsigned delta"
!ll&&!rl&&r
060140
MC/DC50%1/2
defined $reloc->{"type"} and $reloc->{"type"} eq "unsigned delta"
defined $reloc->{"type"} $reloc->{"type"} eq "unsigned delta"
Truth table66%2/3
A and B
A= defined $reloc->{"type"}B= $reloc->{"type"} eq "unsigned delta"
ABresult
0X0
100
111
198 140 $value = pack "L<", $reloc->{item}{serialized_offset} - $object->{serialized_offset} - $reloc->{offset};
199 ▶ } elsif (defined $reloc->{type} and $reloc->{type} eq "signed negative delta") {
Condition33%1/3
defined $reloc->{"type"} and $reloc->{"type"} eq "signed negative delta"
!ll&&!rl&&r
0060
MC/DC0%0/2
defined $reloc->{"type"} and $reloc->{"type"} eq "signed negative delta"
defined $reloc->{"type"} $reloc->{"type"} eq "signed negative delta"
Truth table33%1/3
A and B
A= defined $reloc->{"type"}B= $reloc->{"type"} eq "signed negative delta"
ABresult
0X0
100
111
200 60 $value = pack "l<", $object->{serialized_offset} + $reloc->{offset} - $reloc->{item}{serialized_offset};
201 } else {
202 ...
203 0 }
204 200 substr $data, $object->{serialized_offset} + $reloc->{offset}, length($value), $value;
205 }
206 }
207 }
208  
209 # done, the data is now ready to be deserialized
210 2 return $data
211 }
212  
213 sub serialize_vtable {
214 2 ▶ my ($self, @lengths) = @_;
Subroutine serialize_vtable: called CC=4 SCAR=14.0
Pod undocumented
215  
216 2 my $offset = 4;
217 2 my @table;
218  
219 2 for (@lengths) { # parse table offsets
220 6 ▶ push @table, $_ ? $offset : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 6 0 6
221 6 $offset += $_;
222 }
223  
224 2 unshift @table, $offset; # prefix data length
225 2 unshift @table, 2 * (@table + 1); #prefix vtable length
226 2 ▶ push @table, 0 if @table % 2; # pad if odd count
Branch: 1 branch
BranchTrueFalseTotal
if @table % 2 2 0 2
227 # compile object
228 2 return { type => "vtable", data => pack "S<" x @table, @table }
229 }
230  
231 sub serialize_string {
232 0 ▶ my ($self, $string) = @_;
Subroutine serialize_string: not called CC=2 SCAR=17.9
Pod undocumented
233  
234 0 my $len = pack "L<", length $string;
235 0 $string .= "\0"; # null termination byte because why the fuck not (it's part of flatbuffers)
236  
237 0 my $data = "$len$string";
238 0 ▶ $data .= pack "x" x (4 - (length ($data) % 4)) if length ($data) % 4; # pad to 4 byte boundary
Branch: 1 branch
BranchTrueFalseTotal
if length($data) % 4 0 0 0
239  
240 0 return { type => "string", data => $data }
241 }
242  
243  
244 sub serialize_array {
245 2 ▶ my ($self, $array_type, $array, $cache) = @_;
Subroutine serialize_array: called CC=10 SCAR=35.5
Pod undocumented
246  
247 2 $array_type = $array_type =~ s/\A\[(.*)\]\Z/$1/sr;
248  
249 2 my $data = pack "L<", scalar @$array;
250 2 my @array_objects;
251 my @reloc;
252  
253 2 ▶ if (exists $basic_types{$array_type}) { # array of scalar values
Branch: 3 branches
BranchTrueFalseTotal
if (exists $basic_types{$array_type}) { } 0 2 2
elsif ($array_type eq 'string') { } 0 2 2
elsif ($array_type =~ /\A\[/) { } 0 2 2
254 0 $data .= join "", map { pack $basic_types{$array_type}{format}, $_ } @$array;
255  
256 } elsif ($array_type eq "string") { # array of strings
257 0 $data .= "\0\0\0\0" x @$array;
258 0 for my $i (0 .. $#$array) {
259 0 my $string_object = $self->serialize_string($array->[$i]);
260 0 push @array_objects, $string_object;
261 0 push @reloc, { offset => 4 + $i * 4, item => $string_object, type => "unsigned delta" };
262 }
263 } elsif ($array_type =~ /\A\[/) { # array of arrays
264 0 $data .= "\0\0\0\0" x @$array;
265 0 for my $i (0 .. $#$array) {
266 0 my ($array_object, @child_array_objects) = $self->serialize_array($array_type, $array->[$i], $cache);
267 0 push @array_objects, $array_object, @child_array_objects;
268 0 push @reloc, { offset => 4 + $i * 4, item => $array_object, type => "unsigned delta" };
269 }
270  
271 } else { # else an array of objects
272 2 ▶ if ($array_type->flatbuffers_type eq "table") {
Branch: 2 branches
BranchTrueFalseTotal
if ($array_type->flatbuffers_type eq 'table') { } 2 0 2
elsif ($array_type->flatbuffers_type eq 'struct') { } 0 0 0
273 2 $data .= "\0\0\0\0" x @$array;
274 2 for my $i (0 .. $#$array) {
275 58 my ($root_object, @table_objects) = $array->[$i]->serialize($cache);
276 58 push @array_objects, $root_object, @table_objects;
277 58 push @reloc, { offset => 4 + $i * 4, item => $root_object, type => "unsigned delta" };
278 }
279 } elsif ($array_type->flatbuffers_type eq "struct") {
280 0 for my $i (0 .. $#$array) {
281 0 my ($root_object, @struct_objects) = $array->[$i]->serialize($cache);
282 0 push @array_objects, @struct_objects;
283 0 push @reloc, map { $_->{offset} += length ($data); $_ } @{$root_object->{reloc}};
284 0 $data .= $root_object->{data};
285  
286 }
287 } else {
288 ...
289 0 }
290 }
291  
292 2 return { type => "array", data => $data, reloc => \@reloc }, @array_objects
293 }
294  
295  
296  
297 1 1 # true return from package
298  
« SegmentTree/node.pm ↑ Summary SegmentTree/Builder.pm »