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
21using namespace boost::type_erasure;
22
23template<class T = _self>
24struct common : ::boost::mpl::vector<
25 destructible<T>,
26 copy_constructible<T>,
27 typeid_<T>
28> {};
29
30BOOST_AUTO_TEST_CASE(test_same)
31{
32 typedef ::boost::mpl::vector<common<>, add_assignable<> > test_concept;
33 any<test_concept> x(1);
34 any<test_concept> y(2);
35 any<test_concept>& z(x += y);
36 BOOST_CHECK_EQUAL(any_cast<int>(x), 3);
37 BOOST_CHECK_EQUAL(&x, &z);
38}
39
40BOOST_AUTO_TEST_CASE(test_int1)
41{
42 typedef ::boost::mpl::vector<common<>, add_assignable<_self, int> > test_concept;
43 any<test_concept> x(1);
44 any<test_concept>& z(x += 2);
45 BOOST_CHECK_EQUAL(any_cast<int>(x), 3);
46 BOOST_CHECK_EQUAL(&x, &z);
47}
48
49BOOST_AUTO_TEST_CASE(test_int2)
50{
51 typedef ::boost::mpl::vector<common<>, add_assignable<int, _self> > test_concept;
52 int x = 1;
53 any<test_concept> y(2);
54 int& z(x += y);
55 BOOST_CHECK_EQUAL(x, 3);
56 BOOST_CHECK_EQUAL(&x, &z);
57}
58
59BOOST_AUTO_TEST_CASE(test_mixed)
60{
61 typedef ::boost::mpl::vector<common<_a>, common<_b>, add_assignable<_a, _b> > test_concept;
62 tuple<test_concept, _a, _b> t(1.0, 2);
63 any<test_concept, _a> x(get<0>(t));
64 any<test_concept, _b> y(get<1>(t));
65 any<test_concept, _a>& z(x += y);
66 BOOST_CHECK_EQUAL(any_cast<double>(x), 3.0);
67 BOOST_CHECK_EQUAL(&x, &z);
68}
69
70BOOST_AUTO_TEST_CASE(test_overload)
71{
72 typedef ::boost::mpl::vector<
73 common<_a>,
74 common<_b>,
75 add_assignable<_a>,
76 add_assignable<_a, int>,
77 add_assignable<double, _a>,
78 add_assignable<_b>,
79 add_assignable<_b, int>,
80 add_assignable<double, _b>,
81 add_assignable<_a, _b>
82 > test_concept;
83 tuple<test_concept, _a, _b> t(1.0, 2);
84
85 {
86 any<test_concept, _a> x(get<0>(t));
87 any<test_concept, _a> y(get<0>(t));
88 any<test_concept, _a>& z(x += y);
89 BOOST_CHECK_EQUAL(any_cast<double>(x), 2.0);
90 BOOST_CHECK_EQUAL(&x, &z);
91 }
92
93 {
94 any<test_concept, _a> x(get<0>(t));
95 int y = 5;
96 any<test_concept, _a>& z(x += y);
97 BOOST_CHECK_EQUAL(any_cast<double>(x), 6.0);
98 BOOST_CHECK_EQUAL(&x, &z);
99 }
100
101 {
102 double x = 11;
103 any<test_concept, _a> y(get<0>(t));
104 double& z(x += y);
105 BOOST_CHECK_EQUAL(x, 12);
106 BOOST_CHECK_EQUAL(&x, &z);
107 }
108
109 {
110 any<test_concept, _b> x(get<1>(t));
111 any<test_concept, _b> y(get<1>(t));
112 any<test_concept, _b>& z(x += y);
113 BOOST_CHECK_EQUAL(any_cast<int>(x), 4);
114 BOOST_CHECK_EQUAL(&x, &z);
115 }
116
117 {
118 any<test_concept, _b> x(get<1>(t));
119 int y = 5;
120 any<test_concept, _b>& z(x += y);
121 BOOST_CHECK_EQUAL(any_cast<int>(x), 7);
122 BOOST_CHECK_EQUAL(&x, &z);
123 }
124
125 {
126 double x = 11;
127 any<test_concept, _b> y(get<1>(t));
128 double& z(x += y);
129 BOOST_CHECK_EQUAL(x, 13);
130 BOOST_CHECK_EQUAL(&x, &z);
131 }
132
133 {
134 any<test_concept, _a> x(get<0>(t));
135 any<test_concept, _b> y(get<1>(t));
136 any<test_concept, _a>& z(x += y);
137 BOOST_CHECK_EQUAL(any_cast<double>(x), 3.0);
138 BOOST_CHECK_EQUAL(&x, &z);
139 }
140}
141

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