File: test_capacity.cpp

package info (click to toggle)
boost1.88 1.88.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 576,932 kB
  • sloc: cpp: 4,149,234; xml: 136,789; ansic: 35,092; python: 33,910; asm: 5,698; sh: 4,604; ada: 1,681; makefile: 1,633; pascal: 1,139; perl: 1,124; sql: 640; yacc: 478; ruby: 271; java: 77; lisp: 24; csh: 6
file content (98 lines) | stat: -rw-r--r-- 2,932 bytes parent folder | download | duplicates (3)
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
/* Copyright 2016-2024 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_capacity.hpp"

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

using namespace test_utilities;

template<typename PolyCollection,typename ValueFactory,typename... Types>
void test_capacity()
{
  PolyCollection        p;
  const PolyCollection& cp=p;
  ValueFactory          v;

  BOOST_TEST(cp.empty());
  BOOST_TEST(cp.size()==0);

  register_types<Types...>(p);
  BOOST_TEST(cp.empty());
  do_((BOOST_TEST(cp.empty(typeid_<Types>(cp))),0)...);
  do_((BOOST_TEST(cp.template empty<Types>()),0)...);
  BOOST_TEST(cp.size()==0);
  do_((BOOST_TEST(cp.size(typeid_<Types>(cp))==0),0)...);
  do_((BOOST_TEST(cp.template size<Types>()==0),0)...);

  p.reserve(10);
  do_((BOOST_TEST(cp.capacity(typeid_<Types>(cp))>=10),0)...);
  do_((BOOST_TEST(
    cp.template capacity<Types>()==cp.capacity(typeid_<Types>(cp))),0)...);

  do_((p.reserve(typeid_<Types>(p),20),0)...);
  do_((BOOST_TEST(cp.capacity(typeid_<Types>(cp))>=20),0)...);

  do_((p.template reserve<Types>(30),0)...);
  do_((BOOST_TEST(cp.template capacity<Types>()>=30),0)...);

  fill<constraints<>,Types...>(p,v,30);
  BOOST_TEST(cp.size()==30*sizeof...(Types));
  do_((BOOST_TEST(cp.size(typeid_<Types>(cp))==30),0)...);
  do_((BOOST_TEST(
    cp.template size<Types>()==cp.size(typeid_<Types>(cp))),0)...);

  auto min_capacity=[&]{
    return (std::min)({cp.template capacity<Types>()...});
  };

  p.reserve(min_capacity()+1);
  BOOST_TEST(cp.size()==30*sizeof...(Types));

  auto c=min_capacity();
  p.shrink_to_fit();
  BOOST_TEST(c>=min_capacity());
  c=min_capacity();

  do_((p.erase(cp.template begin<Types>()),0)...);
  BOOST_TEST(c==min_capacity());

  do_((p.shrink_to_fit(typeid_<Types>(p)),0)...);
  BOOST_TEST(c>=min_capacity());
  c=min_capacity();

  p.clear();
  do_((p.template shrink_to_fit<Types>(),0)...);
  BOOST_TEST(c>=min_capacity());
}

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