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