1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
// (C) Copyright 2006-7 Anthony Williams
// Distributed under 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 <boost/test/unit_test.hpp>
#include <boost/test/test_case_template.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/recursive_mutex.hpp>
template<typename Mutex,typename Lock>
struct test_initially_locked
{
void operator()() const
{
Mutex m;
Lock lock(m);
BOOST_CHECK(lock);
BOOST_CHECK(lock.owns_lock());
}
};
template<typename Mutex,typename Lock>
struct test_initially_unlocked_with_defer_lock_parameter
{
void operator()() const
{
Mutex m;
Lock lock(m,boost::defer_lock);
BOOST_CHECK(!lock);
BOOST_CHECK(!lock.owns_lock());
}
};
template<typename Mutex,typename Lock>
struct test_initially_locked_with_adopt_lock_parameter
{
void operator()() const
{
Mutex m;
m.lock();
Lock lock(m,boost::adopt_lock);
BOOST_CHECK(lock);
BOOST_CHECK(lock.owns_lock());
}
};
template<typename Mutex,typename Lock>
struct test_unlocked_after_unlock_called
{
void operator()() const
{
Mutex m;
Lock lock(m);
lock.unlock();
BOOST_CHECK(!lock);
BOOST_CHECK(!lock.owns_lock());
}
};
template<typename Mutex,typename Lock>
struct test_locked_after_lock_called
{
void operator()() const
{
Mutex m;
Lock lock(m,boost::defer_lock);
lock.lock();
BOOST_CHECK(lock);
BOOST_CHECK(lock.owns_lock());
}
};
template<typename Mutex,typename Lock>
struct test_locked_after_try_lock_called
{
void operator()() const
{
Mutex m;
Lock lock(m,boost::defer_lock);
lock.try_lock();
BOOST_CHECK(lock);
BOOST_CHECK(lock.owns_lock());
}
};
template<typename Mutex,typename Lock>
struct test_throws_if_lock_called_when_already_locked
{
void operator()() const
{
Mutex m;
Lock lock(m);
BOOST_CHECK_THROW( lock.lock(), boost::lock_error );
}
};
template<typename Mutex,typename Lock>
struct test_throws_if_try_lock_called_when_already_locked
{
void operator()() const
{
Mutex m;
Lock lock(m);
BOOST_CHECK_THROW( lock.try_lock(), boost::lock_error );
}
};
template<typename Mutex,typename Lock>
struct test_throws_if_unlock_called_when_already_unlocked
{
void operator()() const
{
Mutex m;
Lock lock(m);
lock.unlock();
BOOST_CHECK_THROW( lock.unlock(), boost::lock_error );
}
};
BOOST_TEST_CASE_TEMPLATE_FUNCTION(test_scoped_lock_concept,Mutex)
{
typedef typename Mutex::scoped_lock Lock;
test_initially_locked<Mutex,Lock>()();
test_initially_unlocked_with_defer_lock_parameter<Mutex,Lock>()();
test_initially_locked_with_adopt_lock_parameter<Mutex,Lock>()();
test_unlocked_after_unlock_called<Mutex,Lock>()();
test_locked_after_lock_called<Mutex,Lock>()();
test_throws_if_lock_called_when_already_locked<Mutex,Lock>()();
test_throws_if_unlock_called_when_already_unlocked<Mutex,Lock>()();
}
BOOST_TEST_CASE_TEMPLATE_FUNCTION(test_scoped_try_lock_concept,Mutex)
{
typedef typename Mutex::scoped_try_lock Lock;
test_initially_locked<Mutex,Lock>()();
test_initially_unlocked_with_defer_lock_parameter<Mutex,Lock>()();
test_initially_locked_with_adopt_lock_parameter<Mutex,Lock>()();
test_unlocked_after_unlock_called<Mutex,Lock>()();
test_locked_after_lock_called<Mutex,Lock>()();
test_locked_after_try_lock_called<Mutex,Lock>()();
test_throws_if_lock_called_when_already_locked<Mutex,Lock>()();
test_throws_if_try_lock_called_when_already_locked<Mutex,Lock>()();
test_throws_if_unlock_called_when_already_unlocked<Mutex,Lock>()();
}
boost::unit_test_framework::test_suite* init_unit_test_suite(int, char*[])
{
boost::unit_test_framework::test_suite* test =
BOOST_TEST_SUITE("Boost.Threads: lock concept test suite");
typedef boost::mpl::vector<boost::mutex,boost::try_mutex,boost::timed_mutex,
boost::recursive_mutex,boost::recursive_try_mutex,boost::recursive_timed_mutex> mutex_types;
test->add(BOOST_TEST_CASE_TEMPLATE(test_scoped_lock_concept,mutex_types));
typedef boost::mpl::vector<boost::try_mutex,boost::timed_mutex,
boost::recursive_try_mutex,boost::recursive_timed_mutex> try_mutex_types;
test->add(BOOST_TEST_CASE_TEMPLATE(test_scoped_try_lock_concept,try_mutex_types));
return test;
}
|