diff --git a/CMake/CMakeLists.txt b/CMake/CMakeLists.txt index a05f7f90d3..f077dac8f2 100644 --- a/CMake/CMakeLists.txt +++ b/CMake/CMakeLists.txt @@ -258,7 +258,7 @@ function(polymorphic_archive_test test_name) add_test(${amended_test_name} ${amended_test_name}) set(test_list ${test_list} ${amended_test_name} PARENT_SCOPE) endforeach() -endfunction(archive_test) +endfunction(polymorphic_archive_test) enable_testing() @@ -292,6 +292,9 @@ serialization_test(test_inclusion2) serialization_test(test_smart_cast) serialization_test(test_codecvt_null ../src/codecvt_null) serialization_test(test_strong_typedef) +serialization_test(test_singleton) +serialization_test(test_singleton_inherited) +serialization_test(test_singleton_plain) archive_test(test_native_array A) archive_test(test_boost_array A) diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index c152825785..891fb29355 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -150,6 +150,8 @@ if ! $(BOOST_ARCHIVE_LIST) { [ test-bsl-run test_smart_cast ] [ test-bsl-run test_codecvt_null ] [ test-bsl-run test_singleton ] + [ test-bsl-run test_singleton_inherited ] + [ test-bsl-run test_singleton_plain ] # [ test-bsl-run test_z ] diff --git a/test/test_singleton_inherited.cpp b/test/test_singleton_inherited.cpp new file mode 100644 index 0000000000..08164bcf3f --- /dev/null +++ b/test/test_singleton_inherited.cpp @@ -0,0 +1,81 @@ +/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 +// test_singleton_inherited.cpp: +// Test the singleton class for a "inherited" singleton (used as Foo:public singleton) +// This can be uses as singleton::get_const_instance() OR Foo::get_const_instance() +// +// - is_destroyed returns false when singleton is active or uninitialized +// - is_destroyed returns true when singleton is destructed +// - the singleton is eventually destructed (no memory leak) + +// (C) Copyright 2018 Alexander Grund +// Use, modification and distribution is subject to the Boost Software +// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#include "test_tools.hpp" +#include +#include +#include + +// Can't use BOOST_TEST because: +// a) destructors are called after program exit +// b) This is intended to be used by shared libraries too which would then need their own report_errors call +// We halso have to disable the Wterminate warning as we call this from dtors +// C++ will terminate the program in such cases which is OK here +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wterminate" +#define THROW_ON_FALSE(cond) if(!(cond)) throw std::runtime_error(__FILE__ "(" BOOST_PP_STRINGIZE(__LINE__) ") Assertion failed: " #cond) + +// Enum to designate the state of the singletonized instances +enum ConstructionState{CS_UNINIT, CS_INIT, CS_DESTROYED}; + +// We need another singleton to check for the destruction of the singletons at program exit +// We don't need all the magic for shared library anti-optimization and can keep it very simple +struct controller{ + static controller& getInstance(){ + static controller instance; + return instance; + } + ConstructionState state; +private: + controller() { + state = CS_UNINIT; + } + ~controller(); +}; + +// A simple class that sets its construction state in the controller singleton +struct Foo: boost::serialization::singleton{ + Foo(): i(42) { + // access controller singleton. Therefore controller will be constructed before this + THROW_ON_FALSE(controller::getInstance().state == CS_UNINIT); + controller::getInstance().state = CS_INIT; + } + ~Foo() { + // Because controller is constructed before this, it will be destructed AFTER this. Hence controller is still valid + THROW_ON_FALSE(controller::getInstance().state == CS_INIT); + controller::getInstance().state = CS_DESTROYED; + } + // Volatile to prevent compiler optimization from removing this + volatile int i; +}; + +controller::~controller() { + // If this fails, the singletons were not freed and memory is leaked + THROW_ON_FALSE(state == CS_DESTROYED); + // If this fails, then the destroyed flag is not set and one may use a deleted instance if relying on this flag + THROW_ON_FALSE(boost::serialization::singleton::is_destroyed()); + THROW_ON_FALSE(Foo::is_destroyed()); +} + +int +test_main( int /* argc */, char* /* argv */[] ) +{ + // Check if the singleton is alive and use it + BOOST_CHECK(!boost::serialization::singleton::is_destroyed()); + BOOST_CHECK(!Foo::is_destroyed()); + + BOOST_CHECK(boost::serialization::singleton::get_const_instance().i == 42); + BOOST_CHECK(Foo::get_const_instance().i == 42); + return EXIT_SUCCESS; +} diff --git a/test/test_singleton_plain.cpp b/test/test_singleton_plain.cpp new file mode 100644 index 0000000000..7c3061415f --- /dev/null +++ b/test/test_singleton_plain.cpp @@ -0,0 +1,77 @@ +/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 +// test_singleton_plain.cpp: +// Test the singleton class for a "plain" singleton (used as singleton) +// +// - is_destroyed returns false when singleton is active or uninitialized +// - is_destroyed returns true when singleton is destructed +// - the singleton is eventually destructed (no memory leak) + +// (C) Copyright 2018 Alexander Grund +// Use, modification and distribution is subject to the Boost Software +// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#include "test_tools.hpp" +#include +#include +#include + +// Can't use BOOST_TEST because: +// a) destructors are called after program exit +// b) This is intended to be used by shared libraries too which would then need their own report_errors call +// We halso have to disable the Wterminate warning as we call this from dtors +// C++ will terminate the program in such cases which is OK here +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wterminate" +#define THROW_ON_FALSE(cond) if(!(cond)) throw std::runtime_error(__FILE__ "(" BOOST_PP_STRINGIZE(__LINE__) ") Assertion failed: " #cond) + +// Enum to designate the state of the singletonized instances +enum ConstructionState{CS_UNINIT, CS_INIT, CS_DESTROYED}; + +// We need another singleton to check for the destruction of the singletons at program exit +// We don't need all the magic for shared library anti-optimization and can keep it very simple +struct controller{ + static controller& getInstance(){ + static controller instance; + return instance; + } + ConstructionState state; +private: + controller() { + state = CS_UNINIT; + } + ~controller(); +}; + +// A simple class that sets its construction state in the controller singleton +struct Foo{ + Foo(): i(42) { + // access controller singleton. Therefore controller will be constructed before this + THROW_ON_FALSE(controller::getInstance().state == CS_UNINIT); + controller::getInstance().state = CS_INIT; + } + ~Foo() { + // Because controller is constructed before this, it will be destructed AFTER this. Hence controller is still valid + THROW_ON_FALSE(controller::getInstance().state == CS_INIT); + controller::getInstance().state = CS_DESTROYED; + } + // Volatile to prevent compiler optimization from removing this + volatile int i; +}; + +controller::~controller() { + // If this fails, the singletons were not freed and memory is leaked + THROW_ON_FALSE(state == CS_DESTROYED); + // If this fails, then the destroyed flag is not set and one may use a deleted instance if relying on this flag + THROW_ON_FALSE(boost::serialization::singleton::is_destroyed()); +} + +int +test_main( int /* argc */, char* /* argv */[] ) +{ + // Check if the singleton is alive and use it + BOOST_CHECK(!boost::serialization::singleton::is_destroyed()); + + BOOST_CHECK(boost::serialization::singleton::get_const_instance().i == 42); + return EXIT_SUCCESS; +}