From ed4d0cbb4df1cf41aa2dfe9ab749dcb88fe4c211 Mon Sep 17 00:00:00 2001 From: Danilo Piparo Date: Fri, 20 Nov 2026 13:15:02 +0100 Subject: [PATCH] [test] Add test for #20526 --- roottest/python/cpp/PyROOT_cpptests.py | 74 +++++++++++++++++++++++++- 1 file changed, 73 insertions(+), 1 deletion(-) diff --git a/roottest/python/cpp/PyROOT_cpptests.py b/roottest/python/cpp/PyROOT_cpptests.py index 7bb149f0a3af8..56c3b8e5c6e4c 100644 --- a/roottest/python/cpp/PyROOT_cpptests.py +++ b/roottest/python/cpp/PyROOT_cpptests.py @@ -2,8 +2,9 @@ # Author: Wim Lavrijsen (LBNL, WLavrijsen@lbl.gov) # Author: Enric Tejedor Saavedra # Author: Vincenzo Eduardo Padulano (CERN) +# Author: Danilo Piparo (CERN) # Created: 01/03/05 -# Last: 10/08/21 (MM-DD-YY) +# Last: 20/11/26 (MM-DD-YY) """C++ language interface unit tests for PyROOT package. @@ -31,6 +32,7 @@ 'Cpp2ClassNamingTestCase', 'Cpp3UsingDeclarations', 'Cpp4InheritanceTreeOverloadResolution', + 'Cpp5TemplatedFunctionCorrectTemplateArguments' ] ### C++ language constructs test cases ======================================= @@ -267,6 +269,76 @@ def test2TwoArgumentFunctionAmbiguous(self): # error: call to 'myfunc' is ambiguous self.assertEqual(ROOT.Cpp4.myfunc(ROOT.Cpp4.C(), ROOT.Cpp4.Z()), 1) +class Cpp5TemplatedFunctionCorrectTemplateArguments(MyTestCase): + """ + Tests correct instantiation of function template + """ + @classmethod + def setUpClass(cls): + ROOT.gInterpreter.Declare( + r""" +struct ERDataFrame { + size_t rows; + std::vector elements_size; + std::vector column_names; + std::vector columns; + bool owned = true; + ERDataFrame(size_t s) : rows(s) {} + ~ERDataFrame() { + if (!owned) return; + for (void* i : columns) free(i); + } + template + ERDataFrame Define(std::string name, R (*f)(T...)) { + //std::cout << "Adding column " << columns.size() + 1 << " " << name << std::endl; + column_names.push_back(name); + columns.push_back(new R[rows]); + for (size_t i = 0; i < rows; i++) { + R* res = static_cast(columns.back()) + i; + size_t I = 0; + // Collect arguments safely in sequence + auto args = std::tuple{(*(static_cast(columns[I++]) + i))...}; + *res = std::apply(f, args); + } + owned = false; + return *this; + } + + template + std::string print() { + std::string out = column_names[N]; + out += ": ["; + for (size_t i = 0; i < rows; i++) { + out += *(static_cast(columns[N]) + i); + out += ", "; + } + out += "]"; + return out; + } +}; + +template +std::string print(ERDataFrame &df) { + return df.print(); +} + +double get_one() { return 1.0; } +double plus_one(double x) { return x + 1.0; } +double add(double x, double y) { return x + y; } +double add3(double x, double y, double z) { return x + y + z; } + """) + def testArguments(self): + df = ( + ROOT.ERDataFrame(1) + .Define("one", ROOT.get_one) + .Define("two", ROOT.plus_one) + .Define("add", ROOT.add) + .Define("add3", ROOT.add3)) + + self.assertEqual(ROOT.print[0](df), "one: [\x01, ]") + self.assertEqual(ROOT.print[1](df), "two: [\x02, ]") + self.assertEqual(ROOT.print[2](df), "add: [\x03, ]") + self.assertEqual(ROOT.print[3](df), "add3: [\x06, ]") ## actual test run if __name__ == '__main__':