test_insertion.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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_insertion.hpp"
  9. #include <algorithm>
  10. #include <boost/core/lightweight_test.hpp>
  11. #include <numeric>
  12. #include <vector>
  13. #include "any_types.hpp"
  14. #include "base_types.hpp"
  15. #include "function_types.hpp"
  16. #include "test_utilities.hpp"
  17. using namespace test_utilities;
  18. template<typename PolyCollection,typename ValueFactory,typename... Types>
  19. void test_insertion()
  20. {
  21. {
  22. using unregistered_type=boost::poly_collection::unregistered_type;
  23. using type=first_of<constraints<is_copy_constructible>,Types...>;
  24. PolyCollection p,p2;
  25. ValueFactory v;
  26. p2.insert(v.template make<type>());
  27. check_throw<unregistered_type>(
  28. [&]{p.insert(*p2.begin());},
  29. [&]{p.insert(p.end(),*p2.begin());},
  30. [&]{p.insert(p.cend(),*p2.begin());},
  31. [&]{p.insert(
  32. external_iterator(p2.begin()),external_iterator(p2.end()));},
  33. [&]{p.insert(
  34. p.end(),
  35. external_iterator(p2.begin()),external_iterator(p2.end()));},
  36. [&]{p.insert(
  37. p.cend(),
  38. external_iterator(p2.begin()),external_iterator(p2.end()));});
  39. }
  40. {
  41. using not_copy_constructible=
  42. boost::poly_collection::not_copy_constructible;
  43. using type=first_of<constraints<is_not_copy_constructible>,Types...>;
  44. PolyCollection p,p2;
  45. ValueFactory v;
  46. p.template register_types<type>();
  47. p2.insert(v.template make<type>());
  48. auto p2b=external_iterator(p2.begin()),
  49. p2e=external_iterator(p2.end());
  50. auto p2lb=external_iterator(p2.template begin<type>()),
  51. p2le=external_iterator(p2.template end<type>());
  52. check_throw<not_copy_constructible>(
  53. [&]{p.insert(*p2.begin());},
  54. [&]{p.insert(p.end(),*p2.begin());},
  55. [&]{p.insert(p.cend(),*p2.cbegin());},
  56. [&]{p.insert(p.end(typeid(type)),*p2.begin());},
  57. [&]{p.insert(p.cend(typeid(type)),*p2.begin());},
  58. [&]{p.insert(p.template end<type>(),*p2.begin());},
  59. [&]{p.insert(p.template cend<type>(),*p2.begin());},
  60. [&]{p.insert(p2b,p2e);},
  61. [&]{p.insert(p2lb,p2le);},
  62. [&]{p.insert(p2.begin(),p2.end());},
  63. [&]{p.insert(p2.begin(typeid(type)),p2.end(typeid(type)));},
  64. [&]{p.insert(p2.template begin<type>(),p2.template end<type>());},
  65. [&]{p.insert(p.end(),p2b,p2e);},
  66. [&]{p.insert(p.end(),p2lb,p2le);},
  67. [&]{p.insert(p.end(),p2.begin(),p2.end());},
  68. [&]{p.insert(p.end(),p2.begin(typeid(type)),p2.end(typeid(type)));},
  69. [&]{p.insert(
  70. p.end(),p2.template begin<type>(),p2.template end<type>());},
  71. [&]{p.insert(p.cend(),p2b,p2e);},
  72. [&]{p.insert(p.cend(),p2lb,p2le);},
  73. [&]{p.insert(p.cend(),p2.begin(),p2.end());},
  74. [&]{p.insert(p.cend(),p2.begin(typeid(type)),p2.end(typeid(type)));},
  75. [&]{p.insert(
  76. p.cend(),p2.template begin<type>(),p2.template end<type>());},
  77. [&]{p.insert(p.end(typeid(type)),p2b,p2e);},
  78. [&]{p.insert(p.cend(typeid(type)),p2b,p2e);},
  79. [&]{p.insert(p.template end<type>(),p2b,p2e);},
  80. [&]{p.insert(p.template cend<type>(),p2b,p2e);});
  81. }
  82. {
  83. PolyCollection p;
  84. ValueFactory v;
  85. fill<constraints<>,Types...>(p,v,2);
  86. do_((BOOST_TEST(
  87. is_last(
  88. p,typeid(Types),
  89. p.insert(constref_if_copy_constructible(v.template make<Types>())))
  90. ),0)...);
  91. }
  92. {
  93. PolyCollection p;
  94. ValueFactory v;
  95. fill<constraints<>,Types...>(p,v,2);
  96. auto& info=p.segment_traversal().begin()->type_info();
  97. do_((BOOST_TEST(
  98. info==typeid(Types)?
  99. is_first(
  100. p,typeid(Types),
  101. p.insert(
  102. p.cbegin(),
  103. constref_if_copy_constructible(v.template make<Types>()))):
  104. is_last(
  105. p,typeid(Types),
  106. p.insert(
  107. p.cbegin(),
  108. constref_if_copy_constructible(v.template make<Types>())))
  109. ),0)...);
  110. do_((BOOST_TEST(
  111. is_first(
  112. p,typeid(Types),
  113. p.insert(
  114. p.cbegin(typeid(Types)),
  115. constref_if_copy_constructible(v.template make<Types>())))
  116. ),0)...);
  117. do_((BOOST_TEST(
  118. is_first<Types>(
  119. p,
  120. p.insert(
  121. p.template cbegin<Types>(),
  122. constref_if_copy_constructible(v.template make<Types>())))
  123. ),0)...);
  124. }
  125. {
  126. PolyCollection p,p2;
  127. ValueFactory v;
  128. p.template register_types<Types...>();
  129. p2.template register_types<Types...>();
  130. fill<
  131. constraints<is_copy_constructible,is_equality_comparable>,
  132. Types...
  133. >(p2,v,2);
  134. p.insert(external_iterator(p2.begin()),external_iterator(p2.end()));
  135. BOOST_TEST(p==p2);
  136. p.clear();
  137. p.insert(p2.begin(),p2.end());
  138. BOOST_TEST(p==p2);
  139. p.clear();
  140. p.insert(p2.cbegin(),p2.cend());
  141. BOOST_TEST(p==p2);
  142. p.clear();
  143. for(auto s:p2.segment_traversal()){
  144. p.insert(s.begin(),s.end());
  145. BOOST_TEST(p.size()==p2.size(s.type_info()));
  146. p.clear();
  147. p.insert(s.cbegin(),s.cend());
  148. BOOST_TEST(p.size()==p2.size(s.type_info()));
  149. p.clear();
  150. }
  151. do_((
  152. p.insert(
  153. external_iterator(p2.template begin<Types>()),
  154. external_iterator(p2.template end<Types>())),
  155. BOOST_TEST(p.size()==p2.template size<Types>()),
  156. p.clear()
  157. ,0)...);
  158. do_((
  159. p.insert(p2.template begin<Types>(),p2.template end<Types>()),
  160. BOOST_TEST(p.size()==p2.template size<Types>()),
  161. p.clear()
  162. ,0)...);
  163. do_((
  164. p.insert(p2.template cbegin<Types>(),p2.template cend<Types>()),
  165. BOOST_TEST(p.size()==p2.template size<Types>()),
  166. p.clear()
  167. ,0)...);
  168. }
  169. {
  170. PolyCollection p,p1,p2;
  171. ValueFactory v;
  172. p2.template register_types<Types...>();
  173. fill<
  174. constraints<is_copy_constructible,is_equality_comparable>,
  175. Types...
  176. >(p1,v,2);
  177. fill<
  178. constraints<is_copy_constructible,is_equality_comparable>,
  179. Types...
  180. >(p2,v,2);
  181. auto remove_original=[](PolyCollection& p)
  182. {
  183. bool first=true;
  184. for(auto s:p.segment_traversal()){
  185. if(first)p.erase(s.end()-2,s.end()),first=false;
  186. else p.erase(s.begin(),s.begin()+2);
  187. }
  188. };
  189. p=p1;
  190. p.insert(
  191. p.begin(),external_iterator(p2.begin()),external_iterator(p2.end()));
  192. remove_original(p);
  193. BOOST_TEST(p==p2);
  194. p=p1;
  195. p.insert(p.begin(),p2.begin(),p2.end());
  196. remove_original(p);
  197. BOOST_TEST(p==p2);
  198. p=p1;
  199. p.insert(p.begin(),p2.cbegin(),p2.cend());
  200. remove_original(p);
  201. BOOST_TEST(p==p2);
  202. p=p1;
  203. for(auto s:p2.segment_traversal())p.insert(p.begin(),s.begin(),s.end());
  204. remove_original(p);
  205. BOOST_TEST(p==p2);
  206. p=p1;
  207. for(auto s:p2.segment_traversal())p.insert(p.begin(),s.cbegin(),s.cend());
  208. remove_original(p);
  209. BOOST_TEST(p==p2);
  210. p=p1;
  211. do_((p.insert(
  212. p.begin(),
  213. external_iterator(p2.template begin<Types>()),
  214. external_iterator(p2.template end<Types>())),0)...);
  215. remove_original(p);
  216. BOOST_TEST(p==p2);
  217. p=p1;
  218. do_((p.insert(
  219. p.begin(),p2.template begin<Types>(),p2.template end<Types>()),0)...);
  220. remove_original(p);
  221. BOOST_TEST(p==p2);
  222. p=p1;
  223. do_((p.insert(
  224. p.begin(),p2.template cbegin<Types>(),p2.template cend<Types>()),0)...);
  225. remove_original(p);
  226. BOOST_TEST(p==p2);
  227. }
  228. {
  229. using type=first_of<
  230. constraints<is_copy_constructible,is_equality_comparable>,
  231. Types...
  232. >;
  233. PolyCollection p,p1,p2;
  234. ValueFactory v;
  235. fill<constraints<>,type>(p1,v,2);
  236. fill<constraints<>,type>(p2,v,2);
  237. auto remove_original=[](PolyCollection& p)
  238. {
  239. auto it=p.segment_traversal().begin()->end();
  240. p.erase(it-2,it);
  241. };
  242. p=p1;
  243. BOOST_TEST(is_first(
  244. p,typeid(type),
  245. p.insert(
  246. p.begin(typeid(type)),
  247. external_iterator(p2.begin()),external_iterator(p2.end()))));
  248. remove_original(p);
  249. BOOST_TEST(p==p2);
  250. p=p1;
  251. BOOST_TEST(is_first(
  252. p,typeid(type),
  253. p.insert(
  254. p.cbegin(typeid(type)),
  255. external_iterator(p2.begin()),external_iterator(p2.end()))));
  256. remove_original(p);
  257. BOOST_TEST(p==p2);
  258. p=p1;
  259. BOOST_TEST(is_first<type>(
  260. p,
  261. p.insert(
  262. p.template begin<type>(),
  263. external_iterator(p2.begin()),external_iterator(p2.end()))));
  264. remove_original(p);
  265. BOOST_TEST(p==p2);
  266. p=p1;
  267. BOOST_TEST(is_first<type>(
  268. p,
  269. p.insert(
  270. p.template cbegin<type>(),
  271. external_iterator(p2.begin()),external_iterator(p2.end()))));
  272. remove_original(p);
  273. BOOST_TEST(p==p2);
  274. }
  275. {
  276. using type=first_of<
  277. constraints<
  278. is_constructible_from_int,is_not_copy_constructible,
  279. is_equality_comparable
  280. >,
  281. Types...
  282. >;
  283. PolyCollection p;
  284. std::vector<int> s(10);
  285. ValueFactory v;
  286. fill<constraints<>,type>(p,v,2);
  287. std::iota(s.begin(),s.end(),0);
  288. BOOST_TEST(is_first<type>(
  289. p,
  290. p.insert(p.template begin<type>(),s.begin(),s.end())));
  291. BOOST_TEST(
  292. std::equal(s.begin(),s.end(),p.template begin<type>(),
  293. [](int x,const type& y){return type{x}==y;})
  294. );
  295. }
  296. }
  297. void test_insertion()
  298. {
  299. test_insertion<
  300. any_types::collection,auto_increment,
  301. any_types::t1,any_types::t2,any_types::t3,
  302. any_types::t4,any_types::t5>();
  303. test_insertion<
  304. base_types::collection,auto_increment,
  305. base_types::t1,base_types::t2,base_types::t3,
  306. base_types::t4,base_types::t5>();
  307. test_insertion<
  308. function_types::collection,auto_increment,
  309. function_types::t1,function_types::t2,function_types::t3,
  310. function_types::t4,function_types::t5>();
  311. }