summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/variant/test/variant_reference_test.cpp
blob: 3f7f5812596e2f5550fdeab3156564484faf5c4f (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
//-----------------------------------------------------------------------------
// boost-libs variant/test/variant_reference_test.cpp source file
// See http://www.boost.org for updates, documentation, and revision history.
//-----------------------------------------------------------------------------
//
// Copyright (c) 2003
// Eric Friedman, Itay Maman
//
// 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)

#include "boost/variant.hpp"
#include "boost/core/lightweight_test.hpp"

#include "boost/mpl/bool.hpp"
#include "boost/type_traits/add_reference.hpp"
#include "boost/type_traits/is_pointer.hpp"

/////
// support types and functions

struct base_t { };
struct derived_t : base_t { };

template <typename Base, typename Derived>
bool check_base_derived(Base* b, Derived* d, long)
{
    return b == d;
}

template <typename Base, typename Derived>
bool check_base_derived(Base& b, Derived& d, int)
{
    return &b == &d;
}

template <typename T>
    typename boost::add_reference<T>::type
wknd_get(boost::variant<T&>& var, long)
{
    return boost::get<T>(var);
}

template <typename T>
    typename boost::add_reference<T>::type
wknd_get(boost::variant<T>& var, int)
{
    return boost::get<T>(var);
}

/////
// test functions

template <typename T>
void test_reference_content(T& t, const T& value1, const T& value2)
{
    BOOST_TEST( !(value1 == value2) );

    /////

    boost::variant< T& > var(t);
    BOOST_TEST(( boost::get<T>(&var) == &t ));

    t = value1;
    BOOST_TEST(( boost::get<T>(var) == value1 ));

    /////

    boost::variant< T > var2(var);
    BOOST_TEST(( boost::get<T>(var2) == value1 ));

    t = value2;
    BOOST_TEST(( boost::get<T>(var2) == value1 ));
}

template <typename Base, typename Derived>
void base_derived_test(Derived d)
{
    Base b(d);
    BOOST_TEST((check_base_derived(
          b
        , d
        , 1L
        )));

    boost::variant<Base> base_var(d);
    BOOST_TEST((check_base_derived(
          wknd_get(base_var, 1L)
        , d
        , 1L
        )));

    boost::variant<Derived> derived_var(d);
    boost::variant<Base> base_from_derived_var(derived_var);
    BOOST_TEST((check_base_derived(
          wknd_get(base_from_derived_var, 1L)
        , wknd_get(derived_var, 1L)
        , 1L
        )));
}

int main()
{
    int i = 0;
    test_reference_content(i, 1, 2);

    /////

    derived_t d;
    base_derived_test< int&,int >(i);
    base_derived_test< base_t*,derived_t* >(&d);
    base_derived_test< base_t&,derived_t& >(d);

    return boost::report_errors();
}