]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/tuple/test/tuple_test_bench.cpp
1 // Copyright (C) 1999, 2000 Jaakko Jarvi (jaakko.jarvi@cs.utu.fi)
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)
7 // For more information, see http://www.boost.org
9 // tuple_test_bench.cpp --------------------------------
11 #define BOOST_INCLUDE_MAIN // for testing, include rather than link
12 #include <boost/test/test_tools.hpp> // see "Header Implementation Option"
14 #include "boost/tuple/tuple.hpp"
16 #include "boost/tuple/tuple_comparison.hpp"
18 #include "boost/type_traits/is_const.hpp"
20 #include "boost/ref.hpp"
24 using namespace boost
;
26 // ----------------------------------------------------------------------------
28 // ----------------------------------------------------------------------------
34 // classes with different kinds of conversions
36 class BB
: public AA
{};
37 struct CC
{ CC() {} CC(const BB
&) {} };
38 struct DD
{ operator CC() const { return CC(); }; };
40 // something to prevent warnings for unused variables
41 template<class T
> void dummy(const T
&) {}
43 // no public default constructor
46 explicit foo(int v
) : val(v
) {}
48 bool operator==(const foo
& other
) const {
49 return val
== other
.val
;
57 // another class without a public default constructor
58 class no_def_constructor
{
59 no_def_constructor() {}
61 no_def_constructor(std::string
) {}
64 // A non-copyable class
66 no_copy(const no_copy
&) {}
72 // ----------------------------------------------------------------------------
73 // Testing different element types --------------------------------------------
74 // ----------------------------------------------------------------------------
77 typedef tuple
<int> t1
;
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
;
85 # if !defined(__BORLANDC__) || __BORLAND__ > 0x0551
86 typedef tuple
<B(A::*)(C
&), A
&> t7
;
89 // -----------------------------------------------------------------------
90 // -tuple construction tests ---------------------------------------------
91 // -----------------------------------------------------------------------
95 tuple
<no_copy
&> x
= tuple
<no_copy
&>(y
); // ok
98 tuple
<char(&)[10]> v2(cs
); // ok
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
109 BOOST_CHECK(get
<0>(t1
) == int());
111 tuple
<float> t2(5.5f
);
112 BOOST_CHECK(get
<0>(t2
) > 5.4f
&& get
<0>(t2
) < 5.6f
);
114 tuple
<foo
> t3(foo(12));
115 BOOST_CHECK(get
<0>(t3
) == foo(12));
117 tuple
<double> t4(t2
);
118 BOOST_CHECK(get
<0>(t4
) > 5.4 && get
<0>(t4
) < 5.6);
120 tuple
<int, float> t5
;
121 BOOST_CHECK(get
<0>(t5
) == int());
122 BOOST_CHECK(get
<1>(t5
) == float());
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
);
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
);
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
);
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
144 // testing default values
145 dummy(tuple
<int, double>());
146 dummy(tuple
<int, double>(1));
147 dummy(tuple
<int, double>(1,3.14));
150 // dummy(tuple<double&>()); // should fail, not defaults for references
151 // dummy(tuple<const double&>()); // likewise
154 dummy(tuple
<double&>(dd
)); // ok
156 dummy(tuple
<const double&>(dd
+3.14)); // ok, but dangerous
158 // dummy(tuple<double&>(dd+3.14)); // should fail,
159 // // temporary to non-const reference
163 // ----------------------------------------------------------------------------
164 // - testing element access ---------------------------------------------------
165 // ----------------------------------------------------------------------------
167 void element_access_test()
171 tuple
<int, double&, const A
&, int> t(1, d
, a
, 2);
172 const tuple
<int, double&, const A
, int> ct
= t
;
177 BOOST_CHECK(i
== 1 && i2
== 2);
183 BOOST_CHECK(t
.head
== 5);
185 // get<0>(ct) = 5; // can't assign to const
187 double e
= get
<1>(t
);
188 BOOST_CHECK(e
> 2.69 && e
< 2.71);
191 BOOST_CHECK(get
<1>(t
) > 4.13 && get
<1>(t
) < 4.15);
193 // get<4>(t) = A(); // can't assign to const
194 // dummy(get<5>(ct)); // illegal index
197 BOOST_CHECK(get
<0>(t
) == 6);
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
));
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
));
210 dummy(i
); dummy(i2
); dummy(j
); dummy(e
); // avoid warns for unused variables
214 // ----------------------------------------------------------------------------
215 // - copying tuples -----------------------------------------------------------
216 // ----------------------------------------------------------------------------
223 tuple
<int, char> t1(4, 'a');
224 tuple
<int, char> t2(5, 'b');
226 BOOST_CHECK(get
<0>(t1
) == get
<0>(t2
));
227 BOOST_CHECK(get
<1>(t1
) == get
<1>(t2
));
229 tuple
<long, std::string
> t3(2, "a");
231 BOOST_CHECK((double)get
<0>(t1
) == get
<0>(t3
));
232 BOOST_CHECK(get
<1>(t1
) == get
<1>(t3
)[0]);
234 // testing copy and assignment with implicit conversions between elements
237 tuple
<char, BB
*, BB
, DD
> t
;
238 tuple
<int, AA
*, CC
, CC
> a(t
);
241 int i
; char c
; double d
;
242 tie(i
, c
, d
) = make_tuple(1, 'a', 5.5);
246 BOOST_CHECK(d
>5.4 && d
<5.6);
252 tuple
<int, float, bool, foo
> t1(5, 12.2f
, true, foo(4));
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));
264 // ----------------------------------------------------------------------------
265 // make_tuple tests -----------------------------------------------------------
266 // ----------------------------------------------------------------------------
271 tuple
<int, char> t1
= make_tuple(5, 'a');
272 BOOST_CHECK(get
<0>(t1
) == 5);
273 BOOST_CHECK(get
<1>(t1
) == 'a');
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");
283 make_tuple(boost::cref(a
), b
);
284 make_tuple(boost::ref(a
), b
);
285 make_tuple(boost::ref(a
), boost::cref(b
));
287 make_tuple(boost::ref(ca
));
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)));
293 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
294 make_tuple("Donald", "Daisy"); // should work;
296 // std::make_pair("Doesn't","Work"); // fails
298 // You can store a reference to a function in a tuple
299 tuple
<void(&)()> adf(make_tuple_test
);
301 dummy(adf
); // avoid warning for unused variable
303 // But make_tuple doesn't work
304 // with function references, since it creates a const qualified function type
306 // make_tuple(make_tuple_test);
308 // With function pointers, make_tuple works just fine
310 #if !defined(__BORLANDC__) || __BORLAND__ > 0x0551
311 make_tuple(&make_tuple_test
);
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?
319 // make_tuple(ref(foo3));
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
326 // This is probably not a very common situation, so currently
327 // I don't know how which compiler is right (JJ)
337 tie(a
, b
, c
) = make_tuple(2, 'a', foo(3));
339 BOOST_CHECK(b
== 'a');
340 BOOST_CHECK(c
== foo(3));
342 tie(a
, tuples::ignore
, c
) = make_tuple((short int)5, false, foo(5));
344 BOOST_CHECK(b
== 'a');
345 BOOST_CHECK(c
== foo(5));
347 // testing assignment from std::pair
349 tie (i
, j
) = std::make_pair(1, 2);
350 BOOST_CHECK(i
== 1 && j
== 2);
352 tuple
<int, int, float> ta
;
354 ta
= std::make_pair(1, 2); // should fail, tuple is of length 3, not 2
361 // ----------------------------------------------------------------------------
362 // - testing tuple equality -------------------------------------------------
363 // ----------------------------------------------------------------------------
368 tuple
<int, char> t1(5, 'a');
369 tuple
<int, char> t2(5, 'a');
370 BOOST_CHECK(t1
== t2
);
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
));
380 // ----------------------------------------------------------------------------
381 // - testing tuple comparisons -----------------------------------------------
382 // ----------------------------------------------------------------------------
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
);
402 // ----------------------------------------------------------------------------
403 // - testing cons lists -------------------------------------------------------
404 // ----------------------------------------------------------------------------
408 using tuples::null_type
;
410 cons
<volatile float, null_type
> a(1, null_type());
411 cons
<const int, cons
<volatile float, null_type
> > b(2,a
);
413 cons
<int&, cons
<const int, cons
<volatile float, null_type
> > > c(i
, b
);
414 BOOST_CHECK(make_tuple(3,2,1)==c
);
416 cons
<char, cons
<int, cons
<float, null_type
> > > x
;
420 // ----------------------------------------------------------------------------
421 // - testing const tuples -----------------------------------------------------
422 // ----------------------------------------------------------------------------
423 void const_tuple_test()
425 const tuple
<int, float> t1(5, 3.3f
);
426 BOOST_CHECK(get
<0>(t1
) == 5);
427 BOOST_CHECK(get
<1>(t1
) == 3.3f
);
430 // ----------------------------------------------------------------------------
431 // - testing length -----------------------------------------------------------
432 // ----------------------------------------------------------------------------
433 void tuple_length_test()
435 typedef tuple
<int, float, double> t1
;
437 typedef cons
<int, cons
< float, cons
<double, tuples::null_type
> > > t1_cons
;
439 typedef tuples::null_type t3
;
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);
448 // ----------------------------------------------------------------------------
449 // - testing swap -----------------------------------------------------------
450 // ----------------------------------------------------------------------------
451 void tuple_swap_test()
453 tuple
<int, float, double> t1(1, 2.0f
, 3.0), t2(4, 5.0f
, 6.0);
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);
463 boost::tuple
<int&> t3(i
), t4(j
);
471 // ----------------------------------------------------------------------------
472 // - main ---------------------------------------------------------------------
473 // ----------------------------------------------------------------------------
475 int test_main(int, char *[]) {
478 element_access_test();