SegmentTree/ValueLookup.pm

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