• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*=============================================================================
2     Copyright (c) 2006-2007 Tobias Schwinger
3 
4     Use modification and distribution are subject to the Boost Software
5     License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6     http://www.boost.org/LICENSE_1_0.txt).
7 ==============================================================================*/
8 
9 #include <boost/fusion/functional/adapter/unfused_typed.hpp>
10 #include <boost/detail/lightweight_test.hpp>
11 
12 #include <boost/noncopyable.hpp>
13 
14 #include <boost/mpl/empty_base.hpp>
15 #include <boost/mpl/identity.hpp>
16 #include <boost/mpl/placeholders.hpp>
17 
18 #include <boost/utility/result_of.hpp>
19 
20 #include <boost/fusion/algorithm/iteration/fold.hpp>
21 
22 namespace fusion = boost::fusion;
23 namespace mpl = boost::mpl;
24 using mpl::placeholders::_;
25 
26 using boost::noncopyable;
27 
28 typedef fusion::vector<> types0;
29 typedef fusion::vector<long &> types1;
30 typedef fusion::vector<long &,int,char> types3;
31 
32 template <class Base = boost::mpl::empty_base>
33 struct test_func
34     : Base
35 {
36     template<typename T>
37     struct result;
38 
39     template <class Self, class Seq>
40     struct result< Self(Seq) >
41         : mpl::identity<long>
42     { };
43 
44     template <typename Seq>
operator ()test_func45     long operator()(Seq const & seq) const
46     {
47         long state = 0;
48         return fusion::fold(seq, state, fold_op());
49     }
50 
51     template < typename Seq >
operator ()test_func52     long operator()(Seq const & seq)
53     {
54         long state = 100;
55         return fusion::fold(seq, state, fold_op());
56     }
57 
58   private:
59 
60     struct fold_op
61     {
62         typedef long result_type;
63 
64         template <typename T>
operator ()test_func::fold_op65         long operator()(long value, T const & elem) const
66         {
67           return value + sizeof(T) * elem;
68         }
69 
70         template <typename T>
operator ()test_func::fold_op71         long operator()(long value, T & elem) const
72         {
73           elem += sizeof(T);
74           return value;
75         }
76     };
77 };
78 
result_type_tests()79 void result_type_tests()
80 {
81     using boost::is_same;
82 
83     typedef fusion::unfused_typed< test_func<>, types0 > t0;
84     BOOST_TEST(( is_same< boost::result_of< t0 () >::type, long >::value ));
85     typedef fusion::unfused_typed< test_func<>, types1 > t1;
86     BOOST_TEST(( is_same< boost::result_of< t1 (long &) >::type, long >::value ));
87 }
88 
89 #if defined(BOOST_MSVC) && BOOST_MSVC < 1400
90 #   define BOOST_TEST_NO_VC71(cond) (void)((cond)?0:1)
91 #else
92 #   define BOOST_TEST_NO_VC71(cond) BOOST_TEST(cond)
93 #endif
94 
nullary_tests()95 void nullary_tests()
96 {
97     test_func<noncopyable> f;
98     fusion::unfused_typed< test_func<>, types0 > unfused_func;
99     fusion::unfused_typed< test_func<noncopyable> &, types0 > unfused_func_ref(f);
100     fusion::unfused_typed< test_func<> const, types0 > unfused_func_c;
101     fusion::unfused_typed< test_func<>, types0 > const unfused_func_c2;
102     fusion::unfused_typed< test_func<noncopyable> const &, types0 > unfused_func_c_ref(f);
103 
104     BOOST_TEST(unfused_func() == 100);
105     BOOST_TEST(unfused_func_ref() == 100);
106     BOOST_TEST(unfused_func_c() == 0);
107     BOOST_TEST(unfused_func_c2() == 0);
108     BOOST_TEST(unfused_func_c_ref() == 0);
109 }
110 
unary_tests()111 void unary_tests()
112 {
113     test_func<noncopyable> f;
114     fusion::unfused_typed< test_func<>, types1 > unfused_func;
115     fusion::unfused_typed< test_func<noncopyable> &, types1 > unfused_func_ref(f);
116     fusion::unfused_typed< test_func<> const, types1 > unfused_func_c;
117     fusion::unfused_typed< test_func<>, types1 > const unfused_func_c2;
118     fusion::unfused_typed< test_func<noncopyable> const &, types1 > unfused_func_c_ref(f);
119 
120     long lvalue = 1;
121     BOOST_TEST_NO_VC71(unfused_func(lvalue) == 100);
122     BOOST_TEST(lvalue == 1 + 1*sizeof(lvalue));
123     BOOST_TEST(unfused_func_ref(lvalue) == 100);
124     BOOST_TEST(lvalue == 1 + 2*sizeof(lvalue));
125     BOOST_TEST(unfused_func_c(lvalue) == 0);
126     BOOST_TEST(lvalue == 1 + 3*sizeof(lvalue));
127     BOOST_TEST(unfused_func_c2(lvalue) == 0);
128     BOOST_TEST(lvalue == 1 + 4*sizeof(lvalue));
129     BOOST_TEST(unfused_func_c_ref(lvalue) == 0);
130     BOOST_TEST(lvalue == 1 + 5*sizeof(lvalue));
131 }
132 
ternary_tests()133 void ternary_tests()
134 {
135     test_func<noncopyable> f;
136     fusion::unfused_typed< test_func<>, types3 > unfused_func;
137     fusion::unfused_typed< test_func<noncopyable> &, types3 > unfused_func_ref(f);
138     fusion::unfused_typed< test_func<> const, types3 > unfused_func_c;
139     fusion::unfused_typed< test_func<>, types3 > const unfused_func_c2;
140     fusion::unfused_typed< test_func<noncopyable> const &, types3 > unfused_func_c_ref(f);
141 
142     long lvalue = 1;
143     static const long expected = 2*sizeof(int) + 7*sizeof(char);
144     BOOST_TEST_NO_VC71(unfused_func(lvalue,2,'\007') == 100 + expected);
145     BOOST_TEST(lvalue == 1 + 1*sizeof(lvalue));
146     BOOST_TEST(unfused_func_ref(lvalue,2,'\007') == 100 + expected);
147     BOOST_TEST(lvalue == 1 + 2*sizeof(lvalue));
148     BOOST_TEST(unfused_func_c(lvalue,2,'\007') == 0 + expected);
149     BOOST_TEST(lvalue == 1 + 3*sizeof(lvalue));
150     BOOST_TEST(unfused_func_c2(lvalue,2,'\007') == 0 + expected);
151     BOOST_TEST(lvalue == 1 + 4*sizeof(lvalue));
152     BOOST_TEST(unfused_func_c_ref(lvalue,2,'\007') == 0 + expected);
153     BOOST_TEST(lvalue == 1 + 5*sizeof(lvalue));
154 }
155 
main()156 int main()
157 {
158     result_type_tests();
159     nullary_tests();
160     unary_tests();
161     ternary_tests();
162 
163     return boost::report_errors();
164 }
165 
166