2011-08-26 18:26:44 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2012-03-22 18:48:57 +00:00
|
|
|
// (C) Copyright Ion Gaztanaga 2004-2012. Distributed under the Boost
|
2011-08-26 18:26:44 +00:00
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-09-01 11:01:03 +00:00
|
|
|
#ifndef BOOST_CONTAINER_TEST_VECTOR_TEST_HEADER
|
|
|
|
#define BOOST_CONTAINER_TEST_VECTOR_TEST_HEADER
|
|
|
|
|
2011-08-26 18:26:44 +00:00
|
|
|
#include <boost/container/detail/config_begin.hpp>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <memory>
|
|
|
|
#include <vector>
|
|
|
|
#include <iostream>
|
|
|
|
#include <functional>
|
|
|
|
#include <list>
|
|
|
|
|
2012-11-24 21:09:10 +00:00
|
|
|
#include <boost/move/utility.hpp>
|
2011-08-26 18:26:44 +00:00
|
|
|
#include <boost/container/detail/mpl.hpp>
|
|
|
|
#include "print_container.hpp"
|
|
|
|
#include "check_equal_containers.hpp"
|
|
|
|
#include "movable_int.hpp"
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include "emplace_test.hpp"
|
2012-09-01 11:01:03 +00:00
|
|
|
#include "input_from_forward_iterator.hpp"
|
2012-11-24 21:09:10 +00:00
|
|
|
#include <boost/move/utility.hpp>
|
|
|
|
#include <boost/move/iterator.hpp>
|
2013-01-10 10:55:50 +00:00
|
|
|
#include <boost/detail/no_exceptions_support.hpp>
|
2013-03-16 19:31:32 +00:00
|
|
|
#include "insert_test.hpp"
|
2011-08-26 18:26:44 +00:00
|
|
|
|
|
|
|
namespace boost{
|
|
|
|
namespace container {
|
|
|
|
namespace test{
|
|
|
|
|
|
|
|
template<class V1, class V2>
|
2011-12-22 20:15:57 +00:00
|
|
|
bool vector_copyable_only(V1 *, V2 *, boost::container::container_detail::false_type)
|
2011-08-26 18:26:44 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Function to check if both sets are equal
|
|
|
|
template<class V1, class V2>
|
2011-12-22 20:15:57 +00:00
|
|
|
bool vector_copyable_only(V1 *boostvector, V2 *stdvector, boost::container::container_detail::true_type)
|
2011-08-26 18:26:44 +00:00
|
|
|
{
|
|
|
|
typedef typename V1::value_type IntType;
|
|
|
|
std::size_t size = boostvector->size();
|
|
|
|
boostvector->insert(boostvector->end(), 50, IntType(1));
|
|
|
|
stdvector->insert(stdvector->end(), 50, 1);
|
|
|
|
if(!test::CheckEqualContainers(boostvector, stdvector)) return false;
|
|
|
|
|
|
|
|
{
|
|
|
|
IntType move_me(1);
|
|
|
|
boostvector->insert(boostvector->begin()+size/2, 50, boost::move(move_me));
|
|
|
|
stdvector->insert(stdvector->begin()+size/2, 50, 1);
|
|
|
|
if(!test::CheckEqualContainers(boostvector, stdvector)) return false;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
IntType move_me(2);
|
|
|
|
boostvector->assign(boostvector->size()/2, boost::move(move_me));
|
|
|
|
stdvector->assign(stdvector->size()/2, 2);
|
|
|
|
if(!test::CheckEqualContainers(boostvector, stdvector)) return false;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
IntType move_me(3);
|
|
|
|
boostvector->assign(boostvector->size()*3-1, boost::move(move_me));
|
|
|
|
stdvector->assign(stdvector->size()*3-1, 3);
|
|
|
|
if(!test::CheckEqualContainers(boostvector, stdvector)) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
IntType copy_me(3);
|
|
|
|
const IntType ccopy_me(3);
|
|
|
|
boostvector->push_back(copy_me);
|
|
|
|
stdvector->push_back(int(3));
|
|
|
|
boostvector->push_back(ccopy_me);
|
|
|
|
stdvector->push_back(int(3));
|
|
|
|
if(!test::CheckEqualContainers(boostvector, stdvector)) return false;
|
|
|
|
}
|
2013-03-03 12:26:48 +00:00
|
|
|
{
|
|
|
|
V1 *pv1 = new V1(*boostvector);
|
|
|
|
V2 *pv2 = new V2(*stdvector);
|
2013-03-13 16:14:17 +00:00
|
|
|
boostvector->clear();
|
|
|
|
stdvector->clear();
|
|
|
|
boostvector->assign(pv1->begin(), pv1->end());
|
|
|
|
stdvector->assign(pv2->begin(), pv2->end());
|
|
|
|
if(!test::CheckEqualContainers(boostvector, stdvector)) return 1;
|
2013-03-03 12:26:48 +00:00
|
|
|
delete pv1;
|
|
|
|
delete pv2;
|
|
|
|
}
|
2011-08-26 18:26:44 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class MyBoostVector>
|
|
|
|
int vector_test()
|
|
|
|
{
|
|
|
|
typedef std::vector<int> MyStdVector;
|
|
|
|
typedef typename MyBoostVector::value_type IntType;
|
|
|
|
const int max = 100;
|
|
|
|
|
2013-03-16 19:31:32 +00:00
|
|
|
if(!test_range_insertion<MyBoostVector>()){
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-08-26 18:26:44 +00:00
|
|
|
{
|
2013-01-10 10:55:50 +00:00
|
|
|
BOOST_TRY{
|
2011-08-26 18:26:44 +00:00
|
|
|
MyBoostVector *boostvector = new MyBoostVector;
|
|
|
|
MyStdVector *stdvector = new MyStdVector;
|
|
|
|
boostvector->resize(100);
|
|
|
|
stdvector->resize(100);
|
2012-05-20 10:03:06 +00:00
|
|
|
if(!test::CheckEqualContainers(boostvector, stdvector)) return 1;
|
2011-08-26 18:26:44 +00:00
|
|
|
|
|
|
|
boostvector->resize(200);
|
|
|
|
stdvector->resize(200);
|
2012-05-20 10:03:06 +00:00
|
|
|
if(!test::CheckEqualContainers(boostvector, stdvector)) return 1;
|
2011-08-26 18:26:44 +00:00
|
|
|
|
|
|
|
boostvector->resize(0);
|
|
|
|
stdvector->resize(0);
|
2012-05-20 10:03:06 +00:00
|
|
|
if(!test::CheckEqualContainers(boostvector, stdvector)) return 1;
|
2011-08-26 18:26:44 +00:00
|
|
|
|
|
|
|
for(int i = 0; i < max; ++i){
|
|
|
|
IntType new_int(i);
|
|
|
|
boostvector->insert(boostvector->end(), boost::move(new_int));
|
|
|
|
stdvector->insert(stdvector->end(), i);
|
|
|
|
if(!test::CheckEqualContainers(boostvector, stdvector)) return 1;
|
|
|
|
}
|
|
|
|
if(!test::CheckEqualContainers(boostvector, stdvector)) return 1;
|
|
|
|
|
|
|
|
typename MyBoostVector::iterator boostit(boostvector->begin());
|
|
|
|
typename MyStdVector::iterator stdit(stdvector->begin());
|
|
|
|
typename MyBoostVector::const_iterator cboostit = boostit;
|
2011-12-22 20:15:57 +00:00
|
|
|
(void)cboostit;
|
2011-08-26 18:26:44 +00:00
|
|
|
++boostit; ++stdit;
|
|
|
|
boostvector->erase(boostit);
|
|
|
|
stdvector->erase(stdit);
|
|
|
|
if(!test::CheckEqualContainers(boostvector, stdvector)) return 1;
|
|
|
|
|
|
|
|
boostvector->erase(boostvector->begin());
|
|
|
|
stdvector->erase(stdvector->begin());
|
|
|
|
if(!test::CheckEqualContainers(boostvector, stdvector)) return 1;
|
|
|
|
|
|
|
|
{
|
|
|
|
//Initialize values
|
|
|
|
IntType aux_vect[50];
|
|
|
|
for(int i = 0; i < 50; ++i){
|
|
|
|
IntType new_int(-1);
|
|
|
|
BOOST_STATIC_ASSERT((boost::container::test::is_copyable<boost::container::test::movable_int>::value == false));
|
|
|
|
aux_vect[i] = boost::move(new_int);
|
|
|
|
}
|
|
|
|
int aux_vect2[50];
|
|
|
|
for(int i = 0; i < 50; ++i){
|
|
|
|
aux_vect2[i] = -1;
|
|
|
|
}
|
2012-09-01 11:01:03 +00:00
|
|
|
typename MyBoostVector::iterator insert_it =
|
|
|
|
boostvector->insert(boostvector->end()
|
2011-08-26 18:26:44 +00:00
|
|
|
,boost::make_move_iterator(&aux_vect[0])
|
|
|
|
,boost::make_move_iterator(aux_vect + 50));
|
2012-09-01 11:01:03 +00:00
|
|
|
if(std::size_t(std::distance(insert_it, boostvector->end())) != 50) return 1;
|
2011-08-26 18:26:44 +00:00
|
|
|
stdvector->insert(stdvector->end(), aux_vect2, aux_vect2 + 50);
|
|
|
|
if(!test::CheckEqualContainers(boostvector, stdvector)) return 1;
|
|
|
|
|
|
|
|
for(int i = 0, j = static_cast<int>(boostvector->size()); i < j; ++i){
|
|
|
|
boostvector->erase(boostvector->begin());
|
|
|
|
stdvector->erase(stdvector->begin());
|
|
|
|
}
|
|
|
|
if(!test::CheckEqualContainers(boostvector, stdvector)) return 1;
|
|
|
|
}
|
|
|
|
{
|
2012-09-09 21:47:32 +00:00
|
|
|
boostvector->resize(100);
|
|
|
|
stdvector->resize(100);
|
|
|
|
if(!test::CheckEqualContainers(boostvector, stdvector)) return 1;
|
|
|
|
|
2011-08-26 18:26:44 +00:00
|
|
|
IntType aux_vect[50];
|
|
|
|
for(int i = 0; i < 50; ++i){
|
2013-03-16 16:19:08 +00:00
|
|
|
IntType new_int(-i);
|
2011-08-26 18:26:44 +00:00
|
|
|
aux_vect[i] = boost::move(new_int);
|
|
|
|
}
|
|
|
|
int aux_vect2[50];
|
|
|
|
for(int i = 0; i < 50; ++i){
|
2013-03-16 16:19:08 +00:00
|
|
|
aux_vect2[i] = -i;
|
2011-08-26 18:26:44 +00:00
|
|
|
}
|
2012-09-09 21:47:32 +00:00
|
|
|
typename MyBoostVector::size_type old_size = boostvector->size();
|
2012-09-01 11:01:03 +00:00
|
|
|
typename MyBoostVector::iterator insert_it =
|
2013-03-16 16:19:08 +00:00
|
|
|
boostvector->insert(boostvector->begin() + old_size/2
|
2011-08-26 18:26:44 +00:00
|
|
|
,boost::make_move_iterator(&aux_vect[0])
|
|
|
|
,boost::make_move_iterator(aux_vect + 50));
|
2013-03-16 16:19:08 +00:00
|
|
|
if(boostvector->begin() + old_size/2 != insert_it) return 1;
|
|
|
|
stdvector->insert(stdvector->begin() + old_size/2, aux_vect2, aux_vect2 + 50);
|
2012-09-01 11:01:03 +00:00
|
|
|
if(!test::CheckEqualContainers(boostvector, stdvector)) return 1;
|
|
|
|
|
|
|
|
for(int i = 0; i < 50; ++i){
|
2013-03-16 16:19:08 +00:00
|
|
|
IntType new_int(-i);
|
2012-09-01 11:01:03 +00:00
|
|
|
aux_vect[i] = boost::move(new_int);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(int i = 0; i < 50; ++i){
|
2013-03-16 16:19:08 +00:00
|
|
|
aux_vect2[i] = -i;
|
2012-09-01 11:01:03 +00:00
|
|
|
}
|
2012-09-09 21:47:32 +00:00
|
|
|
old_size = boostvector->size();
|
2012-09-01 11:01:03 +00:00
|
|
|
//Now try with input iterators instead
|
2013-03-16 16:19:08 +00:00
|
|
|
insert_it = boostvector->insert(boostvector->begin() + old_size/2
|
2012-09-09 21:47:32 +00:00
|
|
|
,boost::make_move_iterator(make_input_from_forward_iterator(&aux_vect[0]))
|
|
|
|
,boost::make_move_iterator(make_input_from_forward_iterator(aux_vect + 50))
|
2012-09-01 11:01:03 +00:00
|
|
|
);
|
2013-03-16 16:19:08 +00:00
|
|
|
if(boostvector->begin() + old_size/2 != insert_it) return 1;
|
|
|
|
stdvector->insert(stdvector->begin() + old_size/2, aux_vect2, aux_vect2 + 50);
|
2011-08-26 18:26:44 +00:00
|
|
|
if(!test::CheckEqualContainers(boostvector, stdvector)) return 1;
|
|
|
|
}
|
2012-09-09 21:47:32 +00:00
|
|
|
/* //deque has no reserve
|
2011-08-26 18:26:44 +00:00
|
|
|
boostvector->reserve(boostvector->size()*2);
|
|
|
|
stdvector->reserve(stdvector->size()*2);
|
|
|
|
if(!test::CheckEqualContainers(boostvector, stdvector)) return 1;
|
|
|
|
*/
|
|
|
|
boostvector->shrink_to_fit();
|
|
|
|
MyStdVector(*stdvector).swap(*stdvector);
|
|
|
|
if(!test::CheckEqualContainers(boostvector, stdvector)) return 1;
|
|
|
|
|
|
|
|
boostvector->shrink_to_fit();
|
|
|
|
MyStdVector(*stdvector).swap(*stdvector);
|
|
|
|
if(!test::CheckEqualContainers(boostvector, stdvector)) return 1;
|
|
|
|
|
2012-09-09 21:47:32 +00:00
|
|
|
{ //push_back with not enough capacity
|
2011-08-26 18:26:44 +00:00
|
|
|
IntType push_back_this(1);
|
|
|
|
boostvector->push_back(boost::move(push_back_this));
|
|
|
|
stdvector->push_back(int(1));
|
|
|
|
boostvector->push_back(IntType(1));
|
|
|
|
stdvector->push_back(int(1));
|
2012-09-09 21:47:32 +00:00
|
|
|
if(!test::CheckEqualContainers(boostvector, stdvector)) return 1;
|
|
|
|
}
|
2013-02-24 13:13:36 +00:00
|
|
|
|
|
|
|
{ //test back()
|
|
|
|
const IntType test_this(1);
|
|
|
|
if(test_this != boostvector->back()) return 1;
|
|
|
|
}
|
|
|
|
{ //pop_back with enough capacity
|
2012-09-09 21:47:32 +00:00
|
|
|
boostvector->pop_back();
|
|
|
|
boostvector->pop_back();
|
|
|
|
stdvector->pop_back();
|
|
|
|
stdvector->pop_back();
|
2011-08-26 18:26:44 +00:00
|
|
|
|
2012-09-09 21:47:32 +00:00
|
|
|
IntType push_back_this(1);
|
|
|
|
boostvector->push_back(boost::move(push_back_this));
|
|
|
|
stdvector->push_back(int(1));
|
|
|
|
boostvector->push_back(IntType(1));
|
|
|
|
stdvector->push_back(int(1));
|
2011-08-26 18:26:44 +00:00
|
|
|
if(!test::CheckEqualContainers(boostvector, stdvector)) return 1;
|
2012-09-09 21:47:32 +00:00
|
|
|
}
|
2011-08-26 18:26:44 +00:00
|
|
|
|
|
|
|
if(!vector_copyable_only(boostvector, stdvector
|
2011-12-22 20:15:57 +00:00
|
|
|
,container_detail::bool_<boost::container::test::is_copyable<IntType>::value>())){
|
2011-08-26 18:26:44 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
boostvector->erase(boostvector->begin());
|
|
|
|
stdvector->erase(stdvector->begin());
|
|
|
|
if(!test::CheckEqualContainers(boostvector, stdvector)) return 1;
|
|
|
|
|
|
|
|
for(int i = 0; i < max; ++i){
|
|
|
|
IntType insert_this(i);
|
|
|
|
boostvector->insert(boostvector->begin(), boost::move(insert_this));
|
|
|
|
stdvector->insert(stdvector->begin(), i);
|
|
|
|
boostvector->insert(boostvector->begin(), IntType(i));
|
|
|
|
stdvector->insert(stdvector->begin(), int(i));
|
|
|
|
}
|
|
|
|
if(!test::CheckEqualContainers(boostvector, stdvector)) return 1;
|
|
|
|
|
|
|
|
//Test insertion from list
|
|
|
|
{
|
|
|
|
std::list<int> l(50, int(1));
|
2012-09-01 11:01:03 +00:00
|
|
|
typename MyBoostVector::iterator it_insert =
|
|
|
|
boostvector->insert(boostvector->begin(), l.begin(), l.end());
|
|
|
|
if(boostvector->begin() != it_insert) return 1;
|
2011-08-26 18:26:44 +00:00
|
|
|
stdvector->insert(stdvector->begin(), l.begin(), l.end());
|
|
|
|
if(!test::CheckEqualContainers(boostvector, stdvector)) return 1;
|
|
|
|
boostvector->assign(l.begin(), l.end());
|
|
|
|
stdvector->assign(l.begin(), l.end());
|
|
|
|
if(!test::CheckEqualContainers(boostvector, stdvector)) return 1;
|
2012-09-01 11:01:03 +00:00
|
|
|
|
|
|
|
boostvector->clear();
|
|
|
|
stdvector->clear();
|
|
|
|
boostvector->assign(make_input_from_forward_iterator(l.begin()), make_input_from_forward_iterator(l.end()));
|
|
|
|
stdvector->assign(l.begin(), l.end());
|
|
|
|
if(!test::CheckEqualContainers(boostvector, stdvector)) return 1;
|
2011-08-26 18:26:44 +00:00
|
|
|
}
|
2012-09-09 21:47:32 +00:00
|
|
|
/* deque has no reserve or capacity
|
2011-08-26 18:26:44 +00:00
|
|
|
std::size_t cap = boostvector->capacity();
|
|
|
|
boostvector->reserve(cap*2);
|
|
|
|
stdvector->reserve(cap*2);
|
|
|
|
if(!test::CheckEqualContainers(boostvector, stdvector)) return 1;
|
|
|
|
boostvector->resize(0);
|
|
|
|
stdvector->resize(0);
|
|
|
|
if(!test::CheckEqualContainers(boostvector, stdvector)) return 1;
|
|
|
|
|
|
|
|
boostvector->resize(cap*2);
|
|
|
|
stdvector->resize(cap*2);
|
|
|
|
if(!test::CheckEqualContainers(boostvector, stdvector)) return 1;
|
|
|
|
|
|
|
|
boostvector->clear();
|
|
|
|
stdvector->clear();
|
|
|
|
boostvector->shrink_to_fit();
|
|
|
|
MyStdVector(*stdvector).swap(*stdvector);
|
|
|
|
if(!test::CheckEqualContainers(boostvector, stdvector)) return 1;
|
|
|
|
|
|
|
|
boostvector->resize(cap*2);
|
|
|
|
stdvector->resize(cap*2);
|
|
|
|
if(!test::CheckEqualContainers(boostvector, stdvector)) return 1;
|
|
|
|
*/
|
|
|
|
|
|
|
|
delete stdvector;
|
|
|
|
delete boostvector;
|
|
|
|
}
|
2013-01-10 10:55:50 +00:00
|
|
|
BOOST_CATCH(std::exception &ex){
|
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
2011-08-26 18:26:44 +00:00
|
|
|
std::cout << ex.what() << std::endl;
|
2013-01-10 10:55:50 +00:00
|
|
|
#endif
|
2011-08-26 18:26:44 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2013-01-10 10:55:50 +00:00
|
|
|
BOOST_CATCH_END
|
2011-08-26 18:26:44 +00:00
|
|
|
}
|
2013-03-16 19:31:32 +00:00
|
|
|
|
2011-08-26 18:26:44 +00:00
|
|
|
std::cout << std::endl << "Test OK!" << std::endl;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
} //namespace test{
|
|
|
|
} //namespace container {
|
|
|
|
} //namespace boost{
|
|
|
|
|
|
|
|
#include <boost/container/detail/config_end.hpp>
|
2012-09-01 11:01:03 +00:00
|
|
|
|
|
|
|
#endif //BOOST_CONTAINER_TEST_VECTOR_TEST_HEADER
|