1 | // Boost.TypeErasure library |
2 | // |
3 | // Copyright 2011 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/any.hpp> |
12 | #include <boost/type_erasure/tuple.hpp> |
13 | #include <boost/type_erasure/builtin.hpp> |
14 | #include <boost/type_erasure/operators.hpp> |
15 | #include <boost/type_erasure/any_cast.hpp> |
16 | #include <boost/mpl/vector.hpp> |
17 | |
18 | #define BOOST_TEST_MAIN |
19 | #include <boost/test/unit_test.hpp> |
20 | |
21 | using namespace boost::type_erasure; |
22 | |
23 | template<class T = _self> |
24 | struct common : ::boost::mpl::vector< |
25 | destructible<T>, |
26 | copy_constructible<T>, |
27 | typeid_<T> |
28 | > {}; |
29 | |
30 | BOOST_AUTO_TEST_CASE(test_same) |
31 | { |
32 | typedef ::boost::mpl::vector<common<>, addable<> > test_concept; |
33 | any<test_concept> x(1); |
34 | any<test_concept> y(2); |
35 | any<test_concept> z(x + y); |
36 | int i = any_cast<int>(arg&: z); |
37 | BOOST_CHECK_EQUAL(i, 3); |
38 | } |
39 | |
40 | BOOST_AUTO_TEST_CASE(test_int1) |
41 | { |
42 | typedef ::boost::mpl::vector<common<>, addable<_self, int> > test_concept; |
43 | any<test_concept> x(1); |
44 | any<test_concept> z(x + 2); |
45 | int i = any_cast<int>(arg&: z); |
46 | BOOST_CHECK_EQUAL(i, 3); |
47 | } |
48 | |
49 | BOOST_AUTO_TEST_CASE(test_int2) |
50 | { |
51 | typedef ::boost::mpl::vector<common<>, addable<int, _self, _self> > test_concept; |
52 | any<test_concept> x(1); |
53 | any<test_concept> z(2 + x); |
54 | int i = any_cast<int>(arg&: z); |
55 | BOOST_CHECK_EQUAL(i, 3); |
56 | } |
57 | |
58 | BOOST_AUTO_TEST_CASE(test_mixed) |
59 | { |
60 | typedef ::boost::mpl::vector<common<_a>, common<_b>, addable<_a, _b> > test_concept; |
61 | tuple<test_concept, _a, _b> x(1.0, 2); |
62 | any<test_concept, _a> z(get<0>(t&: x) + get<1>(t&: x)); |
63 | double d = any_cast<double>(arg&: z); |
64 | BOOST_CHECK_EQUAL(d, 3); |
65 | } |
66 | |
67 | BOOST_AUTO_TEST_CASE(test_overload) |
68 | { |
69 | typedef ::boost::mpl::vector< |
70 | common<_a>, |
71 | common<_b>, |
72 | addable<_a>, |
73 | addable<_a, int>, |
74 | addable<int, _a, _a>, |
75 | addable<_b>, |
76 | addable<_b, int>, |
77 | addable<int, _b, _b>, |
78 | addable<_a, _b> |
79 | > test_concept; |
80 | tuple<test_concept, _a, _b> t(1.0, 2); |
81 | any<test_concept, _a> x(get<0>(t)); |
82 | any<test_concept, _b> y(get<1>(t)); |
83 | |
84 | { |
85 | any<test_concept, _a> z(x + x); |
86 | BOOST_CHECK_EQUAL(any_cast<double>(z), 2.0); |
87 | } |
88 | |
89 | { |
90 | any<test_concept, _a> z(x + 3); |
91 | BOOST_CHECK_EQUAL(any_cast<double>(z), 4.0); |
92 | } |
93 | |
94 | { |
95 | any<test_concept, _a> z(3 + x); |
96 | BOOST_CHECK_EQUAL(any_cast<double>(z), 4.0); |
97 | } |
98 | |
99 | { |
100 | any<test_concept, _b> z(y + y); |
101 | BOOST_CHECK_EQUAL(any_cast<int>(z), 4); |
102 | } |
103 | |
104 | { |
105 | any<test_concept, _b> z(y + 3); |
106 | BOOST_CHECK_EQUAL(any_cast<int>(z), 5); |
107 | } |
108 | |
109 | { |
110 | any<test_concept, _b> z(3 + y); |
111 | BOOST_CHECK_EQUAL(any_cast<int>(z), 5); |
112 | } |
113 | |
114 | { |
115 | any<test_concept, _a> z(x + y); |
116 | BOOST_CHECK_EQUAL(any_cast<double>(z), 3); |
117 | } |
118 | } |
119 | |