1
0
forked from boostorg/core
Files
boost_core/test/default_allocator_test.cpp

212 lines
6.0 KiB
C++
Raw Normal View History

2019-04-24 22:21:13 -04:00
/*
Copyright 2019 Glen Joseph Fernandes
(glenjofe@gmail.com)
Distributed under the Boost Software License, Version 1.0.
(http://www.boost.org/LICENSE_1_0.txt)
*/
#include <boost/core/default_allocator.hpp>
#include <boost/core/lightweight_test_trait.hpp>
class type {
public:
explicit type(int) { }
private:
type(const type&);
type& operator=(const type&);
};
void test_value_type()
{
BOOST_TEST_TRAIT_SAME(int,
boost::default_allocator<int>::value_type);
BOOST_TEST_TRAIT_SAME(type,
boost::default_allocator<type>::value_type);
BOOST_TEST_TRAIT_SAME(int[5],
boost::default_allocator<int[5]>::value_type);
BOOST_TEST_TRAIT_SAME(void,
boost::default_allocator<void>::value_type);
}
void test_pointer()
{
BOOST_TEST_TRAIT_SAME(int*,
boost::default_allocator<int>::pointer);
BOOST_TEST_TRAIT_SAME(type*,
boost::default_allocator<type>::pointer);
BOOST_TEST_TRAIT_SAME(int(*)[5],
boost::default_allocator<int[5]>::pointer);
BOOST_TEST_TRAIT_SAME(void*,
boost::default_allocator<void>::pointer);
}
void test_size_type()
{
BOOST_TEST_TRAIT_SAME(std::size_t,
boost::default_allocator<int>::size_type);
BOOST_TEST_TRAIT_SAME(std::size_t,
boost::default_allocator<type>::size_type);
BOOST_TEST_TRAIT_SAME(std::size_t,
boost::default_allocator<int[5]>::size_type);
BOOST_TEST_TRAIT_SAME(std::size_t,
boost::default_allocator<void>::size_type);
}
void test_difference_type()
{
BOOST_TEST_TRAIT_SAME(std::ptrdiff_t,
boost::default_allocator<int>::difference_type);
BOOST_TEST_TRAIT_SAME(std::ptrdiff_t,
boost::default_allocator<type>::difference_type);
BOOST_TEST_TRAIT_SAME(std::ptrdiff_t,
boost::default_allocator<int[5]>::difference_type);
BOOST_TEST_TRAIT_SAME(std::ptrdiff_t,
boost::default_allocator<void>::difference_type);
}
void test_propagate_on_container_move_assignment()
{
BOOST_TEST_TRAIT_TRUE((boost::default_allocator<int>::
propagate_on_container_move_assignment));
BOOST_TEST_TRAIT_TRUE((boost::default_allocator<type>::
propagate_on_container_move_assignment));
BOOST_TEST_TRAIT_TRUE((boost::default_allocator<int[5]>::
propagate_on_container_move_assignment));
BOOST_TEST_TRAIT_TRUE((boost::default_allocator<void>::
propagate_on_container_move_assignment));
}
void test_is_always_equal()
{
BOOST_TEST_TRAIT_TRUE((boost::default_allocator<int>::is_always_equal));
BOOST_TEST_TRAIT_TRUE((boost::default_allocator<type>::is_always_equal));
BOOST_TEST_TRAIT_TRUE((boost::default_allocator<int[5]>::is_always_equal));
BOOST_TEST_TRAIT_TRUE((boost::default_allocator<void>::is_always_equal));
}
void test_rebind()
{
BOOST_TEST_TRAIT_SAME(boost::default_allocator<type>,
boost::default_allocator<int>::rebind<type>::other);
BOOST_TEST_TRAIT_SAME(boost::default_allocator<int[5]>,
boost::default_allocator<type>::rebind<int[5]>::other);
BOOST_TEST_TRAIT_SAME(boost::default_allocator<void>,
boost::default_allocator<int[5]>::rebind<void>::other);
BOOST_TEST_TRAIT_SAME(boost::default_allocator<int>,
boost::default_allocator<void>::rebind<int>::other);
}
void test_default_construct()
{
boost::default_allocator<int> a1;
(void)a1;
boost::default_allocator<type> a2;
(void)a2;
boost::default_allocator<int[5]> a3;
(void)a3;
boost::default_allocator<void> a4;
(void)a4;
}
void test_copy()
{
boost::default_allocator<int> a1;
boost::default_allocator<int> a2(a1);
(void)a2;
boost::default_allocator<int[5]> a3;
boost::default_allocator<int[5]> a4(a3);
(void)a4;
boost::default_allocator<void> a5;
boost::default_allocator<void> a6(a5);
(void)a6;
}
void test_construct_other()
{
boost::default_allocator<int> a1;
boost::default_allocator<type> a2(a1);
boost::default_allocator<int[5]> a3(a2);
boost::default_allocator<void> a4(a3);
boost::default_allocator<int> a5(a4);
(void)a5;
}
template<class T>
std::size_t max_size()
{
return ~static_cast<std::size_t>(0) / sizeof(T);
}
void test_max_size()
{
BOOST_TEST_EQ(max_size<int>(),
boost::default_allocator<int>().max_size());
BOOST_TEST_EQ(max_size<type>(),
boost::default_allocator<type>().max_size());
BOOST_TEST_EQ(max_size<int[5]>(),
boost::default_allocator<int[5]>().max_size());
}
template<class T>
void test_allocate()
{
boost::default_allocator<T> a;
T* p = a.allocate(1);
BOOST_TEST(p != 0);
a.deallocate(p, 1);
p = a.allocate(0);
a.deallocate(p, 0);
BOOST_TEST_THROWS(a.allocate(a.max_size()), std::bad_alloc);
}
void test_allocate_deallocate()
{
test_allocate<int>();
test_allocate<type>();
test_allocate<int[5]>();
}
void test_equals()
{
BOOST_TEST(boost::default_allocator<int>() ==
boost::default_allocator<type>());
BOOST_TEST(boost::default_allocator<type>() ==
boost::default_allocator<int[5]>());
BOOST_TEST(boost::default_allocator<int[5]>() ==
boost::default_allocator<void>());
BOOST_TEST(boost::default_allocator<void>() ==
boost::default_allocator<int>());
}
void test_not_equals()
{
BOOST_TEST(!(boost::default_allocator<int>() !=
boost::default_allocator<type>()));
BOOST_TEST(!(boost::default_allocator<type>() !=
boost::default_allocator<int[5]>()));
BOOST_TEST(!(boost::default_allocator<int[5]>() !=
boost::default_allocator<void>()));
BOOST_TEST(!(boost::default_allocator<void>() !=
boost::default_allocator<int>()));
}
int main()
{
test_value_type();
test_pointer();
test_size_type();
test_difference_type();
test_propagate_on_container_move_assignment();
test_is_always_equal();
test_rebind();
test_default_construct();
test_copy();
test_construct_other();
test_max_size();
test_allocate_deallocate();
test_equals();
test_not_equals();
return boost::report_errors();
}