]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/tuple/test/tuple_test_bench.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / tuple / test / tuple_test_bench.cpp
1 // Copyright (C) 1999, 2000 Jaakko Jarvi (jaakko.jarvi@cs.utu.fi)
2 //
3 // Distributed under the Boost Software License, Version 1.0. (See
4 // accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6
7 // For more information, see http://www.boost.org
8
9 // tuple_test_bench.cpp --------------------------------
10
11 #define BOOST_INCLUDE_MAIN // for testing, include rather than link
12 #include <boost/test/test_tools.hpp> // see "Header Implementation Option"
13
14 #include "boost/tuple/tuple.hpp"
15
16 #include "boost/tuple/tuple_comparison.hpp"
17
18 #include "boost/type_traits/is_const.hpp"
19
20 #include "boost/ref.hpp"
21 #include <string>
22 #include <utility>
23
24 using namespace boost;
25
26 // ----------------------------------------------------------------------------
27 // helpers
28 // ----------------------------------------------------------------------------
29
30 class A {};
31 class B {};
32 class C {};
33
34 // classes with different kinds of conversions
35 class AA {};
36 class BB : public AA {};
37 struct CC { CC() {} CC(const BB&) {} };
38 struct DD { operator CC() const { return CC(); }; };
39
40 // something to prevent warnings for unused variables
41 template<class T> void dummy(const T&) {}
42
43 // no public default constructor
44 class foo {
45 public:
46 explicit foo(int v) : val(v) {}
47
48 bool operator==(const foo& other) const {
49 return val == other.val;
50 }
51
52 private:
53 foo() {}
54 int val;
55 };
56
57 // another class without a public default constructor
58 class no_def_constructor {
59 no_def_constructor() {}
60 public:
61 no_def_constructor(std::string) {}
62 };
63
64 // A non-copyable class
65 class no_copy {
66 no_copy(const no_copy&) {}
67 public:
68 no_copy() {};
69 };
70
71
72 // ----------------------------------------------------------------------------
73 // Testing different element types --------------------------------------------
74 // ----------------------------------------------------------------------------
75
76
77 typedef tuple<int> t1;
78
79 typedef tuple<double&, const double&, const double, double*, const double*> t2;
80 typedef tuple<A, int(*)(char, int), C> t3;
81 typedef tuple<std::string, std::pair<A, B> > t4;
82 typedef tuple<A*, tuple<const A*, const B&, C>, bool, void*> t5;
83 typedef tuple<volatile int, const volatile char&, int(&)(float) > t6;
84
85 # if !defined(__BORLANDC__) || __BORLAND__ > 0x0551
86 typedef tuple<B(A::*)(C&), A&> t7;
87 #endif
88
89 // -----------------------------------------------------------------------
90 // -tuple construction tests ---------------------------------------------
91 // -----------------------------------------------------------------------
92
93
94 no_copy y;
95 tuple<no_copy&> x = tuple<no_copy&>(y); // ok
96
97 char cs[10];
98 tuple<char(&)[10]> v2(cs); // ok
99
100 void
101 construction_test()
102 {
103
104 // Note, the get function can be called without the tuples:: qualifier,
105 // as it is lifted to namespace boost with a "using tuples::get" but
106 // MSVC 6.0 just cannot find get without the namespace qualifier
107
108 tuple<int> t1;
109 BOOST_CHECK(get<0>(t1) == int());
110
111 tuple<float> t2(5.5f);
112 BOOST_CHECK(get<0>(t2) > 5.4f && get<0>(t2) < 5.6f);
113
114 tuple<foo> t3(foo(12));
115 BOOST_CHECK(get<0>(t3) == foo(12));
116
117 tuple<double> t4(t2);
118 BOOST_CHECK(get<0>(t4) > 5.4 && get<0>(t4) < 5.6);
119
120 tuple<int, float> t5;
121 BOOST_CHECK(get<0>(t5) == int());
122 BOOST_CHECK(get<1>(t5) == float());
123
124 tuple<int, float> t6(12, 5.5f);
125 BOOST_CHECK(get<0>(t6) == 12);
126 BOOST_CHECK(get<1>(t6) > 5.4f && get<1>(t6) < 5.6f);
127
128 tuple<int, float> t7(t6);
129 BOOST_CHECK(get<0>(t7) == 12);
130 BOOST_CHECK(get<1>(t7) > 5.4f && get<1>(t7) < 5.6f);
131
132 tuple<long, double> t8(t6);
133 BOOST_CHECK(get<0>(t8) == 12);
134 BOOST_CHECK(get<1>(t8) > 5.4f && get<1>(t8) < 5.6f);
135
136 dummy(
137 tuple<no_def_constructor, no_def_constructor, no_def_constructor>(
138 std::string("Jaba"), // ok, since the default
139 std::string("Daba"), // constructor is not used
140 std::string("Doo")
141 )
142 );
143
144 // testing default values
145 dummy(tuple<int, double>());
146 dummy(tuple<int, double>(1));
147 dummy(tuple<int, double>(1,3.14));
148
149
150 // dummy(tuple<double&>()); // should fail, not defaults for references
151 // dummy(tuple<const double&>()); // likewise
152
153 double dd = 5;
154 dummy(tuple<double&>(dd)); // ok
155
156 dummy(tuple<const double&>(dd+3.14)); // ok, but dangerous
157
158 // dummy(tuple<double&>(dd+3.14)); // should fail,
159 // // temporary to non-const reference
160 }
161
162
163 // ----------------------------------------------------------------------------
164 // - testing element access ---------------------------------------------------
165 // ----------------------------------------------------------------------------
166
167 void element_access_test()
168 {
169 double d = 2.7;
170 A a;
171 tuple<int, double&, const A&, int> t(1, d, a, 2);
172 const tuple<int, double&, const A, int> ct = t;
173
174 int i = get<0>(t);
175 int i2 = get<3>(t);
176
177 BOOST_CHECK(i == 1 && i2 == 2);
178
179 int j = get<0>(ct);
180 BOOST_CHECK(j == 1);
181
182 get<0>(t) = 5;
183 BOOST_CHECK(t.head == 5);
184
185 // get<0>(ct) = 5; // can't assign to const
186
187 double e = get<1>(t);
188 BOOST_CHECK(e > 2.69 && e < 2.71);
189
190 get<1>(t) = 3.14+i;
191 BOOST_CHECK(get<1>(t) > 4.13 && get<1>(t) < 4.15);
192
193 // get<4>(t) = A(); // can't assign to const
194 // dummy(get<5>(ct)); // illegal index
195
196 ++get<0>(t);
197 BOOST_CHECK(get<0>(t) == 6);
198
199 BOOST_STATIC_ASSERT((boost::is_const<boost::tuples::element<0, tuple<int, float> >::type>::value != true));
200 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
201 BOOST_STATIC_ASSERT((boost::is_const<boost::tuples::element<0, const tuple<int, float> >::type>::value));
202 #endif
203
204 BOOST_STATIC_ASSERT((boost::is_const<boost::tuples::element<1, tuple<int, float> >::type>::value != true));
205 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
206 BOOST_STATIC_ASSERT((boost::is_const<boost::tuples::element<1, const tuple<int, float> >::type>::value));
207 #endif
208
209
210 dummy(i); dummy(i2); dummy(j); dummy(e); // avoid warns for unused variables
211 }
212
213
214 // ----------------------------------------------------------------------------
215 // - copying tuples -----------------------------------------------------------
216 // ----------------------------------------------------------------------------
217
218
219
220 void
221 copy_test()
222 {
223 tuple<int, char> t1(4, 'a');
224 tuple<int, char> t2(5, 'b');
225 t2 = t1;
226 BOOST_CHECK(get<0>(t1) == get<0>(t2));
227 BOOST_CHECK(get<1>(t1) == get<1>(t2));
228
229 tuple<long, std::string> t3(2, "a");
230 t3 = t1;
231 BOOST_CHECK((double)get<0>(t1) == get<0>(t3));
232 BOOST_CHECK(get<1>(t1) == get<1>(t3)[0]);
233
234 // testing copy and assignment with implicit conversions between elements
235 // testing tie
236
237 tuple<char, BB*, BB, DD> t;
238 tuple<int, AA*, CC, CC> a(t);
239 a = t;
240
241 int i; char c; double d;
242 tie(i, c, d) = make_tuple(1, 'a', 5.5);
243
244 BOOST_CHECK(i==1);
245 BOOST_CHECK(c=='a');
246 BOOST_CHECK(d>5.4 && d<5.6);
247 }
248
249 void
250 mutate_test()
251 {
252 tuple<int, float, bool, foo> t1(5, 12.2f, true, foo(4));
253 get<0>(t1) = 6;
254 get<1>(t1) = 2.2f;
255 get<2>(t1) = false;
256 get<3>(t1) = foo(5);
257
258 BOOST_CHECK(get<0>(t1) == 6);
259 BOOST_CHECK(get<1>(t1) > 2.1f && get<1>(t1) < 2.3f);
260 BOOST_CHECK(get<2>(t1) == false);
261 BOOST_CHECK(get<3>(t1) == foo(5));
262 }
263
264 // ----------------------------------------------------------------------------
265 // make_tuple tests -----------------------------------------------------------
266 // ----------------------------------------------------------------------------
267
268 void
269 make_tuple_test()
270 {
271 tuple<int, char> t1 = make_tuple(5, 'a');
272 BOOST_CHECK(get<0>(t1) == 5);
273 BOOST_CHECK(get<1>(t1) == 'a');
274
275 tuple<int, std::string> t2;
276 t2 = boost::make_tuple((short int)2, std::string("Hi"));
277 BOOST_CHECK(get<0>(t2) == 2);
278 BOOST_CHECK(get<1>(t2) == "Hi");
279
280
281 A a = A(); B b;
282 const A ca = a;
283 make_tuple(boost::cref(a), b);
284 make_tuple(boost::ref(a), b);
285 make_tuple(boost::ref(a), boost::cref(b));
286
287 make_tuple(boost::ref(ca));
288
289 // the result of make_tuple is assignable:
290 BOOST_CHECK(make_tuple(2, 4, 6) ==
291 (make_tuple(1, 2, 3) = make_tuple(2, 4, 6)));
292
293 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
294 make_tuple("Donald", "Daisy"); // should work;
295 #endif
296 // std::make_pair("Doesn't","Work"); // fails
297
298 // You can store a reference to a function in a tuple
299 tuple<void(&)()> adf(make_tuple_test);
300
301 dummy(adf); // avoid warning for unused variable
302
303 // But make_tuple doesn't work
304 // with function references, since it creates a const qualified function type
305
306 // make_tuple(make_tuple_test);
307
308 // With function pointers, make_tuple works just fine
309
310 #if !defined(__BORLANDC__) || __BORLAND__ > 0x0551
311 make_tuple(&make_tuple_test);
312 #endif
313
314 // NOTE:
315 //
316 // wrapping it the function reference with ref helps on gcc 2.95.2.
317 // on edg 2.43. it results in a catastrophic error?
318
319 // make_tuple(ref(foo3));
320
321 // It seems that edg can't use implicitly the ref's conversion operator, e.g.:
322 // typedef void (&func_t) (void);
323 // func_t fref = static_cast<func_t>(ref(make_tuple_test)); // works fine
324 // func_t fref = ref(make_tuple_test); // error
325
326 // This is probably not a very common situation, so currently
327 // I don't know how which compiler is right (JJ)
328 }
329
330 void
331 tie_test()
332 {
333 int a;
334 char b;
335 foo c(5);
336
337 tie(a, b, c) = make_tuple(2, 'a', foo(3));
338 BOOST_CHECK(a == 2);
339 BOOST_CHECK(b == 'a');
340 BOOST_CHECK(c == foo(3));
341
342 tie(a, tuples::ignore, c) = make_tuple((short int)5, false, foo(5));
343 BOOST_CHECK(a == 5);
344 BOOST_CHECK(b == 'a');
345 BOOST_CHECK(c == foo(5));
346
347 // testing assignment from std::pair
348 int i, j;
349 tie (i, j) = std::make_pair(1, 2);
350 BOOST_CHECK(i == 1 && j == 2);
351
352 tuple<int, int, float> ta;
353 #ifdef E11
354 ta = std::make_pair(1, 2); // should fail, tuple is of length 3, not 2
355 #endif
356
357 dummy(ta);
358 }
359
360
361 // ----------------------------------------------------------------------------
362 // - testing tuple equality -------------------------------------------------
363 // ----------------------------------------------------------------------------
364
365 void
366 equality_test()
367 {
368 tuple<int, char> t1(5, 'a');
369 tuple<int, char> t2(5, 'a');
370 BOOST_CHECK(t1 == t2);
371
372 tuple<int, char> t3(5, 'b');
373 tuple<int, char> t4(2, 'a');
374 BOOST_CHECK(t1 != t3);
375 BOOST_CHECK(t1 != t4);
376 BOOST_CHECK(!(t1 != t2));
377 }
378
379
380 // ----------------------------------------------------------------------------
381 // - testing tuple comparisons -----------------------------------------------
382 // ----------------------------------------------------------------------------
383
384 void
385 ordering_test()
386 {
387 tuple<int, float> t1(4, 3.3f);
388 tuple<short, float> t2(5, 3.3f);
389 tuple<long, double> t3(5, 4.4);
390 BOOST_CHECK(t1 < t2);
391 BOOST_CHECK(t1 <= t2);
392 BOOST_CHECK(t2 > t1);
393 BOOST_CHECK(t2 >= t1);
394 BOOST_CHECK(t2 < t3);
395 BOOST_CHECK(t2 <= t3);
396 BOOST_CHECK(t3 > t2);
397 BOOST_CHECK(t3 >= t2);
398
399 }
400
401
402 // ----------------------------------------------------------------------------
403 // - testing cons lists -------------------------------------------------------
404 // ----------------------------------------------------------------------------
405 void cons_test()
406 {
407 using tuples::cons;
408 using tuples::null_type;
409
410 cons<volatile float, null_type> a(1, null_type());
411 cons<const int, cons<volatile float, null_type> > b(2,a);
412 int i = 3;
413 cons<int&, cons<const int, cons<volatile float, null_type> > > c(i, b);
414 BOOST_CHECK(make_tuple(3,2,1)==c);
415
416 cons<char, cons<int, cons<float, null_type> > > x;
417 dummy(x);
418 }
419
420 // ----------------------------------------------------------------------------
421 // - testing const tuples -----------------------------------------------------
422 // ----------------------------------------------------------------------------
423 void const_tuple_test()
424 {
425 const tuple<int, float> t1(5, 3.3f);
426 BOOST_CHECK(get<0>(t1) == 5);
427 BOOST_CHECK(get<1>(t1) == 3.3f);
428 }
429
430 // ----------------------------------------------------------------------------
431 // - testing length -----------------------------------------------------------
432 // ----------------------------------------------------------------------------
433 void tuple_length_test()
434 {
435 typedef tuple<int, float, double> t1;
436 using tuples::cons;
437 typedef cons<int, cons< float, cons <double, tuples::null_type> > > t1_cons;
438 typedef tuple<> t2;
439 typedef tuples::null_type t3;
440
441 BOOST_STATIC_ASSERT(tuples::length<t1>::value == 3);
442 BOOST_STATIC_ASSERT(tuples::length<t1_cons>::value == 3);
443 BOOST_STATIC_ASSERT(tuples::length<t2>::value == 0);
444 BOOST_STATIC_ASSERT(tuples::length<t3>::value == 0);
445
446 }
447
448 // ----------------------------------------------------------------------------
449 // - testing swap -----------------------------------------------------------
450 // ----------------------------------------------------------------------------
451 void tuple_swap_test()
452 {
453 tuple<int, float, double> t1(1, 2.0f, 3.0), t2(4, 5.0f, 6.0);
454 swap(t1, t2);
455 BOOST_CHECK(get<0>(t1) == 4);
456 BOOST_CHECK(get<1>(t1) == 5.0f);
457 BOOST_CHECK(get<2>(t1) == 6.0);
458 BOOST_CHECK(get<0>(t2) == 1);
459 BOOST_CHECK(get<1>(t2) == 2.0f);
460 BOOST_CHECK(get<2>(t2) == 3.0);
461
462 int i = 1,j = 2;
463 boost::tuple<int&> t3(i), t4(j);
464 swap(t3, t4);
465 BOOST_CHECK(i == 2);
466 BOOST_CHECK(j == 1);
467 }
468
469
470
471 // ----------------------------------------------------------------------------
472 // - main ---------------------------------------------------------------------
473 // ----------------------------------------------------------------------------
474
475 int test_main(int, char *[]) {
476
477 construction_test();
478 element_access_test();
479 copy_test();
480 mutate_test();
481 make_tuple_test();
482 tie_test();
483 equality_test();
484 ordering_test();
485 cons_test();
486 const_tuple_test();
487 tuple_length_test();
488 tuple_swap_test();
489 return 0;
490 }
491
492
493
494
495
496
497