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
13 #include<string>
14 #include "boost/optional/optional.hpp"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
21 #include "boost/utility/in_place_factory.hpp"
22 #include "boost/utility/typed_in_place_factory.hpp"
23 #endif
24
25 #include "boost/core/lightweight_test.hpp"
26 #include "boost/none.hpp"
27
28 struct Guard
29 {
30 double num;
31 std::string str;
GuardGuard32 Guard() : num() {}
GuardGuard33 Guard(double num_, std::string str_) : num(num_), str(str_) {}
34
operator ==(const Guard & lhs,const Guard & rhs)35 friend bool operator==(const Guard& lhs, const Guard& rhs) { return lhs.num == rhs.num && lhs.str == rhs.str; }
operator !=(const Guard & lhs,const Guard & rhs)36 friend bool operator!=(const Guard& lhs, const Guard& rhs) { return !(lhs == rhs); }
37
38 private:
39 Guard(const Guard&);
40 Guard& operator=(const Guard&);
41 };
42
test_ctor()43 void test_ctor()
44 {
45 #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
46 Guard g0, g1(1.0, "one"), g2(2.0, "two");
47
48 boost::optional<Guard> og0 ( boost::in_place() );
49 boost::optional<Guard> og1 ( boost::in_place(1.0, "one") );
50 boost::optional<Guard> og1_( boost::in_place(1.0, "one") );
51 boost::optional<Guard> og2 ( boost::in_place<Guard>(2.0, "two") );
52
53 BOOST_TEST(og0);
54 BOOST_TEST(og1);
55 BOOST_TEST(og1_);
56 BOOST_TEST(og2);
57
58 BOOST_TEST(*og0 == g0);
59 BOOST_TEST(*og1 == g1);
60 BOOST_TEST(*og1_ == g1);
61 BOOST_TEST(*og2 == g2);
62
63 BOOST_TEST(og1_ == og1);
64 BOOST_TEST(og1_ != og2);
65 BOOST_TEST(og1_ != og0);
66
67 boost::optional<unsigned int> o( boost::in_place(5) );
68 BOOST_TEST(o);
69 BOOST_TEST(*o == 5);
70 #endif
71 }
72
test_assign()73 void test_assign()
74 {
75 #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
76 #ifndef BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION
77 Guard g0, g1(1.0, "one"), g2(2.0, "two");
78
79 boost::optional<Guard> og0, og1, og1_, og2;
80
81 og0 = boost::in_place();
82 og1 = boost::in_place(1.0, "one");
83 og1_ = boost::in_place(1.0, "one");
84 og2 = boost::in_place<Guard>(2.0, "two");
85
86 BOOST_TEST(og0);
87 BOOST_TEST(og1);
88 BOOST_TEST(og1_);
89 BOOST_TEST(og2);
90
91 BOOST_TEST(*og0 == g0);
92 BOOST_TEST(*og1 == g1);
93 BOOST_TEST(*og1_ == g1);
94 BOOST_TEST(*og2 == g2);
95
96 BOOST_TEST(og1_ == og1);
97 BOOST_TEST(og1_ != og2);
98 BOOST_TEST(og1_ != og0);
99
100 boost::optional<unsigned int> o;
101 o = boost::in_place(5);
102 BOOST_TEST(o);
103 BOOST_TEST(*o == 5);
104 #endif
105 #endif
106 }
107
main()108 int main()
109 {
110 test_ctor();
111 test_assign();
112 return boost::report_errors();
113 }
114