diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 18:24:20 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 18:24:20 +0000 |
commit | 483eb2f56657e8e7f419ab1a4fab8dce9ade8609 (patch) | |
tree | e5d88d25d870d5dedacb6bbdbe2a966086a0a5cf /src/boost/libs/variant/test/test8.cpp | |
parent | Initial commit. (diff) | |
download | ceph-483eb2f56657e8e7f419ab1a4fab8dce9ade8609.tar.xz ceph-483eb2f56657e8e7f419ab1a4fab8dce9ade8609.zip |
Adding upstream version 14.2.21.upstream/14.2.21upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/boost/libs/variant/test/test8.cpp')
-rw-r--r-- | src/boost/libs/variant/test/test8.cpp | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/src/boost/libs/variant/test/test8.cpp b/src/boost/libs/variant/test/test8.cpp new file mode 100644 index 00000000..94ef65ab --- /dev/null +++ b/src/boost/libs/variant/test/test8.cpp @@ -0,0 +1,113 @@ +//----------------------------------------------------------------------------- +// boost-libs variant/test/test8.cpp header 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/core/lightweight_test.hpp" +#include "boost/variant.hpp" + +#include <iostream> +#include <vector> +#include <string> + +using namespace boost; + +typedef variant<float, std::string, int, std::vector<std::string> > t_var1; + +struct int_sum : static_visitor<> +{ + int_sum() : result_(0) { } + + void operator()(int t) + { + result_ += t; + } + + result_type operator()(float ) { } + result_type operator()(const std::string& ) { } + result_type operator()(const std::vector<std::string>& ) { } + + int result_; +}; + +template <typename T, typename Variant> +T& check_pass(Variant& v, T value) +{ + BOOST_TEST(get<T>(&v)); + + try + { + T& r = get<T>(v); + BOOST_TEST(r == value); + return r; + } + catch(boost::bad_get&) + { + throw; // must never reach + } +} + +template <typename T, typename Variant> +void check_fail(Variant& v) +{ + BOOST_TEST(!relaxed_get<T>(&v)); + + try + { + T& r = relaxed_get<T>(v); + (void)r; // suppress warning about r not being used + BOOST_TEST(false && relaxed_get<T>(&v)); // should never reach + } + catch(const boost::bad_get& e) + { + BOOST_TEST(!!e.what()); // make sure that what() is const qualified and returnes something + } +} + +int main() +{ + int_sum acc; + t_var1 v1 = 800; + + // check get on non-const variant + { + int& r1 = check_pass<int>(v1, 800); + const int& cr1 = check_pass<const int>(v1, 800); + + check_fail<float>(v1); + check_fail<const float>(v1); + check_fail<short>(v1); + check_fail<const short>(v1); + + apply_visitor(acc, v1); + BOOST_TEST(acc.result_ == 800); + + r1 = 920; // NOTE: modifies content of v1 + apply_visitor(acc, v1); + BOOST_TEST(cr1 == 920); + BOOST_TEST(acc.result_ == 800 + 920); + } + + // check const correctness: + { + const t_var1& c = v1; + + check_pass<const int>(c, 920); + + //check_fail<int>(c); + check_fail<const float>(c); + //check_fail<float>(c); + check_fail<const short>(c); + //check_fail<short>(c); + } + + return boost::report_errors(); +} + |