1# mode: run
2# tag: cpp, werror
3
4from cython.operator cimport dereference as deref
5from cython.operator cimport preincrement as incr
6
7from libcpp.list cimport list as cpp_list
8from libcpp cimport bool as cbool
9
10
11def simple_test(double x):
12    """
13    >>> simple_test(55)
14    3
15    """
16    l = new cpp_list[double]()
17    try:
18        l.push_back(1.0)
19        l.push_back(x)
20        from math import pi
21        l.push_back(pi)
22        return l.size()
23    finally:
24        del l
25
26def pylist_test(L):
27    """
28    >>> pylist_test([1,2,4,8])
29    (4, 4)
30    >>> pylist_test([])
31    (0, 0)
32    >>> pylist_test([-1] * 1000)
33    (1000, 1000)
34    """
35    l = new cpp_list[int]()
36    try:
37        for a in L:
38            l.push_back(a)
39        return len(L), l.size()
40    finally:
41        del l
42
43
44def iteration_test(L):
45    """
46    >>> iteration_test([1,2,4,8])
47    1
48    2
49    4
50    8
51    """
52    l = new cpp_list[int]()
53    try:
54        for a in L:
55            l.push_back(a)
56        it = l.begin()
57        while it != l.end():
58            a = deref(it)
59            incr(it)
60            print(a)
61    finally:
62        del l
63
64def reverse_iteration_test(L):
65    """
66    >>> reverse_iteration_test([1,2,4,8])
67    8
68    4
69    2
70    1
71    """
72    l = new cpp_list[int]()
73    try:
74        for a in L:
75            l.push_back(a)
76        it = l.rbegin()
77        while it != l.rend():
78            a = deref(it)
79            incr(it)
80            print(a)
81    finally:
82        del l
83
84def nogil_test(L):
85    """
86    >>> nogil_test([1,2,3])
87    3
88    """
89    cdef int a
90    with nogil:
91        l = new cpp_list[int]()
92    try:
93        for a in L:
94            with nogil:
95                l.push_back(a)
96        return l.size()
97    finally:
98        del l
99
100
101cdef list to_pylist(cpp_list[int]& l):
102    cdef list L = []
103    it = l.begin()
104    while it != l.end():
105        L.append(deref(it))
106        incr(it)
107    return L
108
109
110def item_ptr_test(L, int x):
111    """
112    >>> item_ptr_test(range(10), 100)
113    [100, 1, 2, 3, 4, 5, 6, 7, 8, 9]
114    """
115    cdef cpp_list[int] l = L
116    cdef int* li_ptr = &l.front()
117    li_ptr[0] = x
118    return to_pylist(l)
119
120def test_value_type(x):
121    """
122    >>> test_value_type(2)
123    2.0
124    >>> test_value_type(2.5)
125    2.5
126    """
127    cdef cpp_list[double].value_type val = x
128    return val
129
130def test_value_type_complex(x):
131    """
132    >>> test_value_type_complex(2)
133    (2+0j)
134    """
135    cdef cpp_list[double complex].value_type val = x
136    return val
137
138def test_insert():
139    """
140    >>> test_insert()
141    """
142    cdef cpp_list[int] l
143    cdef cpp_list[int].size_type count = 5
144    cdef int value = 0
145
146    l.insert(l.end(), count, value)
147
148    assert l.size() == count
149    for element in l:
150        assert element == value, '%s != %s' % (element, count)
151
152
153#  Tests GitHub issue #1788.
154cdef cppclass MyList[T](cpp_list):
155    pass
156
157cdef cppclass Ints(MyList[int]):
158    pass
159