1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
################################################################################
#
# Copyright (c) 2002-2024 Marcus Holland-Moritz. All rights reserved.
# This program is free software; you can redistribute it and/or modify
# it under the same terms as Perl itself.
#
################################################################################
use Test;
use Convert::Binary::C @ARGV;
$^W = 1;
BEGIN { plan tests => 27 }
my $CCCFG = require './tests/include/config.pl';
$c = eval { Convert::Binary::C->new( %$CCCFG ) };
ok($@,'',"failed to create Convert::Binary::C objects");
eval { $c->parse_file( 'tests/include/include.c' ) };
ok($@,'',"failed to parse C-file");
$full = $zero = $c->sourcify;
for( $c->typedef_names ) {
next if $c->sizeof($_) == 0;
my $pre = "\n$_ S_$_ = ";
my $post = ";\n";
my $init = $c->unpack( $_, $c->pack($_) );
$zero .= $pre . $c->initializer( $_ ) . $post;
$full .= $pre . $c->initializer( $_, $init ) . $post;
}
$c = eval { Convert::Binary::C->new };
ok($@,'',"failed to create Convert::Binary::C objects");
{
my @warn;
local $SIG{__WARN__} = sub { push @warn, $_[0] };
eval { $c->clean->parse( $zero ) };
ok($@,'',"failed to parse zero initialization code");
eval { $c->clean->parse( $full ) };
ok($@,'',"failed to parse full initialization code");
ok( @warn == 0 );
}
for my $snip ( split /={40,}/, do { local $/; <DATA> } ) {
my($code, @tests) = split /-{40,}/, $snip;
eval { $c->clean->parse($code) };
ok($@,'',"failed to parse code snippet");
for my $test ( @tests ) {
$test =~ s/#.*//gm;
my($data, $id, $ref) = $test =~ /^\s*(?:\$\s*=\s*(.*?))?\s*(\w+)\s*=\s*(.*?)\s*$/;
my $init = defined $data
? $c->initializer( $id, eval $data )
: $c->initializer( $id );
$init =~ s/\s+//g;
$ref =~ s/\s+//g;
print "# ref : $ref\n# init: $init\n";
ok( $init, $ref, "wrong return value" );
}
}
__DATA__
/* check that only the first union member is initialized */
typedef union {
int c[10];
struct {
char a, b;
} d[2];
} uni;
struct xxx {
int a;
union {
struct {
int a, b;
uni;
} a;
int b;
int c[10][10];
} b;
int c;
};
struct test {
union {
union {
union {
int a;
int b;
int c;
} d;
int e;
int f;
} g;
int h;
int i;
} j;
union {
union {
union {
int a;
int b;
int c;
};
int e;
int f;
};
int h;
int i;
};
};
-------------------------------------------------------------------------------
xxx = { 0, { { 0, 0, { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } } } }, 0 }
-------------------------------------------------------------------------------
uni = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }
-------------------------------------------------------------------------------
test = { { { { 0 } } }, { { { 0 } } } }
-------------------------------------------------------------------------------
$ = { j => { g => { d => { a => 42 } } }, a => -4711 }
test = { { { { 42 } } }, { { { -4711 } } } }
-------------------------------------------------------------------------------
$ = { j => { g => { d => { b => 42 } } }, e => -4711, a => 101 }
test = { { { { 0 } } }, { { { 101 } } } }
===============================================================================
/* just another example */
struct foo {
int a;
union {
int a;
struct {
union {
int a;
char b;
} a;
int b;
} b;
char c;
} b;
struct {
int a;
union {
struct {
char a;
int b;
} a;
int b;
} b;
char c;
} c;
int d;
struct {
int xa;
int ba;
};
};
-------------------------------------------------------------------------------
foo = { 0, { 0 }, { 0, { { 0, 0 } }, 0 }, 0, { 0, 0 } }
===============================================================================
/* check that bitfields are working */
struct bits {
int a:3;
int :0;
int c:2;
int d:4;
};
-------------------------------------------------------------------------------
bits = { 0, 0, 0 }
-------------------------------------------------------------------------------
$ = { a => 3, c => 2, d => 1 }
bits = { 3, 2, 1 }
-------------------------------------------------------------------------------
$ = { a => 3, x => 2, d => 1 }
bits = { 3, 0, 1 }
===============================================================================
/* check that bitfield padding is skipped */
struct bits {
int :7;
int a:3;
int c:2;
int d:4;
};
-------------------------------------------------------------------------------
bits = { 0, 0, 0 }
===============================================================================
/* taken from the docs, this revealed a bug introduced
* with flexible array members
*/
struct date {
unsigned year : 12;
unsigned month: 4;
unsigned day : 5;
unsigned hour : 5;
unsigned min : 6;
};
typedef struct {
enum { DATE, QWORD } type;
short number;
union {
struct date date;
unsigned long qword;
} choice;
} data;
-------------------------------------------------------------------------------
data = { 0, 0, { { 0, 0, 0, 0, 0 } } }
-------------------------------------------------------------------------------
# we should be able to initialize enums
$ = { type => 1 }
data = { 1, 0, { { 0, 0, 0, 0, 0 } } }
-------------------------------------------------------------------------------
# even when out of range
$ = { type => 2 }
data = { 2, 0, { { 0, 0, 0, 0, 0 } } }
-------------------------------------------------------------------------------
# also by name
$ = { type => 'DATE' }
data = { DATE, 0, { { 0, 0, 0, 0, 0 } } }
-------------------------------------------------------------------------------
# even if it doesn't exist (could be some define)
$ = { type => 'SOMETHING' }
data = { SOMETHING, 0, { { 0, 0, 0, 0, 0 } } }
-------------------------------------------------------------------------------
# this should work for 'normal' types as well
$ = { number => 'SOMETHING' }
data = { 0, SOMETHING, { { 0, 0, 0, 0, 0 } } }
|