/* 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 #include #include #include class type { public: explicit type(double value) : value_(value) { } private: type(const type&); type& operator=(const type&); double value_; }; void test_value_type() { BOOST_TEST_TRAIT_SAME(int, boost::default_allocator::value_type); BOOST_TEST_TRAIT_SAME(type, boost::default_allocator::value_type); BOOST_TEST_TRAIT_SAME(int[5], boost::default_allocator::value_type); BOOST_TEST_TRAIT_SAME(void, boost::default_allocator::value_type); } void test_pointer() { BOOST_TEST_TRAIT_SAME(int*, boost::default_allocator::pointer); BOOST_TEST_TRAIT_SAME(type*, boost::default_allocator::pointer); BOOST_TEST_TRAIT_SAME(int(*)[5], boost::default_allocator::pointer); BOOST_TEST_TRAIT_SAME(void*, boost::default_allocator::pointer); } void test_const_pointer() { BOOST_TEST_TRAIT_SAME(const int*, boost::default_allocator::const_pointer); BOOST_TEST_TRAIT_SAME(const type*, boost::default_allocator::const_pointer); BOOST_TEST_TRAIT_SAME(const int(*)[5], boost::default_allocator::const_pointer); BOOST_TEST_TRAIT_SAME(const void*, boost::default_allocator::const_pointer); } void test_reference() { BOOST_TEST_TRAIT_SAME(int&, boost::default_allocator::reference); BOOST_TEST_TRAIT_SAME(type&, boost::default_allocator::reference); BOOST_TEST_TRAIT_SAME(int(&)[5], boost::default_allocator::reference); BOOST_TEST_TRAIT_SAME(void, boost::default_allocator::reference); } void test_const_reference() { BOOST_TEST_TRAIT_SAME(const int&, boost::default_allocator::const_reference); BOOST_TEST_TRAIT_SAME(const type&, boost::default_allocator::const_reference); BOOST_TEST_TRAIT_SAME(const int(&)[5], boost::default_allocator::const_reference); BOOST_TEST_TRAIT_SAME(const void, boost::default_allocator::const_reference); } void test_size_type() { BOOST_TEST_TRAIT_SAME(std::size_t, boost::default_allocator::size_type); BOOST_TEST_TRAIT_SAME(std::size_t, boost::default_allocator::size_type); BOOST_TEST_TRAIT_SAME(std::size_t, boost::default_allocator::size_type); BOOST_TEST_TRAIT_SAME(std::size_t, boost::default_allocator::size_type); } void test_difference_type() { BOOST_TEST_TRAIT_SAME(std::ptrdiff_t, boost::default_allocator::difference_type); BOOST_TEST_TRAIT_SAME(std::ptrdiff_t, boost::default_allocator::difference_type); BOOST_TEST_TRAIT_SAME(std::ptrdiff_t, boost::default_allocator::difference_type); BOOST_TEST_TRAIT_SAME(std::ptrdiff_t, boost::default_allocator::difference_type); } void test_propagate_on_container_move_assignment() { BOOST_TEST_TRAIT_TRUE((boost::default_allocator:: propagate_on_container_move_assignment)); BOOST_TEST_TRAIT_TRUE((boost::default_allocator:: propagate_on_container_move_assignment)); BOOST_TEST_TRAIT_TRUE((boost::default_allocator:: propagate_on_container_move_assignment)); BOOST_TEST_TRAIT_TRUE((boost::default_allocator:: propagate_on_container_move_assignment)); } void test_is_always_equal() { BOOST_TEST_TRAIT_TRUE((boost::default_allocator::is_always_equal)); BOOST_TEST_TRAIT_TRUE((boost::default_allocator::is_always_equal)); BOOST_TEST_TRAIT_TRUE((boost::default_allocator::is_always_equal)); BOOST_TEST_TRAIT_TRUE((boost::default_allocator::is_always_equal)); } void test_rebind() { BOOST_TEST_TRAIT_SAME(boost::default_allocator, boost::default_allocator::rebind::other); BOOST_TEST_TRAIT_SAME(boost::default_allocator, boost::default_allocator::rebind::other); BOOST_TEST_TRAIT_SAME(boost::default_allocator, boost::default_allocator::rebind::other); BOOST_TEST_TRAIT_SAME(boost::default_allocator, boost::default_allocator::rebind::other); } void test_default_construct() { boost::default_allocator a1; (void)a1; boost::default_allocator a2; (void)a2; boost::default_allocator a3; (void)a3; boost::default_allocator a4; (void)a4; } void test_copy() { boost::default_allocator a1; boost::default_allocator a2(a1); (void)a2; boost::default_allocator a3; boost::default_allocator a4(a3); (void)a4; boost::default_allocator a5; boost::default_allocator a6(a5); (void)a6; } void test_construct_other() { boost::default_allocator a1; boost::default_allocator a2(a1); boost::default_allocator a3(a2); boost::default_allocator a4(a3); boost::default_allocator a5(a4); (void)a5; } #if defined(PTRDIFF_MAX) && defined(SIZE_MAX) template std::size_t max_size() { return PTRDIFF_MAX < SIZE_MAX / sizeof(T) ? PTRDIFF_MAX : SIZE_MAX / sizeof(T); } #else template std::size_t max_size() { return ~static_cast(0) / sizeof(T); } #endif void test_max_size() { BOOST_TEST_EQ(max_size(), boost::default_allocator().max_size()); BOOST_TEST_EQ(max_size(), boost::default_allocator().max_size()); BOOST_TEST_EQ(max_size(), boost::default_allocator().max_size()); } template void test_allocate() { boost::default_allocator 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() + 1), std::bad_alloc); } void test_allocate_deallocate() { test_allocate(); test_allocate(); test_allocate(); } void test_equals() { BOOST_TEST(boost::default_allocator() == boost::default_allocator()); BOOST_TEST(boost::default_allocator() == boost::default_allocator()); BOOST_TEST(boost::default_allocator() == boost::default_allocator()); BOOST_TEST(boost::default_allocator() == boost::default_allocator()); } void test_not_equals() { BOOST_TEST(!(boost::default_allocator() != boost::default_allocator())); BOOST_TEST(!(boost::default_allocator() != boost::default_allocator())); BOOST_TEST(!(boost::default_allocator() != boost::default_allocator())); BOOST_TEST(!(boost::default_allocator() != boost::default_allocator())); } void test_container() { std::vector > v; v.push_back(1); BOOST_TEST(v.size() == 1); BOOST_TEST(v.front() == 1); std::list > l; l.push_back(1); BOOST_TEST(l.size() == 1); BOOST_TEST(l.front() == 1); } int main() { test_value_type(); test_pointer(); test_const_pointer(); test_reference(); test_const_reference(); 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(); test_container(); return boost::report_errors(); }