File: any_to_ref_test.cpp

package info (click to toggle)
boost 1.33.1-10
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 100,948 kB
  • ctags: 145,103
  • sloc: cpp: 573,492; xml: 49,055; python: 15,626; ansic: 13,588; sh: 2,099; yacc: 858; makefile: 660; perl: 427; lex: 111; csh: 6
file content (57 lines) | stat: -rw-r--r-- 1,478 bytes parent folder | download | duplicates (2)
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
// Copyright 2005 Alexander Nasonov.
//
// 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/any.hpp>
#include <boost/test/test_tools.hpp>

using namespace boost;

void test_cast_to_reference()
{
    int i = 7;
    any a(i), b(a);
    const any c(i);
    
    int&                ra1 = any_cast<int&               >(a);
    int const&          ra2 = any_cast<int const&         >(a);
    int const volatile& ra3 = any_cast<int const volatile&>(a);
    
    BOOST_CHECK(&ra1 == &ra2 && &ra2 == &ra3);

    int&                rb1 = any_cast<int&               >(b);
    int const&          rb2 = any_cast<int const&         >(b);
    int const volatile& rb3 = any_cast<int const volatile&>(b);
    
    BOOST_CHECK(&rb1 == &rb2 && &rb2 == &rb3);
    
    int const&          rc1 = any_cast<int const&         >(c);
    int const volatile& rc2 = any_cast<int const volatile&>(c);
    
    BOOST_CHECK(&rc1 == &rc2);
    
    BOOST_CHECK(&ra1 != &rb1 && &rb1 != &rc1 && &rc1 != &ra1);
    
    BOOST_CHECK(&ra1 != &i && &rb1 != &i && &rc1 != &i);
    
    ++ra1;
    int v = any_cast<int>(a);
    BOOST_CHECK(v == i + 1);
    
    --rb1;
    int v2 = any_cast<int>(b);
    BOOST_CHECK(v2 == i - 1);
    
    BOOST_CHECK_THROW(
        any_cast<char&>(a),
        bad_any_cast);
}

int test_main(int, char* av[])
{
    test_cast_to_reference();
    return 0;
}