• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
dummy(const T &)19     template<class T> void dummy(const T&) {}
20 
21     // no public default constructor
22     class foo
23     {
24     public:
25 
foo(int v)26         explicit foo(int v) : val(v) {}
27 
operator ==(const foo & other) const28         bool operator==(const foo& other) const
29         {
30             return val == other.val;
31         }
32 
33     private:
34 
foo()35         foo() {}
36         int val;
37     };
38 
39     // another class without a public default constructor
40     class no_def_constructor
41     {
no_def_constructor()42         no_def_constructor() {}
43 
44     public:
45 
no_def_constructor(std::string)46         no_def_constructor(std::string) {}
47     };
48 }
49 
50 inline void
test()51 test()
52 {
53     using namespace boost::fusion;
54     using namespace test_detail;
55 
56     nil empty;
57     (void)empty;
58 
59     FUSION_SEQUENCE<> empty0;
60     (void)empty0;
61 
62 #ifndef NO_CONSTRUCT_FROM_NIL
63     FUSION_SEQUENCE<> empty1(empty);
64     (void)empty1;
65 #endif
66 
67     FUSION_SEQUENCE<int> t1;
68     BOOST_TEST(FUSION_AT<0>(t1) == int());
69 
70     FUSION_SEQUENCE<float> t2(5.5f);
71     BOOST_TEST(FUSION_AT<0>(t2) > 5.4f && FUSION_AT<0>(t2) < 5.6f);
72 
73     FUSION_SEQUENCE<foo> t3(foo(12));
74     BOOST_TEST(FUSION_AT<0>(t3) == foo(12));
75 
76     FUSION_SEQUENCE<double> t4(t2);
77     BOOST_TEST(FUSION_AT<0>(t4) > 5.4 && FUSION_AT<0>(t4) < 5.6);
78 
79     FUSION_SEQUENCE<int, float> t5;
80     BOOST_TEST(FUSION_AT<0>(t5) == int());
81     BOOST_TEST(FUSION_AT<1>(t5) == float());
82 
83     FUSION_SEQUENCE<int, float> t6(12, 5.5f);
84     BOOST_TEST(FUSION_AT<0>(t6) == 12);
85     BOOST_TEST(FUSION_AT<1>(t6) > 5.4f && FUSION_AT<1>(t6) < 5.6f);
86 
87     FUSION_SEQUENCE<int, float> t7(t6);
88     BOOST_TEST(FUSION_AT<0>(t7) == 12);
89     BOOST_TEST(FUSION_AT<1>(t7) > 5.4f && FUSION_AT<1>(t7) < 5.6f);
90 
91     FUSION_SEQUENCE<long, double> t8(t6);
92     BOOST_TEST(FUSION_AT<0>(t8) == 12);
93     BOOST_TEST(FUSION_AT<1>(t8) > 5.4f && FUSION_AT<1>(t8) < 5.6f);
94 
95     dummy
96     (
97         FUSION_SEQUENCE<no_def_constructor, no_def_constructor, no_def_constructor>(
98             std::string("Jaba"),        // ok, since the default
99             std::string("Daba"),        // constructor is not used
100             std::string("Doo")
101         )
102     );
103 
104     dummy(FUSION_SEQUENCE<int, double>());
105     dummy(FUSION_SEQUENCE<int, double>(1,3.14));
106 
107 #if defined(FUSION_TEST_FAIL)
108     dummy(FUSION_SEQUENCE<double&>());          // should fail, no defaults for references
109     dummy(FUSION_SEQUENCE<const double&>());    // likewise
110 #endif
111 
112     {
113         double dd = 5;
114         dummy(FUSION_SEQUENCE<double&>(dd)); // ok
115         dummy(FUSION_SEQUENCE<const double&>(dd+3.14)); // ok, but dangerous
116     }
117 
118 #if defined(FUSION_TEST_FAIL)
119     dummy(FUSION_SEQUENCE<double&>(dd+3.14));   // should fail,
120                                                 // temporary to non-const reference
121 #endif
122 }
123