cython-devel

view tests/run/arrayassign.pyx @ 3627:195c170ee761

Disable FlattenInListTransform
author Robert Bradshaw <robertwb@math.washington.edu>
date Sat Jul 31 14:10:52 2010 -0700 (12 hours ago)
parents 41d2107a2074
children
line source
1 # this doesn't work - it would reassign the array address!
2 #
3 #def test_literal_list():
4 # cdef int a[5]
5 # a = [1,2,3,4,5]
6 # return (a[0], a[1], a[2], a[3], a[4])
8 def test_literal_list_slice_all():
9 """
10 >>> test_literal_list_slice_all()
11 (1, 2, 3, 4, 5)
12 """
13 cdef int a[5] # = [5,4,3,2,1]
14 a[:] = [1,2,3,4,5]
15 return (a[0], a[1], a[2], a[3], a[4])
17 def test_literal_list_slice_start():
18 """
19 >>> test_literal_list_slice_start()
20 (1, 2, 3, 4, 5)
21 """
22 cdef int a[7] # = [7,6,5,4,3,2,1]
23 a[2:] = [1,2,3,4,5]
24 return (a[2], a[3], a[4], a[5], a[6])
26 def test_literal_list_slice_end():
27 """
28 >>> test_literal_list_slice_end()
29 (1, 2, 3, 4, 5)
30 """
31 cdef int a[7] # = [7,6,5,4,3,2,1]
32 a[:5] = [1,2,3,4,5]
33 return (a[0], a[1], a[2], a[3], a[4])
35 def test_literal_list_slice_start_end():
36 """
37 >>> test_literal_list_slice_start_end()
38 (1, 2, 3, 4, 5)
39 """
40 cdef int a[9] # = [9,8,7,6,5,4,3,2,1]
41 a[2:7] = [1,2,3,4,5]
42 return (a[2], a[3], a[4], a[5], a[6])
44 def test_literal_list_slice_start_param(s):
45 """
46 >>> test_literal_list_slice_start_param(4)
47 (1, 2, 3, 4, 5)
48 >>> test_literal_list_slice_start_param(3)
49 Traceback (most recent call last):
50 ValueError: Assignment to slice of wrong length, expected 5, got 6
51 >>> test_literal_list_slice_start_param(5)
52 Traceback (most recent call last):
53 ValueError: Assignment to slice of wrong length, expected 5, got 4
54 """
55 cdef int a[9] # = [9,8,7,6,5,4,3,2,1]
56 a[s:] = [1,2,3,4,5]
57 return (a[4], a[5], a[6], a[7], a[8])
58 # return a[s:]
60 def test_literal_list_slice_end_param(e):
61 """
62 >>> test_literal_list_slice_end_param(5)
63 (1, 2, 3, 4, 5)
64 >>> test_literal_list_slice_end_param(4)
65 Traceback (most recent call last):
66 ValueError: Assignment to slice of wrong length, expected 5, got 4
67 >>> test_literal_list_slice_end_param(6)
68 Traceback (most recent call last):
69 ValueError: Assignment to slice of wrong length, expected 5, got 6
70 """
71 cdef int a[9] # = [9,8,7,6,5,4,3,2,1]
72 a[:e] = [1,2,3,4,5]
73 return (a[0], a[1], a[2], a[3], a[4])
74 # return a[:e]
76 def test_literal_list_slice_start_end_param(s,e):
77 """
78 >>> test_literal_list_slice_start_end_param(2,7)
79 (1, 2, 3, 4, 5)
80 >>> test_literal_list_slice_start_end_param(3,7)
81 Traceback (most recent call last):
82 ValueError: Assignment to slice of wrong length, expected 5, got 4
83 >>> test_literal_list_slice_start_end_param(1,7)
84 Traceback (most recent call last):
85 ValueError: Assignment to slice of wrong length, expected 5, got 6
86 >>> test_literal_list_slice_start_end_param(2,6)
87 Traceback (most recent call last):
88 ValueError: Assignment to slice of wrong length, expected 5, got 4
89 >>> test_literal_list_slice_start_end_param(2,8)
90 Traceback (most recent call last):
91 ValueError: Assignment to slice of wrong length, expected 5, got 6
92 >>> test_literal_list_slice_start_end_param(3,6)
93 Traceback (most recent call last):
94 ValueError: Assignment to slice of wrong length, expected 5, got 3
95 >>> test_literal_list_slice_start_end_param(1,8)
96 Traceback (most recent call last):
97 ValueError: Assignment to slice of wrong length, expected 5, got 7
98 """
99 cdef int a[9] # = [9,8,7,6,5,4,3,2,1]
100 a[s:e] = [1,2,3,4,5]
101 return (a[2], a[3], a[4], a[5], a[6])
102 # return a[s:e]
104 def test_ptr_literal_list_slice_all():
105 """
106 >>> test_ptr_literal_list_slice_all()
107 (1, 2, 3, 4, 5)
108 """
109 cdef int *a = [6,5,4,3,2]
110 a[:] = [1,2,3,4,5]
111 return (a[0], a[1], a[2], a[3], a[4])
113 def test_ptr_literal_list_slice_start():
114 """
115 >>> test_ptr_literal_list_slice_start()
116 (1, 2, 3, 4, 5)
117 """
118 cdef int *a = [6,5,4,3,2,1]
119 a[1:] = [1,2,3,4,5]
120 return (a[1], a[2], a[3], a[4], a[5])
122 def test_ptr_literal_list_slice_end():
123 """
124 >>> test_ptr_literal_list_slice_end()
125 (1, 2, 3, 4, 5)
126 """
127 cdef int *a = [6,5,4,3,2,1]
128 a[:5] = [1,2,3,4,5]
129 return (a[0], a[1], a[2], a[3], a[4])
131 # tuples aren't supported (yet)
132 #
133 #def test_literal_tuple():
134 # cdef int a[5]
135 # a = (1,2,3,4,5)
136 # return (a[0], a[1], a[2], a[3], a[4])
138 # this would be nice to have:
139 #
140 #def test_list(list l):
141 # cdef int a[5]
142 # a[:] = l
143 # return (a[0], a[1], a[2], a[3], a[4])