Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / fusion / test / sequence / construction.hpp
1 /*=============================================================================
2     Copyright (c) 1999-2003 Jaakko Jarvi
3     Copyright (c) 2001-2011 Joel de Guzman
4
5     Distributed under the Boost Software License, Version 1.0. (See accompanying
6     file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 ==============================================================================*/
8 #include <boost/detail/lightweight_test.hpp>
9 #include <boost/fusion/sequence/intrinsic/at.hpp>
10 #include <boost/fusion/container/list/cons.hpp>
11
12 #if !defined(FUSION_AT)
13 #define FUSION_AT at_c
14 #endif
15
16 namespace test_detail
17 {
18     // something to prevent warnings for unused variables
19     template<class T> void dummy(const T&) {}
20
21     // no public default constructor
22     class foo
23     {
24     public:
25
26         explicit foo(int v) : val(v) {}
27
28         bool operator==(const foo& other) const
29         {
30             return val == other.val;
31         }
32
33     private:
34
35         foo() {}
36         int val;
37     };
38
39     // another class without a public default constructor
40     class no_def_constructor
41     {
42         no_def_constructor() {}
43
44     public:
45
46         no_def_constructor(std::string) {}
47     };
48 }
49
50 inline void
51 test()
52 {
53     using namespace boost::fusion;
54     using namespace test_detail;
55
56     nil empty;
57
58     FUSION_SEQUENCE<> empty0;
59
60 #ifndef NO_CONSTRUCT_FROM_NIL
61     FUSION_SEQUENCE<> empty1(empty);
62 #endif
63
64     FUSION_SEQUENCE<int> t1;
65     BOOST_TEST(FUSION_AT<0>(t1) == int());
66
67     FUSION_SEQUENCE<float> t2(5.5f);
68     BOOST_TEST(FUSION_AT<0>(t2) > 5.4f && FUSION_AT<0>(t2) < 5.6f);
69
70     FUSION_SEQUENCE<foo> t3(foo(12));
71     BOOST_TEST(FUSION_AT<0>(t3) == foo(12));
72
73     FUSION_SEQUENCE<double> t4(t2);
74     BOOST_TEST(FUSION_AT<0>(t4) > 5.4 && FUSION_AT<0>(t4) < 5.6);
75
76     FUSION_SEQUENCE<int, float> t5;
77     BOOST_TEST(FUSION_AT<0>(t5) == int());
78     BOOST_TEST(FUSION_AT<1>(t5) == float());
79
80     FUSION_SEQUENCE<int, float> t6(12, 5.5f);
81     BOOST_TEST(FUSION_AT<0>(t6) == 12);
82     BOOST_TEST(FUSION_AT<1>(t6) > 5.4f && FUSION_AT<1>(t6) < 5.6f);
83
84     FUSION_SEQUENCE<int, float> t7(t6);
85     BOOST_TEST(FUSION_AT<0>(t7) == 12);
86     BOOST_TEST(FUSION_AT<1>(t7) > 5.4f && FUSION_AT<1>(t7) < 5.6f);
87
88     FUSION_SEQUENCE<long, double> t8(t6);
89     BOOST_TEST(FUSION_AT<0>(t8) == 12);
90     BOOST_TEST(FUSION_AT<1>(t8) > 5.4f && FUSION_AT<1>(t8) < 5.6f);
91
92     dummy
93     (
94         FUSION_SEQUENCE<no_def_constructor, no_def_constructor, no_def_constructor>(
95             std::string("Jaba"),        // ok, since the default
96             std::string("Daba"),        // constructor is not used
97             std::string("Doo")
98         )
99     );
100
101     dummy(FUSION_SEQUENCE<int, double>());
102     dummy(FUSION_SEQUENCE<int, double>(1,3.14));
103
104 #if defined(FUSION_TEST_FAIL)
105     dummy(FUSION_SEQUENCE<double&>());          // should fail, no defaults for references
106     dummy(FUSION_SEQUENCE<const double&>());    // likewise
107 #endif
108
109     {
110         double dd = 5;
111         dummy(FUSION_SEQUENCE<double&>(dd)); // ok
112         dummy(FUSION_SEQUENCE<const double&>(dd+3.14)); // ok, but dangerous
113     }
114
115 #if defined(FUSION_TEST_FAIL)
116     dummy(FUSION_SEQUENCE<double&>(dd+3.14));   // should fail,
117                                                 // temporary to non-const reference
118 #endif
119 }