Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
20.pairs.cpp
Go to the documentation of this file.
1
29#include <utility>
30
31#include <rw_alg_test.h>
32#include <rw_value.h> // for UserClass
33#include <rw_driver.h>
34
35
37struct Y: UserClass { };
38
39
41
42
44
46bool less<Y>::operator() (const Y &a, const Y &b) const
47{
48 ++less_used;
49 return a < b;
50}
51
52} // namespace std
53
54
55#ifndef _RWSTD_NO_EXPLICIT_CTOR_INSTANTIATION
56
59
61
62// enclosing explicit instantiation within the declaring namespace
63// instead of qualifying it with its name to work around compiler
64// bugs (e.g., SunPro 5.4 -- see PR #28117)
65
66#ifndef _RWSTD_NO_EMPTY_MEM_INITIALIZER
67
68template pair<DefaultConstructible, DefaultConstructible>::pair ();
69
70#endif // _RWSTD_NO_EMPTY_MEM_INITIALIZER
71
72template pair<CopyConstructible, CopyConstructible>::
73 pair (const CopyConstructible&, const CopyConstructible&);
74
75} // namespace std
76
77#endif // _RWSTD_NO_EXPLICIT_CTOR_INSTANTIATION
78
79
81template <class T, class U>
82void test_pair (T, const char *tname, U, const char *uname)
83{
84 typedef std::pair<T, U> Pair;
85
86 T *pt = (typename Pair::first_type*)0;
87 U *pu = (typename Pair::second_type*)0;
88
89 rw_info (0, 0, __LINE__, "std::pair<%s, %s> data members", tname, uname);
90
91 if (0) { // compile only
92
93 // verify pair data members and their types
94 Pair *p = 0;
95 pt = &p->first;
96 pu = &p->second;
97
98 _RWSTD_UNUSED (pt);
99 _RWSTD_UNUSED (pu);
100 }
101
102 if (0) { // compile only
103 Pair *p0 = 0;
104 std::pair<long, double> p1 (*p0);
105
107 }
108
109 {
110 rw_info (0, 0, __LINE__, "std::pair<%s, %s>::pair()", tname, uname);
111
113
114 std::pair<Y, Y> py;
115
116 rw_assert (UserClass::n_total_def_ctor_ == 2, 0, __LINE__,
117 "pair<Y, Y>::pair() called %d default ctors, "
118 "expected 2", UserClass::n_total_def_ctor_, 2);
119
120#ifndef _RWSTD_NO_EMPTY_MEM_INITIALIZER
121
122 // exercise lwg issue 265
123 rw_assert (UserClass::n_total_copy_ctor_ == 0, 0, __LINE__,
124 "pair<Y, Y>::pair() called %d copy ctors, "
125 "expected 0", UserClass::n_total_copy_ctor_);
126
127#else // if defined (_RWSTD_NO_EMPTY_MEM_INITIALIZER)
128
129 // 20.2.2, p2: commented out -- calls to copy ctor may be elided
130 // RW_ASSERT (t, UserClass::n_total_copy_ctor_ == 2,
131 // ("std::pair<Y, Y>::pair() called %d copy ctors, "
132 // "expected 2", UserClass::n_total_copy_ctor_));
133
134#endif // _RWSTD_NO_EMPTY_MEM_INITIALIZER
135
136 rw_info (0, 0, __LINE__,
137 "std::pair<%s, %s>::pair(const %s&, const %s&)",
138 tname, uname, tname, uname);
139
140 UserClass x0;
142
144
145 // 20.2.2, p3
146 std::pair<UserClass, UserClass> px0 (x0, x1);
147
148 rw_assert (UserClass::n_total_copy_ctor_ == 2, 0, __LINE__,
149 "pair<T, U>::pair (const T&, const U&) called %d cpy "
150 "ctors, expected 2", UserClass::n_total_copy_ctor_);
151
152
153 rw_info (0, 0, __LINE__,
154 "template <class T, class U> "
155 "std::pair<%s, %s>::pair(const pair<T, U>&)",
156 tname, uname);
157
159
160 // 20.2.2, p4
161 std::pair<UserClass, UserClass> px (py);
162
163 rw_assert (UserClass::n_total_copy_ctor_ == 2, 0, __LINE__,
164 "template <class T, class U> pair<UserClass, "
165 "UserClass>::pair(const std::pair<T, U>&) called "
166 "%d cpy ctors, expected 2",
168
169
170 rw_info (0, 0, __LINE__,
171 "template <class T, class U> "
172 "std::pair<%s, %s>::operator= (const pair<T, U>&)",
173 tname, uname);
174
176
177 // exercise template assignment if provided,
178 // otherwise template ctor and ordinary assignment
179 px = py;
180
181 rw_assert (UserClass::n_total_copy_ctor_ == 0, 0, __LINE__,
182 "template <class T, class U> pair<UserClass, "
183 "UserClass>::operator=(const pair<T, U>&) "
184 "called %d cpy ctors, expected 0",
186
187 rw_assert (UserClass::n_total_op_assign_ == 2, 0, __LINE__,
188 "template <class T, class U> pair<UserClass, "
189 "UserClass>::operator=(const pair<T, U>&) "
190 "called %d assignment operators, "
191 "expected 2", UserClass::n_total_op_assign_);
192 }
193
194 {
195
196// exercise the signature of a nonmember (or static member) function
197#define FUN(result, name, arg_list) do { \
198 rw_info (0, 0, __LINE__, "%s(const pair<%s, %s>&, " \
199 "const pair<%s, %s>&)", #name, \
200 tname, uname, tname, uname); \
201 /* make name unique to prevent bogus gcc -Wshadow warnings */ \
202 result (*_RWSTD_PASTE (pf, __LINE__)) arg_list = &name; \
203 _RWSTD_UNUSED (_RWSTD_PASTE (pf, __LINE__)); \
204 } while (0)
205
206 FUN (bool, std::operator==, (const Pair&, const Pair&));
207 FUN (bool, std::operator!=, (const Pair&, const Pair&));
208 FUN (bool, std::operator<, (const Pair&, const Pair&));
209 FUN (bool, std::operator<=, (const Pair&, const Pair&));
210 FUN (bool, std::operator>, (const Pair&, const Pair&));
211 FUN (bool, std::operator>=, (const Pair&, const Pair&));
212
213 // 20.2.2, p5
214 std::pair<Y, Y> p0;
215
216 Y::n_total_op_lt_ = 0;
217
218 bool b = p0 == p0;
219 rw_assert (b, 0, __LINE__,
220 "operator== (const pair<UserClass, UserClass>&, "
221 "const pair<UserClass, UserClass>&)");
222 rw_assert (2 == Y::n_total_op_eq_, 0, __LINE__,
223 "operator== (const pair<UserClass, UserClass>&, "
224 "const pair<UserClass, UserClass>&)");
225
226
227 // exercise lwg issue 348
228 // (std::less required to be used in order to satisfy 20.3.3, p8)
229 std::pair<Y, Y> p1;
230
231 p0.first.data_.val_ = 1;
232 p0.second.data_.val_ = 2;
233
234 p1.first.data_.val_ = 3;
235 p1.second.data_.val_ = 4;
236
237 less_used = 0;
238
239 b = p0 < p1;
240
241 rw_assert (b, 0, __LINE__,
242 "operator< (const pair<Y, Y>&, const pair<Y, Y>&)");
243
244 rw_assert (1 == less_used, 0, __LINE__,
245 "less<Y>::operator() called %d time(s), expected 1",
246 less_used);
247
248 p1.first.data_.val_ = 1;
249
250 less_used = 0;
251
252 b = p0 < p1;
253
254 rw_assert (b, 0, __LINE__,
255 "operator< (const pair<Y, Y>&, const pair<Y, Y>&)");
256
257 rw_assert (3 == less_used, 0, __LINE__,
258 "less<Y>::operator() called %d time(s), expected 3",
259 less_used);
260
261 p1.first.data_.val_ = 0;
262
263 less_used = 0;
264
265 b = p0 < p1;
266
267 rw_assert (!b, 0, __LINE__,
268 "operator< (const pair<Y, Y>&, const pair<Y, Y>&)");
269
270 rw_assert (2 == less_used, 0, __LINE__,
271 "less<Y>::operator() called %d time(s), expected 2",
272 less_used);
273 }
274}
275
276
278static int
279run_test (int, char**)
280{
281 test_pair (char (), "char", long (), "long");
282 test_pair (int (), "int", float (), "float");
283
284 return 0;
285}
286
287
289int main (int argc, char *argv[])
290{
291 return rw_test (argc, argv, __FILE__,
292 "lib.pairs",
293 0 /* no comment */,
294 run_test,
295 "",
296 (void*)0 /* sentinel */);
297}
#define T
Definition 17.names.cpp:163
#define x1
Definition 17.names.cpp:247
#define a
Definition 17.names.cpp:109
#define b
Definition 17.names.cpp:110
#define p1
Definition 17.names.cpp:239
#define p
Definition 17.names.cpp:132
#define U
Definition 17.names.cpp:164
base< def_ctor > DefaultConstructible
Definition 20.pairs.cpp:57
void test_pair(T, const char *tname, U, const char *uname)
Definition 20.pairs.cpp:82
int less_used
Definition 20.pairs.cpp:40
#define FUN(result, name, arg_list)
base< cpy_ctor > CopyConstructible
Definition 20.pairs.cpp:58
void run_test(const ForwardIterator &dummy, T *, Predicate *pred)
#define main
#define _RWSTD_SPECIALIZED_CLASS
#define _RWSTD_UNUSED(arg)
Definition _defs.h:972
#define _RWSTD_NAMESPACE(name)
Definition _defs.h:567
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
static _RWSTD_SIZE_T n_total_op_assign_
Definition rw_value.h:127
static void reset_totals()
Definition value.cpp:452
static _RWSTD_SIZE_T n_total_def_ctor_
Definition rw_value.h:124
static _RWSTD_SIZE_T n_total_copy_ctor_
Definition rw_value.h:125