optional_test_io.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright (C) 2003, Fernando Luis Cacciola Carballal.
  2. // Copyright (C) 2014 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 <sstream>
  13. #include "boost/optional/optional.hpp"
  14. #include "boost/optional/optional_io.hpp"
  15. #ifdef __BORLANDC__
  16. #pragma hdrstop
  17. #endif
  18. #include "boost/core/lightweight_test.hpp"
  19. using boost::optional;
  20. template<class Opt>
  21. void test2( Opt o, Opt buff )
  22. {
  23. std::stringstream s ;
  24. const int markv = 123 ;
  25. int mark = 0 ;
  26. s << o << " " << markv ;
  27. s >> buff >> mark ;
  28. BOOST_TEST( buff == o ) ;
  29. BOOST_TEST( mark == markv ) ;
  30. }
  31. template<class T>
  32. void test( T v, T w )
  33. {
  34. test2( boost::make_optional(v), optional<T> ());
  35. test2( boost::make_optional(v), boost::make_optional(w));
  36. test2( optional<T> () , optional<T> ());
  37. test2( optional<T> () , boost::make_optional(w));
  38. }
  39. template <class T>
  40. void subtest_tag_none_reversibility_with_optional(optional<T> ov)
  41. {
  42. std::stringstream s;
  43. s << boost::none;
  44. s >> ov;
  45. BOOST_TEST(!ov);
  46. }
  47. template <class T>
  48. void subtest_tag_none_equivalence_with_optional()
  49. {
  50. std::stringstream s, r;
  51. optional<T> ov;
  52. s << boost::none;
  53. r << ov;
  54. BOOST_TEST_EQ(s.str(), r.str());
  55. }
  56. template <class T>
  57. void test_tag_none(T v)
  58. {
  59. subtest_tag_none_reversibility_with_optional(optional<T>(v));
  60. subtest_tag_none_reversibility_with_optional(optional<T>());
  61. subtest_tag_none_equivalence_with_optional<T>();
  62. }
  63. int main()
  64. {
  65. test(1,2);
  66. test(std::string("hello"), std::string("buffer"));
  67. test_tag_none(10);
  68. test_tag_none(std::string("text"));
  69. return boost::report_errors();
  70. }