File Coverage

blib/lib/Protocol/DBus/Pack.pm
Criterion Covered Total %
statement 33 33 100.0
branch 6 6 100.0
condition 3 3 100.0
subroutine 9 9 100.0
pod 0 2 0.0
total 51 53 96.2


line stmt bran cond sub pod time code
1             package Protocol::DBus::Pack;
2              
3 6     6   31 use strict;
  6         10  
  6         126  
4 6     6   19 use warnings;
  6         10  
  6         180  
5              
6 6     6   27 use constant CAN_64 => eval { !!pack 'q' };
  6         6  
  6         12  
  6         507  
7              
8 6         344 use constant NUMERIC => {
9             y => 'C', # uint8
10             b => 'L', # boolean (uint32)
11             n => 's', # int16
12             q => 'S', # uint16
13             i => 'l', # int32
14             u => 'L', # uint32
15             x => 'q', # int64
16             t => 'Q', # uint64
17             d => 'd', # double float (?)
18             h => 'L', # unix fd, uint32
19 6     6   35 };
  6         15  
20              
21 6         581 use constant STRING => {
22             s => 'L/a x',
23             o => 'L/a x',
24             g => 'C/a x',
25 6     6   30 };
  6         8  
26              
27             use constant WIDTH => {
28              
29             # Accommodate 32-bit Perls:
30 60 100 100     288 (map { $_ => ($_ eq 'x' || $_ eq 't') ? 8 : length pack NUMERIC()->{$_} } keys %{ NUMERIC() }),
  6         31  
31 6         9 (map { $_ => length pack STRING()->{$_} } keys %{ STRING() }),
  18         543  
  6         42  
32 6     6   35 };
  6         13  
33              
34             use constant ALIGNMENT => {
35 6         20 %{ WIDTH() },
36 6         6 map { $_ => length pack( substr( STRING()->{$_}, 0, 1 ) ) } keys %{ STRING() },
  18         785  
  6         13  
37 6     6   35 };
  6         12  
38              
39             # Increments the 1st arg in-place to align on a boundary of the 2nd arg.
40             # ex. align( 7, 8 ) will change the $_[0] to be 8.
41             sub align {
42 1770 100   1770 0 2813 if ($_[0] % $_[1]) {
43 213         283 $_[0] += ($_[1] - ($_[0] % $_[1]));
44             }
45             }
46              
47             sub align_str {
48 209 100   209 0 394 if (my $mod = length($_[0]) % $_[1]) {
49 31         59 $_[0] .= "\0" x ($_[1] - $mod);
50             }
51             }
52              
53             1;