1# tag: cpp
2
3cdef extern from "cpp_nested_classes_support.h":
4    cdef cppclass A:
5        cppclass B:
6            int square(int)
7            cppclass C:
8                int cube(int)
9        B* createB()
10        ctypedef int my_int
11        @staticmethod
12        my_int negate(my_int)
13
14    cdef cppclass TypedClass[T]:
15        ctypedef T MyType
16        struct MyStruct:
17            T typed_value
18            int int_value
19        union MyUnion:
20            T typed_value
21            int int_value
22        enum MyEnum:
23            value
24
25    cdef cppclass SpecializedTypedClass(TypedClass[double]):
26        pass
27
28
29def test_nested_classes():
30    """
31    >>> test_nested_classes()
32    """
33    cdef A a
34    cdef A.B b
35    assert b.square(3) == 9
36    cdef A.B.C c
37    assert c.cube(3) == 27
38
39    cdef A.B *b_ptr = a.createB()
40    assert b_ptr.square(4) == 16
41    del b_ptr
42
43def test_nested_typedef(py_x):
44    """
45    >>> test_nested_typedef(5)
46    """
47    cdef A.my_int x = py_x
48    assert A.negate(x) == -py_x
49
50def test_typed_nested_typedef(x):
51    """
52    >>> test_typed_nested_typedef(4)
53    (4, 4.0)
54    """
55    cdef TypedClass[int].MyType ix = x
56    cdef TypedClass[double].MyType dx = x
57    return ix, dx
58
59def test_nested_enum(TypedClass[double].MyEnum x):
60    """
61    >>> test_nested_enum(4)
62    False
63    """
64    return x == 0
65
66def test_nested_union(x):
67    """
68    >>> test_nested_union(2)
69    2.0
70    """
71    cdef TypedClass[double].MyUnion u
72    u.int_value = x
73    assert u.int_value == x
74    u.typed_value = x
75    return u.typed_value
76
77def test_nested_struct(x):
78    """
79    >>> test_nested_struct(2)
80    2.0
81    """
82    cdef TypedClass[double].MyStruct s
83    s.int_value = x
84    assert s.int_value == x
85    s.typed_value = x
86    return s.typed_value
87
88
89
90def test_typed_nested_sub_typedef(x):
91    """
92    >>> test_typed_nested_sub_typedef(4)
93    4.0
94    """
95    cdef SpecializedTypedClass.MyType dx = x
96    return dx
97
98def test_nested_sub_enum(SpecializedTypedClass.MyEnum x):
99    """
100    >>> test_nested_sub_enum(4)
101    False
102    """
103    return x == 0
104
105def test_nested_sub_union(x):
106    """
107    >>> test_nested_sub_union(2)
108    2.0
109    """
110    cdef SpecializedTypedClass.MyUnion u
111    u.int_value = x
112    assert u.int_value == x
113    u.typed_value = x
114    return u.typed_value
115
116def test_nested_sub_struct(x):
117    """
118    >>> test_nested_sub_struct(2)
119    2.0
120    """
121    cdef SpecializedTypedClass.MyStruct s
122    s.int_value = x
123    assert s.int_value == x
124    s.typed_value = x
125    return s.typed_value
126