summaryrefslogtreecommitdiff
path: root/test/fiddle/test_cparser.rb
diff options
context:
space:
mode:
Diffstat (limited to 'test/fiddle/test_cparser.rb')
-rw-r--r--test/fiddle/test_cparser.rb113
1 files changed, 113 insertions, 0 deletions
diff --git a/test/fiddle/test_cparser.rb b/test/fiddle/test_cparser.rb
index 5d9ac3c815..ef8cec5daa 100644
--- a/test/fiddle/test_cparser.rb
+++ b/test/fiddle/test_cparser.rb
@@ -2,6 +2,7 @@
begin
require_relative 'helper'
require 'fiddle/cparser'
+ require 'fiddle/import'
rescue LoadError
end
@@ -68,6 +69,19 @@ module Fiddle
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
@@ -76,6 +90,93 @@ module Fiddle
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
@@ -179,6 +280,18 @@ module Fiddle
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