2012-11-06 14:17:32 +00:00
|
|
|
/*
|
2017-03-06 01:18:16 -05:00
|
|
|
Copyright 2012-2015 Glen Joseph Fernandes
|
|
|
|
(glenjofe@gmail.com)
|
2015-11-09 22:35:34 -05:00
|
|
|
|
2017-03-06 01:18:16 -05:00
|
|
|
Distributed under the Boost Software License, Version 1.0.
|
|
|
|
(http://www.boost.org/LICENSE_1_0.txt)
|
2015-11-09 22:35:34 -05:00
|
|
|
*/
|
|
|
|
#include <boost/core/lightweight_test.hpp>
|
|
|
|
#include <boost/smart_ptr/make_shared.hpp>
|
2012-11-06 14:17:32 +00:00
|
|
|
|
2017-06-20 20:03:52 -04:00
|
|
|
template<class T = void>
|
2017-03-06 01:18:16 -05:00
|
|
|
struct creator {
|
|
|
|
typedef T value_type;
|
|
|
|
|
|
|
|
template<class U>
|
|
|
|
struct rebind {
|
|
|
|
typedef creator<U> other;
|
|
|
|
};
|
|
|
|
|
|
|
|
creator() { }
|
|
|
|
|
|
|
|
template<class U>
|
|
|
|
creator(const creator<U>&) { }
|
|
|
|
|
|
|
|
T* allocate(std::size_t size) {
|
|
|
|
return static_cast<T*>(::operator new(sizeof(T) * size));
|
|
|
|
}
|
|
|
|
|
|
|
|
void deallocate(T* ptr, std::size_t) {
|
|
|
|
::operator delete(ptr);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class T, class U>
|
|
|
|
inline bool
|
|
|
|
operator==(const creator<T>&, const creator<U>&)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T, class U>
|
|
|
|
inline bool
|
|
|
|
operator!=(const creator<T>&, const creator<U>&)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-11-06 14:17:32 +00:00
|
|
|
class type {
|
|
|
|
public:
|
2017-03-06 01:18:16 -05:00
|
|
|
static unsigned instances;
|
|
|
|
|
|
|
|
type() {
|
2012-11-06 14:17:32 +00:00
|
|
|
if (instances == 5) {
|
|
|
|
throw true;
|
|
|
|
}
|
2017-03-06 01:18:16 -05:00
|
|
|
++instances;
|
2012-11-06 14:17:32 +00:00
|
|
|
}
|
2017-03-06 01:18:16 -05:00
|
|
|
|
2012-11-06 14:17:32 +00:00
|
|
|
~type() {
|
2017-03-06 01:18:16 -05:00
|
|
|
--instances;
|
2012-11-06 14:17:32 +00:00
|
|
|
}
|
2017-03-06 01:18:16 -05:00
|
|
|
|
2012-11-06 14:17:32 +00:00
|
|
|
private:
|
|
|
|
type(const type&);
|
|
|
|
type& operator=(const type&);
|
|
|
|
};
|
|
|
|
|
2017-03-06 01:18:16 -05:00
|
|
|
unsigned type::instances = 0;
|
2012-11-06 14:17:32 +00:00
|
|
|
|
2015-11-09 22:35:34 -05:00
|
|
|
int main()
|
|
|
|
{
|
2012-11-06 14:17:32 +00:00
|
|
|
try {
|
2017-03-06 01:18:16 -05:00
|
|
|
boost::allocate_shared<type[]>(creator<type>(), 6);
|
2012-11-06 14:17:32 +00:00
|
|
|
BOOST_ERROR("allocate_shared did not throw");
|
|
|
|
} catch (...) {
|
|
|
|
BOOST_TEST(type::instances == 0);
|
|
|
|
}
|
2012-11-11 19:14:50 +00:00
|
|
|
try {
|
2017-03-06 01:18:16 -05:00
|
|
|
boost::allocate_shared<type[][2]>(creator<type>(), 3);
|
2012-11-11 19:14:50 +00:00
|
|
|
BOOST_ERROR("allocate_shared did not throw");
|
|
|
|
} catch (...) {
|
|
|
|
BOOST_TEST(type::instances == 0);
|
|
|
|
}
|
2012-12-03 05:41:34 +00:00
|
|
|
try {
|
2017-06-20 20:03:52 -04:00
|
|
|
boost::allocate_shared<type[6]>(creator<>());
|
2012-12-03 05:41:34 +00:00
|
|
|
BOOST_ERROR("allocate_shared did not throw");
|
|
|
|
} catch (...) {
|
|
|
|
BOOST_TEST(type::instances == 0);
|
|
|
|
}
|
|
|
|
try {
|
2017-06-20 20:03:52 -04:00
|
|
|
boost::allocate_shared<type[3][2]>(creator<>());
|
2012-12-03 05:41:34 +00:00
|
|
|
BOOST_ERROR("allocate_shared did not throw");
|
|
|
|
} catch (...) {
|
|
|
|
BOOST_TEST(type::instances == 0);
|
|
|
|
}
|
2012-12-11 17:42:47 +00:00
|
|
|
try {
|
2017-06-20 20:03:52 -04:00
|
|
|
boost::allocate_shared_noinit<type[]>(creator<>(), 6);
|
2012-12-11 17:42:47 +00:00
|
|
|
BOOST_ERROR("allocate_shared_noinit did not throw");
|
|
|
|
} catch (...) {
|
|
|
|
BOOST_TEST(type::instances == 0);
|
|
|
|
}
|
|
|
|
try {
|
2017-06-20 20:03:52 -04:00
|
|
|
boost::allocate_shared_noinit<type[][2]>(creator<>(), 3);
|
2012-12-11 17:42:47 +00:00
|
|
|
BOOST_ERROR("allocate_shared_noinit did not throw");
|
|
|
|
} catch (...) {
|
|
|
|
BOOST_TEST(type::instances == 0);
|
|
|
|
}
|
2014-01-23 20:40:46 -08:00
|
|
|
try {
|
2017-06-20 20:03:52 -04:00
|
|
|
boost::allocate_shared_noinit<type[6]>(creator<>());
|
2014-01-23 20:40:46 -08:00
|
|
|
BOOST_ERROR("allocate_shared_noinit did not throw");
|
2014-01-28 02:27:49 -08:00
|
|
|
} catch (...) {
|
2014-01-23 20:40:46 -08:00
|
|
|
BOOST_TEST(type::instances == 0);
|
|
|
|
}
|
|
|
|
try {
|
2017-06-20 20:03:52 -04:00
|
|
|
boost::allocate_shared_noinit<type[3][2]>(creator<>());
|
2014-01-23 20:40:46 -08:00
|
|
|
BOOST_ERROR("allocate_shared_noinit did not throw");
|
2014-01-28 02:27:49 -08:00
|
|
|
} catch (...) {
|
2014-01-23 20:40:46 -08:00
|
|
|
BOOST_TEST(type::instances == 0);
|
|
|
|
}
|
2012-11-06 14:17:32 +00:00
|
|
|
return boost::report_errors();
|
|
|
|
}
|