15Check that ROOT returns the correct values (ints, floats) when calling a C++
26 "struct ReturnValueTests {"
31integral_types = [8, 16, 32, 64]
33floating_types = [
"float",
"double"]
35floating_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()",
41for width
in integral_types:
43 dtype = np.dtype(f
"int{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(f
"int{width}_t& ref_{func_name}() {{ static int{width}_t val = {value}{prefix}; return val; }}")
53 results.append([func_name, value, dtype])
54 results.append([
'ref_' + func_name, value, dtype])
57 dtype = np.dtype(f
"uint{width}")
58 for i, value
in enumerate([0, 1, 2**(width) - 1]):
60 func_name = f
"uint{width}test{i}"
62 prefix =
"ull" if width > 32
else ""
64 testclass.append(f
"uint{width}_t {func_name}() const {{ return {value}{prefix}; }}")
65 testclass.append(f
"uint{width}_t& ref_{func_name}() {{ static uint{width}_t val = {value}{prefix}; return val; }}")
67 results.append([func_name, value, dtype])
68 results.append([
'ref_' + func_name, value, dtype])
71for t
in floating_types:
76 for i, value
in enumerate([-np.inf, info.min, -1, 0, info.tiny, info.eps, 1, info.max, np.inf, np.nan]):
77 func_name = f
"{t}test{i}"
79 expression = repr(value)
if value
not in floating_expressions
else floating_expressions[value].format(t)
80 testclass.append(f
"{t} {func_name}() const {{ return {expression}; }}")
81 testclass.append(f
"{t}& ref_{func_name}() {{ static {t} val = {expression}; return val; }}")
82 results.append([func_name, value, info.dtype])
83 results.append([
'ref_' + func_name, value, info.dtype])
88ROOT.gROOT.ProcessLine(
"\n".join(testclass))
90tests = ROOT.ReturnValueTests()
93for func, value, dtype
in results:
94 ret = getattr(tests, func)()
98 if isinstance(ret, str):
99 ret = np.fromstring(ret.encode(
"latin1"), dtype)[0]
102 print(f
"check {func}(): {value!r} == {ret!r}: ", end=
"")
105 passed = np.isnan(ret)
107 passed = (ret == value)
109 print(
"\033[32mOK\033[0m" if passed
else "\033[31mFAIL\033[0m")