| 1 |
import py, os, sys |
| 2 |
from .support import setup_make, soext |
| 3 |
|
| 4 |
from pypy.module._cppyy import interp_cppyy, executor |
| 5 |
|
| 6 |
currpath = py.path.local(__file__).dirpath() |
| 7 |
test_dct = str(currpath.join("example01Dict"))+soext |
| 8 |
|
| 9 |
def setup_module(mod): |
| 10 |
setup_make("example01") |
| 11 |
|
| 12 |
|
| 13 |
class AppTestPYTHONIFY: |
| 14 |
spaceconfig = dict(usemodules=['_cppyy', '_rawffi', 'itertools']) |
| 15 |
|
| 16 |
def setup_class(cls): |
| 17 |
cls.w_test_dct = cls.space.newtext(test_dct) |
| 18 |
cls.w_example01 = cls.space.appexec([], """(): |
| 19 |
import ctypes, _cppyy |
| 20 |
_cppyy._post_import_startup() |
| 21 |
return ctypes.CDLL(%r, ctypes.RTLD_GLOBAL)""" % (test_dct, )) |
| 22 |
|
| 23 |
def test01_finding_classes(self): |
| 24 |
"""Test the lookup of a class, and its caching.""" |
| 25 |
import _cppyy |
| 26 |
example01_class = _cppyy.gbl.example01 |
| 27 |
cl2 = _cppyy.gbl.example01 |
| 28 |
assert example01_class is cl2 |
| 29 |
|
| 30 |
raises(AttributeError, '_cppyy.gbl.nonexistingclass') |
| 31 |
|
| 32 |
def test02_calling_static_functions(self): |
| 33 |
"""Test calling of static methods.""" |
| 34 |
import _cppyy, sys, math |
| 35 |
example01_class = _cppyy.gbl.example01 |
| 36 |
res = example01_class.staticAddOneToInt(1) |
| 37 |
assert res == 2 |
| 38 |
|
| 39 |
res = example01_class.staticAddOneToInt(1, 2) |
| 40 |
assert res == 4 |
| 41 |
res = example01_class.staticAddOneToInt(-1) |
| 42 |
assert res == 0 |
| 43 |
maxint32 = int(2 ** 31 - 1) |
| 44 |
res = example01_class.staticAddOneToInt(maxint32-1) |
| 45 |
assert res == maxint32 |
| 46 |
res = example01_class.staticAddOneToInt(maxint32) |
| 47 |
assert res == -maxint32-1 |
| 48 |
|
| 49 |
raises(TypeError, 'example01_class.staticAddOneToInt(1, [])') |
| 50 |
raises(TypeError, 'example01_class.staticAddOneToInt(1.)') |
| 51 |
raises(TypeError, 'example01_class.staticAddOneToInt(maxint32+1)') |
| 52 |
res = example01_class.staticAddToDouble(0.09) |
| 53 |
assert res == 0.09 + 0.01 |
| 54 |
|
| 55 |
res = example01_class.staticAtoi("1") |
| 56 |
assert res == 1 |
| 57 |
|
| 58 |
res = example01_class.staticStrcpy("aap") |
| 59 |
assert res == "aap" |
| 60 |
|
| 61 |
res = example01_class.staticStrcpy(u"aap") |
| 62 |
assert res == "aap" |
| 63 |
|
| 64 |
raises(TypeError, 'example01_class.staticStrcpy(1.)') |
| 65 |
|
| 66 |
def test03_constructing_and_calling(self): |
| 67 |
"""Test object and method calls.""" |
| 68 |
import _cppyy |
| 69 |
example01_class = _cppyy.gbl.example01 |
| 70 |
assert example01_class.getCount() == 0 |
| 71 |
instance = example01_class(7) |
| 72 |
assert example01_class.getCount() == 1 |
| 73 |
res = instance.addDataToInt(4) |
| 74 |
assert res == 11 |
| 75 |
res = instance.addDataToInt(-4) |
| 76 |
assert res == 3 |
| 77 |
instance.__destruct__() |
| 78 |
assert example01_class.getCount() == 0 |
| 79 |
raises(ReferenceError, 'instance.addDataToInt(4)') |
| 80 |
|
| 81 |
instance = example01_class(7) |
| 82 |
instance2 = example01_class(8) |
| 83 |
assert example01_class.getCount() == 2 |
| 84 |
instance.__destruct__() |
| 85 |
assert example01_class.getCount() == 1 |
| 86 |
instance2.__destruct__() |
| 87 |
assert example01_class.getCount() == 0 |
| 88 |
|
| 89 |
instance = example01_class(13) |
| 90 |
res = instance.addDataToDouble(16) |
| 91 |
assert round(res-29, 8) == 0. |
| 92 |
instance.__destruct__() |
| 93 |
instance = example01_class(-13) |
| 94 |
res = instance.addDataToDouble(16) |
| 95 |
assert round(res-3, 8) == 0. |
| 96 |
instance.__destruct__() |
| 97 |
|
| 98 |
instance = example01_class(42) |
| 99 |
assert example01_class.getCount() == 1 |
| 100 |
|
| 101 |
res = instance.addDataToAtoi("13") |
| 102 |
assert res == 55 |
| 103 |
|
| 104 |
res = instance.addToStringValue("12") |
| 105 |
assert res == "54" |
| 106 |
res = instance.addToStringValue("-12") |
| 107 |
assert res == "30" |
| 108 |
|
| 109 |
res = instance.staticAddOneToInt(1) |
| 110 |
assert res == 2 |
| 111 |
|
| 112 |
instance.__destruct__() |
| 113 |
assert example01_class.getCount() == 0 |
| 114 |
|
| 115 |
def test04_passing_object_by_pointer(self): |
| 116 |
import _cppyy |
| 117 |
example01_class = _cppyy.gbl.example01 |
| 118 |
payload_class = _cppyy.gbl.payload |
| 119 |
|
| 120 |
e = example01_class(14) |
| 121 |
pl = payload_class(3.14) |
| 122 |
assert round(pl.getData()-3.14, 8) == 0 |
| 123 |
|
| 124 |
example01_class.staticSetPayload(pl, 41.) |
| 125 |
assert pl.getData() == 41. |
| 126 |
example01_class.staticSetPayload(pl, 43.) |
| 127 |
assert pl.getData() == 43. |
| 128 |
e.staticSetPayload(pl, 45.) |
| 129 |
assert pl.getData() == 45. |
| 130 |
|
| 131 |
e.setPayload(pl) |
| 132 |
assert round(pl.getData()-14., 8) == 0 |
| 133 |
|
| 134 |
pl.__destruct__() |
| 135 |
e.__destruct__() |
| 136 |
assert example01_class.getCount() == 0 |
| 137 |
|
| 138 |
def test05_returning_object_by_pointer(self): |
| 139 |
import _cppyy |
| 140 |
example01_class = _cppyy.gbl.example01 |
| 141 |
payload_class = _cppyy.gbl.payload |
| 142 |
|
| 143 |
pl = payload_class(3.14) |
| 144 |
assert round(pl.getData()-3.14, 8) == 0 |
| 145 |
|
| 146 |
pl2 = example01_class.staticCyclePayload(pl, 38.) |
| 147 |
assert pl2.getData() == 38. |
| 148 |
|
| 149 |
e = example01_class(14) |
| 150 |
|
| 151 |
pl2 = e.cyclePayload(pl) |
| 152 |
assert round(pl2.getData()-14., 8) == 0 |
| 153 |
|
| 154 |
pl.__destruct__() |
| 155 |
e.__destruct__() |
| 156 |
assert example01_class.getCount() == 0 |
| 157 |
|
| 158 |
def test06_returning_object_by_value(self): |
| 159 |
import _cppyy |
| 160 |
example01_class = _cppyy.gbl.example01 |
| 161 |
payload_class = _cppyy.gbl.payload |
| 162 |
|
| 163 |
pl = payload_class(3.14) |
| 164 |
assert round(pl.getData()-3.14, 8) == 0 |
| 165 |
|
| 166 |
pl2 = example01_class.staticCopyCyclePayload(pl, 38.) |
| 167 |
assert pl2.getData() == 38. |
| 168 |
pl2.__destruct__() |
| 169 |
|
| 170 |
e = example01_class(14) |
| 171 |
|
| 172 |
pl2 = e.copyCyclePayload(pl) |
| 173 |
assert round(pl2.getData()-14., 8) == 0 |
| 174 |
pl2.__destruct__() |
| 175 |
|
| 176 |
pl.__destruct__() |
| 177 |
e.__destruct__() |
| 178 |
assert example01_class.getCount() == 0 |
| 179 |
|
| 180 |
def test07_global_functions(self): |
| 181 |
import _cppyy |
| 182 |
|
| 183 |
assert _cppyy.gbl.globalAddOneToInt(3) == 4 |
| 184 |
assert _cppyy.gbl.globalAddOneToInt(3) == 4 |
| 185 |
|
| 186 |
assert _cppyy.gbl.ns_example01.globalAddOneToInt(4) == 5 |
| 187 |
assert _cppyy.gbl.ns_example01.globalAddOneToInt(4) == 5 |
| 188 |
|
| 189 |
def test08_memory(self): |
| 190 |
"""Test proper C++ destruction by the garbage collector""" |
| 191 |
|
| 192 |
import _cppyy, gc |
| 193 |
example01_class = _cppyy.gbl.example01 |
| 194 |
payload_class = _cppyy.gbl.payload |
| 195 |
|
| 196 |
pl = payload_class(3.14) |
| 197 |
assert payload_class.count == 1 |
| 198 |
assert round(pl.getData()-3.14, 8) == 0 |
| 199 |
|
| 200 |
pl2 = example01_class.staticCopyCyclePayload(pl, 38.) |
| 201 |
assert payload_class.count == 2 |
| 202 |
assert pl2.getData() == 38. |
| 203 |
pl2 = None |
| 204 |
gc.collect() |
| 205 |
assert payload_class.count == 1 |
| 206 |
|
| 207 |
e = example01_class(14) |
| 208 |
|
| 209 |
pl2 = e.copyCyclePayload(pl) |
| 210 |
assert payload_class.count == 2 |
| 211 |
assert round(pl2.getData()-14., 8) == 0 |
| 212 |
pl2 = None |
| 213 |
gc.collect() |
| 214 |
assert payload_class.count == 1 |
| 215 |
|
| 216 |
pl = None |
| 217 |
e = None |
| 218 |
gc.collect() |
| 219 |
assert payload_class.count == 0 |
| 220 |
assert example01_class.getCount() == 0 |
| 221 |
|
| 222 |
pl = payload_class(3.14) |
| 223 |
pl_a = example01_class.staticCyclePayload(pl, 66.) |
| 224 |
pl_a.getData() == 66. |
| 225 |
assert payload_class.count == 1 |
| 226 |
pl_a = None |
| 227 |
pl = None |
| 228 |
gc.collect() |
| 229 |
assert payload_class.count == 0 |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
def test09_default_arguments(self): |
| 234 |
"""Test propagation of default function arguments""" |
| 235 |
|
| 236 |
import _cppyy |
| 237 |
a = _cppyy.gbl.ArgPasser() |
| 238 |
|
| 239 |
|
| 240 |
f = a.stringRef |
| 241 |
s = _cppyy.gbl.std.string |
| 242 |
assert f(s("aap"), 0, s("noot")) == "aap" |
| 243 |
assert f(s("noot"), 1) == "default" |
| 244 |
assert f(s("mies")) == "mies" |
| 245 |
|
| 246 |
for itype in ['short', 'ushort', 'int', 'uint', 'long', 'ulong']: |
| 247 |
g = getattr(a, '%sValue' % itype) |
| 248 |
raises(TypeError, 'g(1, 2, 3, 4, 6)') |
| 249 |
assert g(11, 0, 12, 13) == 11 |
| 250 |
assert g(11, 1, 12, 13) == 12 |
| 251 |
assert g(11, 1, 12) == 12 |
| 252 |
assert g(11, 2, 12) == 2 |
| 253 |
assert g(11, 1) == 1 |
| 254 |
assert g(11, 2) == 2 |
| 255 |
assert g(11) == 11 |
| 256 |
|
| 257 |
for ftype in ['float', 'double']: |
| 258 |
g = getattr(a, '%sValue' % ftype) |
| 259 |
raises(TypeError, 'g(1., 2, 3., 4., 6.)') |
| 260 |
assert g(11., 0, 12., 13.) == 11. |
| 261 |
assert g(11., 1, 12., 13.) == 12. |
| 262 |
assert g(11., 1, 12.) == 12. |
| 263 |
assert g(11., 2, 12.) == 2. |
| 264 |
assert g(11., 1) == 1. |
| 265 |
assert g(11., 2) == 2. |
| 266 |
assert g(11.) == 11. |
| 267 |
|
| 268 |
def test10_overload_on_arguments(self): |
| 269 |
"""Test functions overloaded on arguments""" |
| 270 |
|
| 271 |
import _cppyy |
| 272 |
e = _cppyy.gbl.example01(1) |
| 273 |
|
| 274 |
assert e.addDataToInt(2) == 3 |
| 275 |
assert e.overloadedAddDataToInt(3) == 4 |
| 276 |
assert e.overloadedAddDataToInt(4, 5) == 10 |
| 277 |
assert e.overloadedAddDataToInt(6, 7, 8) == 22 |
| 278 |
|
| 279 |
def test11_typedefs(self): |
| 280 |
"""Test access and use of typedefs""" |
| 281 |
|
| 282 |
import _cppyy |
| 283 |
|
| 284 |
assert _cppyy.gbl.example01 == _cppyy.gbl.example01_t |
| 285 |
|
| 286 |
def test12_underscore_in_class_name(self): |
| 287 |
"""Test recognition of '_' as part of a valid class name""" |
| 288 |
|
| 289 |
import _cppyy |
| 290 |
|
| 291 |
assert _cppyy.gbl.z_ == _cppyy.gbl.z_ |
| 292 |
|
| 293 |
z = _cppyy.gbl.z_() |
| 294 |
|
| 295 |
assert hasattr(z, 'myint') |
| 296 |
assert z.gime_z_(z) |
| 297 |
|
| 298 |
def test13_bound_unbound_calls(self): |
| 299 |
"""Test (un)bound method calls""" |
| 300 |
|
| 301 |
import _cppyy |
| 302 |
|
| 303 |
raises(TypeError, _cppyy.gbl.example01.addDataToInt, 1) |
| 304 |
|
| 305 |
meth = _cppyy.gbl.example01.addDataToInt |
| 306 |
raises(TypeError, meth) |
| 307 |
raises(TypeError, meth, 1) |
| 308 |
|
| 309 |
e = _cppyy.gbl.example01(2) |
| 310 |
assert 5 == meth(e, 3) |
| 311 |
|
| 312 |
def test14_installable_function(self): |
| 313 |
"""Test installing and calling global C++ function as python method""" |
| 314 |
|
| 315 |
import _cppyy |
| 316 |
|
| 317 |
_cppyy.gbl.example01.fresh = _cppyy.gbl.installableAddOneToInt |
| 318 |
|
| 319 |
e = _cppyy.gbl.example01(0) |
| 320 |
assert 2 == e.fresh(1) |
| 321 |
assert 3 == e.fresh(2) |
| 322 |
|
| 323 |
def test15_subclassing(self): |
| 324 |
"""A sub-class on the python side should have that class as type""" |
| 325 |
|
| 326 |
import _cppyy, gc |
| 327 |
gc.collect() |
| 328 |
|
| 329 |
example01 = _cppyy.gbl.example01 |
| 330 |
|
| 331 |
assert example01.getCount() == 0 |
| 332 |
|
| 333 |
o = example01() |
| 334 |
assert type(o) == example01 |
| 335 |
assert example01.getCount() == 1 |
| 336 |
o.__destruct__() |
| 337 |
assert example01.getCount() == 0 |
| 338 |
|
| 339 |
class MyClass1(example01): |
| 340 |
def myfunc(self): |
| 341 |
return 1 |
| 342 |
|
| 343 |
o = MyClass1() |
| 344 |
assert type(o) == MyClass1 |
| 345 |
assert isinstance(o, example01) |
| 346 |
assert example01.getCount() == 1 |
| 347 |
assert o.myfunc() == 1 |
| 348 |
o.__destruct__() |
| 349 |
assert example01.getCount() == 0 |
| 350 |
|
| 351 |
class MyClass2(example01): |
| 352 |
def __init__(self, what): |
| 353 |
example01.__init__(self) |
| 354 |
self.what = what |
| 355 |
|
| 356 |
o = MyClass2('hi') |
| 357 |
assert type(o) == MyClass2 |
| 358 |
assert example01.getCount() == 1 |
| 359 |
assert o.what == 'hi' |
| 360 |
o.__destruct__() |
| 361 |
|
| 362 |
assert example01.getCount() == 0 |
| 363 |
|
| 364 |
|
| 365 |
class AppTestPYTHONIFY_UI: |
| 366 |
spaceconfig = dict(usemodules=['_cppyy', '_rawffi', 'itertools']) |
| 367 |
|
| 368 |
def setup_class(cls): |
| 369 |
cls.w_test_dct = cls.space.newtext(test_dct) |
| 370 |
cls.w_example01 = cls.space.appexec([], """(): |
| 371 |
import ctypes |
| 372 |
return ctypes.CDLL(%r, ctypes.RTLD_GLOBAL)""" % (test_dct, )) |
| 373 |
|
| 374 |
def test01_pythonizations(self): |
| 375 |
"""Test addition of user-defined pythonizations""" |
| 376 |
|
| 377 |
import _cppyy |
| 378 |
|
| 379 |
def example01a_pythonize(pyclass, name): |
| 380 |
if name == 'example01a': |
| 381 |
def getitem(self, idx): |
| 382 |
return self.addDataToInt(idx) |
| 383 |
pyclass.__getitem__ = getitem |
| 384 |
|
| 385 |
_cppyy.add_pythonization(example01a_pythonize) |
| 386 |
|
| 387 |
e = _cppyy.gbl.example01a(1) |
| 388 |
|
| 389 |
assert e[0] == 1 |
| 390 |
assert e[1] == 2 |
| 391 |
assert e[5] == 6 |
| 392 |
|
| 393 |
def test02_fragile_pythonizations(self): |
| 394 |
"""Test pythonizations error reporting""" |
| 395 |
|
| 396 |
import _cppyy |
| 397 |
|
| 398 |
example01_pythonize = 1 |
| 399 |
raises(TypeError, _cppyy.add_pythonization, 'example01', example01_pythonize) |
| 400 |
|
| 401 |
def test03_write_access_to_globals(self): |
| 402 |
"""Test overwritability of globals""" |
| 403 |
|
| 404 |
import _cppyy |
| 405 |
|
| 406 |
oldval = _cppyy.gbl.ns_example01.gMyGlobalInt |
| 407 |
assert oldval == 99 |
| 408 |
|
| 409 |
proxy = _cppyy.gbl.ns_example01.__class__.__dict__['gMyGlobalInt'] |
| 410 |
_cppyy.gbl.ns_example01.gMyGlobalInt = 3 |
| 411 |
assert proxy.__get__(proxy, None) == 3 |
| 412 |
|
| 413 |
_cppyy.gbl.ns_example01.gMyGlobalInt = oldval |