cython
1# mode: run
2# tag: list, slice, slicing
3
4def test_full(seq):
5"""
6>>> l = [1,2,3,4]
7>>> test_full(l)
8[1, 2, 3, 4]
9>>> l == test_full(l)
10True
11>>> l is test_full(l)
12False
13>>> try: test_full(42)
14... except TypeError: pass
15"""
16obj = seq[:]
17return obj
18
19def test_start(seq, start):
20"""
21>>> l = [1,2,3,4]
22>>> test_start(l, 2)
23[3, 4]
24>>> test_start(l, 3)
25[4]
26>>> test_start(l, 4)
27[]
28>>> test_start(l, 8)
29[]
30>>> test_start(l, -3)
31[2, 3, 4]
32>>> test_start(l, -4)
33[1, 2, 3, 4]
34>>> test_start(l, -8)
35[1, 2, 3, 4]
36>>> test_start(l, 0)
37[1, 2, 3, 4]
38>>> test_start(l, None)
39[1, 2, 3, 4]
40>>> try: test_start(42, 2, 3)
41... except TypeError: pass
42"""
43obj = seq[start:]
44return obj
45
46def test_stop(seq, stop):
47"""
48>>> l = [1,2,3,4]
49>>> test_stop(l, 3)
50[1, 2, 3]
51>>> test_stop(l, -1)
52[1, 2, 3]
53>>> test_stop(l, -3)
54[1]
55>>> test_stop(l, -4)
56[]
57>>> test_stop(l, -8)
58[]
59>>> test_stop(l, 0)
60[]
61>>> test_stop(l, None)
62[1, 2, 3, 4]
63>>> try: test_stop(42, 3)
64... except TypeError: pass
65"""
66obj = seq[:stop]
67return obj
68
69def test_step(seq, step):
70"""
71>>> l = [1,2,3,4]
72>>> test_step(l, -1)
73[4, 3, 2, 1]
74>>> test_step(l, 1)
75[1, 2, 3, 4]
76>>> test_step(l, 2)
77[1, 3]
78>>> test_step(l, 3)
79[1, 4]
80>>> test_step(l, -3)
81[4, 1]
82>>> test_step(l, None)
83[1, 2, 3, 4]
84>>> try: test_step(l, 0)
85... except ValueError: pass
86...
87>>> try: test_step(42, 0)
88... except TypeError: pass
89...
90"""
91obj = seq[::step]
92return obj
93
94def test_start_and_stop(seq, start, stop):
95"""
96>>> l = [1,2,3,4]
97>>> test_start_and_stop(l, 2, 3)
98[3]
99>>> test_start_and_stop(l, -3, -1)
100[2, 3]
101>>> test_start_and_stop(l, None, None)
102[1, 2, 3, 4]
103>>> try: test_start_and_stop(42, 2, 3)
104... except TypeError: pass
105"""
106obj = seq[start:stop]
107return obj
108
109def test_start_stop_and_step(seq, start, stop, step):
110"""
111>>> l = [1,2,3,4,5]
112>>> test_start_stop_and_step(l, 0, 5, 1)
113[1, 2, 3, 4, 5]
114>>> test_start_stop_and_step(l, 5, -1, -1)
115[]
116>>> test_start_stop_and_step(l, 5, None, -1)
117[5, 4, 3, 2, 1]
118>>> test_start_stop_and_step(l, 2, 5, 2)
119[3, 5]
120>>> test_start_stop_and_step(l, -100, 100, 1)
121[1, 2, 3, 4, 5]
122>>> test_start_stop_and_step(l, None, None, None)
123[1, 2, 3, 4, 5]
124>>> try: test_start_stop_and_step(l, None, None, 0)
125... except ValueError: pass
126...
127>>> try: test_start_stop_and_step(42, 1, 2, 3)
128... except TypeError: pass
129"""
130obj = seq[start:stop:step]
131return obj
132
133class A(object):
134pass
135
136def slice_of_temporary_smoketest():
137"""
138>>> slice_of_temporary_smoketest()
139[3, 2]
140"""
141x = A()
142x.a = [1, 2]
143x.a[:] = [3,2]
144return x.a
145