Skip to content

Commit 25cc370

Browse files
committed
start adding tests for slices
1 parent c0be1fb commit 25cc370

File tree

1 file changed

+167
-0
lines changed
  • graalpython/com.oracle.graal.python.test/src/tests/cpyext

1 file changed

+167
-0
lines changed
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
# Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
2+
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3+
#
4+
# The Universal Permissive License (UPL), Version 1.0
5+
#
6+
# Subject to the condition set forth below, permission is hereby granted to any
7+
# person obtaining a copy of this software, associated documentation and/or
8+
# data (collectively the "Software"), free of charge and under any and all
9+
# copyright rights in the Software, and any and all patent rights owned or
10+
# freely licensable by each licensor hereunder covering either (i) the
11+
# unmodified Software as contributed to or provided by such licensor, or (ii)
12+
# the Larger Works (as defined below), to deal in both
13+
#
14+
# (a) the Software, and
15+
#
16+
# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
17+
# one is included with the Software each a "Larger Work" to which the Software
18+
# is contributed by such licensors),
19+
#
20+
# without restriction, including without limitation the rights to copy, create
21+
# derivative works of, display, perform, and distribute the Software and make,
22+
# use, sell, offer for sale, import, export, have made, and have sold the
23+
# Software and the Larger Work(s), and to sublicense the foregoing rights on
24+
# either these or other terms.
25+
#
26+
# This license is subject to the following condition:
27+
#
28+
# The above copyright notice and either this complete permission notice or at a
29+
# minimum a reference to the UPL must be included in all copies or substantial
30+
# portions of the Software.
31+
#
32+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38+
# SOFTWARE.
39+
40+
import sys
41+
from . import CPyExtTestCase, CPyExtFunction, CPyExtFunctionOutVars, unhandled_error_compare, GRAALPYTHON
42+
__dir__ = __file__.rpartition("/")[0]
43+
44+
45+
46+
class TestPySlice(CPyExtTestCase):
47+
def compile_module(self, name):
48+
type(self).mro()[1].__dict__["test_%s" % name].create_module(name)
49+
super(TestPySlice, self).compile_module(name)
50+
51+
52+
def reference_get_indices(slize, length):
53+
r = slize.indices(length)
54+
slicelength = 0
55+
for i in range(r[0], r[1], r[2]):
56+
slicelength += 1
57+
return (0, *r, slicelength)
58+
59+
60+
test_PySlice_GetIndicesEx = CPyExtFunction(
61+
lambda args: TestPySlice.reference_get_indices(*args),
62+
lambda: (
63+
(slice(0,1,2), 10),
64+
(slice(None,-1), 10),
65+
(slice(None,-1), 1),
66+
(slice(1), 3),
67+
(slice(None, -1), 3),
68+
),
69+
code='''PyObject* wrapper_SliceIndices(PyObject* slice, int length) {
70+
Py_ssize_t start, stop, step, slicelength;
71+
int result = PySlice_GetIndicesEx(slice, length, &start, &stop, &step, &slicelength);
72+
return Py_BuildValue("iiiii", result, start, stop, step, slicelength);
73+
}
74+
''',
75+
resultspec="O",
76+
argspec='Oi',
77+
callfunction="wrapper_SliceIndices",
78+
arguments=["PyObject* slice", "int length"],
79+
cmpfunc=unhandled_error_compare
80+
)
81+
82+
def reference_unpack(slize):
83+
start = 0 if slize.start is None else slize.start
84+
stop = -1 if slize.stop is None else slize.stop
85+
step = 1 if slize.step is None else slize.step
86+
if step == 0:
87+
raise ValueError("slice step cannot be zero")
88+
return (0, start, stop, step)
89+
90+
test_PySlice_Unpack = CPyExtFunction(
91+
lambda arg: TestPySlice.reference_unpack(*arg),
92+
lambda: (
93+
(slice(0,1,2),),
94+
(slice(0,-1),),
95+
(slice(None,-1,0),),
96+
(slice(0,1,-1),),
97+
(slice(1,-1),),
98+
(slice(1),),
99+
),
100+
code='''PyObject* wrapper_SliceUnpack(PyObject* slice) {
101+
Py_ssize_t start, stop, step;
102+
int result = PySlice_Unpack(slice, &start, &stop, &step);
103+
if (result < 0) {
104+
return NULL;
105+
}
106+
return Py_BuildValue("iiii", result, start, stop, step);
107+
}
108+
''',
109+
resultspec="O",
110+
argspec='O',
111+
callfunction="wrapper_SliceUnpack",
112+
arguments=["PyObject* slice"],
113+
cmpfunc=unhandled_error_compare
114+
)
115+
116+
def reference_adjust(length, start, stop, step):
117+
slicelength = -1
118+
119+
if start < 0:
120+
start += length
121+
if (start < 0):
122+
start = -1 if step < 0 else 0
123+
elif start >= length:
124+
start = length - 1 if step < 0 else length
125+
126+
if stop < 0:
127+
stop += length
128+
if (stop < 0):
129+
stop = -1 if step < 0 else 0
130+
elif stop >= length:
131+
stop = length - 1 if step < 0 else length
132+
133+
if step < 0:
134+
if stop < start:
135+
slicelength = (start - stop - 1) / (-step) + 1
136+
return slicelength, start, stop
137+
else:
138+
if start < stop:
139+
slicelength = (stop - start - 1) / step + 1
140+
return slicelength, start, stop
141+
142+
return 0, start, stop
143+
144+
145+
test_PySlice_AdjustIndices = CPyExtFunction(
146+
lambda arg: TestPySlice.reference_adjust(*arg),
147+
lambda: (
148+
(3,0,-1,1),
149+
(3,1,-1,1),
150+
(12,-1,-1,-1),
151+
(12,-1,0,-1),
152+
(12,-1,0,-2),
153+
(3,1,-1,2),
154+
),
155+
code='''PyObject* Slice_AdjustIndices(int length, int sta, int sto, int step) {
156+
Py_ssize_t start = sta;
157+
Py_ssize_t stop = sto;
158+
int slicelength = PySlice_AdjustIndices(length, &start, &stop, step);
159+
return Py_BuildValue("iii", slicelength, start, stop);
160+
}
161+
''',
162+
resultspec="O",
163+
argspec='iiii',
164+
callfunction="Slice_AdjustIndices",
165+
arguments=["int length", "int start", "int stop", "int step"],
166+
cmpfunc=unhandled_error_compare
167+
)

0 commit comments

Comments
 (0)