optional_test_inplace_factory.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright (C) 2003, Fernando Luis Cacciola Carballal.
  2. // Copyright (C) 2015 Andrzej Krzemienski.
  3. //
  4. // Use, modification, and distribution is subject to the Boost Software
  5. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // See http://www.boost.org/lib/optional for documentation.
  9. //
  10. // You are welcome to contact the author at:
  11. // fernando_cacciola@hotmail.com
  12. #include<string>
  13. #include "boost/optional/optional.hpp"
  14. #ifdef __BORLANDC__
  15. #pragma hdrstop
  16. #endif
  17. #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
  18. #include "boost/utility/in_place_factory.hpp"
  19. #include "boost/utility/typed_in_place_factory.hpp"
  20. #endif
  21. #include "boost/core/lightweight_test.hpp"
  22. #include "boost/none.hpp"
  23. struct Guard
  24. {
  25. double num;
  26. std::string str;
  27. Guard() : num() {}
  28. Guard(double num_, std::string str_) : num(num_), str(str_) {}
  29. friend bool operator==(const Guard& lhs, const Guard& rhs) { return lhs.num == rhs.num && lhs.str == rhs.str; }
  30. friend bool operator!=(const Guard& lhs, const Guard& rhs) { return !(lhs == rhs); }
  31. private:
  32. Guard(const Guard&);
  33. Guard& operator=(const Guard&);
  34. };
  35. void test_ctor()
  36. {
  37. #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
  38. Guard g0, g1(1.0, "one"), g2(2.0, "two");
  39. boost::optional<Guard> og0 ( boost::in_place() );
  40. boost::optional<Guard> og1 ( boost::in_place(1.0, "one") );
  41. boost::optional<Guard> og1_( boost::in_place(1.0, "one") );
  42. boost::optional<Guard> og2 ( boost::in_place<Guard>(2.0, "two") );
  43. BOOST_TEST(og0);
  44. BOOST_TEST(og1);
  45. BOOST_TEST(og1_);
  46. BOOST_TEST(og2);
  47. BOOST_TEST(*og0 == g0);
  48. BOOST_TEST(*og1 == g1);
  49. BOOST_TEST(*og1_ == g1);
  50. BOOST_TEST(*og2 == g2);
  51. BOOST_TEST(og1_ == og1);
  52. BOOST_TEST(og1_ != og2);
  53. BOOST_TEST(og1_ != og0);
  54. #endif
  55. }
  56. void test_assign()
  57. {
  58. #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
  59. #ifndef BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION
  60. Guard g0, g1(1.0, "one"), g2(2.0, "two");
  61. boost::optional<Guard> og0, og1, og1_, og2;
  62. og0 = boost::in_place();
  63. og1 = boost::in_place(1.0, "one");
  64. og1_ = boost::in_place(1.0, "one");
  65. og2 = boost::in_place<Guard>(2.0, "two");
  66. BOOST_TEST(og0);
  67. BOOST_TEST(og1);
  68. BOOST_TEST(og1_);
  69. BOOST_TEST(og2);
  70. BOOST_TEST(*og0 == g0);
  71. BOOST_TEST(*og1 == g1);
  72. BOOST_TEST(*og1_ == g1);
  73. BOOST_TEST(*og2 == g2);
  74. BOOST_TEST(og1_ == og1);
  75. BOOST_TEST(og1_ != og2);
  76. BOOST_TEST(og1_ != og0);
  77. #endif
  78. #endif
  79. }
  80. int main()
  81. {
  82. test_ctor();
  83. test_assign();
  84. return boost::report_errors();
  85. }