| 1 |
import py, os, sys |
| 2 |
|
| 3 |
|
| 4 |
currpath = py.path.local(__file__).dirpath() |
| 5 |
test_dct = str(currpath.join("overloadsDict.so")) |
| 6 |
|
| 7 |
def setup_module(mod): |
| 8 |
if sys.platform == 'win32': |
| 9 |
py.test.skip("win32 not supported so far") |
| 10 |
err = os.system("cd '%s' && make overloadsDict.so" % currpath) |
| 11 |
if err: |
| 12 |
raise OSError("'make' failed (see stderr)") |
| 13 |
|
| 14 |
class AppTestOVERLOADS: |
| 15 |
spaceconfig = dict(usemodules=['_cppyy', '_rawffi', 'itertools']) |
| 16 |
|
| 17 |
def setup_class(cls): |
| 18 |
cls.w_test_dct = cls.space.newtext(test_dct) |
| 19 |
cls.w_overloads = cls.space.appexec([], """(): |
| 20 |
import ctypes, _cppyy |
| 21 |
_cppyy._post_import_startup() |
| 22 |
return ctypes.CDLL(%r, ctypes.RTLD_GLOBAL)""" % (test_dct, )) |
| 23 |
|
| 24 |
def test01_class_based_overloads(self): |
| 25 |
"""Test functions overloaded on different C++ clases""" |
| 26 |
|
| 27 |
import _cppyy |
| 28 |
a_overload = _cppyy.gbl.a_overload |
| 29 |
b_overload = _cppyy.gbl.b_overload |
| 30 |
c_overload = _cppyy.gbl.c_overload |
| 31 |
d_overload = _cppyy.gbl.d_overload |
| 32 |
|
| 33 |
ns_a_overload = _cppyy.gbl.ns_a_overload |
| 34 |
ns_b_overload = _cppyy.gbl.ns_b_overload |
| 35 |
|
| 36 |
assert c_overload().get_int(a_overload()) == 42 |
| 37 |
assert c_overload().get_int(b_overload()) == 13 |
| 38 |
assert d_overload().get_int(a_overload()) == 42 |
| 39 |
assert d_overload().get_int(b_overload()) == 13 |
| 40 |
|
| 41 |
assert c_overload().get_int(ns_a_overload.a_overload()) == 88 |
| 42 |
assert c_overload().get_int(ns_b_overload.a_overload()) == -33 |
| 43 |
|
| 44 |
assert d_overload().get_int(ns_a_overload.a_overload()) == 88 |
| 45 |
assert d_overload().get_int(ns_b_overload.a_overload()) == -33 |
| 46 |
|
| 47 |
def test02_class_based_overloads_explicit_resolution(self): |
| 48 |
"""Test explicitly resolved function overloads""" |
| 49 |
|
| 50 |
import _cppyy |
| 51 |
a_overload = _cppyy.gbl.a_overload |
| 52 |
b_overload = _cppyy.gbl.b_overload |
| 53 |
c_overload = _cppyy.gbl.c_overload |
| 54 |
d_overload = _cppyy.gbl.d_overload |
| 55 |
|
| 56 |
ns_a_overload = _cppyy.gbl.ns_a_overload |
| 57 |
|
| 58 |
c = c_overload() |
| 59 |
raises(TypeError, c.__dispatch__, 'get_int', 12) |
| 60 |
raises(LookupError, c.__dispatch__, 'get_int', 'does_not_exist') |
| 61 |
assert c.__dispatch__('get_int', 'a_overload*')(a_overload()) == 42 |
| 62 |
assert c_overload.get_int.__overload__('a_overload*')(c, a_overload()) == 42 |
| 63 |
assert c.__dispatch__('get_int', 'b_overload*')(b_overload()) == 13 |
| 64 |
assert c_overload.get_int.__overload__('b_overload*')(c, b_overload()) == 13 |
| 65 |
|
| 66 |
assert c_overload().__dispatch__('get_int', 'a_overload*')(a_overload()) == 42 |
| 67 |
|
| 68 |
|
| 69 |
d = d_overload() |
| 70 |
assert d.__dispatch__('get_int', 'a_overload*')(a_overload()) == 42 |
| 71 |
assert d_overload.get_int.__overload__('a_overload*')(d, a_overload()) == 42 |
| 72 |
assert d.__dispatch__('get_int', 'b_overload*')(b_overload()) == 13 |
| 73 |
assert d_overload.get_int.__overload__('b_overload*')(d, b_overload()) == 13 |
| 74 |
|
| 75 |
nb = ns_a_overload.b_overload() |
| 76 |
raises(TypeError, nb.f, c_overload()) |
| 77 |
|
| 78 |
def test03_fragile_class_based_overloads(self): |
| 79 |
"""Test functions overloaded on void* and non-existing classes""" |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
import _cppyy |
| 84 |
more_overloads = _cppyy.gbl.more_overloads |
| 85 |
aa_ol = _cppyy.gbl.aa_ol |
| 86 |
|
| 87 |
cc_ol = _cppyy.gbl.cc_ol |
| 88 |
|
| 89 |
|
| 90 |
assert more_overloads().call(aa_ol()) == "aa_ol" |
| 91 |
|
| 92 |
assert more_overloads().call(cc_ol()) == "cc_ol" |
| 93 |
|
| 94 |
|
| 95 |
def test04_fully_fragile_overloads(self): |
| 96 |
"""Test that unknown* is preferred over unknown&""" |
| 97 |
|
| 98 |
|
| 99 |
return |
| 100 |
|
| 101 |
import _cppyy |
| 102 |
more_overloads2 = _cppyy.gbl.more_overloads2 |
| 103 |
bb_ol = _cppyy.gbl.bb_ol |
| 104 |
dd_ol = _cppyy.gbl.dd_ol |
| 105 |
|
| 106 |
assert more_overloads2().call(bb_ol()) == "bb_olptr" |
| 107 |
assert more_overloads2().call(dd_ol(), 1) == "dd_olptr" |
| 108 |
|
| 109 |
def test05_array_overloads(self): |
| 110 |
"""Test functions overloaded on different arrays""" |
| 111 |
|
| 112 |
import _cppyy |
| 113 |
c_overload = _cppyy.gbl.c_overload |
| 114 |
d_overload = _cppyy.gbl.d_overload |
| 115 |
|
| 116 |
from array import array |
| 117 |
|
| 118 |
ai = array('i', [525252]) |
| 119 |
assert c_overload().get_int(ai) == 525252 |
| 120 |
assert d_overload().get_int(ai) == 525252 |
| 121 |
|
| 122 |
ah = array('h', [25]) |
| 123 |
assert c_overload().get_int(ah) == 25 |
| 124 |
assert d_overload().get_int(ah) == 25 |
| 125 |
|
| 126 |
def test06_double_int_overloads(self): |
| 127 |
"""Test overloads on int/doubles""" |
| 128 |
|
| 129 |
import _cppyy |
| 130 |
more_overloads = _cppyy.gbl.more_overloads |
| 131 |
|
| 132 |
assert more_overloads().call(1) == "int" |
| 133 |
assert more_overloads().call(1.) == "double" |
| 134 |
assert more_overloads().call1(1) == "int" |
| 135 |
assert more_overloads().call1(1.) == "double" |
| 136 |
|
| 137 |
def test07_mean_overloads(self): |
| 138 |
"""Adapted test for array overloading""" |
| 139 |
|
| 140 |
import _cppyy, array |
| 141 |
cmean = _cppyy.gbl.calc_mean |
| 142 |
|
| 143 |
numbers = [8, 2, 4, 2, 4, 2, 4, 4, 1, 5, 6, 3, 7] |
| 144 |
mean, median = 4.0, 4.0 |
| 145 |
|
| 146 |
for l in ['f', 'd', 'i', 'h', 'l']: |
| 147 |
a = array.array(l, numbers) |
| 148 |
assert round(cmean(len(a), a) - mean, 8) == 0 |