File: test_erasure.cpp

package info (click to toggle)
boost1.74 1.74.0-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 464,084 kB
  • sloc: cpp: 3,338,324; xml: 131,293; python: 33,088; ansic: 14,336; asm: 4,034; sh: 3,351; makefile: 1,193; perl: 1,036; yacc: 478; php: 212; ruby: 102; lisp: 24; sql: 13; csh: 6
file content (151 lines) | stat: -rw-r--r-- 4,356 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* Copyright 2016-2017 Joaquin M Lopez Munoz.
 * 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/poly_collection for library home page.
 */

#include "test_erasure.hpp"

#include <boost/core/lightweight_test.hpp>
#include <iterator>
#include "any_types.hpp"
#include "base_types.hpp"
#include "function_types.hpp"
#include "test_utilities.hpp"

using namespace test_utilities;

template<typename Type,typename PolyCollection>
void test_local_erase(const PolyCollection& p2)
{
  using size_type=typename PolyCollection::size_type;

  for(size_type i=0;i<p2.template size<Type>();++i){
    PolyCollection p=p2;
    auto it=p.erase(p.template cbegin<Type>()+i);
    BOOST_TEST(it-p.template begin<Type>()==(std::ptrdiff_t)i);
    BOOST_TEST(p.template size<Type>()==p2.template size<Type>()-1);
  }  
}

template<typename Type,typename PolyCollection>
void test_local_range_erase(const PolyCollection& p2)
{
  using size_type=typename PolyCollection::size_type;

  for(size_type i=0;i<=p2.template size<Type>();++i){
    for(size_type j=i;j<=p2.template size<Type>();++j){
      PolyCollection p=p2;
      auto first=p.template cbegin<Type>()+i,
           last=p.template cbegin<Type>()+j;
      auto it=p.erase(first,last);
      BOOST_TEST(it-p.template begin<Type>()==(std::ptrdiff_t)i);
      BOOST_TEST(p.template size<Type>()==p2.template size<Type>()-(j-i));
    }
  }
}

template<typename Type,typename PolyCollection>
void test_local_clear(const PolyCollection& p2)
{
  PolyCollection p=p2;
  p.template clear<Type>();
  BOOST_TEST(p.template empty<Type>());
  BOOST_TEST(p.size()==p2.size()-p2.template size<Type>());
}

template<typename PolyCollection,typename ValueFactory,typename... Types>
void test_erasure()
{
  using size_type=typename PolyCollection::size_type;

  PolyCollection p,p2;
  ValueFactory   v;

  fill<constraints<is_copy_constructible>,Types...>(p2,v,5);
  auto sit=p2.segment_traversal().begin();
  p2.clear(sit->type_info());
  ++sit;++sit;
  p2.clear(sit->type_info());

  for(size_type i=0;i<p2.size();++i){
    p=p2;
    auto it=p.erase(std::next(p.cbegin(),i));
    BOOST_TEST(std::distance(p.begin(),it)==(std::ptrdiff_t)i);
    BOOST_TEST(p.size()==p2.size()-1);
  }

  for(auto s:p2.segment_traversal()){
    auto& info=s.type_info();
    for(size_type i=0;i<p2.size(info);++i){
      p=p2;
      auto it=p.erase(p.cbegin(info)+i);
      BOOST_TEST(it-p.begin(info)==(std::ptrdiff_t)i);
      BOOST_TEST(p.size(info)==p2.size(info)-1);
    }
  }

  do_((
    p2.template is_registered<Types>()?test_local_erase<Types>(p2),0:0)...);

  for(size_type i=0;i<=p2.size();++i){
    for(size_type j=i;j<=p2.size();++j){
      p=p2;
      auto first=std::next(p.cbegin(),i),
            last=std::next(p.cbegin(),j);
      auto it=p.erase(first,last);
      BOOST_TEST(std::distance(p.begin(),it)==(std::ptrdiff_t)i);
      BOOST_TEST(p.size()==p2.size()-(j-i));
    }
  }

  for(auto s:p2.segment_traversal()){
    auto& info=s.type_info();
    for(size_type i=0;i<=p2.size(info);++i){
      for(size_type j=i;j<=p2.size(info);++j){
        p=p2;
        auto first=p.cbegin(info)+i,
              last=p.cbegin(info)+j;
        auto it=p.erase(first,last);
        BOOST_TEST(it-p.begin(info)==(std::ptrdiff_t)i);
        BOOST_TEST(p.size(info)==p2.size(info)-(j-i));
      }
    }
  }

  do_((p2.template is_registered<Types>()?
    test_local_range_erase<Types>(p2),0:0)...);

  p=p2;
  p.clear();
  BOOST_TEST(p.empty());

  for(auto s:p2.segment_traversal()){
    auto& info=s.type_info();
    p=p2;
    p.clear(info);
    BOOST_TEST(p.empty(info));
    BOOST_TEST(p.size()==p2.size()-p2.size(info));
  }

  do_((p2.template is_registered<Types>()?
    test_local_clear<Types>(p2),0:0)...);
}

void test_erasure()
{
  test_erasure<
    any_types::collection,auto_increment,
    any_types::t1,any_types::t2,any_types::t3,
    any_types::t4,any_types::t5>();
  test_erasure<
    base_types::collection,auto_increment,
    base_types::t1,base_types::t2,base_types::t3,
    base_types::t4,base_types::t5>();
  test_erasure<
    function_types::collection,auto_increment,
    function_types::t1,function_types::t2,function_types::t3,
    function_types::t4,function_types::t5>();
}