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 7     7   56 use strict;
  7         14  
  7         191  
4 7     7   30 use warnings;
  7         14  
  7         284  
5              
6 7     7   38 use constant CAN_64 => eval { !!pack 'q' };
  7         13  
  7         12  
  7         609  
7              
8 7         534 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 7     7   65 };
  7         16  
20              
21 7         944 use constant STRING => {
22             s => 'L/a x',
23             o => 'L/a x',
24             g => 'C/a x',
25 7     7   44 };
  7         14  
26              
27             use constant WIDTH => {
28              
29             # Accommodate 32-bit Perls:
30 70 100 100     366 (map { $_ => ($_ eq 'x' || $_ eq 't') ? 8 : length pack NUMERIC()->{$_} } keys %{ NUMERIC() }),
  7         31  
31 7         14 (map { $_ => length pack STRING()->{$_} } keys %{ STRING() }),
  21         954  
  7         27  
32 7     7   49 };
  7         13  
33              
34             use constant ALIGNMENT => {
35 7         31 %{ WIDTH() },
36 7         18 map { $_ => length pack( substr( STRING()->{$_}, 0, 1 ) ) } keys %{ STRING() },
  21         1422  
  7         20  
37 7     7   86 };
  7         14  
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 1789 100   1789 0 3638 if ($_[0] % $_[1]) {
43 222         456 $_[0] += ($_[1] - ($_[0] % $_[1]));
44             }
45             }
46              
47             sub align_str {
48 207 100   207 0 501 if (my $mod = length($_[0]) % $_[1]) {
49 31         111 $_[0] .= "\0" x ($_[1] - $mod);
50             }
51             }
52              
53             1;