1 /*=============================================================================
2 Copyright (c) 2001-2011 Joel de Guzman
3
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 ==============================================================================*/
7 #include <boost/detail/lightweight_test.hpp>
8 #include <boost/fusion/container/map.hpp>
9 #include <boost/fusion/container/set.hpp>
10 #include <boost/fusion/container/vector/vector.hpp>
11 #include <boost/fusion/view/joint_view/joint_view.hpp>
12 #include <boost/fusion/sequence/io/out.hpp>
13 #include <boost/fusion/sequence/comparison/equal_to.hpp>
14 #include <boost/fusion/container/generation/make_vector.hpp>
15 #include <boost/fusion/sequence/intrinsic/at.hpp>
16 #include <boost/fusion/sequence/intrinsic/has_key.hpp>
17 #include <boost/fusion/sequence/intrinsic/begin.hpp>
18 #include <boost/fusion/iterator/next.hpp>
19 #include <boost/fusion/iterator/key_of.hpp>
20 #include <boost/fusion/iterator/value_of.hpp>
21 #include <boost/fusion/iterator/deref_data.hpp>
22 #include <boost/mpl/assert.hpp>
23 #include <string>
24
25 struct X
26 {
operator char const*X27 operator char const*() const
28 {
29 return "<X-object>";
30 }
31 };
32
33 int
main()34 main()
35 {
36 using namespace boost::fusion;
37 namespace fusion = boost::fusion;
38
39 std::cout << tuple_open('[');
40 std::cout << tuple_close(']');
41 std::cout << tuple_delimiter(", ");
42
43 /// Testing joint_view
44
45 {
46 vector<int> t1(3);
47 vector<X> t2;
48 typedef joint_view<vector<int>, vector<X> > view_type;
49 view_type view(t1, t2);
50
51 std::cout << view << std::endl;
52 BOOST_TEST((view == make_vector(3, X())));
53 }
54
55 {
56 vector<int, char> t1(3, 'x');
57 vector<X> t2;
58 typedef joint_view<vector<int, char>, vector<X> > view_type;
59 view_type view(t1, t2);
60 std::cout << view << std::endl;
61 BOOST_TEST((view == make_vector(3, 'x', X())));
62
63 *begin(view) = 4;
64 BOOST_TEST(at_c<0>(t1) == 4);
65 }
66
67 {
68 vector<int, char> t1(3, 'x');
69 vector<X, int> t2;
70 typedef joint_view<vector<int, char>, vector<X, int> > view_type;
71 view_type view(t1, t2);
72 std::cout << view << std::endl;
73 BOOST_TEST((view == make_vector(3, 'x', X(), 0)));
74 }
75
76 {
77 typedef vector<int> t1_type;
78 t1_type t1(777);
79 typedef vector<int, char, double> t2_type;
80 t2_type t2(1, 'x', 3.3);
81
82 {
83 typedef joint_view<t1_type, t2_type> view_type;
84 view_type view(t1, t2);
85 std::cout << view << std::endl;
86 BOOST_TEST((view == make_vector(777, 1, 'x', 3.3)));
87 }
88
89 {
90 typedef joint_view<t2_type, t1_type> view_type;
91 view_type view(t2, t1);
92 std::cout << view << std::endl;
93 BOOST_TEST((view == make_vector(1, 'x', 3.3, 777)));
94 }
95
96 {
97 typedef joint_view<t2_type, t1_type> jv_type;
98 typedef joint_view<jv_type, jv_type> jv2_type;
99
100 jv_type jv(t2, t1);
101 jv2_type jv2(jv, jv);
102
103 std::cout << jv << std::endl;
104 std::cout << jv2 << std::endl;
105
106 BOOST_TEST(jv2
107 == make_vector(1, 'x', 3.3, 777, 1, 'x', 3.3, 777));
108 }
109
110 {
111 typedef joint_view<t2_type, t1_type> jt_type;
112 typedef joint_view<t1_type, t2_type> jv2_type;
113 typedef joint_view<jt_type, jv2_type> jv3_type;
114
115 jt_type jt(t2, t1);
116 jv2_type jv2(t1, t2);
117 jv3_type jv3(jt, jv2);
118
119 std::cout << jt << std::endl;
120 std::cout << jv2 << std::endl;
121 std::cout << jv3 << std::endl;
122
123 BOOST_TEST(jv3
124 == make_vector(1, 'x', 3.3, 777, 777, 1, 'x', 3.3));
125 }
126
127 {
128 typedef joint_view<vector<>, t1_type> jt_type;
129 vector<> empty;
130 jt_type jt(empty, t1);
131 std::cout << jt << std::endl;
132 BOOST_TEST(jt == make_vector(777));
133 }
134
135 {
136 typedef joint_view<t1_type, vector<> > jt_type;
137 vector<> empty;
138 jt_type jt(t1, empty);
139 std::cout << jt << std::endl;
140 BOOST_TEST(jt == make_vector(777));
141 }
142
143 {
144 typedef joint_view<vector<>, vector<> > jt_type;
145 vector<> empty;
146 jt_type jt(empty, empty);
147 std::cout << jt << std::endl;
148 BOOST_TEST(jt == make_vector());
149 }
150 }
151
152 {
153 typedef map<pair<void,int> > map_type;
154 map_type m(make_pair<void>(0));
155
156 typedef set<std::string, float> set_type;
157 set_type s("foo", 1.3f);
158
159 typedef joint_view<map_type, set_type> joint_view_type;
160 joint_view_type j(m,s);
161
162 BOOST_MPL_ASSERT((boost::fusion::result_of::has_key<joint_view_type, void>::type));
163 BOOST_MPL_ASSERT((boost::fusion::result_of::has_key<joint_view_type, std::string>::type));
164 BOOST_MPL_ASSERT((boost::fusion::result_of::has_key<joint_view_type, float>::type));
165
166 BOOST_MPL_ASSERT((boost::is_same<boost::fusion::result_of::key_of<boost::fusion::result_of::begin<joint_view_type>::type>::type, void>));
167 BOOST_MPL_ASSERT((boost::is_same<boost::fusion::result_of::key_of<boost::fusion::result_of::next<boost::fusion::result_of::begin<joint_view_type>::type>::type>::type, std::string>));
168 BOOST_MPL_ASSERT((boost::is_same<
169 boost::fusion::result_of::key_of<boost::fusion::result_of::next<boost::fusion::result_of::next<boost::fusion::result_of::begin<joint_view_type>::type>::type>::type>::type
170 , float>));
171
172 BOOST_MPL_ASSERT((boost::is_same<boost::fusion::result_of::value_of_data<boost::fusion::result_of::begin<joint_view_type>::type>::type, int>));
173 BOOST_MPL_ASSERT((boost::is_same<boost::fusion::result_of::value_of_data<boost::fusion::result_of::next<boost::fusion::result_of::begin<joint_view_type>::type>::type>::type, std::string>));
174 BOOST_MPL_ASSERT((boost::is_same<
175 boost::fusion::result_of::value_of_data<boost::fusion::result_of::next<boost::fusion::result_of::next<boost::fusion::result_of::begin<joint_view_type>::type>::type>::type>::type
176 , float>));
177
178 std::cout << deref_data(begin(j)) << std::endl;
179 std::cout << deref_data(fusion::next(begin(j))) << std::endl;
180 std::cout << deref_data(fusion::next(fusion::next(begin(j)))) << std::endl;
181 BOOST_TEST((deref_data(begin(j)) == 0));
182 BOOST_TEST((deref_data(fusion::next(begin(j))) == "foo"));
183 BOOST_TEST((deref_data(fusion::next(fusion::next(begin(j)))) == 1.3f));
184 }
185
186 return boost::report_errors();
187 }
188
189