Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/variant/test/variant_reference_test.cpp @ 29

Last change on this file since 29 was 29, checked in by landauf, 17 years ago

updated boost from 1_33_1 to 1_34_1

File size: 2.7 KB
Line 
1//-----------------------------------------------------------------------------
2// boost-libs variant/test/variant_reference_test.cpp source file
3// See http://www.boost.org for updates, documentation, and revision history.
4//-----------------------------------------------------------------------------
5//
6// Copyright (c) 2003
7// Eric Friedman, Itay Maman
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/variant.hpp"
14#include "boost/test/minimal.hpp"
15
16#include "boost/mpl/bool.hpp"
17#include "boost/type_traits/add_reference.hpp"
18#include "boost/type_traits/is_pointer.hpp"
19
20/////
21// support types and functions
22
23struct base_t { };
24struct derived_t : base_t { };
25
26template <typename Base, typename Derived>
27bool check_base_derived(Base* b, Derived* d, long)
28{
29    return b == d;
30}
31
32template <typename Base, typename Derived>
33bool check_base_derived(Base& b, Derived& d, int)
34{
35    return &b == &d;
36}
37
38template <typename T>
39    typename boost::add_reference<T>::type
40wknd_get(boost::variant<T&>& var, long)
41{
42    return boost::get<T>(var);
43}
44
45template <typename T>
46    typename boost::add_reference<T>::type
47wknd_get(boost::variant<T>& var, int)
48{
49    return boost::get<T>(var);
50}
51
52/////
53// test functions
54
55template <typename T>
56void test_reference_content(T& t, const T& value1, const T& value2)
57{
58    BOOST_CHECK( !(value1 == value2) );
59
60    /////
61
62    boost::variant< T& > var(t);
63    BOOST_CHECK(( boost::get<T>(&var) == &t ));
64
65    t = value1;
66    BOOST_CHECK(( boost::get<T>(var) == value1 ));
67
68    /////
69
70    boost::variant< T > var2(var);
71    BOOST_CHECK(( boost::get<T>(var2) == value1 ));
72
73    t = value2;
74    BOOST_CHECK(( boost::get<T>(var2) == value1 ));
75}
76
77template <typename Base, typename Derived>
78void base_derived_test(Derived d)
79{
80    typedef typename boost::is_pointer<Base>::type is_ptr;
81
82    Base b(d);
83    BOOST_CHECK((check_base_derived(
84          b
85        , d
86        , 1L
87        )));
88
89    boost::variant<Base> base_var(d);
90    BOOST_CHECK((check_base_derived(
91          wknd_get(base_var, 1L)
92        , d
93        , 1L
94        )));
95
96    boost::variant<Derived> derived_var(d);
97    boost::variant<Base> base_from_derived_var(derived_var);
98    BOOST_CHECK((check_base_derived(
99          wknd_get(base_from_derived_var, 1L)
100        , wknd_get(derived_var, 1L)
101        , 1L
102        )));
103}
104
105int test_main(int , char* [])
106{
107    int i = 0;
108    test_reference_content(i, 1, 2);
109
110    /////
111
112    derived_t d;
113    base_derived_test< int&,int >(i);
114    base_derived_test< base_t*,derived_t* >(&d);
115    base_derived_test< base_t&,derived_t& >(d);
116
117    return boost::exit_success;
118}
Note: See TracBrowser for help on using the repository browser.