summaryrefslogtreecommitdiff
path: root/libs/variant/test/recursive_variant_test.cpp
blob: fc9eaf2a5d92d49d01a976b40b6fdcdded5cf0ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
//-----------------------------------------------------------------------------
// boost-libs variant/test/recursive_variant_test.cpp source file
// See http://www.boost.org for updates, documentation, and revision history.
//-----------------------------------------------------------------------------
//
// Copyright (c) 2003 Eric Friedman, Itay Maman
// Copyright (c) 2013 Antony Polukhin
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)


// This file is used in two test cases:
//
// 1) recursive_variant_test.cpp that tests recursive usage of variant
//
// 2) variant_noexcept_test that tests Boost.Variant ability to compile 
// and work with disabled exceptions

#ifdef BOOST_NO_EXCEPTIONS
// `boost/test/minimal.hpp` cannot work with exceptions disabled,
// so we need the following workarounds for that case:
namespace boost {
    int exit_success = 0;
}

int test_main(int , char* []);

int main( int argc, char* argv[] )
{
    return test_main(argc, argv);
}

#include <stdlib.h>
#define BOOST_CHECK(exp) if (!(exp)) exit(EXIT_FAILURE)

#else // BOOST_NO_EXCEPTIONS
#   include "boost/test/minimal.hpp"
#endif // BOOST_NO_EXCEPTIONS


#include "boost/variant.hpp"
#include "boost/mpl/vector.hpp"
#include "boost/mpl/copy.hpp"

#include <iostream>
#include <sstream>
#include <vector>
#include <map>
#if !defined(BOOST_VARIANT_DO_NOT_USE_VARIADIC_TEMPLATES) && !defined(BOOST_NO_CXX11_HDR_TUPLE)
#include <tuple>
#endif // !defined(BOOST_VARIANT_DO_NOT_USE_VARIADIC_TEMPLATES) && !defined(BOOST_NO_CXX11_HDR_TUPLE)

struct printer
    : boost::static_visitor<std::string>
{
    template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
    std::string operator()(
            const boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> &var) const
    {
        return boost::apply_visitor( printer(), var );
    }

    template <typename T>
    std::string operator()(const std::vector<T>& vec) const
    {
        std::ostringstream ost;

        ost << "( ";

        typename std::vector<T>::const_iterator it = vec.begin();
        for (; it != vec.end(); ++it)
            ost << printer()( *it );

        ost << ") ";

        return ost.str();
    }

#if !defined(BOOST_VARIANT_DO_NOT_USE_VARIADIC_TEMPLATES) && !defined(BOOST_NO_CXX11_HDR_TUPLE)
    template <int...> struct indices {};
    template <typename... Ts, int... Is>
    std::string operator()(const std::tuple<Ts...>& tup, indices<Is...>)
    {
        std::ostringstream ost;
        ost << "( ";
        (void) (int []){0, (ost << printer()( std::get<Is>(tup) ), 0)... };
        ost << ") ";
        return ost.str();
    }

    template <int N, int... Is>
        struct make_indices : make_indices<N-1, N-1, Is...> {};
    template <int... Is>
        struct make_indices<0, Is...> : indices<Is...> {};
    template <typename... Ts>
    std::string operator()(const std::tuple<Ts...>& tup) const
    {
        return printer()(tup, make_indices<sizeof...(Ts)>());
    }
#endif // !defined(BOOST_VARIANT_DO_NOT_USE_VARIADIC_TEMPLATES) && !defined(BOOST_NO_CXX11_HDR_TUPLE)

    template <typename T>
    std::string operator()(const T& operand) const
    {
        std::ostringstream ost;
        ost << operand << ' ';
        return ost.str();
    }
};

void test_recursive_variant()
{
    typedef boost::make_recursive_variant<
          int
        , std::vector<boost::recursive_variant_>
        >::type var1_t;

    std::vector<var1_t> vec1;
    vec1.push_back(3);
    vec1.push_back(5);
    vec1.push_back(vec1);
    vec1.push_back(7);

    var1_t var1(vec1);
    std::string result1( printer()(var1) );

    std::cout << "result1: " << result1 << '\n';
    BOOST_CHECK(result1 == "( 3 5 ( 3 5 ) 7 ) ");

    std::vector<var1_t> vec1_copy = vec1;
    vec1_copy.erase(vec1_copy.begin() + 2);
    vec1_copy.insert(vec1_copy.begin() + 2, vec1_copy);
    var1 = vec1_copy;
    result1 = printer()(var1);
    std::cout << "result1+: " << result1 << '\n';
    BOOST_CHECK(result1 == "( 3 5 ( 3 5 7 ) 7 ) ");

    // Uses move construction on compilers with rvalue references support
    result1 = printer()(
        var1_t(
            std::vector<var1_t>(vec1_copy)
        )
    );
    std::cout << "result1++: " << result1 << '\n';
    BOOST_CHECK(result1 == "( 3 5 ( 3 5 7 ) 7 ) ");


    var1_t vec1_another_copy(vec1_copy);
    vec1_copy[2].swap(vec1_another_copy);
    result1 = printer()(
        var1_t(vec1_copy)
    );
    std::cout << "result1+++1: " << result1 << '\n';
    BOOST_CHECK(result1 == "( 3 5 ( 3 5 ( 3 5 7 ) 7 ) 7 ) ");

    result1 = printer()(vec1_another_copy);
    std::cout << "result1++2: " << result1 << '\n';
    BOOST_CHECK(result1 == "( 3 5 7 ) ");

    vec1_copy[2].swap(vec1_copy[2]);
    result1 = printer()(
        var1_t(vec1_copy)
    );
    std::cout << "result1.2: " << result1 << '\n';
    BOOST_CHECK(result1 == "( 3 5 ( 3 5 ( 3 5 7 ) 7 ) 7 ) ");

    typedef boost::make_recursive_variant<
          boost::variant<int, double>
        , std::vector<boost::recursive_variant_>
        >::type var2_t;

    std::vector<var2_t> vec2;
    vec2.push_back(boost::variant<int, double>(3));
    vec2.push_back(boost::variant<int, double>(3.5));
    vec2.push_back(vec2);
    vec2.push_back(boost::variant<int, double>(7));

    var2_t var2(vec2);
    std::string result2( printer()(var2) );

    std::cout << "result2: " << result2 << '\n';
    BOOST_CHECK(result2 == "( 3 3.5 ( 3 3.5 ) 7 ) ");
    
    typedef boost::make_recursive_variant<
          int
        , std::vector<
              boost::variant<
                    double
                  , std::vector<boost::recursive_variant_>
                  >
              >
        >::type var3_t;

    typedef boost::variant<double, std::vector<var3_t> > var4_t;

    std::vector<var3_t> vec3;
    vec3.push_back(3);
    vec3.push_back(5);
    std::vector<var4_t> vec4;
    vec4.push_back(3.5);
    vec4.push_back(vec3);
    vec3.push_back(vec4);
    vec3.push_back(7);

    var4_t var4(vec3);
    std::string result3( printer()(var4) );

    std::cout << "result2: " << result3 << '\n';
    BOOST_CHECK(result3 == "( 3 5 ( 3.5 ( 3 5 ) ) 7 ) ");

    typedef boost::make_recursive_variant<
          double,
          std::vector<var1_t>
        >::type var5_t;

    std::vector<var5_t> vec5;
    vec5.push_back(3.5);
    vec5.push_back(vec1);
    vec5.push_back(17.25);

    std::string result5( printer()(vec5) );

    std::cout << "result5: " << result5 << '\n';
    BOOST_CHECK(result5 == "( 3.5 ( 3 5 ( 3 5 ) 7 ) 17.25 ) ");

    typedef boost::make_recursive_variant<
          int,
          std::map<int, boost::recursive_variant_>
        >::type var6_t;
    var6_t var6;

#if !defined(BOOST_VARIANT_DO_NOT_USE_VARIADIC_TEMPLATES) && !defined(BOOST_NO_CXX11_HDR_TUPLE)
    typedef boost::make_recursive_variant<
          int,
          std::tuple<int, boost::recursive_variant_>
        >::type var7_t;
    var7_t var7 = 0;
    var7 = std::tuple<int, var7_t>(1, var7);
    var7 = std::tuple<int, var7_t>(2, var7);

    std::string result7( printer()(var7) );

    std::cout << "result7: " << result7 << '\n';
    BOOST_CHECK(result7 == "( 2 ( 1 0 ) ) ");
#endif // !defined(BOOST_VARIANT_DO_NOT_USE_VARIADIC_TEMPLATES) && !defined(BOOST_NO_CXX11_HDR_TUPLE)
}

void test_recursive_variant_over()
{
    typedef boost::make_recursive_variant_over<
          boost::mpl::vector<
              int
            , std::vector<boost::recursive_variant_>
          >
        >::type var1_t;

    std::vector<var1_t> vec1;
    vec1.push_back(3);
    vec1.push_back(5);
    vec1.push_back(vec1);
    vec1.push_back(7);

    var1_t var1(vec1);
    std::string result1( printer()(var1) );

    std::cout << "result1: " << result1 << '\n';
    BOOST_CHECK(result1 == "( 3 5 ( 3 5 ) 7 ) ");

    std::vector<var1_t> vec1_copy = vec1;
    vec1_copy.erase(vec1_copy.begin() + 2);
    vec1_copy.insert(vec1_copy.begin() + 2, vec1_copy);
    var1 = vec1_copy;
    result1 = printer()(var1);
    std::cout << "result1+: " << result1 << '\n';
    BOOST_CHECK(result1 == "( 3 5 ( 3 5 7 ) 7 ) ");

    // Uses move construction on compilers with rvalue references support
    result1 = printer()(
        var1_t(
            std::vector<var1_t>(vec1_copy)
        )
    );
    std::cout << "result1++: " << result1 << '\n';
    BOOST_CHECK(result1 == "( 3 5 ( 3 5 7 ) 7 ) ");


    var1_t vec1_another_copy(vec1_copy);
    vec1_copy[2].swap(vec1_another_copy);
    result1 = printer()(
        var1_t(vec1_copy)
    );
    std::cout << "result1+++1: " << result1 << '\n';
    BOOST_CHECK(result1 == "( 3 5 ( 3 5 ( 3 5 7 ) 7 ) 7 ) ");

    result1 = printer()(vec1_another_copy);
    std::cout << "result1++2: " << result1 << '\n';
    BOOST_CHECK(result1 == "( 3 5 7 ) ");

    typedef boost::make_recursive_variant_over<
          boost::mpl::vector<
              boost::make_variant_over<boost::mpl::vector<int, double> >::type
            , std::vector<boost::recursive_variant_>
            >
        >::type var2_t;

    std::vector<var2_t> vec2;
    vec2.push_back(boost::variant<int, double>(3));
    vec2.push_back(boost::variant<int, double>(3.5));
    vec2.push_back(vec2);
    vec2.push_back(boost::variant<int, double>(7));

    var2_t var2(vec2);
    std::string result2( printer()(var2) );

    std::cout << "result2: " << result2 << '\n';
    BOOST_CHECK(result2 == "( 3 3.5 ( 3 3.5 ) 7 ) ");
    
    typedef boost::make_recursive_variant_over<
          boost::mpl::vector<
              int
            , std::vector<
                  boost::make_variant_over<
                      boost::mpl::vector<
                          double
                        , std::vector<boost::recursive_variant_>
                        >
                    >::type
                >
            >
        >::type var3_t;

    typedef boost::make_variant_over<
          boost::mpl::copy<
              boost::mpl::vector<
                  double
                , std::vector<var3_t>
                >
            >::type
        >::type var4_t;

    std::vector<var3_t> vec3;
    vec3.push_back(3);
    vec3.push_back(5);
    std::vector<var4_t> vec4;
    vec4.push_back(3.5);
    vec4.push_back(vec3);
    vec3.push_back(vec4);
    vec3.push_back(7);

    var4_t var3(vec3);
    std::string result3( printer()(var3) );

    std::cout << "result2: " << result3 << '\n';
    BOOST_CHECK(result3 == "( 3 5 ( 3.5 ( 3 5 ) ) 7 ) ");
    
    typedef boost::make_recursive_variant_over<
          boost::mpl::vector<
              double
            , std::vector<var1_t>
            >
        >::type var5_t;

    std::vector<var5_t> vec5;
    vec5.push_back(3.5);
    vec5.push_back(vec1);
    vec5.push_back(17.25);

    std::string result5( printer()(vec5) );

    std::cout << "result5: " << result5 << '\n';
    BOOST_CHECK(result5 == "( 3.5 ( 3 5 ( 3 5 ) 7 ) 17.25 ) ");
}

int test_main(int , char* [])
{
    test_recursive_variant();
    test_recursive_variant_over();

    return boost::exit_success;
}