Debian Codegraph @

pypy/module/_cppyy/test/test_cppyy.py

raw symbols SCIP
1 import py, os, sys
2 from pypy.module._cppyy import interp_cppyy, executor
3 from .support import setup_make, soext
4
5 currpath = py.path.local(__file__).dirpath()
6 test_dct = str(currpath.join("example01Dict"))+soext
7
8 def setup_module(mod):
9 setup_make("example01")
10
11
12 class TestCPPYYImplementation:
13 def test01_class_query(self, space):
14 # NOTE: this test needs to run before test_pythonify.py
15 import ctypes
16 dct = ctypes.CDLL(test_dct)
17 w_cppyyclass = interp_cppyy.scope_byname(space, "example01")
18 w_cppyyclass2 = interp_cppyy.scope_byname(space, "example01")
19 assert space.is_w(w_cppyyclass, w_cppyyclass2)
20 adddouble = w_cppyyclass.overloads["staticAddToDouble"]
21 func, = adddouble.functions
22 assert func.executor is None
23 func._setup(None) # creates executor
24 assert isinstance(func.executor, executor._executors['double'])
25 assert func.arg_defs == [("double", "")]
26
27
28 class AppTestCPPYY:
29 spaceconfig = dict(usemodules=['_cppyy', '_rawffi', 'itertools'])
30
31 def setup_class(cls):
32 cls.w_lib, cls.w_instantiate, cls.w_example01, cls.w_payload = \
33 cls.space.unpackiterable(cls.space.appexec([], """():
34 import _cppyy, ctypes
35 _cppyy._post_import_startup()
36 lib = ctypes.CDLL(%r, ctypes.RTLD_GLOBAL)
37 def cpp_instantiate(tt, *args):
38 inst = _cppyy._bind_object(0, tt, True)
39 ol = tt.get_overload("__init__").__get__(inst)
40 ol(*args)
41 return inst
42 return lib, cpp_instantiate, _cppyy._scope_byname('example01'),\
43 _cppyy._scope_byname('payload')""" % (test_dct, )))
44
45 def test01_static_int(self):
46 """Test passing of an int, returning of an int, and overloading on a
47 differening number of arguments."""
48
49 import sys, math
50 t = self.example01
51
52 pylong = int
53 if sys.hexversion < 0x3000000:
54 pylong = long
55
56 res = t.get_overload("staticAddOneToInt")(1)
57 assert res == 2
58 res = t.get_overload("staticAddOneToInt")(pylong(1))
59 assert res == 2
60 res = t.get_overload("staticAddOneToInt")(1, 2)
61 assert res == 4
62 res = t.get_overload("staticAddOneToInt")(-1)
63 assert res == 0
64 maxint32 = int(2 ** 31 - 1)
65 res = t.get_overload("staticAddOneToInt")(maxint32-1)
66 assert res == maxint32
67 res = t.get_overload("staticAddOneToInt")(maxint32)
68 assert res == -maxint32-1
69
70 raises(TypeError, 't.get_overload("staticAddOneToInt")(1, [])')
71 raises(TypeError, 't.get_overload("staticAddOneToInt")(1.)')
72 raises(TypeError, 't.get_overload("staticAddOneToInt")(maxint32+1)')
73
74 def test02_static_double(self):
75 """Test passing of a double and returning of a double on a static function."""
76
77 t = self.example01
78
79 res = t.get_overload("staticAddToDouble")(0.09)
80 assert res == 0.09 + 0.01
81
82 def test03_static_constcharp(self):
83 """Test passing of a C string and returning of a C string on a static
84 function."""
85
86 t = self.example01
87
88 res = t.get_overload("staticAtoi")("1")
89 assert res == 1
90 res = t.get_overload("staticStrcpy")("aap") # TODO: this leaks
91 assert res == "aap"
92 res = t.get_overload("staticStrcpy")(u"aap") # TODO: this leaks
93 assert res == "aap"
94
95 raises(TypeError, 't.get_overload("staticStrcpy")(1.)') # TODO: this leaks
96
97 def test04_method_int(self):
98 """Test passing of a int, returning of a int, and memory cleanup, on
99 a method."""
100 import _cppyy
101
102 t = self.example01
103
104 assert t.get_overload("getCount")() == 0
105
106 e1 = self.instantiate(t, 7)
107 assert t.get_overload("getCount")() == 1
108 res = t.get_overload("addDataToInt")(e1, 4)
109 assert res == 11
110 res = t.get_overload("addDataToInt")(e1, -4)
111 assert res == 3
112 e1.__destruct__()
113 assert t.get_overload("getCount")() == 0
114 raises(ReferenceError, 't.get_overload("addDataToInt")(e1, 4)')
115
116 e1 = self.instantiate(t, 7)
117 e2 = self.instantiate(t, 8)
118 assert t.get_overload("getCount")() == 2
119 e1.__destruct__()
120 assert t.get_overload("getCount")() == 1
121 e2.__destruct__()
122 assert t.get_overload("getCount")() == 0
123
124 e2.__destruct__()
125 assert t.get_overload("getCount")() == 0
126
127 raises(TypeError, t.get_overload("addDataToInt"), 41, 4)
128
129 def test05_memory(self):
130 """Test memory destruction and integrity."""
131
132 import gc
133 import _cppyy
134
135 t = self.example01
136
137 assert t.get_overload("getCount")() == 0
138
139 e1 = self.instantiate(t, 7)
140 assert t.get_overload("getCount")() == 1
141 res = t.get_overload("addDataToInt")(e1, 4)
142 assert res == 11
143 res = t.get_overload("addDataToInt")(e1, -4)
144 assert res == 3
145 e1 = None
146 gc.collect()
147 assert t.get_overload("getCount")() == 0
148
149 e1 = self.instantiate(t, 7)
150 e2 = self.instantiate(t, 8)
151 assert t.get_overload("getCount")() == 2
152 e1 = None
153 gc.collect()
154 assert t.get_overload("getCount")() == 1
155 e2.__destruct__()
156 assert t.get_overload("getCount")() == 0
157 e2 = None
158 gc.collect()
159 assert t.get_overload("getCount")() == 0
160
161 def test05a_memory2(self):
162 """Test ownership control."""
163
164 import gc, _cppyy
165
166 t = self.example01
167
168 assert t.get_overload("getCount")() == 0
169
170 e1 = self.instantiate(t, 7)
171 assert t.get_overload("getCount")() == 1
172 assert e1.__python_owns__ == True
173 e1.__python_owns__ = False
174 e1 = None
175 gc.collect()
176 assert t.get_overload("getCount")() == 1
177
178 # forced fix-up of object count for later tests
179 t.get_overload("setCount")(0)
180
181
182 def test06_method_double(self):
183 """Test passing of a double and returning of double on a method."""
184
185 import _cppyy
186
187 t = self.example01
188
189 e = self.instantiate(t, 13)
190 res = t.get_overload("addDataToDouble")(e, 16)
191 assert round(res-29, 8) == 0.
192 e.__destruct__()
193
194 e = self.instantiate(t, -13)
195 res = t.get_overload("addDataToDouble")(e, 16)
196 assert round(res-3, 8) == 0.
197 e.__destruct__()
198 assert t.get_overload("getCount")() == 0
199
200 def test07_method_constcharp(self):
201 """Test passing of a C string and returning of a C string on a
202 method."""
203 import _cppyy
204
205 t = self.example01
206
207 e = self.instantiate(t, 42)
208 res = t.get_overload("addDataToAtoi")(e, "13")
209 assert res == 55
210 res = t.get_overload("addToStringValue")(e, "12") # TODO: this leaks
211 assert res == "54"
212 res = t.get_overload("addToStringValue")(e, "-12") # TODO: this leaks
213 assert res == "30"
214 e.__destruct__()
215 assert t.get_overload("getCount")() == 0
216
217 def test08_pass_object_by_pointer(self):
218 """Test passing of an instance as an argument."""
219 import _cppyy
220
221 t1 = self.example01
222 t2 = self.payload
223
224 pl = self.instantiate(t2, 3.14)
225 assert round(t2.get_overload("getData")(pl)-3.14, 8) == 0
226 t1.get_overload("staticSetPayload")(pl, 41.)
227 assert t2.get_overload("getData")(pl) == 41.
228
229 e = self.instantiate(t1, 50)
230 t1.get_overload("setPayload")(e, pl);
231 assert round(t2.get_overload("getData")(pl)-50., 8) == 0
232
233 e.__destruct__()
234 pl.__destruct__()
235 assert t1.get_overload("getCount")() == 0
236
237 def test09_return_object_by_pointer(self):
238 """Test returning of an instance as an argument."""
239 import _cppyy
240
241 t1 = self.example01
242 t2 = self.payload
243
244 pl1 = self.instantiate(t2, 3.14)
245 assert round(t2.get_overload("getData")(pl1)-3.14, 8) == 0
246 pl2 = t1.get_overload("staticCyclePayload")(pl1, 38.)
247 assert t2.get_overload("getData")(pl2) == 38.
248
249 e = self.instantiate(t1, 50)
250 pl2 = t1.get_overload("cyclePayload")(e, pl1);
251 assert round(t2.get_overload("getData")(pl2)-50., 8) == 0
252
253 e.__destruct__()
254 pl1.__destruct__()
255 assert t1.get_overload("getCount")() == 0