Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / variant / test / variant_multivisit_test.cpp
1 //-----------------------------------------------------------------------------
2 // boost-libs variant/test/variant_multivisit_test.cpp source file
3 // See http://www.boost.org for updates, documentation, and revision history.
4 //-----------------------------------------------------------------------------
5 //
6 // Copyright (c) 2013
7 // Antony Polukhin
8 //
9 // Distributed under the Boost Software License, Version 1.0. (See
10 // accompanying file LICENSE_1_0.txt or copy at
11 // http://www.boost.org/LICENSE_1_0.txt)
12
13 #include "boost/config.hpp"
14 #define BOOST_VARAINT_MAX_MULTIVIZITOR_PARAMS 5
15 #include "boost/variant/multivisitors.hpp"
16
17 #include "boost/test/minimal.hpp"
18
19 typedef boost::variant<char, unsigned char, signed char, unsigned short, int, unsigned int>         variant6_t;
20
21 struct test_visitor: boost::static_visitor<> {
22     // operators that shall not be called
23     template <class  T1, class  T2, class  T3>
24     void operator()(T1, T2, T3) const 
25     {
26         BOOST_CHECK(false);
27     }
28
29     template <class  T1, class  T2, class  T3, class  T4>
30     void operator()(T1, T2, T3, T4) const 
31     {
32         BOOST_CHECK(false);
33     }
34
35     template <class  T1, class  T2, class  T3, class  T4, class  T5>
36     void operator()(T1, T2, T3, T4, T5) const 
37     {
38         BOOST_CHECK(false);
39     }
40
41     // operators that are OK to call
42     void operator()(char v0, unsigned char v1, signed char v2) const 
43     {
44         BOOST_CHECK(v0 == 0);
45         BOOST_CHECK(v1 == 1);
46         BOOST_CHECK(v2 == 2);
47     }
48
49     void operator()(char v0, unsigned char v1, signed char v2, unsigned short v3) const 
50     {
51         BOOST_CHECK(v0 == 0);
52         BOOST_CHECK(v1 == 1);
53         BOOST_CHECK(v2 == 2);
54         BOOST_CHECK(v3 == 3);
55     }
56
57     void operator()(char v0, unsigned char v1, signed char v2, unsigned short v3, int v4) const 
58     {
59         BOOST_CHECK(v0 == 0);
60         BOOST_CHECK(v1 == 1);
61         BOOST_CHECK(v2 == 2);
62         BOOST_CHECK(v3 == 3);
63         BOOST_CHECK(v4 == 4);
64     }
65 };
66
67 typedef boost::variant<int, double, bool> bool_like_t;
68 typedef boost::variant<int, double> arithmetics_t;
69
70 struct if_visitor: public boost::static_visitor<arithmetics_t> {
71     template <class T1, class T2>
72     arithmetics_t operator()(bool b, T1 v1, T2 v2) const {
73         if (b) {
74             return v1;
75         } else {
76             return v2;
77         }
78     }
79 };
80
81
82 int test_main(int , char* [])
83 {
84     test_visitor v;
85
86     variant6_t v_array6[6];
87     v_array6[0] = char(0);
88     v_array6[1] = static_cast<unsigned char>(1);
89     v_array6[2] = static_cast<signed char>(2);
90     v_array6[3] = static_cast<unsigned short>(3);
91     v_array6[4] = static_cast<int>(4);
92     v_array6[5] = static_cast<unsigned int>(5);
93
94     boost::apply_visitor(v, v_array6[0], v_array6[1], v_array6[2]);
95     boost::apply_visitor(test_visitor(), v_array6[0], v_array6[1], v_array6[2]);
96
97 // Following test also pass, but requires many Gigabytes of RAM for compilation and compile for about 15 minutes
98 //#define BOOST_VARIANT_MULTIVISITORS_TEST_VERY_EXTREME
99 #ifdef BOOST_VARIANT_MULTIVISITORS_TEST_VERY_EXTREME    
100     boost::apply_visitor(v, v_array6[0], v_array6[1], v_array6[2], v_array6[3]);
101     boost::apply_visitor(test_visitor(), v_array6[0], v_array6[1], v_array6[2], v_array6[3]);
102
103     boost::apply_visitor(v, v_array6[0], v_array6[1], v_array6[2], v_array6[3], v_array6[4]);
104     boost::apply_visitor(test_visitor(), v_array6[0], v_array6[1], v_array6[2], v_array6[3], v_array6[4]);
105 #endif
106
107     bool_like_t v0(1), v1(true), v2(1.0);
108
109     BOOST_CHECK(
110         boost::apply_visitor(if_visitor(), v0, v1, v2)
111         ==
112         arithmetics_t(true)
113     );
114
115     /* Delayed multi visitation is not implemented
116     if_visitor if_vis;
117     BOOST_CHECK(
118         boost::apply_visitor(if_vis)(v0, v1, v2)
119         ==
120         arithmetics_t(true)
121     );
122     */
123     return boost::exit_success;
124 }