• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2016-2017 Joaquin M Lopez Munoz.
2  * Distributed under the Boost Software License, Version 1.0.
3  * (See accompanying file LICENSE_1_0.txt or copy at
4  * http://www.boost.org/LICENSE_1_0.txt)
5  *
6  * See http://www.boost.org/libs/poly_collection for library home page.
7  */
8 
9 #include "test_erasure.hpp"
10 
11 #include <boost/core/lightweight_test.hpp>
12 #include <iterator>
13 #include "any_types.hpp"
14 #include "base_types.hpp"
15 #include "function_types.hpp"
16 #include "test_utilities.hpp"
17 
18 using namespace test_utilities;
19 
20 template<typename Type,typename PolyCollection>
test_local_erase(const PolyCollection & p2)21 void test_local_erase(const PolyCollection& p2)
22 {
23   using size_type=typename PolyCollection::size_type;
24 
25   for(size_type i=0;i<p2.template size<Type>();++i){
26     PolyCollection p=p2;
27     auto it=p.erase(p.template cbegin<Type>()+i);
28     BOOST_TEST(it-p.template begin<Type>()==(std::ptrdiff_t)i);
29     BOOST_TEST(p.template size<Type>()==p2.template size<Type>()-1);
30   }
31 }
32 
33 template<typename Type,typename PolyCollection>
test_local_range_erase(const PolyCollection & p2)34 void test_local_range_erase(const PolyCollection& p2)
35 {
36   using size_type=typename PolyCollection::size_type;
37 
38   for(size_type i=0;i<=p2.template size<Type>();++i){
39     for(size_type j=i;j<=p2.template size<Type>();++j){
40       PolyCollection p=p2;
41       auto first=p.template cbegin<Type>()+i,
42            last=p.template cbegin<Type>()+j;
43       auto it=p.erase(first,last);
44       BOOST_TEST(it-p.template begin<Type>()==(std::ptrdiff_t)i);
45       BOOST_TEST(p.template size<Type>()==p2.template size<Type>()-(j-i));
46     }
47   }
48 }
49 
50 template<typename Type,typename PolyCollection>
test_local_clear(const PolyCollection & p2)51 void test_local_clear(const PolyCollection& p2)
52 {
53   PolyCollection p=p2;
54   p.template clear<Type>();
55   BOOST_TEST(p.template empty<Type>());
56   BOOST_TEST(p.size()==p2.size()-p2.template size<Type>());
57 }
58 
59 template<typename PolyCollection,typename ValueFactory,typename... Types>
test_erasure()60 void test_erasure()
61 {
62   using size_type=typename PolyCollection::size_type;
63 
64   PolyCollection p,p2;
65   ValueFactory   v;
66 
67   fill<constraints<is_copy_constructible>,Types...>(p2,v,5);
68   auto sit=p2.segment_traversal().begin();
69   p2.clear(sit->type_info());
70   ++sit;++sit;
71   p2.clear(sit->type_info());
72 
73   for(size_type i=0;i<p2.size();++i){
74     p=p2;
75     auto it=p.erase(std::next(p.cbegin(),i));
76     BOOST_TEST(std::distance(p.begin(),it)==(std::ptrdiff_t)i);
77     BOOST_TEST(p.size()==p2.size()-1);
78   }
79 
80   for(auto s:p2.segment_traversal()){
81     auto& info=s.type_info();
82     for(size_type i=0;i<p2.size(info);++i){
83       p=p2;
84       auto it=p.erase(p.cbegin(info)+i);
85       BOOST_TEST(it-p.begin(info)==(std::ptrdiff_t)i);
86       BOOST_TEST(p.size(info)==p2.size(info)-1);
87     }
88   }
89 
90   do_((
91     p2.template is_registered<Types>()?test_local_erase<Types>(p2),0:0)...);
92 
93   for(size_type i=0;i<=p2.size();++i){
94     for(size_type j=i;j<=p2.size();++j){
95       p=p2;
96       auto first=std::next(p.cbegin(),i),
97             last=std::next(p.cbegin(),j);
98       auto it=p.erase(first,last);
99       BOOST_TEST(std::distance(p.begin(),it)==(std::ptrdiff_t)i);
100       BOOST_TEST(p.size()==p2.size()-(j-i));
101     }
102   }
103 
104   for(auto s:p2.segment_traversal()){
105     auto& info=s.type_info();
106     for(size_type i=0;i<=p2.size(info);++i){
107       for(size_type j=i;j<=p2.size(info);++j){
108         p=p2;
109         auto first=p.cbegin(info)+i,
110               last=p.cbegin(info)+j;
111         auto it=p.erase(first,last);
112         BOOST_TEST(it-p.begin(info)==(std::ptrdiff_t)i);
113         BOOST_TEST(p.size(info)==p2.size(info)-(j-i));
114       }
115     }
116   }
117 
118   do_((p2.template is_registered<Types>()?
119     test_local_range_erase<Types>(p2),0:0)...);
120 
121   p=p2;
122   p.clear();
123   BOOST_TEST(p.empty());
124 
125   for(auto s:p2.segment_traversal()){
126     auto& info=s.type_info();
127     p=p2;
128     p.clear(info);
129     BOOST_TEST(p.empty(info));
130     BOOST_TEST(p.size()==p2.size()-p2.size(info));
131   }
132 
133   do_((p2.template is_registered<Types>()?
134     test_local_clear<Types>(p2),0:0)...);
135 }
136 
test_erasure()137 void test_erasure()
138 {
139   test_erasure<
140     any_types::collection,auto_increment,
141     any_types::t1,any_types::t2,any_types::t3,
142     any_types::t4,any_types::t5>();
143   test_erasure<
144     base_types::collection,auto_increment,
145     base_types::t1,base_types::t2,base_types::t3,
146     base_types::t4,base_types::t5>();
147   test_erasure<
148     function_types::collection,auto_increment,
149     function_types::t1,function_types::t2,function_types::t3,
150     function_types::t4,function_types::t5>();
151 }
152