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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
# frozen_string_literal: true
begin
require_relative 'helper'
require 'fiddle/cparser'
require 'fiddle/import'
rescue LoadError
end
module Fiddle
class TestCParser < TestCase
include CParser
def test_char_ctype
assert_equal(TYPE_CHAR, parse_ctype('char'))
assert_equal(TYPE_CHAR, parse_ctype('signed char'))
assert_equal(-TYPE_CHAR, parse_ctype('unsigned char'))
end
def test_short_ctype
assert_equal(TYPE_SHORT, parse_ctype('short'))
assert_equal(TYPE_SHORT, parse_ctype('short int'))
assert_equal(TYPE_SHORT, parse_ctype('signed short'))
assert_equal(TYPE_SHORT, parse_ctype('signed short int'))
assert_equal(-TYPE_SHORT, parse_ctype('unsigned short'))
assert_equal(-TYPE_SHORT, parse_ctype('unsigned short int'))
end
def test_int_ctype
assert_equal(TYPE_INT, parse_ctype('int'))
assert_equal(TYPE_INT, parse_ctype('signed int'))
assert_equal(-TYPE_INT, parse_ctype('uint'))
assert_equal(-TYPE_INT, parse_ctype('unsigned int'))
end
def test_long_ctype
assert_equal(TYPE_LONG, parse_ctype('long'))
assert_equal(TYPE_LONG, parse_ctype('long int'))
assert_equal(TYPE_LONG, parse_ctype('signed long'))
assert_equal(TYPE_LONG, parse_ctype('signed long int'))
assert_equal(-TYPE_LONG, parse_ctype('unsigned long'))
assert_equal(-TYPE_LONG, parse_ctype('unsigned long int'))
end
def test_size_t_ctype
assert_equal(TYPE_SIZE_T, parse_ctype("size_t"))
end
def test_ssize_t_ctype
assert_equal(TYPE_SSIZE_T, parse_ctype("ssize_t"))
end
def test_ptrdiff_t_ctype
assert_equal(TYPE_PTRDIFF_T, parse_ctype("ptrdiff_t"))
end
def test_intptr_t_ctype
assert_equal(TYPE_INTPTR_T, parse_ctype("intptr_t"))
end
def test_uintptr_t_ctype
assert_equal(TYPE_UINTPTR_T, parse_ctype("uintptr_t"))
end
def test_undefined_ctype
assert_raise(DLError) { parse_ctype('DWORD') }
end
def test_undefined_ctype_with_type_alias
assert_equal(-TYPE_LONG, parse_ctype('DWORD', {"DWORD" => "unsigned long"}))
end
def expand_struct_types(types)
types.collect do |type|
case type
when Class
[expand_struct_types(type.types)]
when Array
[expand_struct_types([type[0]])[0][0], type[1]]
else
type
end
end
end
def test_struct_basic
assert_equal [[TYPE_INT, TYPE_CHAR], ['i', 'c']], parse_struct_signature(['int i', 'char c'])
end
def test_struct_array
assert_equal [[[TYPE_CHAR,80],[TYPE_INT,5]], ['buffer','x']], parse_struct_signature(['char buffer[80]', 'int[5] x'])
end
def test_struct_nested_struct
types, members = parse_struct_signature([
'int x',
{inner: ['int i', 'char c']},
])
assert_equal([[TYPE_INT, [[TYPE_INT, TYPE_CHAR]]],
['x', ['inner', ['i', 'c']]]],
[expand_struct_types(types),
members])
end
def test_struct_nested_defined_struct
inner = Fiddle::Importer.struct(['int i', 'char c'])
assert_equal([[TYPE_INT, inner],
['x', ['inner', ['i', 'c']]]],
parse_struct_signature([
'int x',
{inner: inner},
]))
end
def test_struct_double_nested_struct
types, members = parse_struct_signature([
'int x',
{
outer: [
'int y',
{inner: ['int i', 'char c']},
],
},
])
assert_equal([[TYPE_INT, [[TYPE_INT, [[TYPE_INT, TYPE_CHAR]]]]],
['x', ['outer', ['y', ['inner', ['i', 'c']]]]]],
[expand_struct_types(types),
members])
end
def test_struct_nested_struct_array
types, members = parse_struct_signature([
'int x',
{
'inner[2]' => [
'int i',
'char c',
],
},
])
assert_equal([[TYPE_INT, [[TYPE_INT, TYPE_CHAR], 2]],
['x', ['inner', ['i', 'c']]]],
[expand_struct_types(types),
members])
end
def test_struct_double_nested_struct_inner_array
types, members = parse_struct_signature(outer: [
'int x',
{
'inner[2]' => [
'int i',
'char c',
],
},
])
assert_equal([[[[TYPE_INT, [[TYPE_INT, TYPE_CHAR], 2]]]],
[['outer', ['x', ['inner', ['i', 'c']]]]]],
[expand_struct_types(types),
members])
end
def test_struct_double_nested_struct_outer_array
types, members = parse_struct_signature([
'int x',
{
'outer[2]' => {
inner: [
'int i',
'char c',
],
},
},
])
assert_equal([[TYPE_INT, [[[[TYPE_INT, TYPE_CHAR]]], 2]],
['x', ['outer', [['inner', ['i', 'c']]]]]],
[expand_struct_types(types),
members])
end
def test_struct_array_str
assert_equal [[[TYPE_CHAR,80],[TYPE_INT,5]], ['buffer','x']], parse_struct_signature('char buffer[80], int[5] x')
end
def test_struct_function_pointer
assert_equal [[TYPE_VOIDP], ['cb']], parse_struct_signature(['void (*cb)(const char*)'])
end
def test_struct_function_pointer_str
assert_equal [[TYPE_VOIDP,TYPE_VOIDP], ['cb', 'data']], parse_struct_signature('void (*cb)(const char*), const char* data')
end
def test_struct_string
assert_equal [[TYPE_INT,TYPE_VOIDP,TYPE_VOIDP], ['x', 'cb', 'name']], parse_struct_signature('int x; void (*cb)(); const char* name')
end
def test_struct_undefined
assert_raise(DLError) { parse_struct_signature(['int i', 'DWORD cb']) }
end
def test_struct_undefined_with_type_alias
assert_equal [[TYPE_INT,-TYPE_LONG], ['i', 'cb']], parse_struct_signature(['int i', 'DWORD cb'], {"DWORD" => "unsigned long"})
end
def test_signature_basic
func, ret, args = parse_signature('void func()')
assert_equal 'func', func
assert_equal TYPE_VOID, ret
assert_equal [], args
end
def test_signature_semi
func, ret, args = parse_signature('void func();')
assert_equal 'func', func
assert_equal TYPE_VOID, ret
assert_equal [], args
end
def test_signature_void_arg
func, ret, args = parse_signature('void func(void)')
assert_equal 'func', func
assert_equal TYPE_VOID, ret
assert_equal [], args
end
def test_signature_type_args
types = [
'char', 'unsigned char',
'short', 'unsigned short',
'int', 'unsigned int',
'long', 'unsigned long',
defined?(TYPE_LONG_LONG) && \
[
'long long', 'unsigned long long',
],
'float', 'double',
'const char*', 'void*',
].flatten.compact
func, ret, args = parse_signature("void func(#{types.join(',')})")
assert_equal 'func', func
assert_equal TYPE_VOID, ret
assert_equal [
TYPE_CHAR, -TYPE_CHAR,
TYPE_SHORT, -TYPE_SHORT,
TYPE_INT, -TYPE_INT,
TYPE_LONG, -TYPE_LONG,
defined?(TYPE_LONG_LONG) && \
[
TYPE_LONG_LONG, -TYPE_LONG_LONG,
],
TYPE_FLOAT, TYPE_DOUBLE,
TYPE_VOIDP, TYPE_VOIDP,
].flatten.compact, args
end
def test_signature_single_variable
func, ret, args = parse_signature('void func(int x)')
assert_equal 'func', func
assert_equal TYPE_VOID, ret
assert_equal [TYPE_INT], args
end
def test_signature_multiple_variables
func, ret, args = parse_signature('void func(int x, const char* s)')
assert_equal 'func', func
assert_equal TYPE_VOID, ret
assert_equal [TYPE_INT, TYPE_VOIDP], args
end
def test_signature_array_variable
func, ret, args = parse_signature('void func(int x[], int y[40])')
assert_equal 'func', func
assert_equal TYPE_VOID, ret
assert_equal [TYPE_VOIDP, TYPE_VOIDP], args
end
def test_signature_function_pointer
func, ret, args = parse_signature('int func(int (*sum)(int x, int y), int x, int y)')
assert_equal 'func', func
assert_equal TYPE_INT, ret
assert_equal [TYPE_VOIDP, TYPE_INT, TYPE_INT], args
end
def test_signature_variadic_arguments
unless Fiddle.const_defined?("TYPE_VARIADIC")
skip "libffi doesn't support variadic arguments"
end
assert_equal([
"printf",
TYPE_INT,
[TYPE_VOIDP, TYPE_VARIADIC],
],
parse_signature('int printf(const char *format, ...)'))
end
def test_signature_return_pointer
func, ret, args = parse_signature('void* malloc(size_t)')
assert_equal 'malloc', func
assert_equal TYPE_VOIDP, ret
assert_equal [TYPE_SIZE_T], args
end
def test_signature_return_array
func, ret, args = parse_signature('int (*func())[32]')
assert_equal 'func', func
assert_equal TYPE_VOIDP, ret
assert_equal [], args
end
def test_signature_return_array_with_args
func, ret, args = parse_signature('int (*func(const char* s))[]')
assert_equal 'func', func
assert_equal TYPE_VOIDP, ret
assert_equal [TYPE_VOIDP], args
end
def test_signature_return_function_pointer
func, ret, args = parse_signature('int (*func())(int x, int y)')
assert_equal 'func', func
assert_equal TYPE_VOIDP, ret
assert_equal [], args
end
def test_signature_return_function_pointer_with_args
func, ret, args = parse_signature('int (*func(int z))(int x, int y)')
assert_equal 'func', func
assert_equal TYPE_VOIDP, ret
assert_equal [TYPE_INT], args
end
end
end if defined?(Fiddle)
|