]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 | ||
7c673cae FG |
13 | #include "boost/optional/optional.hpp" |
14 | #include "boost/optional/optional_io.hpp" | |
1e59de90 TL |
15 | #include "boost/core/lightweight_test.hpp" |
16 | ||
17 | #ifndef BOOST_NO_IOSTREAM | |
18 | ||
19 | #include <sstream> | |
7c673cae | 20 | |
20effc67 | 21 | #ifdef BOOST_BORLANDC |
7c673cae FG |
22 | #pragma hdrstop |
23 | #endif | |
24 | ||
1e59de90 | 25 | |
7c673cae FG |
26 | |
27 | using boost::optional; | |
7c673cae FG |
28 | |
29 | template<class Opt> | |
30 | void test2( Opt o, Opt buff ) | |
31 | { | |
32 | std::stringstream s ; | |
33 | ||
34 | const int markv = 123 ; | |
35 | int mark = 0 ; | |
1e59de90 | 36 | |
7c673cae FG |
37 | s << o << " " << markv ; |
38 | s >> buff >> mark ; | |
39 | ||
40 | BOOST_TEST( buff == o ) ; | |
41 | BOOST_TEST( mark == markv ) ; | |
42 | } | |
43 | ||
44 | ||
45 | template<class T> | |
46 | void test( T v, T w ) | |
47 | { | |
b32b8144 FG |
48 | test2( boost::make_optional(v), optional<T> ()); |
49 | test2( boost::make_optional(v), boost::make_optional(w)); | |
7c673cae | 50 | test2( optional<T> () , optional<T> ()); |
b32b8144 | 51 | test2( optional<T> () , boost::make_optional(w)); |
7c673cae FG |
52 | } |
53 | ||
54 | ||
55 | template <class T> | |
56 | void subtest_tag_none_reversibility_with_optional(optional<T> ov) | |
57 | { | |
58 | std::stringstream s; | |
59 | s << boost::none; | |
60 | s >> ov; | |
61 | BOOST_TEST(!ov); | |
62 | } | |
63 | ||
64 | template <class T> | |
65 | void subtest_tag_none_equivalence_with_optional() | |
66 | { | |
67 | std::stringstream s, r; | |
68 | optional<T> ov; | |
69 | s << boost::none; | |
70 | r << ov; | |
71 | BOOST_TEST_EQ(s.str(), r.str()); | |
72 | } | |
73 | ||
74 | template <class T> | |
75 | void test_tag_none(T v) | |
76 | { | |
77 | subtest_tag_none_reversibility_with_optional(optional<T>(v)); | |
78 | subtest_tag_none_reversibility_with_optional(optional<T>()); | |
79 | subtest_tag_none_equivalence_with_optional<T>(); | |
80 | } | |
81 | ||
82 | ||
83 | int main() | |
84 | { | |
85 | test(1,2); | |
86 | test(std::string("hello"), std::string("buffer")); | |
87 | test_tag_none(10); | |
88 | test_tag_none(std::string("text")); | |
89 | ||
90 | return boost::report_errors(); | |
91 | } | |
1e59de90 TL |
92 | |
93 | #else // BOOST_NO_IOSTREAM | |
94 | ||
95 | int main() | |
96 | { | |
97 | return boost::report_errors(); | |
98 | } | |
99 | ||
100 | #endif // BOOST_NO_IOSTREAM |