File: 227_flexarray.t

package info (click to toggle)
libconvert-binary-c-perl 0.86-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,264 kB
  • sloc: ansic: 47,836; perl: 4,980; yacc: 2,143; makefile: 61
file content (139 lines) | stat: -rw-r--r-- 3,040 bytes parent folder | download | duplicates (2)
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
################################################################################
#
# 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 => 49 }

my $c = Convert::Binary::C->new( IntSize => 4, ShortSize => 2, Alignment => 4 );

$c->parse(<<ENDC);

struct flex1 {

  int   a;
  char  b;

  struct {
    char  a;
    short b;
    char  c;
    int   d[][4];
  }     c;

  char  d[5];

  struct {
    char  a;
    short b[][4];
  }     e[];

};

struct flex2 {

  int  a;
  struct {
    char  a[2];
    short b;
  } b[];

};

struct flex3 {
  int a;
  int b[][2][3][4];
};

typedef int incomplete[];
typedef int incomplete2[1];

ENDC

ok($c->sizeof('flex1'), 24);
ok($c->offsetof('flex1', 'e[1].a'), 24);
ok($c->offsetof('flex1', 'e[0].b[0][0]'), 24);

ok($c->sizeof('flex2'), 4);
ok($c->offsetof('flex2', 'b[0].b'), 6);

ok($c->sizeof('flex3'), 4);
ok($c->sizeof('flex3.b'), 0);
ok($c->sizeof('flex3.b[0]'), 96);
ok($c->sizeof('flex3.b[0][1]'), 48);
ok($c->sizeof('flex3.b[0][1][2]'), 16);
ok($c->sizeof('flex3.b[0][1][2][3]'), 4);
ok($c->offsetof('flex3', 'b[0]'), 4);
ok($c->offsetof('flex3', 'b[0][0]'), 4);
ok($c->offsetof('flex3', 'b[0][0][0]'), 4);
ok($c->offsetof('flex3', 'b[0][0][0][0]'), 4);
ok($c->offsetof('flex3', 'b[1]'), 100);
ok($c->offsetof('flex3', 'b[0][1]'), 52);
ok($c->offsetof('flex3', 'b[0][0][1]'), 20);
ok($c->offsetof('flex3', 'b[0][0][0][1]'), 8);

ok($c->sizeof('incomplete'), 0);
ok($c->offsetof('incomplete', '[10]'), 40);

my($u, $p);

my $d = pack 'C*', 1 .. 4;
$u = $c->unpack('flex2', $d);
ok(scalar @{$u->{b}}, 0);

$d .= pack 'C', 5;
$u = $c->unpack('flex2', $d);
ok(scalar @{$u->{b}}, 1);
ok(scalar @{$u->{b}[0]{a}}, 2);
ok($u->{b}[0]{a}[0], 5);
ok(not defined $u->{b}[0]{b});

$d .= pack 'C', 6;
$u = $c->unpack('flex2', $d);
ok(scalar @{$u->{b}}, 1);
ok(scalar @{$u->{b}[0]{a}}, 2);
ok($u->{b}[0]{a}[0], 5);
ok($u->{b}[0]{a}[1], 6);
ok(not defined $u->{b}[0]{b});

$d .= pack 'C', 7;
$u = $c->unpack('flex2', $d);
ok(scalar @{$u->{b}}, 1);
ok(scalar @{$u->{b}[0]{a}}, 2);
ok($u->{b}[0]{a}[0], 5);
ok($u->{b}[0]{a}[1], 6);
ok(exists $u->{b}[0]{b});
ok(not defined $u->{b}[0]{b});

$d .= pack 'C', 8;
$u = $c->unpack('flex2', $d);
ok(scalar @{$u->{b}}, 1);
ok(scalar @{$u->{b}[0]{a}}, 2);
ok($u->{b}[0]{a}[0], 5);
ok($u->{b}[0]{a}[1], 6);
ok(exists $u->{b}[0]{b});
ok(defined $u->{b}[0]{b});

$d .= pack 'C', 9;
$u = $c->unpack('flex2', $d);
ok(scalar @{$u->{b}}, 2);

$d = pack 'C*', map { $_ % 256 } 1 .. (10*$c->sizeof('flex3.b[0]'));
$u = $c->unpack('flex3.b', $d);
$p = $c->pack('flex3.b', $u);
ok($d, $p);

for my $member (qw( b[0] b[0][1] b[0][1][2] b[0][1][2][3] )) {
  $d = pack 'C*', 1 .. $c->sizeof("flex3.$member");
  $u = $c->unpack("flex3.$member", $d);
  $p = $c->pack("flex3.$member", $u);
  ok($d, $p);
}