Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
23.vector.capacity.cpp
Go to the documentation of this file.
1
29#include <stdexcept> // for length_error
30#include <vector> // for vector
31
32#include <rw_alg_test.h>
33#include <rw_value.h> // for UserClass
34#include <rw_driver.h>
35
36
38static unsigned rw_opt_nloops = 1024;
39static int rw_opt_no_exceptions;
40
41
43typedef std::vector<UserClass, std::allocator<UserClass> > Vector;
44
45// exercise vector<>::capacity() and vector<>::reserve()
46// focus on the complexity of the function
47void test_capacity (Vector::size_type nelems)
48{
49 // create a vector with nelems elements
50 Vector v (nelems);
51
52 const Vector::size_type cap = v.capacity ();
53 const Vector::size_type size = v.size ();
54 Vector::const_pointer begin = size ? &v [0] : 0;
55
57
58 // call reserve capacity that is less than or equal to the current value
59 v.reserve (cap / 2);
60
61 // verify that the call had no effect
62 rw_assert (v.capacity () == cap, 0, __LINE__,
63 "vector<UserClass>(%zu).reserve(%zu); capacity() == %zu, "
64 "got %zu", nelems, cap / 2, cap, v.capacity ());
65
66 if (size) {
67 // verify that no reallocation took place
68 rw_assert (begin == &v [0], 0, __LINE__,
69 "vector<UserClass>(%zu).reserve(%zu) unexpectedly "
70 "reallocated", nelems, cap / 2);
71 }
72
73 // call reserve the same capacity as the current value
74 v.reserve (cap);
75
76 // verify that the call had no effect
77 rw_assert (v.capacity () == cap, 0, __LINE__,
78 "vector<UserClass>(%zu).reserve(%zu); capacity() == %zu, "
79 "got %zu", nelems, cap / 2, cap, v.capacity ());
80
81 if (size) {
82 // verify that no reallocation took place
83 rw_assert (begin == &v [0], 0, __LINE__,
84 "vector<UserClass>(%zu).reserve(%zu) unexpectedly "
85 "reallocated", nelems, cap / 2);
86 }
87
88 // call reserve with a larger capacity then is available
89 v.reserve (cap + 1);
90
91 // 23.2.4.2, p2: After reserve (), capacity () is greater or equal
92 // to the reserve value if reallocation happens
93 rw_assert (v.capacity () >= cap + 1, 0, __LINE__,
94 "vector<UserClass>(%zu).reserve(%zu); capacity() > %zu, got %zu",
95 nelems, cap + 1, cap, v.capacity ());
96
97 // 23.2.3.2, p3: reserve shall not change the size of the sequence
98 rw_assert (v.size () == size, 0, __LINE__,
99 "vector<UserClass>(%zu).reserve(); size() == %zu, got %zu",
100 nelems, size, v.size ());
101
102 // 23.2.3.2, p3: takes at most linear time in the size of the sequence
103 rw_assert (UserClass::n_total_copy_ctor_ == v.size (), 0, __LINE__,
104 "vector<UserClass>(%zu).reserve(%zu) complexity: "
105 "copy ctor called %zu times when size() = %zu",
106 nelems, cap + 1, UserClass::n_total_copy_ctor_, v.size ());
107
108 if (size) {
109 begin = &v [0];
110
111 // verify 23.2.4.2, p5: no reallocation takes place until
112 // the size of the container would exceed its capacity
113 for (Vector::size_type i = 0; i != v.capacity () - size; ++i) {
114 v.push_back (UserClass ());
115
116 rw_assert (begin == &v [0], 0, __LINE__,
117 "vector<UserClass>(%zu).reserve(%zu); insertion of "
118 "element %zu unexpectedly reallocated; size() = %zu",
119 nelems, cap + 1, v.size ());
120 }
121 }
122
123 if (rw_opt_no_exceptions)
124 return;
125
126#ifndef _RWSTD_NO_EXCEPTIONS
127
128 // exercise reserve exception
129 const char *caught = 0;
130
131 Vector::size_type too_much = v.max_size () + 1;
132
133 if (!too_much)
134 too_much = v.max_size ();
135
136 try {
137 v.reserve (too_much);
138 }
139 catch (std::length_error) {
140 caught = "";
141 }
142 catch (...) {
143 caught = "unknown exception";
144 }
145
146 rw_assert (0 != caught, 0, __LINE__,
147 "vector<UserClass>(%zu).reserve(%zu) "
148 "expected exception not thrown",
149 nelems, too_much);
150
151 if (caught)
152 rw_assert ('\0' == *caught, 0, __LINE__,
153 "vector<UserClass>(%zu).reserve(%zu) "
154 "expected length_error, got %s",
155 nelems, too_much, caught);
156
157#endif // _RWSTD_NO_EXCEPTIONS
158
159}
160
161
163static int
164run_test (int /* argc */, char** /* argv */)
165{
166 rw_info (0, 0, 0, "std::vector<UserClass>::capacity() const");
167 rw_info (0, 0, 0, "std::vector<UserClass>::reserve(size_type)");
168
169 const Vector::size_type max_elems = Vector::size_type (rw_opt_nloops);
170
171 rw_note (0 == rw_opt_no_exceptions, 0, 0, "exception test disabled");
172
173 for (Vector::size_type i = 0; i < max_elems; ++i) {
175 }
176
177 return 0;
178}
179
180
182int main (int argc, char** argv)
183{
184 return rw_test (argc, argv, __FILE__,
185 "lib.vector.capacity",
186 0 /* no comment */,
187 run_test,
188 "|-no-exceptions# "
189 "|-nloops#0",
190 &rw_opt_no_exceptions,
192}
#define i
Definition 17.names.cpp:126
#define v
Definition 17.names.cpp:138
std::vector< UserClass, std::allocator< UserClass > > Vector
void test_capacity(Vector::size_type nelems)
int rw_opt_nloops
Definition 25.copy.cpp:235
void run_test(const ForwardIterator &dummy, T *, Predicate *pred)
#define main
int caught
_TEST_EXPORT int rw_test(int argc, char *argv[], const char *filename, const char *clause, const char *comment, int(*testfun)(int, char **), const char *optspec,...)
_TEST_EXPORT int rw_info(int, const char *, int, const char *,...)
Definition driver.cpp:1681
_TEST_EXPORT int rw_note(int, const char *, int, const char *,...)
Definition driver.cpp:1664
_TEST_EXPORT int rw_assert(int, const char *, int, const char *,...)
Definition driver.cpp:1630
static void reset_totals()
Definition value.cpp:452
static _RWSTD_SIZE_T n_total_copy_ctor_
Definition rw_value.h:125