]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/variant/test/test8.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / variant / test / test8.cpp
CommitLineData
7c673cae
FG
1//-----------------------------------------------------------------------------
2// boost-libs variant/test/test8.cpp header 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/test/minimal.hpp"
14#include "boost/variant.hpp"
15
16#include <iostream>
17#include <vector>
18#include <string>
19
20using namespace std;
21using namespace boost;
22
23typedef variant<float, std::string, int, std::vector<std::string> > t_var1;
24
25struct int_sum : static_visitor<>
26{
27 int_sum() : result_(0) { }
28
29 void operator()(int t)
30 {
31 result_ += t;
32 }
33
34 result_type operator()(float ) { }
35 result_type operator()(const std::string& ) { }
36 result_type operator()(const std::vector<std::string>& ) { }
37
38 int result_;
39};
40
41template <typename T, typename Variant>
42T& check_pass(Variant& v, T value)
43{
44 BOOST_CHECK(get<T>(&v));
45
46 try
47 {
48 T& r = get<T>(v);
49 BOOST_CHECK(r == value);
50 return r;
51 }
52 catch(boost::bad_get&)
53 {
54 throw; // must never reach
55 }
56}
57
58template <typename T, typename Variant>
59void check_fail(Variant& v)
60{
61 BOOST_CHECK(!relaxed_get<T>(&v));
62
63 try
64 {
65 T& r = relaxed_get<T>(v);
66 (void)r; // suppress warning about r not being used
67 BOOST_CHECK(false && relaxed_get<T>(&v)); // should never reach
68 }
69 catch(const boost::bad_get& e)
70 {
71 BOOST_CHECK(!!e.what()); // make sure that what() is const qualified and returnes something
72 }
73}
74
75int test_main(int , char* [])
76{
77 int_sum acc;
78 t_var1 v1 = 800;
79
80 // check get on non-const variant
81 {
82 int& r1 = check_pass<int>(v1, 800);
83 const int& cr1 = check_pass<const int>(v1, 800);
84
85 check_fail<float>(v1);
86 check_fail<const float>(v1);
87 check_fail<short>(v1);
88 check_fail<const short>(v1);
89
90 apply_visitor(acc, v1);
91 BOOST_CHECK(acc.result_ == 800);
92
93 r1 = 920; // NOTE: modifies content of v1
94 apply_visitor(acc, v1);
95 BOOST_CHECK(cr1 == 920);
96 BOOST_CHECK(acc.result_ == 800 + 920);
97 }
98
99 // check const correctness:
100 {
101 const t_var1& c = v1;
102
103 check_pass<const int>(c, 920);
104
105 //check_fail<int>(c);
106 check_fail<const float>(c);
107 //check_fail<float>(c);
108 check_fail<const short>(c);
109 //check_fail<short>(c);
110 }
111
112 return boost::exit_success;
113}
114