1# mode: run
2# tag: cpp, werror
3
4from cython.operator cimport dereference as d
5from cython.operator cimport preincrement as incr
6
7from libcpp.vector cimport vector
8from libcpp cimport bool as cbool
9
10def simple_test(double x):
11    """
12    >>> simple_test(55)
13    3
14    """
15    v = new vector[double]()
16    try:
17        v.push_back(1.0)
18        v.push_back(x)
19        from math import pi
20        v.push_back(pi)
21        return v.size()
22    finally:
23        del v
24
25def list_test(L):
26    """
27    >>> list_test([1,2,4,8])
28    (4, 4)
29    >>> list_test([])
30    (0, 0)
31    >>> list_test([-1] * 1000)
32    (1000, 1000)
33    """
34    v = new vector[int]()
35    try:
36        for a in L:
37            v.push_back(a)
38        return len(L), v.size()
39    finally:
40        del v
41
42def index_test(L):
43    """
44    >>> index_test([1,2,4,8])
45    (1.0, 8.0)
46    >>> index_test([1.25])
47    (1.25, 1.25)
48    """
49    v = new vector[double]()
50    try:
51        for a in L:
52            v.push_back(a)
53        return v[0][0], v[0][len(L)-1]
54    finally:
55        del v
56
57
58def index_set_test(L):
59    """
60    >>> index_set_test([1,2,4,8])
61    (-1.0, -8.0)
62    >>> index_set_test([1.25])
63    (-1.25, -1.25)
64    """
65    v = new vector[double]()
66    try:
67        for a in L:
68            v.push_back(a)
69        for i in range(v.size()):
70            d(v)[i] = -d(v)[i]
71        return d(v)[0], d(v)[v.size()-1]
72    finally:
73        del v
74
75def iteration_test(L):
76    """
77    >>> iteration_test([1,2,4,8])
78    1
79    2
80    4
81    8
82    """
83    v = new vector[int]()
84    try:
85        for a in L:
86            v.push_back(a)
87        it = v.begin()
88        while it != v.end():
89            a = d(it)
90            incr(it)
91            print(a)
92    finally:
93        del v
94
95def reverse_iteration_test(L):
96    """
97    >>> reverse_iteration_test([1,2,4,8])
98    8
99    4
100    2
101    1
102    """
103    v = new vector[int]()
104    try:
105        for a in L:
106            v.push_back(a)
107        it = v.rbegin()
108        while it != v.rend():
109            a = d(it)
110            incr(it)
111            print(a)
112    finally:
113        del v
114
115def nogil_test(L):
116    """
117    >>> nogil_test([1,2,3])
118    3
119    """
120    cdef int a
121    with nogil:
122        v = new vector[int]()
123    try:
124        for a in L:
125            with nogil:
126                v.push_back(a)
127        return v.size()
128    finally:
129        del v
130
131def item_ptr_test(L, int i, int x):
132    """
133    >>> item_ptr_test(range(10), 7, 100)
134    [0, 1, 2, 3, 4, 5, 6, 100, 8, 9]
135    """
136    cdef vector[int] v = L
137    cdef int* vi_ptr = &v[i]
138    vi_ptr[0] = x
139    return v
140
141def test_value_type(x):
142    """
143    >>> test_value_type(2)
144    2.0
145    >>> test_value_type(2.5)
146    2.5
147    """
148    cdef vector[double].value_type val = x
149    return val
150
151def test_value_type_complex(x):
152    """
153    >>> test_value_type_complex(2)
154    (2+0j)
155    """
156    cdef vector[double complex].value_type val = x
157    return val
158
159def test_bool_vector_convert(o):
160    """
161    >>> test_bool_vector_convert([True, False, None, 3])
162    [True, False, False, True]
163    """
164    cdef vector[cbool] v = o
165    return v
166
167def test_bool_vector_get_set():
168    """
169    >>> test_bool_vector_get_set()
170    """
171    cdef vector[cbool] v = range(5)
172    # Test access.
173    assert not v[0], v
174    assert v[1], v
175    assert not v.at(0), v
176    assert v.at(1), v
177    v[0] = True
178    v[1] = False
179    assert <object>v == [True, False, True, True, True]
180
181ctypedef vector[cbool] vector_bool
182ctypedef vector[int] vector_int
183
184def test_typedef_vector(L):
185    """
186    >>> test_typedef_vector([0, 1, True])
187    ([0, 1, 1, 0, 1, 1], 0, [False, True, True, False, True, True], False)
188    """
189    cdef vector_int vi = L
190    cdef vector_int vi2 = vi
191    vi.insert(vi.begin(), vi2.begin(), vi2.end())
192
193    cdef vector_bool vb = L
194    cdef vector_bool vb2 = vb
195    vb.insert(vb.begin(), vb2.begin(), vb2.end())
196
197    return vi, vi.at(0), vb, vb.at(0)
198
199
200def test_insert():
201    """
202    >>> test_insert()
203    """
204    cdef vector[int] v
205    cdef vector[int].size_type count = 5
206    cdef int value = 0
207
208    v.insert(v.end(), count, value)
209
210    assert v.size() == count
211    for element in v:
212        assert element == value, '%s != %s' % (element, count)
213
214
215#  Tests GitHub issue #1788.
216cdef cppclass MyVector[T](vector):
217    pass
218
219cdef cppclass Ints(MyVector[int]):
220    pass
221