1# mode: run 2 3cimport cython 4 5def test_literal_list(): 6 """ 7 >>> test_literal_list() 8 (1, 2, 3, 4, 5) 9 """ 10 cdef int a[5] 11 a = [1,2,3,4,5] 12 return (a[0], a[1], a[2], a[3], a[4]) 13 14def test_literal_list_multiplied(): 15 """ 16 >>> test_literal_list_multiplied() 17 (1, 2, 1, 2, 1, 2) 18 """ 19 cdef int a[6] 20 a = [1,2] * 3 21 return (a[0], a[1], a[2], a[3], a[4], a[5]) 22 23def test_literal_list_slice_all(): 24 """ 25 >>> test_literal_list_slice_all() 26 (1, 2, 3, 4, 5) 27 """ 28 cdef int a[5] # = [5,4,3,2,1] 29 a[:] = [1,2,3,4,5] 30 return (a[0], a[1], a[2], a[3], a[4]) 31 32def test_literal_list_slice_start(): 33 """ 34 >>> test_literal_list_slice_start() 35 (1, 2, 3, 4, 5) 36 """ 37 cdef int a[7] # = [7,6,5,4,3,2,1] 38 a[2:] = [1,2,3,4,5] 39 return (a[2], a[3], a[4], a[5], a[6]) 40 41def test_literal_list_slice_end(): 42 """ 43 >>> test_literal_list_slice_end() 44 (1, 2, 3, 4, 5) 45 """ 46 cdef int a[7] # = [7,6,5,4,3,2,1] 47 a[:5] = [1,2,3,4,5] 48 return (a[0], a[1], a[2], a[3], a[4]) 49 50def test_literal_list_slice_start_end(): 51 """ 52 >>> test_literal_list_slice_start_end() 53 (1, 2, 3, 4, 5) 54 """ 55 cdef int a[9] # = [9,8,7,6,5,4,3,2,1] 56 a[2:7] = [1,2,3,4,5] 57 return (a[2], a[3], a[4], a[5], a[6]) 58 59def test_literal_list_slice_start_param(s): 60 """ 61 >>> test_literal_list_slice_start_param(4) 62 (1, 2, 3, 4, 5) 63 >>> test_literal_list_slice_start_param(3) 64 Traceback (most recent call last): 65 ValueError: Assignment to slice of wrong length, expected 5, got 6 66 >>> test_literal_list_slice_start_param(5) 67 Traceback (most recent call last): 68 ValueError: Assignment to slice of wrong length, expected 5, got 4 69 """ 70 cdef int a[9] # = [9,8,7,6,5,4,3,2,1] 71 a[s:] = [1,2,3,4,5] 72 return (a[4], a[5], a[6], a[7], a[8]) 73# return a[s:] 74 75def test_literal_list_slice_end_param(e): 76 """ 77 >>> test_literal_list_slice_end_param(5) 78 (1, 2, 3, 4, 5) 79 >>> test_literal_list_slice_end_param(4) 80 Traceback (most recent call last): 81 ValueError: Assignment to slice of wrong length, expected 5, got 4 82 >>> test_literal_list_slice_end_param(6) 83 Traceback (most recent call last): 84 ValueError: Assignment to slice of wrong length, expected 5, got 6 85 """ 86 cdef int a[9] # = [9,8,7,6,5,4,3,2,1] 87 a[:e] = [1,2,3,4,5] 88 return (a[0], a[1], a[2], a[3], a[4]) 89# return a[:e] 90 91def test_literal_list_slice_start_end_param(s,e): 92 """ 93 >>> test_literal_list_slice_start_end_param(2,7) 94 (1, 2, 3, 4, 5) 95 >>> test_literal_list_slice_start_end_param(3,7) 96 Traceback (most recent call last): 97 ValueError: Assignment to slice of wrong length, expected 5, got 4 98 >>> test_literal_list_slice_start_end_param(1,7) 99 Traceback (most recent call last): 100 ValueError: Assignment to slice of wrong length, expected 5, got 6 101 >>> test_literal_list_slice_start_end_param(2,6) 102 Traceback (most recent call last): 103 ValueError: Assignment to slice of wrong length, expected 5, got 4 104 >>> test_literal_list_slice_start_end_param(2,8) 105 Traceback (most recent call last): 106 ValueError: Assignment to slice of wrong length, expected 5, got 6 107 >>> test_literal_list_slice_start_end_param(3,6) 108 Traceback (most recent call last): 109 ValueError: Assignment to slice of wrong length, expected 5, got 3 110 >>> test_literal_list_slice_start_end_param(1,8) 111 Traceback (most recent call last): 112 ValueError: Assignment to slice of wrong length, expected 5, got 7 113 """ 114 cdef int a[9] # = [9,8,7,6,5,4,3,2,1] 115 a[s:e] = [1,2,3,4,5] 116 return (a[2], a[3], a[4], a[5], a[6]) 117# return a[s:e] 118 119def test_ptr_literal_list_slice_all(): 120 """ 121 >>> test_ptr_literal_list_slice_all() 122 (1, 2, 3, 4, 5) 123 """ 124 cdef int *a = [6,5,4,3,2] 125 a[:] = [1,2,3,4,5] 126 return (a[0], a[1], a[2], a[3], a[4]) 127 128def test_ptr_literal_list_slice_start(): 129 """ 130 >>> test_ptr_literal_list_slice_start() 131 (1, 2, 3, 4, 5) 132 """ 133 cdef int *a = [6,5,4,3,2,1] 134 a[1:] = [1,2,3,4,5] 135 return (a[1], a[2], a[3], a[4], a[5]) 136 137def test_ptr_literal_list_slice_end(): 138 """ 139 >>> test_ptr_literal_list_slice_end() 140 (1, 2, 3, 4, 5) 141 """ 142 cdef int *a = [6,5,4,3,2,1] 143 a[:5] = [1,2,3,4,5] 144 return (a[0], a[1], a[2], a[3], a[4]) 145 146 147@cython.test_assert_path_exists( 148 '//ReturnStatNode//CoerceToPyTypeNode' 149) 150def test_starred_from_array(): 151 """ 152 >>> test_starred_from_array() 153 (1, [2, 3, 4], 5) 154 """ 155 cdef int[5] a 156 a[0] = 1 157 a[1] = 2 158 a[2] = 3 159 a[3] = 4 160 a[4] = 5 161 x, *y, z = a 162 return x, y, z 163 164 165@cython.test_fail_if_path_exists( 166 '//ParallelAssignmentNode//CoerceToPyTypeNode', 167 '//ParallelAssignmentNode//CoerceFromPyTypeNode', 168) 169@cython.test_assert_path_exists( 170 '//ParallelAssignmentNode', 171 '//ReturnStatNode//CoerceToPyTypeNode' 172) 173def test_multiple_from_array(): 174 """ 175 >>> test_multiple_from_array() 176 (1, 2, 3) 177 """ 178 cdef int[3] a 179 a[0] = 1 180 a[1] = 2 181 a[2] = 3 182 x, y, z = a 183 return x, y, z 184 185 186@cython.test_fail_if_path_exists( 187 '//ParallelAssignmentNode//CoerceToPyTypeNode' 188) 189@cython.test_assert_path_exists( 190 '//ParallelAssignmentNode', 191 '//ReturnStatNode//CoerceToPyTypeNode' 192) 193def test_multiple_from_array_full_slice(): 194 """ 195 >>> test_multiple_from_array_full_slice() 196 (1, 2, 3) 197 """ 198 cdef int[3] a 199 a[0] = 1 200 a[1] = 2 201 a[2] = 3 202 x, y, z = a[:] 203 return x, y, z 204 205 206@cython.test_fail_if_path_exists( 207 '//ParallelAssignmentNode//CoerceToPyTypeNode' 208) 209@cython.test_assert_path_exists( 210 '//ParallelAssignmentNode', 211 '//ReturnStatNode//CoerceToPyTypeNode' 212) 213def test_multiple_from_slice(): 214 """ 215 >>> test_multiple_from_slice() 216 (5, 4, 3) 217 """ 218 cdef int *a = [6,5,4,3,2,1] 219 x, y, z = a[1:4] 220 return x, y, z 221 222 223def test_slice_from_multiple(): 224 """ 225 >>> test_slice_from_multiple() 226 (6, -1, -2, -3, 2, 1) 227 """ 228 cdef int *a = [6,5,4,3,2,1] 229 a[1:4] = -1, -2, -3 230 return a[0], a[1], a[2], a[3], a[4], a[5] 231 232def test_literal_tuple(): 233 """ 234 >>> test_literal_tuple() 235 (1, 2, 3, 4, 5) 236 """ 237 cdef int a[5] 238 a = (1,2,3,4,5) 239 return (a[0], a[1], a[2], a[3], a[4]) 240 241def test_list(list l): 242 """ 243 >>> test_list([1, 2, 3, 4, 5]) 244 (1, 2, 3, 4, 5) 245 """ 246 cdef int a[5] 247 a[:] = l 248 return (a[0], a[1], a[2], a[3], a[4]) 249 250 251def assign_all_from_pointer(): 252 """ 253 >>> assign_all_from_pointer() 254 (1, 2, 3, 4, 5) 255 """ 256 cdef int *v = [1, 2, 3, 4, 5] 257 cdef int[5] a 258 a = v 259 return (a[0], a[1], a[2], a[3], a[4]) 260 261 262def assign_full_from_pointer(): 263 """ 264 >>> assign_full_from_pointer() 265 (1, 2, 3, 4, 5) 266 """ 267 cdef int *v = [1, 2, 3, 4, 5] 268 cdef int[5] a 269 a[:] = v 270 return (a[0], a[1], a[2], a[3], a[4]) 271 272 273def assign_slice_end_from_pointer(): 274 """ 275 >>> assign_slice_end_from_pointer() 276 (1, 2, 3, 4, 123) 277 """ 278 cdef int *v = [1, 2, 3, 4, 5] 279 cdef int[5] a 280 a[4] = 123 281 a[:4] = v 282 return (a[0], a[1], a[2], a[3], a[4]) 283 284 285def assign_slice_start_from_pointer(): 286 """ 287 >>> assign_slice_start_from_pointer() 288 (123, 234, 1, 2, 3) 289 """ 290 cdef int *v = [1, 2, 3, 4, 5] 291 cdef int[5] a 292 a[0] = 123 293 a[1] = 234 294 a[2:] = v 295 return (a[0], a[1], a[2], a[3], a[4]) 296 297 298def assign_slice_start_end_from_pointer(): 299 """ 300 >>> assign_slice_start_end_from_pointer() 301 (123, 234, 1, 2, 345) 302 """ 303 cdef int *v = [1, 2, 3, 4, 5] 304 cdef int[5] a 305 a[0] = 123 306 a[1] = 234 307 a[4] = 345 308 a[2:4] = v 309 return (a[0], a[1], a[2], a[3], a[4]) 310 311 312''' 313# FIXME: make this work: 314def assign_slice_start_end_from_sliced_pointer(): 315 """ 316 >>> assign_slice_start_end_from_sliced_pointer() 317 (123, 234, 3, 4, 345) 318 """ 319 cdef int *v = [1, 2, 3, 4, 5] 320 cdef int[5] a 321 a[0] = 123 322 a[1] = 234 323 a[4] = 345 324 a[2:4] = v[2:4] 325 return (a[0], a[1], a[2], a[3], a[4]) 326 327 328def assign_from_longer_array_slice(): 329 """ 330 >>> assign_from_longer_array_slice() 331 [3, 4, 5] 332 """ 333 cdef int[5] a 334 cdef int[3] b 335 a[0] = 1 336 a[1] = 2 337 a[2] = 3 338 a[3] = 4 339 a[4] = 5 340 b[0] = 11 341 b[1] = 12 342 b[2] = 13 343 b = a[2:] 344 return b 345''' 346 347 348def assign_slice_from_shorter_array(): 349 """ 350 >>> assign_slice_from_shorter_array() 351 [1, 11, 12, 13, 5] 352 """ 353 cdef int[5] a 354 cdef int[3] b 355 a[0] = 1 356 a[1] = 2 357 a[2] = 3 358 a[3] = 4 359 a[4] = 5 360 b[0] = 11 361 b[1] = 12 362 b[2] = 13 363 a[1:4] = b 364 return a 365 366 367cdef enum: 368 SIZE = 2 369 370ctypedef int[SIZE] int_array_dyn 371 372 373def assign_ptr_to_unknown_csize(): 374 """ 375 >>> assign_ptr_to_unknown_csize() 376 [1, 2] 377 """ 378 cdef int* v = [1, 2, 3, 4, 5] 379 cdef int_array_dyn d 380 d = v 381 return d 382 383 384def assign_to_wrong_csize(): 385 """ 386 >>> assign_to_wrong_csize() 387 Traceback (most recent call last): 388 ValueError: Assignment to slice of wrong length, expected 3, got 2 389 """ 390 cdef int_array_dyn d 391 cdef int v[3] 392 v[0] = 1 393 v[1] = 2 394 v[2] = 3 395 d = v 396 return d 397 398 399def assign_full_array_slice_to_array(): 400 """ 401 >>> assign_full_array_slice_to_array() 402 [1, 2, 3] 403 """ 404 cdef int[3] x, y 405 x[0] = 1 406 x[1] = 2 407 x[2] = 3 408 y = x[:] 409 return y 410 411 412cdef class ArrayOwner: 413 cdef readonly int[3] array 414 415 def __init__(self, a, b, c): 416 self.array = (a, b, c) 417 418 419def assign_from_array_attribute(): 420 """ 421 >>> assign_from_array_attribute() 422 [1, 2, 3] 423 """ 424 cdef int[3] v 425 a = ArrayOwner(1, 2, 3) 426 v = a.array[:] 427 return v 428