• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1from cffi.model import *
2
3
4def test_void_type():
5    assert void_type.get_c_name() == "void"
6    assert void_type.get_c_name("foo") == "void foo"
7    assert void_type.get_c_name("*foo") == "void *foo"
8
9def test_primitive_type():
10    int_type = PrimitiveType("int")
11    assert int_type.get_c_name() == "int"
12    assert int_type.get_c_name("foo") == "int foo"
13    assert int_type.get_c_name("*foo") == "int *foo"
14    assert int_type.get_c_name("[5]") == "int[5]"
15
16def test_raw_function_type():
17    int_type = PrimitiveType("int")
18    fn_type = RawFunctionType([], int_type, False)
19    assert fn_type.get_c_name() == "int()(void)"
20    assert fn_type.get_c_name("*") == "int( *)(void)"
21    assert fn_type.get_c_name("*foo") == "int( *foo)(void)"
22    fn_type = RawFunctionType([int_type], int_type, False)
23    assert fn_type.get_c_name() == "int()(int)"
24    fn_type = RawFunctionType([int_type] * 2, int_type, False)
25    assert fn_type.get_c_name() == "int()(int, int)"
26    #
27    fn_type = RawFunctionType([int_type], int_type, True)
28    assert fn_type.get_c_name() == "int()(int, ...)"
29    assert fn_type.get_c_name("*foo") == "int( *foo)(int, ...)"
30    #
31    res_type = FunctionPtrType([int_type], int_type, True)
32    fn_type = RawFunctionType([int_type], res_type, True)
33    assert fn_type.get_c_name("x") == "int(*( x)(int, ...))(int, ...)"
34
35def test_function_ptr_type():
36    int_type = PrimitiveType("int")
37    fn_type = FunctionPtrType([], int_type, False)
38    assert fn_type.get_c_name() == "int(*)(void)"
39    assert fn_type.get_c_name("*") == "int(* *)(void)"
40    assert fn_type.get_c_name("*foo") == "int(* *foo)(void)"
41    fn_type = FunctionPtrType([int_type], int_type, False)
42    assert fn_type.get_c_name() == "int(*)(int)"
43    fn_type = FunctionPtrType([int_type] * 2, int_type, False)
44    assert fn_type.get_c_name() == "int(*)(int, int)"
45    #
46    fn_type = FunctionPtrType([int_type], int_type, True)
47    assert fn_type.get_c_name() == "int(*)(int, ...)"
48
49def test_pointer_type():
50    ptr_type = PointerType(PrimitiveType("int"))
51    assert ptr_type.get_c_name("x") == "int * x"
52
53def test_const_pointer_type():
54    ptr_type = ConstPointerType(PrimitiveType("int"))
55    assert ptr_type.get_c_name("x") == "int const * x"
56    ptr_type = ConstPointerType(ArrayType(PrimitiveType("int"), 5))
57    assert ptr_type.get_c_name("") == "int(const *)[5]"
58    assert ptr_type.get_c_name("*x") == "int(const * *x)[5]"
59
60def test_qual_pointer_type():
61    ptr_type = PointerType(PrimitiveType("long long"), Q_RESTRICT)
62    assert ptr_type.get_c_name("") == "long long __restrict *"
63    assert const_voidp_type.get_c_name("") == "void const *"
64
65def test_unknown_pointer_type():
66    ptr_type = unknown_ptr_type("foo_p")
67    assert ptr_type.get_c_name("") == "foo_p"
68    assert ptr_type.get_c_name("x") == "foo_p x"
69
70def test_unknown_type():
71    u_type = unknown_type("foo_t")
72    assert u_type.get_c_name("") == "foo_t"
73    assert u_type.get_c_name("x") == "foo_t x"
74
75def test_array_type():
76    a_type = ArrayType(PrimitiveType("int"), None)
77    assert a_type.get_c_name("") == "int[]"
78    assert a_type.get_c_name("x") == "int x[]"
79    assert a_type.get_c_name("*x") == "int(*x)[]"
80    assert a_type.get_c_name(" *x") == "int(*x)[]"
81    assert a_type.get_c_name("[5]") == "int[5][]"
82    a_type = ArrayType(unknown_type("foo_t"), 5)
83    assert a_type.get_c_name("") == "foo_t[5]"
84    assert a_type.get_c_name("x") == "foo_t x[5]"
85    assert a_type.get_c_name("*x") == "foo_t(*x)[5]"
86    a_type = ArrayType(unknown_ptr_type("foo_p"), None)
87    assert a_type.get_c_name("") == "foo_p[]"
88    assert a_type.get_c_name("x") == "foo_p x[]"
89    assert a_type.get_c_name("*x") == "foo_p(*x)[]"
90    a_type = ArrayType(ConstPointerType(PrimitiveType("int")), None)
91    assert a_type.get_c_name("") == "int const *[]"
92    assert a_type.get_c_name("x") == "int const * x[]"
93    assert a_type.get_c_name("*x") == "int const *(*x)[]"
94    fn_type = FunctionPtrType([], PrimitiveType("int"), False)
95    a_type = ArrayType(fn_type, 5)
96    assert a_type.get_c_name("") == "int(*[5])(void)"
97    assert a_type.get_c_name("x") == "int(* x[5])(void)"
98    assert a_type.get_c_name("*x") == "int(*(*x)[5])(void)"
99
100def test_struct_type():
101    struct_type = StructType("foo_s", None, None, None)
102    assert struct_type.get_c_name() == "struct foo_s"
103    assert struct_type.get_c_name("*x") == "struct foo_s *x"
104
105def test_union_type():
106    union_type = UnionType("foo_s", None, None, None)
107    assert union_type.get_c_name() == "union foo_s"
108
109def test_enum_type():
110    enum_type = EnumType("foo_e", [], [])
111    assert enum_type.get_c_name() == "enum foo_e"
112