1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // make.hpp |
3 | // |
4 | // Copyright 2008 Eric Niebler. Distributed under the Boost |
5 | // Software License, Version 1.0. (See accompanying file |
6 | // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
7 | |
8 | #include <boost/proto/core.hpp> |
9 | #include <boost/proto/transform/arg.hpp> |
10 | #include <boost/proto/transform/make.hpp> |
11 | #include <boost/mpl/identity.hpp> |
12 | #include <boost/test/unit_test.hpp> |
13 | |
14 | namespace mpl = boost::mpl; |
15 | namespace proto = boost::proto; |
16 | using proto::_; |
17 | |
18 | template<typename T> |
19 | struct type2type {}; |
20 | |
21 | template<typename T> |
22 | struct wrapper |
23 | { |
24 | T t_; |
25 | explicit wrapper(T const & t = T()) : t_(t) {} |
26 | }; |
27 | |
28 | template<typename T> |
29 | struct careful |
30 | { |
31 | typedef typename T::not_there not_there; |
32 | }; |
33 | |
34 | // Test that when no substitution is done, we don't instantiate templates |
35 | struct MakeTest1 |
36 | : proto::make< type2type< careful<int> > > |
37 | {}; |
38 | |
39 | void make_test1() |
40 | { |
41 | proto::terminal<int>::type i = {.child0: 42}; |
42 | type2type< careful<int> > res = MakeTest1()(i); |
43 | } |
44 | |
45 | // Test that when substitution is done, and there is no nested ::type |
46 | // typedef, the result is the wrapper |
47 | struct MakeTest2 |
48 | : proto::make< wrapper< proto::_value > > |
49 | {}; |
50 | |
51 | void make_test2() |
52 | { |
53 | proto::terminal<int>::type i = {.child0: 42}; |
54 | wrapper<int> res = MakeTest2()(i); |
55 | BOOST_CHECK_EQUAL(res.t_, 0); |
56 | } |
57 | |
58 | // Test that when substitution is done, and there is no nested ::type |
59 | // typedef, the result is the wrapper |
60 | struct MakeTest3 |
61 | : proto::make< wrapper< proto::_value >(proto::_value) > |
62 | {}; |
63 | |
64 | void make_test3() |
65 | { |
66 | proto::terminal<int>::type i = {.child0: 42}; |
67 | wrapper<int> res = MakeTest3()(i); |
68 | BOOST_CHECK_EQUAL(res.t_, 42); |
69 | } |
70 | |
71 | // Test that when substitution is done, and there is no nested ::type |
72 | // typedef, the result is the wrapper |
73 | struct MakeTest4 |
74 | : proto::make< mpl::identity< proto::_value >(proto::_value) > |
75 | {}; |
76 | |
77 | void make_test4() |
78 | { |
79 | proto::terminal<int>::type i = {.child0: 42}; |
80 | int res = MakeTest4()(i); |
81 | BOOST_CHECK_EQUAL(res, 42); |
82 | } |
83 | |
84 | using namespace boost::unit_test; |
85 | /////////////////////////////////////////////////////////////////////////////// |
86 | // init_unit_test_suite |
87 | // |
88 | test_suite* init_unit_test_suite( int argc, char* argv[] ) |
89 | { |
90 | test_suite *test = BOOST_TEST_SUITE("test the make transform" ); |
91 | |
92 | test->add(BOOST_TEST_CASE(&make_test1)); |
93 | test->add(BOOST_TEST_CASE(&make_test2)); |
94 | test->add(BOOST_TEST_CASE(&make_test3)); |
95 | test->add(BOOST_TEST_CASE(&make_test4)); |
96 | |
97 | return test; |
98 | } |
99 | |