File: 216_language.t

package info (click to toggle)
libconvert-binary-c-perl 0.85-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 13,260 kB
  • sloc: ansic: 47,820; perl: 4,980; yacc: 2,143; makefile: 61
file content (171 lines) | stat: -rw-r--r-- 3,803 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
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
################################################################################
#
# 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 => 43 }

eval {
  $c = Convert::Binary::C->new;
};
ok($@,'',"failed to create Convert::Binary::C object");

#------------------------
# check the void keyword
#------------------------

eval {
  $c->clean->DisabledKeywords( [] );
  $c->parse( "typedef int void;" );
};
ok($@, qr/(parse|syntax) error/);

eval {
  $c->clean->DisabledKeywords( ['void'] );
  $c->parse( "typedef int void;" );
  @td = $c->typedef_names;
};
ok($@,'');
ok( scalar @td, 1 );
ok( $td[0], 'void' );

#------------------------
# check the C99 keywords
#------------------------

eval {
  $c->clean->DisabledKeywords( [] );
  $c->parse( "struct inline { int restrict; };" );
};
ok($@, qr/(parse|syntax) error/);

eval {
  $c->clean->DisabledKeywords( [qw( inline restrict )] );
  $c->parse( "struct inline { int restrict; };" );
  @st = $c->struct_names;
};
ok($@, '');
ok( scalar @st, 1 );
ok( $st[0], 'inline' );

my @c99decl = (
  'void funky(const int * const restrict foo[const restrict 8]);',
  'void funky(const int * const foo[const restrict 8]);',
  'void funky(const int * const foo[static const restrict 8]);',
  'void funky(const int * const foo[const restrict static 8]);',
  'void funky(const int * const foo[static 8]);',
  'void funky(restrict int * const foo[restrict 8]);',
);

$c->DisabledKeywords([]);

for my $c99 (@c99decl) {
  eval { $c->clean->parse($c99) };
  ok($@, '');
}

#--------------------
# check C++ comments
#--------------------

eval {
  $c->clean->DisabledKeywords( [] );
  $c->parse( "struct foo { int a[8//*comment*/4]; };\n" )
};
ok($@, qr/(parse|syntax) error/);

eval {
  $c->clean->HasCPPComments( 0 );
  $c->parse( "struct foo { char a[8//*comment*/4]; };\n" );
  $s = $c->sizeof('foo');
};
ok($@, '');
ok( $s, 2 );

#-----------------------------
# check (some) GNU extensions
#-----------------------------

eval {
  $c->clean->parse( "typedef __signed __extension__ long long _signed;" );
};
ok($@, qr/(parse|syntax) error/);

eval {
  $c->clean->Define( qw( __signed=signed __extension__= ) );
  $c->parse( "typedef __signed __extension__ long long _signed;" );
};
ok($@, '');

eval {
  $c->clean->parse( <<END );
#undef __signed
typedef __signed __extension__ long long _signed;
END
};
ok($@, qr/(parse|syntax) error/);

eval {
  $c->clean->KeywordMap( { __signed => 'signed', __extension__ => undef } );
  $c->parse( <<END );
#undef __signed
typedef __signed __extension__ long long _signed;
END
};
ok($@, '');

eval {
  $c->clean->Define( [] );
  $c->parse( <<END );
typedef __signed __extension__ long long _signed;
END
};
ok($@, '');

eval {
  $c->clean->parse( <<END );
typedef __signed __extension__ long long signed;
END
};
ok($@, qr/(parse|syntax) error/);

eval {
  $c->clean->DisabledKeywords( ['signed'] );
  $c->parse( <<END );
typedef __signed __extension__ long long signed;
END
};
ok($@, '');

#------------------------------
# check empty structs / unions
#------------------------------

for my $code (
               "struct test { };",
               "typedef struct { } test;",
               "union test { };",
               "typedef union { } test;",
               "struct test { } s = { };",
               "union test { } u = { };",
             )
{
  my $s = -1;
  my @m;
  eval {
    $c->clean->parse("$code\n");
    $s = $c->sizeof('test');
    @m = $c->member('test');
  };
  ok($@, '', $code);
  ok($s, 0);
  ok(scalar @m, 0);
}