-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Sébastien LAIGRE <[email protected]>
- Loading branch information
Showing
7 changed files
with
100 additions
and
4 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
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
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 |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
|
||
set(UNIT_TEST_SOURCES | ||
stdcxx.cpp | ||
CastTest.cpp | ||
DateTimeTest.cpp | ||
DemangleTest.cpp | ||
MathTest.cpp | ||
|
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,84 @@ | ||
/** | ||
* Copyright (c) 2021, RTE (http://www.rte-france.com) | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
#include <memory> | ||
|
||
#include <boost/test/unit_test.hpp> | ||
|
||
#include <powsybl/stdcxx/cast.hpp> | ||
#include <powsybl/stdcxx/make_unique.hpp> | ||
|
||
namespace stdcxx { | ||
|
||
BOOST_AUTO_TEST_SUITE(CaseTestSuite) | ||
|
||
class BaseClass { | ||
public: | ||
BaseClass() = default; | ||
|
||
virtual ~BaseClass() = default; | ||
|
||
int getBaseAttr() const; | ||
|
||
void setBaseAttr(int newValue); | ||
|
||
private: | ||
int m_baseAttr = 1; | ||
}; | ||
|
||
int BaseClass::getBaseAttr() const { | ||
return m_baseAttr; | ||
} | ||
|
||
void BaseClass::setBaseAttr(int newValue) { | ||
m_baseAttr = newValue; | ||
} | ||
|
||
class DerivedClass : public BaseClass { | ||
public: | ||
DerivedClass() = default; | ||
|
||
~DerivedClass() override = default; | ||
|
||
int getDerivedAttr() const; | ||
|
||
void setDerivedAttr(int newValue); | ||
|
||
private: | ||
int m_derivedAttr = 2; | ||
}; | ||
|
||
int DerivedClass::getDerivedAttr() const { | ||
return m_derivedAttr; | ||
} | ||
|
||
void DerivedClass::setDerivedAttr(int newValue) { | ||
m_derivedAttr = newValue; | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE(unique_ptrCastTest) { | ||
std::unique_ptr<BaseClass> basePtr = stdcxx::make_unique<DerivedClass>(); | ||
BOOST_CHECK_EQUAL(1, basePtr->getBaseAttr()); | ||
basePtr->setBaseAttr(11); | ||
BOOST_CHECK_EQUAL(11, basePtr->getBaseAttr()); | ||
|
||
std::unique_ptr<DerivedClass> derivedPtr = stdcxx::downcast<DerivedClass>(basePtr); | ||
BOOST_CHECK(!basePtr); | ||
BOOST_CHECK_EQUAL(11, derivedPtr->getBaseAttr()); | ||
BOOST_CHECK_EQUAL(2, derivedPtr->getDerivedAttr()); | ||
derivedPtr->setBaseAttr(111); | ||
derivedPtr->setDerivedAttr(22); | ||
BOOST_CHECK_EQUAL(22, derivedPtr->getDerivedAttr()); | ||
|
||
std::unique_ptr<BaseClass> otherBasePtr = stdcxx::upcast<BaseClass>(derivedPtr); | ||
BOOST_CHECK(!derivedPtr); | ||
BOOST_CHECK_EQUAL(111, otherBasePtr->getBaseAttr()); | ||
} | ||
|
||
BOOST_AUTO_TEST_SUITE_END() | ||
|
||
} // namespace stdcxx |