Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 73 additions & 1 deletion roottest/python/cpp/PyROOT_cpptests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -17,24 +18,25 @@
upstream equivalents.
"""

import sys, os, unittest

Check failure on line 21 in roottest/python/cpp/PyROOT_cpptests.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (I001)

roottest/python/cpp/PyROOT_cpptests.py:21:1: I001 Import block is un-sorted or un-formatted help: Organize imports

Check failure on line 21 in roottest/python/cpp/PyROOT_cpptests.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (E401)

roottest/python/cpp/PyROOT_cpptests.py:21:1: E401 Multiple imports on one line help: Split imports
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
from sys import maxsize

import ROOT
from ROOT import TObject, TLorentzVector, TVectorF, TROOT, gROOT, TMatrixD, TString, std

Check failure on line 26 in roottest/python/cpp/PyROOT_cpptests.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (F401)

roottest/python/cpp/PyROOT_cpptests.py:26:18: F401 `ROOT.TObject` imported but unused help: Remove unused import: `ROOT.TObject`
from ROOT import MakeNullPointer, AsCObject, BindObject, AddressOf, addressof

Check failure on line 27 in roottest/python/cpp/PyROOT_cpptests.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (F401)

roottest/python/cpp/PyROOT_cpptests.py:27:58: F401 `ROOT.AddressOf` imported but unused help: Remove unused import: `ROOT.AddressOf`
from common import *

Check failure on line 28 in roottest/python/cpp/PyROOT_cpptests.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (F403)

roottest/python/cpp/PyROOT_cpptests.py:28:1: F403 `from common import *` used; unable to detect undefined names

Check failure on line 28 in roottest/python/cpp/PyROOT_cpptests.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (I001)

roottest/python/cpp/PyROOT_cpptests.py:23:1: I001 Import block is un-sorted or un-formatted help: Organize imports

__all__ = [
'Cpp1LanguageFeatureTestCase',
'Cpp2ClassNamingTestCase',
'Cpp3UsingDeclarations',
'Cpp4InheritanceTreeOverloadResolution',
'Cpp5TemplatedFunctionCorrectTemplateArguments'
]

### C++ language constructs test cases =======================================
class Cpp1LanguageFeatureTestCase( MyTestCase ):

Check failure on line 39 in roottest/python/cpp/PyROOT_cpptests.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (F405)

roottest/python/cpp/PyROOT_cpptests.py:39:36: F405 `MyTestCase` may be undefined, or defined from star imports
# NOTE: test04NsEnumType and test06ScopedEnum are kept although covered by
# cppyy's test_datatypes.py::test12_enum_scopes, because that upstream test
# is currently marked xfail.
Expand Down Expand Up @@ -140,7 +142,7 @@
def test14OpaquePointerPassing( self ):
"""Test passing around of opaque pointers"""

import ROOT

Check failure on line 145 in roottest/python/cpp/PyROOT_cpptests.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (F401)

roottest/python/cpp/PyROOT_cpptests.py:145:14: F401 `ROOT` imported but unused help: Remove unused import: `ROOT`

s = TString( "Hello World!" )
co = AsCObject( s )
Expand Down Expand Up @@ -180,7 +182,7 @@


### C++ language naming of classes ===========================================
class Cpp2ClassNamingTestCase( MyTestCase ):

Check failure on line 185 in roottest/python/cpp/PyROOT_cpptests.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (F405)

roottest/python/cpp/PyROOT_cpptests.py:185:32: F405 `MyTestCase` may be undefined, or defined from star imports
def test03NamespaceInTemplates( self ):
"""Templated data members need to retain namespaces of arguments"""

Expand All @@ -193,7 +195,7 @@


### C++ language using declarations ===========================================
class Cpp3UsingDeclarations( MyTestCase ):

Check failure on line 198 in roottest/python/cpp/PyROOT_cpptests.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (F405)

roottest/python/cpp/PyROOT_cpptests.py:198:30: F405 `MyTestCase` may be undefined, or defined from star imports
def test1TGraphMultiErrorsSetLineColor(self):
"""Test that a using function declaration is picked up by the overload resolution"""

Expand Down Expand Up @@ -267,6 +269,76 @@
# 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<size_t> elements_size;
std::vector<std::string> column_names;
std::vector<void*> columns;
bool owned = true;
ERDataFrame(size_t s) : rows(s) {}
~ERDataFrame() {
if (!owned) return;
for (void* i : columns) free(i);
}
template <typename R, typename... T>
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<R*>(columns.back()) + i;
size_t I = 0;
// Collect arguments safely in sequence
auto args = std::tuple{(*(static_cast<T*>(columns[I++]) + i))...};
*res = std::apply(f, args);
}
owned = false;
return *this;
}

template <typename C, size_t N>
std::string print() {
std::string out = column_names[N];
out += ": [";
for (size_t i = 0; i < rows; i++) {
out += *(static_cast<C*>(columns[N]) + i);
out += ", ";
}
out += "]";
return out;
}
};

template<size_t N>
std::string print(ERDataFrame &df) {
return df.print<double, N>();
}

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__':
Expand Down
Loading