-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PyROOT] Add tests of automatic injection of move
Test that we automatically inject an `std::move` in the TClingCallFunc wrapper call in cases where a copy constructor is not available for the class.
- Loading branch information
1 parent
4784b0d
commit 1c7a439
Showing
3 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import unittest | ||
import ROOT | ||
|
||
|
||
class CallFuncWrappers(unittest.TestCase): | ||
""" | ||
Tests for pass-by-value semantics of classes with deleted copy constructor | ||
""" | ||
|
||
def test_pass_by_value_unique_ptr(self): | ||
""" | ||
It is possible to pass a unique_ptr by value in a function call, | ||
whether it is a default argument or not | ||
""" | ||
ROOT.gInterpreter.Declare(r''' | ||
struct uptrtype{ | ||
int mVal{42}; | ||
uptrtype() {} | ||
uptrtype(int val): mVal(val) {} | ||
}; | ||
int foo(std::unique_ptr<uptrtype> ptr = std::make_unique<uptrtype>()) { return ptr->mVal; } | ||
''') | ||
|
||
# The correct value is returned by the default argument | ||
self.assertEqual(ROOT.foo(), 42) | ||
|
||
# Creating a unique_ptr explicitly is also valid | ||
ptr = ROOT.std.make_unique[ROOT.uptrtype](33) | ||
self.assertEqual(ROOT.foo(ptr), 33) | ||
|
||
def test_pass_by_value_templated_move_constructor(self): | ||
""" | ||
It is possible to pass a class with a deleted copy constructor | ||
and a templated move constructor in a function by value | ||
""" | ||
ROOT.gInterpreter.Declare(r''' | ||
struct templmovetype{ | ||
int mVal{42}; | ||
templmovetype() {} | ||
templmovetype(int val): mVal(val) {} | ||
templmovetype(const templmovetype&) = delete; | ||
template<typename T = int> | ||
templmovetype(templmovetype &&other): mVal(other.mVal) {} | ||
}; | ||
int foo(templmovetype val = templmovetype{}) { return val.mVal; } | ||
''') | ||
|
||
# The correct value is returned by the default argument | ||
self.assertEqual(ROOT.foo(), 42) | ||
|
||
val = ROOT.templmovetype(33) | ||
self.assertEqual(ROOT.foo(val), 33) | ||
|
||
self.assertEqual(ROOT.foo(ROOT.templmovetype(55)), 55) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters