Convert-Binary-C
view release on metacpan or search on metacpan
tests/101_basic.t view on Meta::CPAN
# reconfigure the parser
#------------------------
eval {
$p->configure( Alignment => 8, EnumSize => 0 );
};
ok($@,'');
#--------------------------------
# and parse some additional code
#--------------------------------
$code = <<'CCODE';
typedef struct {
abc_type xxx;
u32 dusel, *fusel;
int musel[((1<<1)+4)&0x00000002];
union {
char bytes[(12/2)%4][(0x10|010)>>3];
day today;
long value;
} test;
struct ints fubar;
union zap hello;
} husel;
#pragma pack( push, 1 )
struct packer {
char i;
short am;
char really;
long packed;
};
#pragma pack( pop )
struct nopack {
char i;
short am;
char not;
long packed;
};
CCODE
$c99_code = <<'CCODE' . $code;
#define \
MYINTS( ... \
) { int __VA_ARGS__; }
struct ints MYINTS( a, b, c );
CCODE
#-----------------------
# try to parse the code
#-----------------------
eval {
$q->HasMacroVAARGS( 0 );
$q->parse( $c99_code );
};
ok($@ =~ /invalid macro argument/);
eval { $p->parse( $c99_code ) };
ok($@,'');
#------------------------
# reconfigure the parser
#------------------------
eval { $p->Alignment( 4 ) };
ok($@,'');
#-------------------
# test some offsets
#-------------------
ok($p->offsetof('packer', 'i'), 0);
ok($p->offsetof('packer', 'am'), 1);
ok($p->offsetof('packer', 'really'), 3);
ok($p->offsetof('packer', 'packed'), 4);
ok($p->offsetof('nopack', 'i'), 0);
ok($p->offsetof('nopack', 'am'), 2);
ok($p->offsetof('nopack', 'not'), 4);
ok($p->offsetof('nopack', 'packed'), 8);
#------------------------
# now try some unpacking
#------------------------
# on a pack()ed struct
$data = pack( 'cnCN', -47, 0x1234, 0x55, 2000000000 );
eval { $result = $p->unpack( 'packer', $data ) };
ok($@,'');
$refres = {
i => -47,
am => 0x1234,
really => 0x55,
packed => 2000000000,
};
reccmp( $refres, $result );
# on a 'normal' struct
$data = pack( 'cxnCx3N', -47, 0x1234, 0x55, 2000000000 );
eval { $result = $p->unpack( 'nopack', $data ) };
ok($@,'');
$refres = {
'i' => -47,
'am' => 0x1234,
'not' => 0x55,
'packed' => 2000000000,
( run in 0.514 second using v1.01-cache-2.11-cpan-39bf76dae61 )