1 /*=============================================================================
2 Copyright (c) 1999-2003 Jaakko Jarvi
3 Copyright (c) 2001-2013 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/fusion/container/map/convert.hpp>
11 #include <boost/detail/lightweight_test.hpp>
12 #include <boost/fusion/sequence/intrinsic.hpp>
13 #include <boost/fusion/support/is_sequence.hpp>
14 #include <boost/fusion/mpl.hpp>
15 #include <boost/mpl/find.hpp>
16 #include <boost/mpl/equal.hpp>
17 #include <boost/mpl/int.hpp>
18 #include <boost/mpl/integral_c.hpp>
19 #include <boost/mpl/is_sequence.hpp>
20 #include <boost/type_traits/is_same.hpp>
21 #include <string>
22
23 struct k1 {};
24 struct k2 {};
25 struct k3 {};
26 struct k4 {};
27
28 template <typename S1, typename S2>
29 struct is_same
30 {
31 };
32
33 namespace fn = boost::fusion;
34
35 struct test_intrinsics1
36 {
37 // test at, begin, end, next, prior, advance, size, deref, etc.
38
39 typedef fn::map<
40 fn::pair<k1, int>, fn::pair<k2, float>,
41 fn::pair<k3, bool>, fn::pair<k3, char> >
42 sequence;
43
44 typedef boost::mpl::begin<sequence>::type first;
45 typedef boost::mpl::next<first>::type second;
46 typedef boost::mpl::next<second>::type third;
47 typedef boost::mpl::next<third>::type fourth;
48 typedef boost::mpl::end<sequence>::type last;
49
50 BOOST_STATIC_ASSERT((boost::is_same<
51 boost::mpl::deref<first>::type, fn::pair<k1, int> >::value));
52
53 BOOST_STATIC_ASSERT((boost::is_same<
54 boost::mpl::deref<second>::type, fn::pair<k2, float> >::value));
55
56 BOOST_STATIC_ASSERT((boost::is_same<
57 boost::mpl::deref<third>::type, fn::pair<k3, bool> >::value));
58
59 BOOST_STATIC_ASSERT((boost::is_same<
60 boost::mpl::deref<fourth>::type, fn::pair<k3, char> >::value));
61
62 BOOST_STATIC_ASSERT((boost::is_same<
63 boost::mpl::at_c<sequence, 2>::type, fn::pair<k3, bool> >::value));
64
65 BOOST_STATIC_ASSERT((boost::is_same<
66 boost::mpl::front<sequence>::type, fn::pair<k1, int> >::value));
67
68 BOOST_STATIC_ASSERT((boost::is_same<
69 boost::mpl::deref<
70 boost::mpl::advance_c<second, 2>::type>::type, fn::pair<k3, char> >::value));
71
72 BOOST_STATIC_ASSERT((boost::mpl::size<sequence>::value == 4));
73 BOOST_STATIC_ASSERT(!(boost::mpl::empty<sequence>::value));
74 BOOST_STATIC_ASSERT((boost::mpl::distance<second, fourth>::value == 2));
75
76 typedef boost::mpl::prior<last>::type fourth_;
77 typedef boost::mpl::prior<fourth_>::type third_;
78 typedef boost::mpl::prior<third_>::type second_;
79 typedef boost::mpl::prior<second_>::type first_;
80
81 BOOST_STATIC_ASSERT((boost::is_same<
82 boost::mpl::deref<first_>::type, fn::pair<k1, int> >::value));
83
84 BOOST_STATIC_ASSERT((boost::is_same<
85 boost::mpl::deref<second_>::type, fn::pair<k2, float> >::value));
86
87 BOOST_STATIC_ASSERT((boost::is_same<
88 boost::mpl::deref<third_>::type, fn::pair<k3, bool> >::value));
89
90 BOOST_STATIC_ASSERT((boost::is_same<
91 boost::mpl::deref<fourth_>::type, fn::pair<k3, char> >::value));
92
93 BOOST_STATIC_ASSERT((boost::is_same<
94 boost::mpl::back<sequence>::type, fn::pair<k3, char> >::value));
95
96 };
97
98 void
test()99 test()
100 {
101 using namespace boost::fusion;
102
103 { // testing const sequences
104
105 const map<pair<k1, int>, pair<k2, float> > t1(5, 3.3f);
106 BOOST_TEST(at_c<0>(t1).second == 5);
107 BOOST_TEST(at_c<1>(t1).second == 3.3f);
108 }
109
110 { // testing at<N> works with MPL integral constants
111 const map<pair<k1, int>, pair<k2, char> > t1(101, 'z');
112 BOOST_TEST(boost::fusion::at<boost::mpl::int_<0> >(t1).second == 101);
113 BOOST_TEST(boost::fusion::at<boost::mpl::int_<1> >(t1).second == 'z');
114 // explicitly try something other than mpl::int_
115 BOOST_TEST((boost::fusion::at<boost::mpl::integral_c<long, 0> >(t1).second == 101));
116 BOOST_TEST((boost::fusion::at<boost::mpl::integral_c<long, 1> >(t1).second == 'z'));
117 }
118
119 { // testing size & empty
120
121 typedef map<pair<k1, int>, pair<k2, float>, pair<k3, double> > t1;
122 typedef map<> t2;
123
124 BOOST_STATIC_ASSERT(boost::fusion::result_of::size<t1>::value == 3);
125 BOOST_STATIC_ASSERT(boost::fusion::result_of::size<t2>::value == 0);
126 BOOST_STATIC_ASSERT(!boost::fusion::result_of::empty<t1>::value);
127 BOOST_STATIC_ASSERT(boost::fusion::result_of::empty<t2>::value);
128 }
129
130 { // testing front & back
131
132 typedef map<pair<k1, int>, pair<k2, float>, pair<k3, std::string> > tup;
133 tup t(1, 2.2f, std::string("Kimpo"));
134
135 BOOST_TEST(front(t).second == 1);
136 BOOST_TEST(back(t).second == "Kimpo");
137 }
138
139 { // testing is_sequence
140
141 typedef map<pair<k1, int>, pair<k2, float>, pair<k3, double> > t1;
142 typedef map<> t2;
143 typedef map<pair<k1, char> > t3;
144
145 BOOST_STATIC_ASSERT(traits::is_sequence<t1>::value);
146 BOOST_STATIC_ASSERT(traits::is_sequence<t2>::value);
147 BOOST_STATIC_ASSERT(traits::is_sequence<t3>::value);
148 BOOST_STATIC_ASSERT(!traits::is_sequence<int>::value);
149 BOOST_STATIC_ASSERT(!traits::is_sequence<char>::value);
150 }
151
152 { // testing mpl::is_sequence
153
154 typedef map<pair<k1, int>, pair<k2, float>, pair<k3, double> > t1;
155 typedef map<> t2;
156 typedef map<pair<k1, char> > t3;
157
158 BOOST_STATIC_ASSERT(boost::mpl::is_sequence<t1>::value);
159 BOOST_STATIC_ASSERT(boost::mpl::is_sequence<t2>::value);
160 BOOST_STATIC_ASSERT(boost::mpl::is_sequence<t3>::value);
161 }
162
163 { // testing mpl compatibility
164
165 // test an algorithm
166 typedef map<pair<k1, int>, pair<k2, float>, pair<k3, double> > t1;
167 typedef boost::mpl::find<t1, pair<k2, float> >::type iter;
168 typedef boost::mpl::deref<iter>::type type;
169 BOOST_STATIC_ASSERT((boost::is_same<type, pair<k2, float> >::value));
170 }
171 }
172
173 int
main()174 main()
175 {
176 test();
177 return boost::report_errors();
178 }
179
180