////////////////////////////////////////////////////////////////////////////// // // (C) Copyright Ion Gaztanaga 2004-2013. 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) // // See http://www.boost.org/libs/container for documentation. // ////////////////////////////////////////////////////////////////////////////// #include #include #include #include #include #include #include "print_container.hpp" #include "check_equal_containers.hpp" #include "dummy_test_allocator.hpp" #include "movable_int.hpp" #include #include #include #include #include #include "emplace_test.hpp" #include "propagate_allocator_test.hpp" #include "vector_test.hpp" #include "default_init_test.hpp" #include "../../intrusive/test/iterator_test.hpp" #include using namespace boost::container; //Function to check if both sets are equal template bool deque_copyable_only(V1 &, V2 &, dtl::false_type) { return true; } //Function to check if both sets are equal template bool deque_copyable_only(V1 &cntc, V2 &stdc, dtl::true_type) { typedef typename V1::value_type IntType; std::size_t size = cntc.size(); stdc.insert(stdc.end(), 50u, 1); cntc.insert(cntc.end(), 50u, IntType(1)); if(!test::CheckEqualContainers(cntc, stdc)) return false; { IntType move_me(1); stdc.insert(stdc.begin()+std::ptrdiff_t(size)/2, 50u, 1); cntc.insert(cntc.begin()+std::ptrdiff_t(size/2), 50u, boost::move(move_me)); if(!test::CheckEqualContainers(cntc, stdc)) return false; } { IntType move_me(2); cntc.assign(cntc.size()/2, boost::move(move_me)); stdc.assign(stdc.size()/2, 2); if(!test::CheckEqualContainers(cntc, stdc)) return false; } { IntType move_me(1); stdc.clear(); cntc.clear(); stdc.insert(stdc.begin(), 50u, 1); cntc.insert(cntc.begin(), 50u, boost::move(move_me)); if(!test::CheckEqualContainers(cntc, stdc)) return false; stdc.insert(stdc.begin()+20, 50u, 1); cntc.insert(cntc.begin()+20, 50u, boost::move(move_me)); if(!test::CheckEqualContainers(cntc, stdc)) return false; stdc.insert(stdc.begin()+20, 20u, 1); cntc.insert(cntc.begin()+20, 20u, boost::move(move_me)); if(!test::CheckEqualContainers(cntc, stdc)) return false; } { IntType move_me(1); stdc.clear(); cntc.clear(); stdc.insert(stdc.end(), 50u, 1); cntc.insert(cntc.end(), 50u, boost::move(move_me)); if(!test::CheckEqualContainers(cntc, stdc)) return false; stdc.insert(stdc.end()-20, 50u, 1); cntc.insert(cntc.end()-20, 50u, boost::move(move_me)); if(!test::CheckEqualContainers(cntc, stdc)) return false; stdc.insert(stdc.end()-20, 20u, 1); cntc.insert(cntc.end()-20, 20u, boost::move(move_me)); if(!test::CheckEqualContainers(cntc, stdc)) return false; } return true; } //Test recursive structures class recursive_deque { public: recursive_deque (const recursive_deque &x) : deque_(x.deque_) {} recursive_deque & operator=(const recursive_deque &x) { this->deque_ = x.deque_; return *this; } deque deque_; deque::iterator it_; deque::const_iterator cit_; deque::reverse_iterator rit_; deque::const_reverse_iterator crit_; }; bool do_recursive_deque_test() { //Test for recursive types { deque recursive_deque_deque; } { //Now test move semantics deque original; deque move_ctor(boost::move(original)); deque move_assign; move_assign = boost::move(move_ctor); move_assign.swap(original); } return true; } template bool do_test() { typedef typename deque_options >::type Options; { typedef deque MyCnt; ::boost::movelib::unique_ptr const pcntc = ::boost::movelib::make_unique(); pcntc->erase(pcntc->cbegin(), pcntc->cend()); } //Alias deque types typedef deque MyCnt; typedef std::deque MyStd; const int max = 100; { ::boost::movelib::unique_ptr const pcntc = ::boost::movelib::make_unique(); ::boost::movelib::unique_ptr const pstdc = ::boost::movelib::make_unique(); MyCnt &cntc = *pcntc; MyStd &stdc = *pstdc; for(int i = 0; i < max*100; ++i){ IntType move_me(i); cntc.insert(cntc.end(), boost::move(move_me)); stdc.insert(stdc.end(), i); if(!test::CheckEqualContainers(cntc, stdc)) return false; } if(!test::CheckEqualContainers(cntc, stdc)) return false; cntc.clear(); stdc.clear(); for(int i = 0; i < max*100; ++i){ IntType move_me(i); cntc.push_back(boost::move(move_me)); stdc.push_back(i); if(!test::CheckEqualContainers(cntc, stdc)) return false; } if(!test::CheckEqualContainers(cntc, stdc)) return false; cntc.clear(); stdc.clear(); for(int i = 0; i < max*100; ++i){ IntType move_me(i); cntc.push_front(boost::move(move_me)); stdc.push_front(i); if(!test::CheckEqualContainers(cntc, stdc)) return false; } if(!test::CheckEqualContainers(cntc, stdc)) return false; typename MyCnt::iterator it; typename MyCnt::const_iterator cit = it; (void)cit; cntc.erase(cntc.begin()++); stdc.erase(stdc.begin()++); if(!test::CheckEqualContainers(cntc, stdc)) return false; cntc.erase(cntc.erase(cntc.begin()++)); stdc.erase(stdc.erase(stdc.begin()++)); if(!test::CheckEqualContainers(cntc, stdc)) return false; cntc.erase(cntc.erase(cntc.begin()+3)); stdc.erase(stdc.erase(stdc.begin()+3)); if(!test::CheckEqualContainers(cntc, stdc)) return false; cntc.erase(cntc.erase(cntc.end()-2)); stdc.erase(stdc.erase(stdc.end()-2)); if(!test::CheckEqualContainers(cntc, stdc)) return false; cntc.erase(cntc.erase(cntc.end()-4)); stdc.erase(stdc.erase(stdc.end()-4)); if(!test::CheckEqualContainers(cntc, stdc)) return false; cntc.erase(cntc.begin()); stdc.erase(stdc.begin()); if(!test::CheckEqualContainers(cntc, stdc)) return false; cntc.erase(cntc.end()-1); stdc.erase(stdc.end()-1); if(!test::CheckEqualContainers(cntc, stdc)) return false; { //Initialize values IntType aux_vect[50]; for(int i = 0; i < 50; ++i){ IntType move_me (-1); aux_vect[i] = boost::move(move_me); } int aux_vect2[50]; for(int i = 0; i < 50; ++i){ aux_vect2[i] = -1; } cntc.insert(cntc.end() ,boost::make_move_iterator(&aux_vect[0]) ,boost::make_move_iterator(aux_vect + 50)); stdc.insert(stdc.end(), aux_vect2, aux_vect2 + 50); if(!test::CheckEqualContainers(cntc, stdc)) return false; for(int i = 0; i < 50; ++i){ IntType move_me (i); aux_vect[i] = boost::move(move_me); } for(int i = 0; i < 50; ++i){ aux_vect2[i] = i; } cntc.insert(cntc.begin()+std::ptrdiff_t(cntc.size()) ,boost::make_move_iterator(&aux_vect[0]) ,boost::make_move_iterator(aux_vect + 50)); stdc.insert(stdc.begin()+std::ptrdiff_t(stdc.size()), aux_vect2, aux_vect2 + 50); if(!test::CheckEqualContainers(cntc, stdc)) return false; for(int i = 0, j = static_cast(cntc.size()); i < j; ++i){ cntc.erase(cntc.begin()); stdc.erase(stdc.begin()); } if(!test::CheckEqualContainers(cntc, stdc)) return false; } { IntType aux_vect[50]; for(int i = 0; i < 50; ++i){ IntType move_me(-1); aux_vect[i] = boost::move(move_me); } int aux_vect2[50]; for(int i = 0; i < 50; ++i){ aux_vect2[i] = -1; } cntc.insert(cntc.begin() ,boost::make_move_iterator(&aux_vect[0]) ,boost::make_move_iterator(aux_vect + 50)); stdc.insert(stdc.begin(), aux_vect2, aux_vect2 + 50); if(!test::CheckEqualContainers(cntc, stdc)) return false; } if(!deque_copyable_only(cntc, stdc ,dtl::bool_::value>())){ return false; } cntc.erase(cntc.begin()); stdc.erase(stdc.begin()); if(!test::CheckEqualContainers(cntc, stdc)) return false; for(int i = 0; i < max; ++i){ IntType move_me(i); cntc.insert(cntc.begin(), boost::move(move_me)); stdc.insert(stdc.begin(), i); } if(!test::CheckEqualContainers(cntc, stdc)) return false; //Test insertion from list { std::list l(50, int(1)); cntc.insert(cntc.begin(), l.begin(), l.end()); stdc.insert(stdc.begin(), l.begin(), l.end()); if(!test::CheckEqualContainers(cntc, stdc)) return 1; cntc.assign(l.begin(), l.end()); stdc.assign(l.begin(), l.end()); if(!test::CheckEqualContainers(cntc, stdc)) return 1; } cntc.resize(100); stdc.resize(100); if(!test::CheckEqualContainers(cntc, stdc)) return 1; cntc.resize(200); stdc.resize(200); if(!test::CheckEqualContainers(cntc, stdc)) return 1; } #ifndef BOOST_CONTAINER_NO_CXX17_CTAD //Check Constructor Template Auto Deduction { auto gold = MyStd{ 1, 2, 3 }; auto test = deque(gold.begin(), gold.end()); if(!test::CheckEqualContainers(gold, test)) return false; } { auto gold = MyStd{ 1, 2, 3 }; auto test = deque(gold.begin(), gold.end(), new_allocator()); if(!test::CheckEqualContainers(gold, test)) return false; } #endif std::cout << std::endl << "Test OK!" << std::endl; return true; } template struct GetAllocatorCont { template struct apply { typedef deque< ValueType , typename allocator_traits ::template portable_rebind_alloc::type , typename deque_options >::type > type; }; }; template int test_cont_variants() { typedef typename GetAllocatorCont::template apply::type MyCont; typedef typename GetAllocatorCont::template apply::type MyMoveCont; typedef typename GetAllocatorCont::template apply::type MyCopyMoveCont; typedef typename GetAllocatorCont::template apply::type MyCopyCont; typedef typename GetAllocatorCont::template apply::type MyMoveConstructCont; if (test::vector_test()) return 1; if (test::vector_test()) return 1; if (test::vector_test()) return 1; if (test::vector_test()) return 1; if (test::vector_test()) return 1; return 0; } template struct char_holder { char chars[N]; }; bool do_test_default_block_size() { //Check power of two sizes by default BOOST_TEST(deque >::get_block_size() == 16*sizeof(void*)); BOOST_TEST(deque >::get_block_size() == 16*sizeof(void*)); BOOST_TEST(deque >::get_block_size() == 8*sizeof(void*)); BOOST_TEST(deque >::get_block_size() == 8*sizeof(void*)); BOOST_TEST(deque >::get_block_size() == 8*sizeof(void*)); BOOST_TEST(deque >::get_block_size() == 8*sizeof(void*)); BOOST_TEST(deque >::get_block_size() == 4*sizeof(void*)); BOOST_TEST(deque >::get_block_size() == 4*sizeof(void*)); BOOST_TEST(deque >::get_block_size() == 4*sizeof(void*)); BOOST_TEST(deque >::get_block_size() == 4*sizeof(void*)); BOOST_TEST(deque >::get_block_size() == 4*sizeof(void*)); BOOST_TEST(deque >::get_block_size() == 4*sizeof(void*)); BOOST_TEST(deque >::get_block_size() == 4*sizeof(void*)); BOOST_TEST(deque >::get_block_size() == 4*sizeof(void*)); BOOST_TEST(deque >::get_block_size() == 2*sizeof(void*)); BOOST_TEST(deque >::get_block_size() == 2*sizeof(void*)); BOOST_TEST(deque >::get_block_size() == 2*sizeof(void*)); BOOST_TEST(deque >::get_block_size() == 2*sizeof(void*)); BOOST_TEST(deque >::get_block_size() == 2*sizeof(void*)); BOOST_TEST(deque >::get_block_size() == 2*sizeof(void*)); BOOST_TEST(deque >::get_block_size() == 2*sizeof(void*)); BOOST_TEST(deque >::get_block_size() == 2*sizeof(void*)); //Minimal 8 elements BOOST_TEST(deque >::get_block_size() == 8u); return 0 == boost::report_errors(); } struct boost_container_deque; namespace boost { namespace container { namespace test { template<> struct alloc_propagate_base { template struct apply { typedef boost::container::deque type; }; }; }}} //namespace boost::container::test int main () { if(!do_recursive_deque_test()) return 1; //Non-reservable deque if(!do_test()) return 1; if(!do_test()) return 1; if(!do_test()) return 1; if(!do_test()) return 1; //Default block size if(!do_test_default_block_size()) return 1; //Test non-copy-move operations { deque d; d.emplace_back(); d.emplace_front(1); d.resize(10); d.resize(1); } //////////////////////////////////// // Allocator implementations //////////////////////////////////// // std:allocator if(test_cont_variants< std::allocator, false >()){ std::cerr << "test_cont_variants< std::allocator > failed" << std::endl; return 1; } // boost::container::allocator if(test_cont_variants< allocator, true >()){ std::cerr << "test_cont_variants< allocator > failed" << std::endl; return 1; } //////////////////////////////////// // Default init test //////////////////////////////////// if(!test::default_init_test< deque > >()){ std::cerr << "Default init test failed" << std::endl; return 1; } //////////////////////////////////// // Emplace testing //////////////////////////////////// const test::EmplaceOptions Options = (test::EmplaceOptions)(test::EMPLACE_BACK | test::EMPLACE_FRONT | test::EMPLACE_BEFORE); if(!boost::container::test::test_emplace < deque, Options>()) return 1; //////////////////////////////////// // Allocator propagation testing //////////////////////////////////// if(!boost::container::test::test_propagate_allocator()) return 1; //////////////////////////////////// // Initializer lists testing //////////////////////////////////// if(!boost::container::test::test_vector_methods_with_initializer_list_as_argument_for < boost::container::deque >()) { return 1; } //////////////////////////////////// // Iterator testing //////////////////////////////////// { typedef boost::container::deque cont_int; for(std::size_t i = 1; i <= 10000; i*=10){ cont_int a; for (int j = 0; j < (int)i; ++j) a.push_back((int)j); boost::intrusive::test::test_iterator_random< cont_int >(a); if(boost::report_errors() != 0) { return 1; } } } //////////////////////////////////// // has_trivial_destructor_after_move testing //////////////////////////////////// // default allocator { typedef boost::container::deque cont; typedef cont::allocator_type allocator_type; typedef boost::container::allocator_traits::pointer pointer; BOOST_CONTAINER_STATIC_ASSERT_MSG(!(boost::has_trivial_destructor_after_move::value != boost::has_trivial_destructor_after_move::value && boost::has_trivial_destructor_after_move::value) , "has_trivial_destructor_after_move(std::allocator) test failed"); } // std::allocator { typedef boost::container::deque > cont; typedef cont::allocator_type allocator_type; typedef boost::container::allocator_traits::pointer pointer; BOOST_CONTAINER_STATIC_ASSERT_MSG(!(boost::has_trivial_destructor_after_move::value != boost::has_trivial_destructor_after_move::value && boost::has_trivial_destructor_after_move::value) , "has_trivial_destructor_after_move(std::allocator) test failed"); } return 0; }