test8.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #include "boost/core/lightweight_test.hpp"
  13. #include "boost/variant.hpp"
  14. #include <iostream>
  15. #include <vector>
  16. #include <string>
  17. using namespace boost;
  18. typedef variant<float, std::string, int, std::vector<std::string> > t_var1;
  19. struct int_sum : static_visitor<>
  20. {
  21. int_sum() : result_(0) { }
  22. void operator()(int t)
  23. {
  24. result_ += t;
  25. }
  26. result_type operator()(float ) { }
  27. result_type operator()(const std::string& ) { }
  28. result_type operator()(const std::vector<std::string>& ) { }
  29. int result_;
  30. };
  31. template <typename T, typename Variant>
  32. T& check_pass(Variant& v, T value)
  33. {
  34. BOOST_TEST(get<T>(&v));
  35. try
  36. {
  37. T& r = get<T>(v);
  38. BOOST_TEST(r == value);
  39. return r;
  40. }
  41. catch(boost::bad_get&)
  42. {
  43. throw; // must never reach
  44. }
  45. }
  46. template <typename T, typename Variant>
  47. void check_fail(Variant& v)
  48. {
  49. BOOST_TEST(!relaxed_get<T>(&v));
  50. try
  51. {
  52. T& r = relaxed_get<T>(v);
  53. (void)r; // suppress warning about r not being used
  54. BOOST_TEST(false && relaxed_get<T>(&v)); // should never reach
  55. }
  56. catch(const boost::bad_get& e)
  57. {
  58. BOOST_TEST(!!e.what()); // make sure that what() is const qualified and returnes something
  59. }
  60. }
  61. int main()
  62. {
  63. int_sum acc;
  64. t_var1 v1 = 800;
  65. // check get on non-const variant
  66. {
  67. int& r1 = check_pass<int>(v1, 800);
  68. const int& cr1 = check_pass<const int>(v1, 800);
  69. check_fail<float>(v1);
  70. check_fail<const float>(v1);
  71. check_fail<short>(v1);
  72. check_fail<const short>(v1);
  73. apply_visitor(acc, v1);
  74. BOOST_TEST(acc.result_ == 800);
  75. r1 = 920; // NOTE: modifies content of v1
  76. apply_visitor(acc, v1);
  77. BOOST_TEST(cr1 == 920);
  78. BOOST_TEST(acc.result_ == 800 + 920);
  79. }
  80. // check const correctness:
  81. {
  82. const t_var1& c = v1;
  83. check_pass<const int>(c, 920);
  84. //check_fail<int>(c);
  85. check_fail<const float>(c);
  86. //check_fail<float>(c);
  87. check_fail<const short>(c);
  88. //check_fail<short>(c);
  89. }
  90. return boost::report_errors();
  91. }