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