Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
23.deque.special.cpp
Go to the documentation of this file.
1
29#include <deque> // for deque
30
31#include <cstddef> // for size_t
32
33#include <rw_value.h> // for UserClass
34#include <rw_driver.h> // for rw_test(), ...
35
36
38struct DequeValueType { };
39
40typedef std::allocator<DequeValueType> DequeAllocator;
41typedef std::deque<DequeValueType, DequeAllocator> DequeType;
42
43
45
47
48// define an explicit specialization of the deque::swap() member
49// to verify tha the non-member swap function calls the member
50
52void DequeType::swap (DequeType&)
53{
55}
56
57} // namespace std
58
59
62{
63 rw_info (0, 0, 0,
64 "Testing std::swap (std::deque&, std::deque&) "
65 "calls std::deque::swap");
66
67 // verify the signature of the function specialization
68 void (*pswap)(DequeType&, DequeType&) =
69 &std::swap<DequeValueType, DequeAllocator>;
70
71 _RWSTD_UNUSED (pswap);
72
73 // verify that std::swap() calls std::deque::swap()
75
76 std::swap (d, d);
77
78 rw_assert (1 == deque_swap_called, 0, __LINE__,
79 "std::swap (std::deque<T, A>&, std::deque<T, A>&) called "
80 "std::deque<T, A>::swap (std::deque<T, A>&) exactly once; "
81 "got %d times", deque_swap_called);
82}
83
84
86typedef std::deque<UserClass, std::allocator<UserClass> > Deque;
87
88Deque::size_type new_capacity;
89
90namespace __rw {
91
93inline Deque::size_type
94__rw_new_capacity<Deque>(Deque::size_type n, const Deque*)
95{
96 if (n) {
97 // non-zero size argument indicates a request for an increase
98 // in the capacity of a deque object's dynamically sizable
99 // vector of nodes
100 return n * 2;
101 }
102
103 // zero size argument is a request for the initial size of a deque
104 // object's dynamically sizable vector of nodes or for the size of
105 // the objects's fixed-size buffer for elements
106 return new_capacity;
107}
108
109}
110
111
113template <class T, class Allocator>
114void test_swap (const T *lhs_seq, std::size_t lhs_seq_len,
115 const T *rhs_seq, std::size_t rhs_seq_len,
116 std::deque<T, Allocator>*,
117 const char *tname)
118{
119 typedef std::deque<T, Allocator> Deque;
120 typedef typename Deque::iterator Iterator;
121 typedef typename Deque::size_type SizeType;
122
123 // create two containers from the provided sequences
124 Deque lhs (lhs_seq, lhs_seq + lhs_seq_len);
125 Deque rhs (rhs_seq, rhs_seq + rhs_seq_len);
126
127 // save the begin and and iterators and the size
128 // of each container before swapping the objects
129 const Iterator lhs_begin_0 = lhs.begin ();
130 const Iterator lhs_end_0 = lhs.end ();
131 const SizeType lhs_size_0 = lhs.size ();
132
133 const Iterator rhs_begin_0 = rhs.begin ();
134 const Iterator rhs_end_0 = rhs.end ();
135 const SizeType rhs_size_0 = rhs.size ();
136
137 // swap the two containers
138 lhs.swap (rhs);
139
140 // compute the begin and and iterators and the size
141 // of each container after swapping the objects
142 const Iterator lhs_begin_1 = lhs.begin ();
143 const Iterator lhs_end_1 = lhs.end ();
144 const SizeType lhs_size_1 = lhs.size ();
145
146 const Iterator rhs_begin_1 = rhs.begin ();
147 const Iterator rhs_end_1 = rhs.end ();
148 const SizeType rhs_size_1 = rhs.size ();
149
150 static const int cwidth = sizeof (T);
151
152 // verify that the iterators and sizes
153 // of the two objects were swapped
154 rw_assert (lhs_begin_0 == rhs_begin_1 && lhs_begin_1 == rhs_begin_0,
155 0, __LINE__,
156 "begin() not swapped for \"%{X=*.*}\" and \"%{X=*.*}\"",
157 cwidth, int (lhs_seq_len), -1, lhs_seq,
158 cwidth, int (rhs_seq_len), -1, rhs_seq);
159
160 rw_assert (lhs_end_0 == rhs_end_1 && lhs_end_1 == rhs_end_0,
161 0, __LINE__,
162 "end() not swapped for \"%{X=*.*}\" and \"%{X=*.*}\"",
163 cwidth, int (lhs_seq_len), -1, lhs_seq,
164 cwidth, int (rhs_seq_len), -1, rhs_seq);
165
166 rw_assert (lhs_size_0 == rhs_size_1 && lhs_size_1 == rhs_size_0,
167 0, __LINE__,
168 "size() not swapped for \"%{X=*.*}\" and \"%{X=*.*}\"",
169 cwidth, int (lhs_seq_len), -1, lhs_seq,
170 cwidth, int (rhs_seq_len), -1, rhs_seq);
171
172 // swap one of the containers with an empty unnamed temporary
173 // container and verify that the object is empty
174 { Deque ().swap (lhs); }
175
176 const Iterator lhs_begin_2 = lhs.begin ();
177 const Iterator lhs_end_2 = lhs.end ();
178 const SizeType lhs_size_2 = lhs.size ();
179
180 rw_assert (lhs_begin_2 == lhs_end_2, 0, __LINE__,
181 "deque<%s>().begin() not swapped for \"%{X=*.*}\"",
182 tname, cwidth, int (rhs_seq_len), -1, rhs_seq);
183
184 rw_assert (0 == lhs_size_2, 0, __LINE__,
185 "deque<%s>().size() not swapped for \"%{X=*.*}\"",
186 tname, cwidth, int (rhs_seq_len), -1, rhs_seq);
187}
188
189
190template <class T>
191void test_swap (const T*, const char* tname)
192{
193 rw_info (0, 0, 0,
194 "std::deque<%s>::swap(deque<%1$s>&)", tname);
195
196 typedef std::deque<T, std::allocator<T> > MyDeque;
197 typedef typename MyDeque::iterator Iterator;
198
199 // create two empty deque objects
200 MyDeque empty [2];
201
202 // save their begin and end iterators before calling swap
203 const Iterator before [2][2] = {
204 { empty [0].begin (), empty [0].end () },
205 { empty [1].begin (), empty [1].end () }
206 };
207
208 // swap the two containers
209 empty [0].swap (empty [1]);
210
211 // get the new begin and end iterators
212 const Iterator after [2][2] = {
213 { empty [0].begin (), empty [0].end () },
214 { empty [1].begin (), empty [1].end () }
215 };
216
217 // verify that the iterators have not been invalidated
218 rw_assert ( before [0][0] == after [1][0]
219 && before [1][0] == after [0][0], 0, __LINE__,
220 "deque<%s>().begin() not swapped", tname);
221
222 rw_assert ( before [0][1] == after [1][1]
223 && before [1][1] == after [0][1], 0, __LINE__,
224 "deque<%s>().end() not swapped", tname);
225
226 // static to zero-initialize if T is a POD type
227 static T seq [32];
228
229 const std::size_t seq_len = sizeof seq / sizeof *seq;
230
231 for (std::size_t i = 0; i != seq_len; ++i) {
232 for (std::size_t j = 0; j != seq_len; ++j) {
233 test_swap (seq, i, seq, j, (MyDeque*)0, tname);
234 }
235 }
236}
237
238
241{
242 test_swap ((int*)0, "int");
243 test_swap ((UserClass*)0, "UserClass");
244}
245
246
248int run_test (int, char**)
249{
250 // Test std::swap calling std::deque::swap
251 test_std_swap ();
252
253 static const Deque::size_type caps[] = {
254 2, 3, 4, 5, 16, 32
255 };
256
257 for (std::size_t i = 0; i != sizeof caps / sizeof *caps; ++i) {
258
259 new_capacity = caps [i];
260
261 rw_info (0, 0, 0,
262 "__rw::__rw_new_capacity<std::deque<UserClass> >(0) = %u",
263 _RW::__rw_new_capacity (0, (Deque*)0));
264
265 test_swap ();
266 }
267
268 return 0;
269}
270
271
273int main (int argc, char** argv)
274{
275 return rw_test (argc, argv, __FILE__,
276 "lib.deque.special",
277 0 /* no comment */,
278 run_test,
279 0 /* co command line options */);
280}
InputIter< UserClass > Iterator
#define i
Definition 17.names.cpp:126
#define T
Definition 17.names.cpp:163
#define rhs
Definition 17.names.cpp:536
#define d
Definition 17.names.cpp:115
#define lhs
Definition 17.names.cpp:489
#define j
Definition 17.names.cpp:127
#define n
Definition 17.names.cpp:130
std::size_t new_capacity
int run_test(int, char **)
int deque_swap_called
std::deque< UserClass, std::allocator< UserClass > > Deque
void test_std_swap()
Deque::size_type new_capacity
void test_swap()
std::allocator< DequeValueType > DequeAllocator
std::deque< DequeValueType, DequeAllocator > DequeType
#define main
#define _RWSTD_UNUSED(arg)
Definition _defs.h:972
#define _RWSTD_NAMESPACE(name)
Definition _defs.h:567
#define _RWSTD_SPECIALIZED_FUNCTION
Definition _defs.h:556
std::deque< std::string, std::allocator< std::string > > Deque
Definition deque.cpp:35
_RWSTD_SPECIALIZED_FUNCTION Deque::size_type __rw_new_capacity< Deque >(Deque::size_type n, const Deque *)
Definition json.h:5363
_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_assert(int, const char *, int, const char *,...)
Definition driver.cpp:1630