|
2 | 2 |
|
3 | 3 | a = [] |
4 | 4 | assert a[:] == [] |
5 | | -assert a[:2**100] == [] |
6 | | -assert a[-2**100:] == [] |
7 | | -assert a[::2**100] == [] |
| 5 | +assert a[: 2 ** 100] == [] |
| 6 | +assert a[-2 ** 100 :] == [] |
| 7 | +assert a[:: 2 ** 100] == [] |
8 | 8 | assert a[10:20] == [] |
9 | 9 | assert a[-20:-10] == [] |
10 | 10 |
|
11 | 11 | b = [1, 2] |
12 | 12 |
|
13 | 13 | assert b[:] == [1, 2] |
14 | | -assert b[:2**100] == [1, 2] |
15 | | -assert b[-2**100:] == [1, 2] |
16 | | -assert b[2**100:] == [] |
17 | | -assert b[::2**100] == [1] |
| 14 | +assert b[: 2 ** 100] == [1, 2] |
| 15 | +assert b[-2 ** 100 :] == [1, 2] |
| 16 | +assert b[2 ** 100 :] == [] |
| 17 | +assert b[:: 2 ** 100] == [1] |
18 | 18 | assert b[-10:1] == [1] |
19 | 19 | assert b[0:0] == [] |
20 | 20 | assert b[1:0] == [] |
21 | 21 |
|
22 | | -assert_raises(ValueError, lambda: b[::0], 'zero step slice') |
| 22 | +assert_raises(ValueError, lambda: b[::0], "zero step slice") |
23 | 23 |
|
24 | 24 | assert b[::-1] == [2, 1] |
25 | 25 | assert b[1::-1] == [2, 1] |
|
33 | 33 | assert c[9:6:-3] == [9] |
34 | 34 | assert c[9::-3] == [9, 6, 3, 0] |
35 | 35 | assert c[9::-4] == [9, 5, 1] |
36 | | -assert c[8::-2**100] == [8] |
| 36 | +assert c[8 :: -2 ** 100] == [8] |
37 | 37 |
|
38 | 38 | assert c[7:7:-2] == [] |
39 | 39 | assert c[7:8:-2] == [] |
|
43 | 43 | assert d[3::-1] == "4321" |
44 | 44 | assert d[4::-3] == "52" |
45 | 45 |
|
| 46 | +assert [1, 2, 3, 5, 6][-1:-5:-1] == [6, 5, 3, 2] # #746 |
46 | 47 |
|
47 | 48 | slice_a = slice(5) |
48 | 49 | assert slice_a.start is None |
@@ -71,3 +72,59 @@ def __setitem__(self, key, value): |
71 | 72 | ss = SubScript() |
72 | 73 | _ = ss[:] |
73 | 74 | ss[:1] = 1 |
| 75 | + |
| 76 | + |
| 77 | +def test_all_slices(): |
| 78 | + """ |
| 79 | + test all possible slices except big number |
| 80 | + """ |
| 81 | + MODE = None # set to "build" to rebuild slice_res.py |
| 82 | + ll = [0, 1, 2, 3] |
| 83 | + start = list(range(-7, 7)) |
| 84 | + end = list(range(-7, 7)) |
| 85 | + step = list(range(-5, 5)) |
| 86 | + step.pop(step.index(0)) |
| 87 | + |
| 88 | + for i in [start, end, step]: |
| 89 | + i.append(None) |
| 90 | + |
| 91 | + def build(): |
| 92 | + # loop used to build slices_res.py with cpython |
| 93 | + with open("slice_res.py", "wt") as f: |
| 94 | + for s in start: |
| 95 | + for e in end: |
| 96 | + for t in step: |
| 97 | + f.write(str(ll[s:e:t]) + "\n") |
| 98 | + |
| 99 | + def run(): |
| 100 | + # test utility |
| 101 | + from slice_res import SLICES_RES |
| 102 | + |
| 103 | + count = 0 |
| 104 | + failures = [] |
| 105 | + for s in start: |
| 106 | + for e in end: |
| 107 | + for t in step: |
| 108 | + lhs = ll[s:e:t] |
| 109 | + try: |
| 110 | + assert lhs == SLICES_RES[count] |
| 111 | + except AssertionError: |
| 112 | + failures.append( |
| 113 | + "start: {} ,stop: {}, step {}. Expected: {}, found: {}".format( |
| 114 | + s, e, t, lhs, SLICES_RES[count] |
| 115 | + ) |
| 116 | + ) |
| 117 | + count += 1 |
| 118 | + |
| 119 | + if failures: |
| 120 | + for f in failures: |
| 121 | + print(f) |
| 122 | + print(len(failures), "slices failed") |
| 123 | + |
| 124 | + if MODE == "build": |
| 125 | + build() |
| 126 | + else: |
| 127 | + run() |
| 128 | + |
| 129 | + |
| 130 | +test_all_slices() |
0 commit comments