variant_over_joint_view_test.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright (c) 2017
  2. // Mikhail Maximov
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. // The test is base on https://svn.boost.org/trac/boost/ticket/8554
  8. // variant was not able to extract types from mpl::joint_view
  9. #include <string>
  10. #include "boost/config.hpp"
  11. #include "boost/core/lightweight_test.hpp"
  12. #include "boost/variant.hpp"
  13. #include "boost/mpl/joint_view.hpp"
  14. #include "boost/mpl/insert_range.hpp"
  15. #include "boost/mpl/set.hpp"
  16. template<class T, class Variant>
  17. void check_exception_on_get(Variant& v) {
  18. try {
  19. boost::get<T>(v);
  20. BOOST_ERROR("Expected exception boost::bad_get, but got nothing.");
  21. } catch (boost::bad_get&) { //okay it is expected behaviour
  22. } catch (...) { BOOST_ERROR("Expected exception boost::bad_get, but got something else."); }
  23. }
  24. void test_joint_view() {
  25. typedef boost::variant<int> v1;
  26. typedef boost::variant<std::string> v2;
  27. typedef boost::make_variant_over<boost::mpl::joint_view<v1::types, v2::types>::type>::type v3;
  28. v1 a = 1;
  29. v2 b = "2";
  30. v3 c = a;
  31. BOOST_TEST(boost::get<int>(c) == 1);
  32. BOOST_TEST(c.which() == 0);
  33. v3 d = b;
  34. BOOST_TEST(boost::get<std::string>(d) == "2");
  35. BOOST_TEST(d.which() == 1);
  36. check_exception_on_get<std::string>(c);
  37. check_exception_on_get<int>(d);
  38. }
  39. void test_set() {
  40. typedef boost::mpl::set2< std::string, int > types;
  41. typedef boost::make_variant_over< types >::type v;
  42. v a = 1;
  43. BOOST_TEST(boost::get<int>(a) == 1);
  44. check_exception_on_get<std::string>(a);
  45. a = "2";
  46. BOOST_TEST(boost::get<std::string>(a) == "2");
  47. check_exception_on_get<int>(a);
  48. }
  49. int main()
  50. {
  51. test_joint_view();
  52. test_set();
  53. return boost::report_errors();
  54. }