Cython has moved to github.

cython-devel

view tests/run/carray_slicing.pyx @ 2953:4b2e6c18fe38

enable for-in iteration also for C arrays of known size
author Stefan Behnel <scoder@users.berlios.de>
date Thu Feb 11 20:42:35 2010 +0100 (2 years ago)
parents 953b9e2b3a9a
children 9b979c860ef9
line source
2 cimport cython
4 ############################################################
5 # tests for char* slicing
7 cdef char* cstring = "abcABCqtp"
9 def slice_charptr_end():
10 """
11 >>> print(str(slice_charptr_end()).replace("b'", "'"))
12 ('a', 'abc', 'abcABCqtp')
13 """
14 return cstring[:1], cstring[:3], cstring[:9]
16 @cython.test_assert_path_exists("//ForFromStatNode",
17 "//ForFromStatNode//SliceIndexNode")
18 @cython.test_fail_if_path_exists("//ForInStatNode")
19 def slice_charptr_for_loop_py():
20 """
21 >>> slice_charptr_for_loop_py()
22 ['a', 'b', 'c']
23 ['b', 'c', 'A', 'B']
24 ['B', 'C', 'q', 't', 'p']
25 """
26 print str([ c for c in cstring[:3] ]).replace(" b'", " '").replace("[b'", "['")
27 print str([ c for c in cstring[1:5] ]).replace(" b'", " '").replace("[b'", "['")
28 print str([ c for c in cstring[4:9] ]).replace(" b'", " '").replace("[b'", "['")
30 @cython.test_assert_path_exists("//ForFromStatNode",
31 "//ForFromStatNode//IndexNode")
32 @cython.test_fail_if_path_exists("//ForInStatNode")
33 def slice_charptr_for_loop_c():
34 """
35 >>> slice_charptr_for_loop_c()
36 ['a', 'b', 'c']
37 ['b', 'c', 'A', 'B']
38 ['B', 'C', 'q', 't', 'p']
39 """
40 cdef char c
41 print [ chr(c) for c in cstring[:3] ]
42 print [ chr(c) for c in cstring[1:5] ]
43 print [ chr(c) for c in cstring[4:9] ]
45 ## @cython.test_assert_path_exists("//ForFromStatNode",
46 ## "//ForFromStatNode//IndexNode")
47 ## @cython.test_fail_if_path_exists("//ForInStatNode")
48 ## def slice_charptr_for_loop_c_step():
49 ## """
50 ## >>> slice_charptr_for_loop_c()
51 ## ['c', 'b', 'a']
52 ## ['b', 'c', 'A', 'B']
53 ## ['p', 't', 'q', 'C', 'B']
54 ## """
55 ## cdef char c
56 ## print [ chr(c) for c in cstring[:3:-1] ]
57 ## print [ chr(c) for c in cstring[1:5:2] ]
58 ## print [ chr(c) for c in cstring[4:9:-1] ]
60 @cython.test_assert_path_exists("//ForFromStatNode",
61 "//ForFromStatNode//IndexNode")
62 @cython.test_fail_if_path_exists("//ForInStatNode")
63 def slice_charptr_for_loop_c_dynamic_bounds():
64 """
65 >>> slice_charptr_for_loop_c_dynamic_bounds()
66 ['a', 'b', 'c']
67 ['b', 'c', 'A', 'B']
68 ['B', 'C', 'q', 't', 'p']
69 """
70 cdef char c
71 print [ chr(c) for c in cstring[0:return3()] ]
72 print [ chr(c) for c in cstring[return1():return5()] ]
73 print [ chr(c) for c in cstring[return4():return9()] ]
75 cdef return1(): return 1
76 cdef return3(): return 3
77 cdef return4(): return 4
78 cdef return5(): return 5
79 cdef return9(): return 9
81 @cython.test_assert_path_exists("//ForFromStatNode",
82 "//ForFromStatNode//SliceIndexNode")
83 @cython.test_fail_if_path_exists("//ForInStatNode")
84 def slice_charptr_for_loop_py_enumerate():
85 """
86 >>> slice_charptr_for_loop_py_enumerate()
87 [(0, 'a'), (1, 'b'), (2, 'c')]
88 [(0, 'b'), (1, 'c'), (2, 'A'), (3, 'B')]
89 [(0, 'B'), (1, 'C'), (2, 'q'), (3, 't'), (4, 'p')]
90 """
91 print str([ (i,c) for i,c in enumerate(cstring[:3]) ]).replace(" b'", " '")
92 print str([ (i,c) for i,c in enumerate(cstring[1:5]) ]).replace(" b'", " '")
93 print str([ (i,c) for i,c in enumerate(cstring[4:9]) ]).replace(" b'", " '")
95 @cython.test_assert_path_exists("//ForFromStatNode",
96 "//ForFromStatNode//IndexNode")
97 @cython.test_fail_if_path_exists("//ForInStatNode")
98 def slice_charptr_for_loop_c_enumerate():
99 """
100 >>> slice_charptr_for_loop_c_enumerate()
101 [(0, 97), (1, 98), (2, 99)]
102 [(0, 98), (1, 99), (2, 65), (3, 66)]
103 [(0, 66), (1, 67), (2, 113), (3, 116), (4, 112)]
104 """
105 cdef int c,i
106 print [ (i,c) for i,c in enumerate(cstring[:3]) ]
107 print [ (i,c) for i,c in enumerate(cstring[1:5]) ]
108 print [ (i,c) for i,c in enumerate(cstring[4:9]) ]
111 ############################################################
112 # tests for int* slicing
114 cdef int cints[6]
115 for i in range(6):
116 cints[i] = i
118 @cython.test_assert_path_exists("//ForFromStatNode",
119 "//ForFromStatNode//IndexNode")
120 @cython.test_fail_if_path_exists("//ForInStatNode")
121 def slice_intptr_for_loop_c():
122 """
123 >>> slice_intptr_for_loop_c()
124 [0, 1, 2]
125 [1, 2, 3, 4]
126 [4, 5]
127 """
128 cdef int i
129 print [ i for i in cints[:3] ]
130 print [ i for i in cints[1:5] ]
131 print [ i for i in cints[4:6] ]
133 @cython.test_assert_path_exists("//ForFromStatNode",
134 "//ForFromStatNode//IndexNode")
135 @cython.test_fail_if_path_exists("//ForInStatNode")
136 def iter_intptr_for_loop_c():
137 """
138 >>> iter_intptr_for_loop_c()
139 [0, 1, 2, 3, 4, 5]
140 """
141 cdef int i
142 print [ i for i in cints ]