Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/tuple/test/tuple_test_bench.cpp @ 29

Last change on this file since 29 was 29, checked in by landauf, 16 years ago

updated boost from 1_33_1 to 1_34_1

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