Skip to content

Commit 176bfb2

Browse files
committed
add tests
1 parent cabe8bb commit 176bfb2

File tree

4 files changed

+162
-10
lines changed

4 files changed

+162
-10
lines changed

graalpython/com.oracle.graal.python.test/src/tests/cpyext/test_abstract.py

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1219,7 +1219,102 @@ def compile_module(self, name):
12191219
arguments=["PyObject* haystack", "PyObject* needle"],
12201220
cmpfunc=unhandled_error_compare
12211221
)
1222-
1222+
1223+
def index_func(args):
1224+
for idx,obj in enumerate(args[0]):
1225+
if obj == args[1]:
1226+
return idx
1227+
raise ValueError
1228+
1229+
test_PySequence_Index = CPyExtFunction(
1230+
index_func,
1231+
lambda: (
1232+
(tuple(), 1),
1233+
((1, 2, 3), 1),
1234+
((1, 2, 3), 4),
1235+
((None,), 1),
1236+
([], 1),
1237+
(['a', 'b', 'c'], 'a'),
1238+
(['a', 'b', 'c'], 'd'),
1239+
([None], 1),
1240+
(set(), 1),
1241+
({'a', 'b'}, 'a'),
1242+
({'a', 'b'}, 'c'),
1243+
(DummyListSubclass(), 1),
1244+
('hello', 'e'),
1245+
('hello', 'x'),
1246+
),
1247+
resultspec="i",
1248+
argspec='OO',
1249+
arguments=["PyObject* haystack", "PyObject* needle"],
1250+
cmpfunc=unhandled_error_compare
1251+
)
1252+
1253+
def count_func(args):
1254+
c = 0
1255+
for obj in args[0]:
1256+
if obj == args[1]:
1257+
c += 1
1258+
return c
1259+
1260+
test_PySequence_Count = CPyExtFunction(
1261+
count_func,
1262+
lambda: (
1263+
(tuple(), 1),
1264+
((1, 2, 3), 1),
1265+
((1, 2, 3), 4),
1266+
((None,), 1),
1267+
([], 1),
1268+
(['a', 'b', 'c'], 'a'),
1269+
(['a', 'b', 'c'], 'd'),
1270+
([None], 1),
1271+
(set(), 1),
1272+
({'a', 'b'}, 'a'),
1273+
({'a', 'b'}, 'c'),
1274+
(DummyListSubclass(), 1),
1275+
('hello', 'e'),
1276+
('hello', 'x'),
1277+
),
1278+
resultspec="i",
1279+
argspec='OO',
1280+
arguments=["PyObject* haystack", "PyObject* needle"],
1281+
cmpfunc=unhandled_error_compare
1282+
)
1283+
1284+
def _reference_setslice(args):
1285+
args[0][args[1]:args[2]] = args[3]
1286+
return 0;
1287+
1288+
test_PySequence_SetSlice = CPyExtFunction(
1289+
_reference_setslice,
1290+
lambda: (
1291+
([1,2,3,4],0,4,[5,6,7,8]),
1292+
([],1,2, [5,6]),
1293+
([1,2,3,4],10,20,[5,6,7,8]),
1294+
),
1295+
resultspec="i",
1296+
argspec='OnnO',
1297+
arguments=["PyObject* op", "Py_ssize_t ilow", "Py_ssize_t ihigh", "PyObject* v"],
1298+
cmpfunc=unhandled_error_compare
1299+
)
1300+
1301+
def _reference_delslice(args):
1302+
del args[0][args[1]:args[2]]
1303+
return 0;
1304+
1305+
test_PySequence_DelSlice = CPyExtFunction(
1306+
_reference_delslice,
1307+
lambda: (
1308+
([1,2,3,4],0,4),
1309+
([],1,2),
1310+
([1,2,3,4],10,20),
1311+
),
1312+
resultspec="i",
1313+
argspec='Onn',
1314+
arguments=["PyObject* op", "Py_ssize_t ilow", "Py_ssize_t ihigh"],
1315+
cmpfunc=unhandled_error_compare
1316+
)
1317+
12231318
test_PySequence_ITEM = CPyExtFunction(
12241319
_reference_getitem,
12251320
lambda: (

graalpython/com.oracle.graal.python.test/src/tests/cpyext/test_dict.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,18 @@ def _reference_values(args):
6060
return list(d.values())
6161

6262

63+
def _reference_items(args):
64+
d = args[0]
65+
return list(d.items())
66+
67+
6368
def _reference_pop(args):
6469
d = args[0]
6570
if(len(args) == 2):
6671
return d.pop(args[1])
6772
else:
6873
return d.pop(args[1], args[2])
69-
74+
7075

7176
def _reference_get_item_with_error(args):
7277
d = args[0]
@@ -127,7 +132,7 @@ def _reference_merge(args):
127132
if not k in a:
128133
a[k] = b[k]
129134
return 0
130-
except:
135+
except:
131136
raise AttributeError("'int' object has no attribute 'keys'")
132137

133138
class SubDict(dict):
@@ -158,7 +163,7 @@ def keys(self):
158163
return "ab"
159164
def __getitem__(self, key):
160165
return key
161-
166+
162167
class TestPyDict(CPyExtTestCase):
163168

164169
def compile_module(self, name):
@@ -174,7 +179,7 @@ def compile_module(self, name):
174179
arguments=("PyObject* dict", "PyObject* key", "PyObject* deflt"),
175180
cmpfunc=unhandled_error_compare
176181
)
177-
182+
178183
# PyDict_SetItem
179184
test_PyDict_SetItem = CPyExtFunction(
180185
_reference_set_item,
@@ -208,7 +213,7 @@ def compile_module(self, name):
208213
callfunction="wrap_PyDict_GetItem",
209214
cmpfunc=unhandled_error_compare
210215
)
211-
216+
212217
# PyDict_GetItemWithError
213218
test_PyDict_GetItemWithError = CPyExtFunction(
214219
_reference_get_item_with_error,
@@ -344,12 +349,12 @@ def compile_module(self, name):
344349
lambda: (({'a': "hello"}, ),),
345350
code='''PyObject* wrap__PyDict_SetItem_KnownHash(PyObject* dict) {
346351
PyObject* result = PyDict_New();
347-
352+
348353
Py_ssize_t ppos = 0;
349354
PyObject* key;
350355
PyObject* value;
351-
Py_hash_t phash;
352-
356+
Py_hash_t phash;
357+
353358
_PyDict_Next(dict, &ppos, &key, &value, &phash);
354359
_PyDict_SetItem_KnownHash(result, key, value, phash);
355360
return result;
@@ -499,7 +504,7 @@ def compile_module(self, name):
499504
argspec="O",
500505
cmpfunc=unhandled_error_compare
501506
)
502-
507+
503508
# PyDict_Values
504509
test_PyDict_Values = CPyExtFunction(
505510
_reference_values,
@@ -508,3 +513,11 @@ def compile_module(self, name):
508513
argspec="O",
509514
cmpfunc=unhandled_error_compare
510515
)
516+
517+
test_PyDict_Items = CPyExtFunction(
518+
_reference_items,
519+
lambda: (({},), ({'a': "hello"},)),
520+
resultspec="O",
521+
argspec="O",
522+
cmpfunc=unhandled_error_compare
523+
)

graalpython/com.oracle.graal.python.test/src/tests/cpyext/test_exceptionobject.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,18 @@ def test_set_exc_info(self):
127127
else:
128128
assert False
129129

130+
131+
def raise_exception_with_cause():
132+
try:
133+
raise RuntimeError()
134+
except RuntimeError as e:
135+
exc = e
136+
try:
137+
raise IndexError from exc
138+
except IndexError as e:
139+
return e
140+
141+
130142
class TestExceptionobjectFunctions(CPyExtTestCase):
131143
def compile_module(self, name):
132144
type(self).mro()[1].__dict__["test_%s" % name].create_module(name)
@@ -143,3 +155,13 @@ def compile_module(self, name):
143155
argspec="OO",
144156
arguments=["PyObject* exc", "PyObject* tb"],
145157
)
158+
159+
test_PyException_GetCause = CPyExtFunction(
160+
lambda args: args[0].__cause__,
161+
lambda: (
162+
(raise_exception_with_cause(),),
163+
),
164+
resultspec="O",
165+
argspec="O",
166+
arguments=["PyObject* exc"],
167+
)

graalpython/com.oracle.graal.python.test/src/tests/cpyext/test_pythonrun.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,25 @@ def compile_module(self, name):
8484
arguments=["char* source", "int type", "PyObject* globals", "PyObject* locals", "PyCompilerFlags* ignored"],
8585
cmpfunc=unhandled_error_compare
8686
)
87+
88+
test_Py_CompileString = CPyExtFunction(
89+
lambda args: compile(
90+
args[0],
91+
args[1],
92+
{
93+
256: "single",
94+
257: "exec",
95+
258: "eval"
96+
}[args[2]]
97+
),
98+
lambda: (
99+
("1 + 2", "foo.py", 256),
100+
("1 + 2", "foo.py", 257),
101+
("1 + 2", "foo.py", 258),
102+
("x = 2", "foo.py", 258),
103+
),
104+
resultspec="O",
105+
argspec='ssi',
106+
arguments=["char* source", "char* filename", "int type"],
107+
cmpfunc=unhandled_error_compare
108+
)

0 commit comments

Comments
 (0)