cython

Форк
0
/
list.pyx 
308 строк · 7.1 Кб
1

2
cimport cython
3

4
def f(obj1, obj2, obj3, obj4, obj5):
5
    """
6
    >>> f(1, 2, 3, 4, 5)
7
    []
8
    """
9
    obj1 = []
10
    return obj1
11

12
def g(obj1, obj2, obj3, obj4, obj5):
13
    """
14
    >>> g(1, 2, 3, 4, 5)
15
    [2]
16
    """
17
    obj1 = [obj2]
18
    return obj1
19

20
def h(obj1, obj2, obj3, obj4, obj5):
21
    """
22
    >>> h(1, 2, 3, 4, 5)
23
    [2, 3]
24
    """
25
    obj1 = [obj2, obj3]
26
    return obj1
27

28
def j(obj1, obj2, obj3, obj4, obj5):
29
    """
30
    >>> j(1, 2, 3, 4, 5)
31
    [2, 3, 4]
32
    """
33
    obj1 = [obj2, obj3, obj4]
34
    return obj1
35

36
def k(obj1, obj2, obj3, obj4, obj5):
37
    """
38
    >>> k(1, 2, 3, 4, 5)
39
    [17, 42, 88]
40
    """
41
    obj1 = [17, 42, 88]
42
    return obj1
43

44
@cython.test_fail_if_path_exists("//SimpleCallNode")
45
def test_list_call(ob):
46
    """
47
    >>> def f():
48
    ...     yield 1
49
    ...     yield 2
50
    ...
51
    >>> list(f())
52
    [1, 2]
53
    """
54
    return list(ob)
55

56
def test_list_sort():
57
    """
58
    >>> test_list_sort()
59
    [1, 2, 3, 4]
60
    """
61
    cdef list l1
62
    l1 = [2,3,1,4]
63
    l1.sort()
64
    return l1
65

66
def test_list_sort_reversed():
67
    cdef list l1
68
    l1 = [2,3,1,4]
69
    l1.sort(reversed=True)
70
    return l1
71

72
def test_list_reverse():
73
    """
74
    >>> test_list_reverse()
75
    [1, 2, 3, 4]
76
    """
77
    cdef list l1
78
    l1 = [4,3,2,1]
79
    l1.reverse()
80
    return l1
81

82

83
@cython.test_assert_path_exists(
84
    '//SimpleCallNode//AttributeNode[@entry.cname = "__Pyx_PyList_Append"]',
85
)
86
def test_list_append():
87
    """
88
    >>> test_list_append()
89
    [1, 2, 3, 4]
90
    """
91
    cdef list l1 = [1,2]
92
    l1.append(3)
93
    l1.append(4)
94
    return l1
95

96

97
@cython.test_assert_path_exists(
98
    '//SimpleCallNode//NameNode[@entry.cname = "__Pyx_PyList_Append"]',
99
)
100
def test_list_append_unbound():
101
    """
102
    >>> test_list_append_unbound()
103
    [1, 2, 3, 4]
104
    """
105
    cdef list l1 = [1,2]
106
    list.append(l1, 3)
107
    list.append(l1, 4)
108
    return l1
109

110

111
@cython.test_assert_path_exists(
112
    '//SimpleCallNode//NameNode[@entry.cname = "__Pyx_PyList_Append"]',
113
)
114
def test_list_append_unbound_assigned():
115
    """
116
    >>> test_list_append_unbound_assigned()
117
    [1, 2, 3, 4]
118
    """
119
    append = list.append
120
    cdef list l1 = [1,2]
121
    append(l1, 3)
122
    append(l1, 4)
123
    return l1
124

125

126
def test_list_append_insert():
127
    """
128
    >>> test_list_append_insert()
129
    ['first', 'second']
130
    """
131
    cdef list l = []
132
    l.append("second")
133
    l.insert(0, "first")
134
    return l
135

136
def test_list_pop():
137
    """
138
    >>> test_list_pop()
139
    (2, [1])
140
    """
141
    cdef list l1
142
    l1 = [1,2]
143
    two = l1.pop()
144
    return two, l1
145

146
def test_list_pop0():
147
    """
148
    >>> test_list_pop0()
149
    (1, [2])
150
    """
151
    cdef list l1
152
    l1 = [1,2]
153
    one = l1.pop(0)
154
    return one, l1
155

156
def test_list_pop_all():
157
    """
158
    >>> test_list_pop_all()
159
    True
160
    """
161
    cdef list l1
162
    l1 = [1,2]
163
    i = 0
164
    try:
165
        l1.pop()
166
        i = 1
167
        l1.pop(-1)
168
        i = 2
169
        l1.pop(0)
170
        i = 3
171
    except IndexError:
172
        return i == 2
173
    return False
174

175

176
@cython.test_assert_path_exists(
177
    '//PythonCapiCallNode//PythonCapiFunctionNode[@cname = "__Pyx_ListComp_Append"]',
178
    '//PythonCapiCallNode//PythonCapiFunctionNode[@cname = "__Pyx_PyList_Append"]',
179
)
180
def test_list_extend(seq=None, x=4):
181
    """
182
    >>> test_list_extend()
183
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
184
    >>> test_list_extend([])
185
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
186
    >>> test_list_extend([1])
187
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1]
188
    >>> test_list_extend([1, 2])
189
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1, 2]
190
    """
191
    cdef list l = [1,2,3]
192
    l.extend([])
193
    l.extend(())
194
    l.extend(set())  # not currently optimised (not worth the trouble)
195
    assert l == [1,2,3]
196
    assert len(l) == 3
197
    l.extend([4,x+1,6])
198
    l.extend([7,8,9,10,11,12,13,14,15,16])
199
    if seq is not None:
200
        l.extend(seq)
201
    return l
202

203

204
@cython.test_assert_path_exists(
205
    '//PythonCapiCallNode//PythonCapiFunctionNode[@cname = "__Pyx_ListComp_Append"]',
206
    '//PythonCapiCallNode//PythonCapiFunctionNode[@cname = "__Pyx_PyList_Append"]',
207
)
208
def test_list_extend_unbound(seq=None, x=4):
209
    """
210
    >>> test_list_extend_unbound()
211
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
212
    >>> test_list_extend_unbound([])
213
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
214
    >>> test_list_extend_unbound([1])
215
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1]
216
    >>> test_list_extend_unbound([1, 2])
217
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1, 2]
218
    """
219
    cdef list l = [1,2,3]
220
    list.extend(l, [])
221
    list.extend(l, ())
222
    try:
223
        list.extend((), ())
224
    except TypeError:
225
        pass
226
    else:
227
        assert False, "TypeError not raised!"
228
    list.extend(l, set())  # not currently optimised (not worth the trouble)
229
    assert l == [1,2,3]
230
    assert len(l) == 3
231
    list.extend(l, [4,x+1,6])
232
    list.extend(l, [7,8,9,10,11,12,13,14,15,16])
233
    if seq is not None:
234
        list.extend(l, seq)
235
    return l
236

237
@cython.test_assert_path_exists(
238
    '//PythonCapiCallNode//PythonCapiFunctionNode[@cname = "__Pyx_ListComp_Append"]',
239
    '//PythonCapiCallNode//PythonCapiFunctionNode[@cname = "__Pyx_PyList_Append"]',
240
)
241
def test_list_extend_sideeffect(seq=None, exc=False):
242
    """
243
    >>> test_list_extend_sideeffect()
244
    ([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], [4, 6, 7, 8])
245
    >>> test_list_extend_sideeffect([])
246
    ([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], [4, 6, 7, 8])
247
    >>> test_list_extend_sideeffect([], exc=True)
248
    ([1, 2, 3, 10, 11, 12, 13, 14, 15, 16], [4, 7, 8])
249
    >>> test_list_extend_sideeffect([1])
250
    ([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1], [4, 6, 7, 8])
251
    >>> test_list_extend_sideeffect([1], exc=True)
252
    ([1, 2, 3, 10, 11, 12, 13, 14, 15, 16, 1], [4, 7, 8])
253
    >>> test_list_extend_sideeffect([1, 2])
254
    ([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1, 2], [4, 6, 7, 8])
255
    """
256
    calls = []
257
    def sideeffect(value):
258
        calls.append(value)
259
        return value
260
    def fail(value):
261
        if exc:
262
            raise TypeError("HUHU")
263
        return value
264

265
    cdef list l = [1,2,3]
266
    l.extend([])
267
    l.extend(())
268
    l.extend(set())  # not currently optimised (not worth the trouble)
269
    assert l == [1,2,3]
270
    assert len(l) == 3
271

272
    # Must first build all items, then append them in order.
273
    # If building one value fails, none of them must be appended.
274
    try:
275
        l.extend([sideeffect(4), fail(5), sideeffect(6)])
276
    except TypeError as e:
277
        assert exc
278
        assert "HUHU" in str(e)
279
    else:
280
        assert not exc
281

282
    try:
283
        l.extend([sideeffect(7), sideeffect(8), fail(9)])
284
    except TypeError as e:
285
        assert exc
286
        assert "HUHU" in str(e)
287
    else:
288
        assert not exc
289

290
    l.extend([10,11,12,13,14,15,16])
291
    if seq is not None:
292
        l.extend(seq)
293
    return l, calls
294

295

296
def test_none_list_extend(list l):
297
    """
298
    >>> test_none_list_extend([])
299
    [1, 2, 3]
300
    >>> test_none_list_extend([0, 0, 0])
301
    [0, 0, 0, 1, 2, 3]
302
    >>> test_none_list_extend(None)
303
    123
304
    """
305
    try:
306
        l.extend([1,2,3])
307
    except AttributeError:
308
        return 123
309
    return l
310

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.