1// Boost.TypeErasure library
2//
3// Copyright 2012 Steven Watanabe
4//
5// Distributed under the Boost Software License Version 1.0. (See
6// accompanying file LICENSE_1_0.txt or copy at
7// http://www.boost.org/LICENSE_1_0.txt)
8//
9// $Id$
10
11#include <boost/type_erasure/static_binding.hpp>
12#include <boost/type_erasure/binding.hpp>
13#include <boost/type_erasure/placeholder.hpp>
14#include <boost/type_erasure/builtin.hpp>
15#include <boost/mpl/vector.hpp>
16#include <boost/mpl/map.hpp>
17
18#define BOOST_TEST_MAIN
19#include <boost/test/unit_test.hpp>
20
21using namespace boost::type_erasure;
22
23BOOST_AUTO_TEST_CASE(test_empty_binding)
24{
25 boost::mpl::map<> m1;
26 binding<boost::mpl::vector<> > b1(m1);
27 binding<boost::mpl::vector<> > b2(make_binding<boost::mpl::map<> >());
28 BOOST_CHECK(b1 == b2);
29 BOOST_CHECK(!(b1 != b2));
30
31 boost::mpl::map<boost::mpl::pair<_a, int> > m2;
32 binding<boost::mpl::vector<> > b3(m2);
33 BOOST_CHECK(b3 == b1);
34 BOOST_CHECK(!(b3 != b1));
35 binding<boost::mpl::vector<> > b4(make_binding<boost::mpl::map<boost::mpl::pair<_a, int> > >());
36 BOOST_CHECK(b4 == b1);
37 BOOST_CHECK(!(b4 != b1));
38
39 binding<boost::mpl::vector<> > b5(b1, m1);
40 BOOST_CHECK(b5 == b1);
41 BOOST_CHECK(!(b5 != b1));
42 binding<boost::mpl::vector<> > b6(b1, make_binding<boost::mpl::map<> >());
43 BOOST_CHECK(b6 == b1);
44 BOOST_CHECK(!(b6 != b1));
45
46 boost::mpl::map<boost::mpl::pair<_a, _b> > m3;
47 binding<boost::mpl::vector<> > b7(b1, m3);
48 BOOST_CHECK(b7 == b1);
49 BOOST_CHECK(!(b7 != b1));
50 binding<boost::mpl::vector<> > b8(b1, make_binding<boost::mpl::map<boost::mpl::pair<_a, _b> > >());
51 BOOST_CHECK(b8 == b1);
52 BOOST_CHECK(!(b8 != b1));
53}
54
55BOOST_AUTO_TEST_CASE(test_binding_one)
56{
57 boost::mpl::map<boost::mpl::pair<_a, int> > m1;
58 binding<typeid_<_a> > b1(m1);
59 BOOST_CHECK(b1.find<typeid_<_a> >()() == typeid(int));
60 binding<typeid_<_a> > b2(make_binding<boost::mpl::map<boost::mpl::pair<_a, int> > >());
61 BOOST_CHECK(b2.find<typeid_<_a> >()() == typeid(int));
62 BOOST_CHECK(b1 == b2);
63 BOOST_CHECK(!(b1 != b2));
64
65 boost::mpl::map<boost::mpl::pair<_a, _a> > m2;
66 binding<typeid_<_a> > b3(b1, m2);
67 BOOST_CHECK(b3.find<typeid_<_a> >()() == typeid(int));
68 BOOST_CHECK(b3 == b1);
69 BOOST_CHECK(!(b3 != b1));
70 binding<typeid_<_a> > b4(b1, make_binding<boost::mpl::map<boost::mpl::pair<_a, _a> > >());
71 BOOST_CHECK(b4.find<typeid_<_a> >()() == typeid(int));
72 BOOST_CHECK(b4 == b1);
73 BOOST_CHECK(!(b4 != b1));
74
75 boost::mpl::map<boost::mpl::pair<_b, _a> > m3;
76 binding<typeid_<_b> > b5(b1, m3);
77 BOOST_CHECK(b5.find<typeid_<_b> >()() == typeid(int));
78 binding<typeid_<_b> > b6(b1, make_binding<boost::mpl::map<boost::mpl::pair<_b, _a> > >());
79 BOOST_CHECK(b6.find<typeid_<_b> >()() == typeid(int));
80}
81
82BOOST_AUTO_TEST_CASE(test_binding_two)
83{
84 boost::mpl::map<boost::mpl::pair<_a, int>, boost::mpl::pair<_b, char> > m1;
85 binding<boost::mpl::vector<typeid_<_a>, typeid_<_b> > > b1(m1);
86 BOOST_CHECK(b1.find<typeid_<_a> >()() == typeid(int));
87 BOOST_CHECK(b1.find<typeid_<_b> >()() == typeid(char));
88 binding<boost::mpl::vector<typeid_<_a>, typeid_<_b> > > b2(
89 make_binding<boost::mpl::map<boost::mpl::pair<_a, int>, boost::mpl::pair<_b, char> > >());
90 BOOST_CHECK(b2.find<typeid_<_a> >()() == typeid(int));
91 BOOST_CHECK(b2.find<typeid_<_b> >()() == typeid(char));
92 BOOST_CHECK(b1 == b2);
93 BOOST_CHECK(!(b1 != b2));
94
95 // select the first
96 boost::mpl::map<boost::mpl::pair<_a, _a> > m2;
97 binding<typeid_<_a> > b3(b1, m2);
98 BOOST_CHECK(b3.find<typeid_<_a> >()() == typeid(int));
99 binding<typeid_<_a> > b4(b1, make_binding<boost::mpl::map<boost::mpl::pair<_a, _a> > >());
100 BOOST_CHECK(b4.find<typeid_<_a> >()() == typeid(int));
101
102 // select the second
103 boost::mpl::map<boost::mpl::pair<_b, _b> > m3;
104 binding<typeid_<_b> > b5(b1, m3);
105 BOOST_CHECK(b5.find<typeid_<_b> >()() == typeid(char));
106 binding<typeid_<_b> > b6(b1, make_binding<boost::mpl::map<boost::mpl::pair<_b, _b> > >());
107 BOOST_CHECK(b6.find<typeid_<_b> >()() == typeid(char));
108
109 // rename both
110 boost::mpl::map<boost::mpl::pair<_c, _a>, boost::mpl::pair<_d, _b> > m4;
111 binding<boost::mpl::vector<typeid_<_c>, typeid_<_d> > > b7(b1, m4);
112 BOOST_CHECK(b7.find<typeid_<_c> >()() == typeid(int));
113 BOOST_CHECK(b7.find<typeid_<_d> >()() == typeid(char));
114 binding<boost::mpl::vector<typeid_<_c>, typeid_<_d> > > b8(b1,
115 make_binding<boost::mpl::map<boost::mpl::pair<_c, _a>, boost::mpl::pair<_d, _b> > >());
116 BOOST_CHECK(b8.find<typeid_<_c> >()() == typeid(int));
117 BOOST_CHECK(b8.find<typeid_<_d> >()() == typeid(char));
118
119 // switch the placeholders
120 boost::mpl::map<boost::mpl::pair<_a, _b>, boost::mpl::pair<_b, _a> > m5;
121 binding<boost::mpl::vector<typeid_<_a>, typeid_<_b> > > b9(b1, m5);
122 BOOST_CHECK(b9.find<typeid_<_b> >()() == typeid(int));
123 BOOST_CHECK(b9.find<typeid_<_a> >()() == typeid(char));
124 binding<boost::mpl::vector<typeid_<_a>, typeid_<_b> > > b10(b1,
125 make_binding<boost::mpl::map<boost::mpl::pair<_a, _b>, boost::mpl::pair<_b, _a> > >());
126 BOOST_CHECK(b10.find<typeid_<_b> >()() == typeid(int));
127 BOOST_CHECK(b10.find<typeid_<_a> >()() == typeid(char));
128}
129

source code of boost/libs/type_erasure/test/test_binding.cpp