2017-09-12 07:52:23 -04:00
|
|
|
# frozen_string_literal: true
|
2013-05-25 10:29:02 -04:00
|
|
|
begin
|
|
|
|
require_relative 'helper'
|
|
|
|
require 'fiddle/cparser'
|
2020-12-10 19:41:12 -05:00
|
|
|
require 'fiddle/import'
|
2013-05-25 10:29:02 -04:00
|
|
|
rescue LoadError
|
|
|
|
end
|
2012-11-27 19:02:49 -05:00
|
|
|
|
|
|
|
module Fiddle
|
|
|
|
class TestCParser < TestCase
|
|
|
|
include CParser
|
|
|
|
|
2015-01-02 19:54:37 -05:00
|
|
|
def test_char_ctype
|
|
|
|
assert_equal(TYPE_CHAR, parse_ctype('char'))
|
2021-02-17 02:54:35 -05:00
|
|
|
assert_equal(TYPE_CHAR, parse_ctype('const char'))
|
2015-01-02 19:54:37 -05:00
|
|
|
assert_equal(TYPE_CHAR, parse_ctype('signed char'))
|
2021-02-17 02:54:35 -05:00
|
|
|
assert_equal(TYPE_CHAR, parse_ctype('const signed char'))
|
2015-01-02 19:54:37 -05:00
|
|
|
assert_equal(-TYPE_CHAR, parse_ctype('unsigned char'))
|
2021-02-17 02:54:35 -05:00
|
|
|
assert_equal(-TYPE_CHAR, parse_ctype('const unsigned char'))
|
2015-01-02 19:54:37 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_short_ctype
|
|
|
|
assert_equal(TYPE_SHORT, parse_ctype('short'))
|
2021-02-17 02:54:35 -05:00
|
|
|
assert_equal(TYPE_SHORT, parse_ctype('const short'))
|
2015-01-02 19:54:37 -05:00
|
|
|
assert_equal(TYPE_SHORT, parse_ctype('short int'))
|
2021-02-17 02:54:35 -05:00
|
|
|
assert_equal(TYPE_SHORT, parse_ctype('const short int'))
|
2015-01-02 19:54:37 -05:00
|
|
|
assert_equal(TYPE_SHORT, parse_ctype('signed short'))
|
2021-02-17 02:54:35 -05:00
|
|
|
assert_equal(TYPE_SHORT, parse_ctype('const signed short'))
|
2015-01-02 19:54:37 -05:00
|
|
|
assert_equal(TYPE_SHORT, parse_ctype('signed short int'))
|
2021-02-17 02:54:35 -05:00
|
|
|
assert_equal(TYPE_SHORT, parse_ctype('const signed short int'))
|
2015-01-02 19:54:37 -05:00
|
|
|
assert_equal(-TYPE_SHORT, parse_ctype('unsigned short'))
|
2021-02-17 02:54:35 -05:00
|
|
|
assert_equal(-TYPE_SHORT, parse_ctype('const unsigned short'))
|
2015-01-02 19:54:37 -05:00
|
|
|
assert_equal(-TYPE_SHORT, parse_ctype('unsigned short int'))
|
2021-02-17 02:54:35 -05:00
|
|
|
assert_equal(-TYPE_SHORT, parse_ctype('const unsigned short int'))
|
2015-01-02 19:54:37 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_int_ctype
|
|
|
|
assert_equal(TYPE_INT, parse_ctype('int'))
|
2021-02-17 02:54:35 -05:00
|
|
|
assert_equal(TYPE_INT, parse_ctype('const int'))
|
2015-01-02 19:54:37 -05:00
|
|
|
assert_equal(TYPE_INT, parse_ctype('signed int'))
|
2021-02-17 02:54:35 -05:00
|
|
|
assert_equal(TYPE_INT, parse_ctype('const signed int'))
|
2012-11-27 19:02:49 -05:00
|
|
|
assert_equal(-TYPE_INT, parse_ctype('uint'))
|
2021-02-17 02:54:35 -05:00
|
|
|
assert_equal(-TYPE_INT, parse_ctype('const uint'))
|
2015-01-02 19:54:37 -05:00
|
|
|
assert_equal(-TYPE_INT, parse_ctype('unsigned int'))
|
2021-02-17 02:54:35 -05:00
|
|
|
assert_equal(-TYPE_INT, parse_ctype('const unsigned int'))
|
2015-01-02 19:54:37 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_long_ctype
|
|
|
|
assert_equal(TYPE_LONG, parse_ctype('long'))
|
2021-02-17 02:54:35 -05:00
|
|
|
assert_equal(TYPE_LONG, parse_ctype('const long'))
|
2015-01-02 19:54:37 -05:00
|
|
|
assert_equal(TYPE_LONG, parse_ctype('long int'))
|
2021-02-17 02:54:35 -05:00
|
|
|
assert_equal(TYPE_LONG, parse_ctype('const long int'))
|
2015-01-02 19:54:37 -05:00
|
|
|
assert_equal(TYPE_LONG, parse_ctype('signed long'))
|
2021-02-17 02:54:35 -05:00
|
|
|
assert_equal(TYPE_LONG, parse_ctype('const signed long'))
|
2015-01-02 19:54:37 -05:00
|
|
|
assert_equal(TYPE_LONG, parse_ctype('signed long int'))
|
2021-02-17 02:54:35 -05:00
|
|
|
assert_equal(TYPE_LONG, parse_ctype('const signed long int'))
|
2015-01-02 19:54:37 -05:00
|
|
|
assert_equal(-TYPE_LONG, parse_ctype('unsigned long'))
|
2021-02-17 02:54:35 -05:00
|
|
|
assert_equal(-TYPE_LONG, parse_ctype('const unsigned long'))
|
2015-01-02 19:54:37 -05:00
|
|
|
assert_equal(-TYPE_LONG, parse_ctype('unsigned long int'))
|
2021-02-17 02:54:35 -05:00
|
|
|
assert_equal(-TYPE_LONG, parse_ctype('const unsigned long int'))
|
2012-11-27 19:02:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_size_t_ctype
|
|
|
|
assert_equal(TYPE_SIZE_T, parse_ctype("size_t"))
|
2021-02-17 02:54:35 -05:00
|
|
|
assert_equal(TYPE_SIZE_T, parse_ctype("const size_t"))
|
2012-11-27 19:02:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_ssize_t_ctype
|
|
|
|
assert_equal(TYPE_SSIZE_T, parse_ctype("ssize_t"))
|
2021-02-17 02:54:35 -05:00
|
|
|
assert_equal(TYPE_SSIZE_T, parse_ctype("const ssize_t"))
|
2012-11-27 19:02:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_ptrdiff_t_ctype
|
|
|
|
assert_equal(TYPE_PTRDIFF_T, parse_ctype("ptrdiff_t"))
|
2021-02-17 02:54:35 -05:00
|
|
|
assert_equal(TYPE_PTRDIFF_T, parse_ctype("const ptrdiff_t"))
|
2012-11-27 19:02:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_intptr_t_ctype
|
|
|
|
assert_equal(TYPE_INTPTR_T, parse_ctype("intptr_t"))
|
2021-02-17 02:54:35 -05:00
|
|
|
assert_equal(TYPE_INTPTR_T, parse_ctype("const intptr_t"))
|
2012-11-27 19:02:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_uintptr_t_ctype
|
|
|
|
assert_equal(TYPE_UINTPTR_T, parse_ctype("uintptr_t"))
|
2021-02-17 02:54:35 -05:00
|
|
|
assert_equal(TYPE_UINTPTR_T, parse_ctype("const uintptr_t"))
|
2012-11-27 19:02:49 -05:00
|
|
|
end
|
2015-01-02 19:54:37 -05:00
|
|
|
|
2015-01-03 04:23:43 -05:00
|
|
|
def test_undefined_ctype
|
2017-07-13 05:46:16 -04:00
|
|
|
assert_raise(DLError) { parse_ctype('DWORD') }
|
2015-01-03 04:23:43 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_undefined_ctype_with_type_alias
|
2021-02-17 02:54:35 -05:00
|
|
|
assert_equal(-TYPE_LONG,
|
|
|
|
parse_ctype('DWORD', {"DWORD" => "unsigned long"}))
|
|
|
|
assert_equal(-TYPE_LONG,
|
|
|
|
parse_ctype('const DWORD', {"DWORD" => "unsigned long"}))
|
2015-01-03 04:23:43 -05:00
|
|
|
end
|
|
|
|
|
2020-12-10 19:41:12 -05:00
|
|
|
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
|
|
|
|
|
2015-01-02 19:54:37 -05:00
|
|
|
def test_struct_basic
|
2021-02-17 02:54:35 -05:00
|
|
|
assert_equal([[TYPE_INT, TYPE_CHAR], ['i', 'c']],
|
|
|
|
parse_struct_signature(['int i', 'char c']))
|
|
|
|
assert_equal([[TYPE_INT, TYPE_CHAR], ['i', 'c']],
|
|
|
|
parse_struct_signature(['const int i', 'const char c']))
|
2015-01-02 19:54:37 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_struct_array
|
2021-02-17 02:54:35 -05:00
|
|
|
assert_equal([[[TYPE_CHAR, 80], [TYPE_INT, 5]],
|
|
|
|
['buffer', 'x']],
|
|
|
|
parse_struct_signature(['char buffer[80]',
|
|
|
|
'int[5] x']))
|
|
|
|
assert_equal([[[TYPE_CHAR, 80], [TYPE_INT, 5]],
|
|
|
|
['buffer', 'x']],
|
|
|
|
parse_struct_signature(['const char buffer[80]',
|
|
|
|
'const int[5] x']))
|
2015-01-02 19:54:37 -05:00
|
|
|
end
|
|
|
|
|
2020-12-10 19:41:12 -05:00
|
|
|
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
|
|
|
|
|
2015-01-02 19:54:37 -05:00
|
|
|
def test_struct_array_str
|
2021-02-17 02:54:35 -05:00
|
|
|
assert_equal([[[TYPE_CHAR, 80], [TYPE_INT, 5]],
|
|
|
|
['buffer', 'x']],
|
|
|
|
parse_struct_signature('char buffer[80], int[5] x'))
|
|
|
|
assert_equal([[[TYPE_CHAR, 80], [TYPE_INT, 5]],
|
|
|
|
['buffer', 'x']],
|
|
|
|
parse_struct_signature('const char buffer[80], const int[5] x'))
|
2015-01-02 19:54:37 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_struct_function_pointer
|
2021-02-17 02:54:35 -05:00
|
|
|
assert_equal([[TYPE_VOIDP], ['cb']],
|
|
|
|
parse_struct_signature(['void (*cb)(const char*)']))
|
2015-01-02 19:54:37 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_struct_function_pointer_str
|
2021-02-17 02:54:35 -05:00
|
|
|
assert_equal([[TYPE_VOIDP, TYPE_VOIDP], ['cb', 'data']],
|
|
|
|
parse_struct_signature('void (*cb)(const char*), const char* data'))
|
2015-01-02 19:54:37 -05:00
|
|
|
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
|
|
|
|
|
2015-01-03 04:23:43 -05:00
|
|
|
def test_struct_undefined
|
2017-07-13 05:46:16 -04:00
|
|
|
assert_raise(DLError) { parse_struct_signature(['int i', 'DWORD cb']) }
|
2015-01-03 04:23:43 -05:00
|
|
|
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
|
|
|
|
|
2015-01-02 19:54:37 -05:00
|
|
|
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',
|
2018-02-17 04:51:23 -05:00
|
|
|
defined?(TYPE_LONG_LONG) && \
|
|
|
|
[
|
2015-01-02 19:54:37 -05:00
|
|
|
'long long', 'unsigned long long',
|
2018-02-17 04:51:23 -05:00
|
|
|
],
|
2015-01-02 19:54:37 -05:00
|
|
|
'float', 'double',
|
|
|
|
'const char*', 'void*',
|
2018-02-17 04:51:23 -05:00
|
|
|
].flatten.compact
|
2015-01-02 19:54:37 -05:00
|
|
|
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,
|
2018-02-17 04:51:23 -05:00
|
|
|
defined?(TYPE_LONG_LONG) && \
|
|
|
|
[
|
2015-01-02 19:54:37 -05:00
|
|
|
TYPE_LONG_LONG, -TYPE_LONG_LONG,
|
2018-02-17 04:51:23 -05:00
|
|
|
],
|
2015-01-02 19:54:37 -05:00
|
|
|
TYPE_FLOAT, TYPE_DOUBLE,
|
|
|
|
TYPE_VOIDP, TYPE_VOIDP,
|
2018-02-17 04:51:23 -05:00
|
|
|
].flatten.compact, args
|
2015-01-02 19:54:37 -05:00
|
|
|
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
|
|
|
|
|
2020-12-10 19:41:12 -05:00
|
|
|
def test_signature_variadic_arguments
|
|
|
|
unless Fiddle.const_defined?("TYPE_VARIADIC")
|
2021-09-04 15:54:25 -04:00
|
|
|
omit "libffi doesn't support variadic arguments"
|
2020-12-10 19:41:12 -05:00
|
|
|
end
|
|
|
|
assert_equal([
|
|
|
|
"printf",
|
|
|
|
TYPE_INT,
|
|
|
|
[TYPE_VOIDP, TYPE_VARIADIC],
|
|
|
|
],
|
|
|
|
parse_signature('int printf(const char *format, ...)'))
|
|
|
|
end
|
|
|
|
|
2015-01-02 19:54:37 -05:00
|
|
|
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
|
2012-11-27 19:02:49 -05:00
|
|
|
end
|
2013-05-25 10:29:02 -04:00
|
|
|
end if defined?(Fiddle)
|