signals2/test/threading_models_test.cpp
2015-04-12 14:34:16 -04:00

91 lines
2.4 KiB
C++

// thread_safe_signals library
// basic test for alternate threading models
// Copyright Frank Mori Hess 2008
// 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)
// For more information, see http://www.boost.org
// note boost/test/minimal.hpp can cause windows.h to get included, which
// can screw up our checks of _WIN32_WINNT if it is included
// after boost/signals2/mutex.hpp. Frank Hess 2009-03-07.
#include <boost/test/minimal.hpp>
#include <boost/signals2.hpp>
#include <boost/thread/mutex.hpp>
// combiner that returns the number of slots invoked
struct slot_counter {
typedef unsigned result_type;
template<typename InputIterator>
unsigned operator()(InputIterator first, InputIterator last) const
{
unsigned count = 0;
for (; first != last; ++first)
{
try
{
*first;
++count;
}
catch(const boost::bad_weak_ptr &)
{}
}
return count;
}
};
void myslot()
{
}
template<typename signal_type>
void simple_test()
{
signal_type sig;
sig.connect(typename signal_type::slot_type(&myslot));
BOOST_CHECK(sig() == 1);
sig.disconnect(&myslot);
BOOST_CHECK(sig() == 0);
}
class recursion_checking_dummy_mutex
{
int recursion_count;
public:
recursion_checking_dummy_mutex(): recursion_count(0)
{}
void lock()
{
BOOST_REQUIRE(recursion_count == 0);
++recursion_count;
}
bool try_lock()
{
lock();
return true;
}
void unlock()
{
--recursion_count;
BOOST_REQUIRE(recursion_count == 0);
}
};
int test_main(int, char*[])
{
typedef boost::signals2::signal<void (), slot_counter, int, std::less<int>, boost::function<void ()>,
boost::function<void (const boost::signals2::connection &)>, recursion_checking_dummy_mutex> sig0_rc_type;
simple_test<sig0_rc_type>();
typedef boost::signals2::signal<void (), slot_counter, int, std::less<int>, boost::function<void ()>,
boost::function<void (const boost::signals2::connection &)>, boost::mutex> sig0_mt_type;
simple_test<sig0_mt_type>();
typedef boost::signals2::signal<void (), slot_counter, int, std::less<int>, boost::function<void ()>,
boost::function<void (const boost::signals2::connection &)>, boost::signals2::dummy_mutex> sig0_st_type;
simple_test<sig0_st_type>();
return 0;
}