SegmentTree/node.pm

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