1 /*=============================================================================
2 Copyright (c) 2010, 2012 Christopher Schmidt, Nathan Ridge
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
8 #include <boost/detail/lightweight_test.hpp>
9 #include <boost/fusion/sequence.hpp>
10 #include <boost/fusion/container.hpp>
11 #include <boost/fusion/support.hpp>
12 #include <boost/fusion/sequence/io/out.hpp>
13 #include <boost/fusion/adapted/struct/define_struct_inline.hpp>
14 #include <boost/preprocessor/empty.hpp>
15 #include <boost/mpl/assert.hpp>
16 #include <boost/static_assert.hpp>
17 #include <iostream>
18 #include <string>
19
20 struct cls
21 {
22 BOOST_FUSION_DEFINE_STRUCT_INLINE(
23 point,
24 (int, x)
25 (int, y)
26 )
27 };
28
29 template <typename = int>
30 struct tpl_cls
31 {
32 BOOST_FUSION_DEFINE_STRUCT_INLINE(
33 point,
34 (int, x)
35 (int, y)
36 )
37 };
38
39 namespace ns
40 {
41 BOOST_FUSION_DEFINE_STRUCT_INLINE(s, (int, m))
42
43 BOOST_FUSION_DEFINE_STRUCT_INLINE(empty_struct, )
44
45 // Testing non-constexpr compatible types
46 BOOST_FUSION_DEFINE_STRUCT_INLINE(
47 employee,
48 (std::string, name)
49 (std::string, nickname)
50 )
51 }
52
53 template <typename Point>
run_test()54 void run_test()
55 {
56 using namespace boost::fusion;
57
58 std::cout << tuple_open('[');
59 std::cout << tuple_close(']');
60 std::cout << tuple_delimiter(", ");
61
62 {
63 BOOST_STATIC_ASSERT(boost::fusion::result_of::size<ns::empty_struct>::value == 0);
64 BOOST_STATIC_ASSERT(boost::fusion::result_of::empty<ns::empty_struct>::value);
65 }
66
67 {
68 BOOST_MPL_ASSERT_NOT((traits::is_view<Point>));
69 BOOST_STATIC_ASSERT(!traits::is_view<Point>::value);
70 Point p(123, 456);
71
72 std::cout << at_c<0>(p) << std::endl;
73 std::cout << at_c<1>(p) << std::endl;
74 std::cout << p << std::endl;
75 BOOST_TEST(p == make_vector(123, 456));
76
77 at_c<0>(p) = 6;
78 at_c<1>(p) = 9;
79 BOOST_TEST(p == make_vector(6, 9));
80
81 BOOST_STATIC_ASSERT(boost::fusion::result_of::size<Point>::value == 2);
82 BOOST_STATIC_ASSERT(!boost::fusion::result_of::empty<Point>::value);
83
84 BOOST_TEST(front(p) == 6);
85 BOOST_TEST(back(p) == 9);
86 }
87
88 {
89 vector<int, float> v1(4, 2.0f);
90 Point v2(5, 3);
91 vector<long, double> v3(5, 4.);
92 BOOST_TEST(v1 < v2);
93 BOOST_TEST(v1 <= v2);
94 BOOST_TEST(v2 > v1);
95 BOOST_TEST(v2 >= v1);
96 BOOST_TEST(v2 < v3);
97 BOOST_TEST(v2 <= v3);
98 BOOST_TEST(v3 > v2);
99 BOOST_TEST(v3 >= v2);
100 }
101
102 {
103 // conversion from Point to vector
104 Point p(5, 3);
105 vector<int, long> v(p);
106 v = p;
107 }
108
109 {
110 // conversion from Point to list
111 Point p(5, 3);
112 list<int, long> l(p);
113 l = p;
114 }
115
116 { // begin/end
117 using namespace boost::fusion;
118
119 typedef boost::fusion::result_of::begin<ns::s>::type b;
120 typedef boost::fusion::result_of::end<ns::s>::type e;
121 // this fails
122 BOOST_MPL_ASSERT((boost::is_same<boost::fusion::result_of::next<b>::type, e>));
123 }
124
125 {
126 Point p = make_list(5,3);
127 BOOST_TEST(p == make_vector(5,3));
128
129 p = make_list(3,5);
130 BOOST_TEST(p == make_vector(3,5));
131 }
132 }
133
134 int
main()135 main()
136 {
137 run_test<cls::point>(); // test with non-template enclosing class
138 run_test<tpl_cls<>::point>(); // test with template enclosing class
139
140 {
141 using namespace boost::fusion;
142
143 ns::employee emp = make_list("John Doe", "jdoe");
144 std::cout << at_c<0>(emp) << std::endl;
145 std::cout << at_c<1>(emp) << std::endl;
146
147 BOOST_TEST(emp == make_vector("John Doe", "jdoe"));
148 }
149
150 return boost::report_errors();
151
152 }
153
154