Files
revng-revng/tests/Unit/classsentinel.cpp
T
Alessandro Di Federico 8c148607d3 Introduce ClassSentinel
Sometimes, dealing with move-semantics in C++ can be challenging. To
mitigate this problem, this commit introduces `ClassSentinel`, a simple
class that can be added as a member of a class and that will allow the
user to easily monitor if an instance of a class is used after being
moved in an unwanted way. To do so, simply call the
`ClassSentinel::check` method on the class member.

`ClassSentinel` performs similar (but less reliable) checks on usage of
destroyed objects.

`ClassSentinel` can also (optionally, though the `SENTINEL_STACKTRACES`
macro) collect stack traces of the points where the object was moved or
destroyed.

This commit also includes some basic testing for the class.
2018-05-30 12:45:45 +02:00

39 lines
813 B
C++

/// \file sentinel.cpp
/// \brief Tests for ClassSentinel
//
// This file is distributed under the MIT License. See LICENSE.md for details.
//
// Standard includes
// Boost includes
#define BOOST_TEST_MODULE StackAnalysis
#include <boost/test/unit_test.hpp>
// Local includes
#include "classsentinel.h"
struct TestClass {
ClassSentinel Sentinel;
};
BOOST_AUTO_TEST_CASE(Sentinel) {
TestClass *DanglingPointer = nullptr;
{
TestClass Instance;
BOOST_TEST(!Instance.Sentinel.isMoved());
BOOST_TEST(!Instance.Sentinel.isDestroyed());
TestClass OtherInstance = std::move(Instance);
BOOST_TEST(Instance.Sentinel.isMoved());
BOOST_TEST(!Instance.Sentinel.isDestroyed());
DanglingPointer = &Instance;
}
BOOST_TEST(DanglingPointer->Sentinel.isDestroyed());
}