15 Check that ROOT returns the correct values (ints, floats) when calling a C++
26 "struct ReturnValueTests {"
31 integral_types = [8, 16, 32, 64]
33 floating_types = [
"float",
"double"]
35 floating_expressions = {
36 -np.inf:
"-std::numeric_limits<{0}>::infinity()",
37 np.inf:
"std::numeric_limits<{0}>::infinity()",
38 np.nan:
"std::numeric_limits<{0}>::quiet_NaN()",
41 for width
in integral_types:
43 dtype = np.dtype(
"int%d" % width)
44 for i, value
in enumerate([-2**(width - 1), -1, 0, 1, 2**(width - 1) - 1]):
46 func_name = f
"int{width}test{i}"
48 prefix =
"ull" if width > 32
else ""
50 testclass.append(f
"int{width}_t {func_name}() const {{ return {value}{prefix}; }}")
51 testclass.append(
"int{0}_t& ref_{1}() {{ static int{0}_t val = {2}{3}; return val; }}".format(
52 width, func_name, value, prefix))
54 results.append([func_name, value, dtype])
55 results.append([
'ref_' + func_name, value, dtype])
58 dtype = np.dtype(
"uint%d" % width)
59 for i, value
in enumerate([0, 1, 2**(width) - 1]):
61 func_name = f
"uint{width}test{i}"
63 prefix =
"ull" if width > 32
else ""
65 testclass.append(f
"uint{width}_t {func_name}() const {{ return {value}{prefix}; }}")
66 testclass.append(f
"uint{width}_t& ref_{func_name}() {{ static uint{width}_t val = {value}{prefix}; return val; }}")
68 results.append([func_name, value, dtype])
69 results.append([
'ref_' + func_name, value, dtype])
72 for t
in floating_types:
77 for i, value
in enumerate([-np.inf, info.min, -1, 0, info.tiny, info.eps, 1, info.max, np.inf, np.nan]):
78 func_name = f
"{t}test{i}"
80 expression = repr(value)
if value
not in floating_expressions
else floating_expressions[value].format(t)
81 testclass.append(f
"{t} {func_name}() const {{ return {expression}; }}")
82 testclass.append(
"{0}& ref_{1}() {{ static {0} val = {2}; return val; }}".format(t, func_name, expression))
83 results.append([func_name, value, info.dtype])
84 results.append([
'ref_' + func_name, value, info.dtype])
88 testclass.append(
"};")
89 ROOT.gROOT.ProcessLine(
"\n".join(testclass))
91 tests = ROOT.ReturnValueTests()
94 for func, value, dtype
in results:
95 ret = getattr(tests, func)()
99 if isinstance(ret, str):
100 ret = np.fromstring(ret.encode(
"latin1"), dtype)[0]
103 print(f
"check {func}(): {value!r} == {ret!r}: ", end=
"")
106 passed = np.isnan(ret)
108 passed = (ret == value)
110 print(
"\033[32mOK\033[0m" if passed
else "\033[31mFAIL\033[0m")