5 Check that ROOT returns the correct values (ints, floats) when calling a C++
16 "struct ReturnValueTests {"
21 integral_types = [8, 16, 32, 64]
23 floating_types = [
"float",
"double"]
25 floating_expressions = {
26 -np.inf:
"-std::numeric_limits<{0}>::infinity()",
27 np.inf:
"std::numeric_limits<{0}>::infinity()",
28 np.nan:
"std::numeric_limits<{0}>::quiet_NaN()",
31 for width
in integral_types:
33 dtype = np.dtype(
"int%d" % width)
34 for i, value
in enumerate([-2**(width-1), -1, 0, 1, 2**(width-1) - 1]):
36 func_name =
"int{0}test{1}".format(width, i)
38 prefix =
"ull" if width > 32
else ""
40 testclass.append(
"int{0}_t {1}() const {{ return {2}{3}; }}".format(width, func_name, value, prefix))
41 testclass.append(
"int{0}_t& ref_{1}() {{ static int{0}_t val = {2}{3}; return val; }}".format(
42 width, func_name, value, prefix))
44 results.append([func_name, value, dtype])
45 results.append([
'ref_' + func_name, value, dtype])
48 dtype = np.dtype(
"uint%d" % width)
49 for i, value
in enumerate([0, 1, 2**(width) - 1]):
51 func_name =
"uint{0}test{1}".format(width, i)
53 prefix =
"ull" if width > 32
else ""
55 testclass.append(
"uint{0}_t {1}() const {{ return {2}ull; }}".format(width, func_name, value, prefix))
56 testclass.append(
"uint{0}_t& ref_{1}() {{ static uint{0}_t val = {2}{3}; return val; }}".format(
57 width, func_name, value, prefix))
59 results.append([func_name, value, dtype])
60 results.append([
'ref_' + func_name, value, dtype])
63 for t
in floating_types:
68 for i, value
in enumerate([-np.inf, info.min, -1, 0, info.tiny, info.eps, 1, info.max, np.inf, np.nan]):
69 func_name =
"{0}test{1}".format(t, i)
71 expression = repr(value)
if value
not in floating_expressions
else floating_expressions[value].format(t)
72 testclass.append(
"{0} {1}() const {{ return {2}; }}".format(t, func_name, expression))
73 testclass.append(
"{0}& ref_{1}() {{ static {0} val = {2}; return val; }}".format(t, func_name, expression))
74 results.append([func_name, value, info.dtype])
75 results.append([
'ref_' + func_name, value, info.dtype])
79 testclass.append(
"};")
80 ROOT.gROOT.ProcessLine(
"\n".join(testclass))
82 tests = ROOT.ReturnValueTests()
85 for func, value, dtype
in results:
86 ret = getattr(tests, func)()
90 if isinstance(ret, str):
91 ret = np.fromstring(ret.encode(
"latin1"), dtype)[0]
94 print(
"check %s(): %r == %r: " % (func, value, ret), end=
"")
97 passed = np.isnan(ret)
99 passed = (ret == value)
101 print(
"\033[32mOK\033[0m" if passed
else "\033[31mFAIL\033[0m")