stdcpp/tsrc/Boost_test/multi_index/src/test_copy_assignment.cpp
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 /* Boost.MultiIndex test for copying and assignment.
       
     2  *
       
     3  * Copyright 2003-2006 Joaquín M López Muñoz.
       
     4  * Distributed under the Boost Software License, Version 1.0.
       
     5  * (See accompanying file LICENSE_1_0.txt or copy at
       
     6  * http://www.boost.org/LICENSE_1_0.txt)
       
     7  *
       
     8  * See http://www.boost.org/libs/multi_index for library home page.
       
     9  */
       
    10 
       
    11 #include "test_copy_assignment.hpp"
       
    12 
       
    13 #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
       
    14 #include <algorithm>
       
    15 #include <list>
       
    16 #include <numeric>
       
    17 #include <vector>
       
    18 #include "pre_multi_index.hpp"
       
    19 #include "employee.hpp"
       
    20 #include <boost/test/test_tools.hpp>
       
    21 
       
    22 using namespace boost::multi_index;
       
    23 
       
    24 #if BOOST_WORKAROUND(__MWERKS__,<=0x3003)
       
    25 /* The "ISO C++ Template Parser" option makes CW8.3 incorrectly fail at
       
    26  * expressions of the form sizeof(x) where x is an array local to a
       
    27  * template function.
       
    28  */
       
    29 
       
    30 #pragma parse_func_templ off
       
    31 #endif
       
    32 
       
    33 template<typename Sequence>
       
    34 static void test_assign(BOOST_EXPLICIT_TEMPLATE_TYPE(Sequence))
       
    35 {
       
    36   Sequence s;
       
    37 
       
    38   int a[]={0,1,2,3,4,5};
       
    39   std::size_t sa=sizeof(a)/sizeof(a[0]);
       
    40 
       
    41   s.assign(&a[0],&a[sa]);
       
    42 
       
    43   BOOST_CHECK(s.size()==sa&&std::equal(s.begin(),s.end(),&a[0]));
       
    44 
       
    45   s.assign(&a[0],&a[sa]);
       
    46 
       
    47   BOOST_CHECK(s.size()==sa&&std::equal(s.begin(),s.end(),&a[0]));
       
    48 
       
    49   s.assign((std::size_t)18,37);
       
    50   BOOST_CHECK(s.size()==18&&std::accumulate(s.begin(),s.end(),0)==666);
       
    51 
       
    52   s.assign((std::size_t)12,167);
       
    53   BOOST_CHECK(s.size()==12&&std::accumulate(s.begin(),s.end(),0)==2004);
       
    54 }
       
    55 
       
    56 #if BOOST_WORKAROUND(__MWERKS__,<=0x3003)
       
    57 #pragma parse_func_templ reset
       
    58 #endif
       
    59 
       
    60 void test_copy_assignment()
       
    61 {
       
    62   employee_set es;
       
    63   employee_set es2(es);
       
    64 
       
    65   employee_set::allocator_type al=es.get_allocator();
       
    66   al=get<1>(es).get_allocator();
       
    67   al=get<2>(es).get_allocator();
       
    68   al=get<3>(es).get_allocator();
       
    69   al=get<4>(es).get_allocator();
       
    70   al=get<5>(es).get_allocator();
       
    71 
       
    72   BOOST_CHECK(es2.empty());
       
    73 
       
    74   es2.insert(employee(0,"Joe",31,1123));
       
    75   es2.insert(employee(1,"Robert",27,5601));
       
    76   es2.insert(employee(2,"John",40,7889));
       
    77   es2.insert(employee(2,"Aristotle",2388,3357)); /* clash */
       
    78   es2.insert(employee(3,"Albert",20,9012));
       
    79   es2.insert(employee(4,"John",57,1002));
       
    80   es2.insert(employee(0,"Andrew",60,2302));      /* clash */
       
    81 
       
    82   employee_set es3(es2);
       
    83 
       
    84   BOOST_CHECK(es2==es3);
       
    85   BOOST_CHECK(get<2>(es2)==get<2>(es3));
       
    86   BOOST_CHECK(get<3>(es2)==get<3>(es3));
       
    87   BOOST_CHECK(get<5>(es2)==get<5>(es3));
       
    88 
       
    89   employee_set es4;
       
    90   employee_set_by_name& i1=get<name>(es4);
       
    91   i1=get<1>(es2);
       
    92 
       
    93   BOOST_CHECK(es4==es2);
       
    94 
       
    95   employee_set es5;
       
    96   employee_set_by_age& i2=get<age>(es5);
       
    97   i2=get<2>(es2);
       
    98 
       
    99   BOOST_CHECK(i2==get<2>(es2));
       
   100 
       
   101   employee_set es6;
       
   102   employee_set_as_inserted& i3=get<as_inserted>(es6);
       
   103   i3=get<3>(es2);
       
   104 
       
   105   BOOST_CHECK(i3==get<3>(es2));
       
   106 
       
   107   employee_set es7;
       
   108   employee_set_randomly& i5=get<randomly>(es7);
       
   109   i5=get<5>(es2);
       
   110 
       
   111   BOOST_CHECK(i5==get<5>(es2));
       
   112 
       
   113   std::list<employee> l;
       
   114   l.push_back(employee(3,"Anna",31,5388));
       
   115   l.push_back(employee(1,"Rachel",27,9012));
       
   116   l.push_back(employee(2,"Agatha",40,1520));
       
   117 
       
   118 #if BOOST_WORKAROUND(BOOST_MSVC,<1300)
       
   119   employee_set es8;
       
   120   es8.insert(l.begin(),l.end());
       
   121 #else
       
   122   employee_set es8(l.begin(),l.end());
       
   123 #endif
       
   124 
       
   125   l.sort();
       
   126 
       
   127   BOOST_CHECK(es8.size()==l.size()&&
       
   128               std::equal(es8.begin(),es8.end(),l.begin()));
       
   129 
       
   130   /* MSVC++ 6.0 chokes on test_assign without this explicit instantiation */
       
   131   multi_index_container<int,indexed_by<sequenced<> > > s1;
       
   132   test_assign<multi_index_container<int,indexed_by<sequenced<> > > >();
       
   133 
       
   134   multi_index_container<int,indexed_by<random_access<> > > s2;
       
   135   test_assign<multi_index_container<int,indexed_by<random_access<> > > >();
       
   136 }