test_capacity.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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_capacity.hpp"
  9. #include <algorithm>
  10. #include <boost/core/lightweight_test.hpp>
  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 PolyCollection,typename ValueFactory,typename... Types>
  17. void test_capacity()
  18. {
  19. PolyCollection p;
  20. const PolyCollection& cp=p;
  21. ValueFactory v;
  22. BOOST_TEST(cp.empty());
  23. BOOST_TEST(cp.size()==0);
  24. p.template register_types<Types...>();
  25. BOOST_TEST(cp.empty());
  26. do_((BOOST_TEST(cp.empty(typeid(Types))),0)...);
  27. do_((BOOST_TEST(cp.template empty<Types>()),0)...);
  28. BOOST_TEST(cp.size()==0);
  29. do_((BOOST_TEST(cp.size(typeid(Types))==0),0)...);
  30. do_((BOOST_TEST(cp.template size<Types>()==0),0)...);
  31. p.reserve(10);
  32. do_((BOOST_TEST(cp.capacity(typeid(Types))>=10),0)...);
  33. do_((BOOST_TEST(
  34. cp.template capacity<Types>()==cp.capacity(typeid(Types))),0)...);
  35. do_((p.reserve(typeid(Types),20),0)...);
  36. do_((BOOST_TEST(cp.capacity(typeid(Types))>=20),0)...);
  37. do_((p.template reserve<Types>(30),0)...);
  38. do_((BOOST_TEST(cp.template capacity<Types>()>=30),0)...);
  39. fill<constraints<>,Types...>(p,v,30);
  40. BOOST_TEST(cp.size()==30*sizeof...(Types));
  41. do_((BOOST_TEST(cp.size(typeid(Types))==30),0)...);
  42. do_((BOOST_TEST(cp.template size<Types>()==cp.size(typeid(Types))),0)...);
  43. auto min_capacity=[&]{
  44. return (std::min)({cp.template capacity<Types>()...});
  45. };
  46. p.reserve(min_capacity()+1);
  47. BOOST_TEST(cp.size()==30*sizeof...(Types));
  48. auto c=min_capacity();
  49. p.shrink_to_fit();
  50. BOOST_TEST(c>=min_capacity());
  51. c=min_capacity();
  52. do_((p.erase(cp.template begin<Types>()),0)...);
  53. BOOST_TEST(c==min_capacity());
  54. do_((p.shrink_to_fit(typeid(Types)),0)...);
  55. BOOST_TEST(c>=min_capacity());
  56. c=min_capacity();
  57. p.clear();
  58. do_((p.template shrink_to_fit<Types>(),0)...);
  59. BOOST_TEST(c>=min_capacity());
  60. }
  61. void test_capacity()
  62. {
  63. test_capacity<
  64. any_types::collection,auto_increment,
  65. any_types::t1,any_types::t2,any_types::t3,
  66. any_types::t4,any_types::t5>();
  67. test_capacity<
  68. base_types::collection,auto_increment,
  69. base_types::t1,base_types::t2,base_types::t3,
  70. base_types::t4,base_types::t5>();
  71. test_capacity<
  72. function_types::collection,auto_increment,
  73. function_types::t1,function_types::t2,function_types::t3,
  74. function_types::t4,function_types::t5>();
  75. }