test_erasure.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. #include "test_erasure.hpp"
  9. #include <boost/core/lightweight_test.hpp>
  10. #include <iterator>
  11. #include "any_types.hpp"
  12. #include "base_types.hpp"
  13. #include "function_types.hpp"
  14. #include "test_utilities.hpp"
  15. using namespace test_utilities;
  16. template<typename Type,typename PolyCollection>
  17. void test_local_erase(const PolyCollection& p2)
  18. {
  19. using size_type=typename PolyCollection::size_type;
  20. for(size_type i=0;i<p2.template size<Type>();++i){
  21. PolyCollection p=p2;
  22. auto it=p.erase(p.template cbegin<Type>()+i);
  23. BOOST_TEST(it-p.template begin<Type>()==(std::ptrdiff_t)i);
  24. BOOST_TEST(p.template size<Type>()==p2.template size<Type>()-1);
  25. }
  26. }
  27. template<typename Type,typename PolyCollection>
  28. void test_local_range_erase(const PolyCollection& p2)
  29. {
  30. using size_type=typename PolyCollection::size_type;
  31. for(size_type i=0;i<=p2.template size<Type>();++i){
  32. for(size_type j=i;j<=p2.template size<Type>();++j){
  33. PolyCollection p=p2;
  34. auto first=p.template cbegin<Type>()+i,
  35. last=p.template cbegin<Type>()+j;
  36. auto it=p.erase(first,last);
  37. BOOST_TEST(it-p.template begin<Type>()==(std::ptrdiff_t)i);
  38. BOOST_TEST(p.template size<Type>()==p2.template size<Type>()-(j-i));
  39. }
  40. }
  41. }
  42. template<typename Type,typename PolyCollection>
  43. void test_local_clear(const PolyCollection& p2)
  44. {
  45. PolyCollection p=p2;
  46. p.template clear<Type>();
  47. BOOST_TEST(p.template empty<Type>());
  48. BOOST_TEST(p.size()==p2.size()-p2.template size<Type>());
  49. }
  50. template<typename PolyCollection,typename ValueFactory,typename... Types>
  51. void test_erasure()
  52. {
  53. using size_type=typename PolyCollection::size_type;
  54. PolyCollection p,p2;
  55. ValueFactory v;
  56. fill<constraints<is_copy_constructible>,Types...>(p2,v,5);
  57. auto sit=p2.segment_traversal().begin();
  58. p2.clear(sit->type_info());
  59. ++sit;++sit;
  60. p2.clear(sit->type_info());
  61. for(size_type i=0;i<p2.size();++i){
  62. p=p2;
  63. auto it=p.erase(std::next(p.cbegin(),i));
  64. BOOST_TEST(std::distance(p.begin(),it)==(std::ptrdiff_t)i);
  65. BOOST_TEST(p.size()==p2.size()-1);
  66. }
  67. for(auto s:p2.segment_traversal()){
  68. auto& info=s.type_info();
  69. for(size_type i=0;i<p2.size(info);++i){
  70. p=p2;
  71. auto it=p.erase(p.cbegin(info)+i);
  72. BOOST_TEST(it-p.begin(info)==(std::ptrdiff_t)i);
  73. BOOST_TEST(p.size(info)==p2.size(info)-1);
  74. }
  75. }
  76. do_((
  77. p2.template is_registered<Types>()?test_local_erase<Types>(p2),0:0)...);
  78. for(size_type i=0;i<=p2.size();++i){
  79. for(size_type j=i;j<=p2.size();++j){
  80. p=p2;
  81. auto first=std::next(p.cbegin(),i),
  82. last=std::next(p.cbegin(),j);
  83. auto it=p.erase(first,last);
  84. BOOST_TEST(std::distance(p.begin(),it)==(std::ptrdiff_t)i);
  85. BOOST_TEST(p.size()==p2.size()-(j-i));
  86. }
  87. }
  88. for(auto s:p2.segment_traversal()){
  89. auto& info=s.type_info();
  90. for(size_type i=0;i<=p2.size(info);++i){
  91. for(size_type j=i;j<=p2.size(info);++j){
  92. p=p2;
  93. auto first=p.cbegin(info)+i,
  94. last=p.cbegin(info)+j;
  95. auto it=p.erase(first,last);
  96. BOOST_TEST(it-p.begin(info)==(std::ptrdiff_t)i);
  97. BOOST_TEST(p.size(info)==p2.size(info)-(j-i));
  98. }
  99. }
  100. }
  101. do_((p2.template is_registered<Types>()?
  102. test_local_range_erase<Types>(p2),0:0)...);
  103. p=p2;
  104. p.clear();
  105. BOOST_TEST(p.empty());
  106. for(auto s:p2.segment_traversal()){
  107. auto& info=s.type_info();
  108. p=p2;
  109. p.clear(info);
  110. BOOST_TEST(p.empty(info));
  111. BOOST_TEST(p.size()==p2.size()-p2.size(info));
  112. }
  113. do_((p2.template is_registered<Types>()?
  114. test_local_clear<Types>(p2),0:0)...);
  115. }
  116. void test_erasure()
  117. {
  118. test_erasure<
  119. any_types::collection,auto_increment,
  120. any_types::t1,any_types::t2,any_types::t3,
  121. any_types::t4,any_types::t5>();
  122. test_erasure<
  123. base_types::collection,auto_increment,
  124. base_types::t1,base_types::t2,base_types::t3,
  125. base_types::t4,base_types::t5>();
  126. test_erasure<
  127. function_types::collection,auto_increment,
  128. function_types::t1,function_types::t2,function_types::t3,
  129. function_types::t4,function_types::t5>();
  130. }