]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 | ||
20effc67 | 16 | #ifdef BOOST_BORLANDC |
7c673cae FG |
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; | |
32 | Guard() : num() {} | |
33 | Guard(double num_, std::string str_) : num(num_), str(str_) {} | |
f67539c2 | 34 | |
7c673cae FG |
35 | friend bool operator==(const Guard& lhs, const Guard& rhs) { return lhs.num == rhs.num && lhs.str == rhs.str; } |
36 | friend bool operator!=(const Guard& lhs, const Guard& rhs) { return !(lhs == rhs); } | |
f67539c2 | 37 | |
7c673cae FG |
38 | private: |
39 | Guard(const Guard&); | |
40 | Guard& operator=(const Guard&); | |
41 | }; | |
42 | ||
43 | void test_ctor() | |
44 | { | |
45 | #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT | |
46 | Guard g0, g1(1.0, "one"), g2(2.0, "two"); | |
f67539c2 | 47 | |
7c673cae FG |
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") ); | |
f67539c2 | 52 | |
7c673cae FG |
53 | BOOST_TEST(og0); |
54 | BOOST_TEST(og1); | |
55 | BOOST_TEST(og1_); | |
56 | BOOST_TEST(og2); | |
f67539c2 | 57 | |
7c673cae FG |
58 | BOOST_TEST(*og0 == g0); |
59 | BOOST_TEST(*og1 == g1); | |
60 | BOOST_TEST(*og1_ == g1); | |
61 | BOOST_TEST(*og2 == g2); | |
f67539c2 | 62 | |
7c673cae FG |
63 | BOOST_TEST(og1_ == og1); |
64 | BOOST_TEST(og1_ != og2); | |
65 | BOOST_TEST(og1_ != og0); | |
f67539c2 TL |
66 | |
67 | boost::optional<unsigned int> o( boost::in_place(5) ); | |
68 | BOOST_TEST(o); | |
69 | BOOST_TEST(*o == 5); | |
70 | #endif | |
7c673cae FG |
71 | } |
72 | ||
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"); | |
f67539c2 | 78 | |
7c673cae | 79 | boost::optional<Guard> og0, og1, og1_, og2; |
f67539c2 | 80 | |
7c673cae FG |
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"); | |
f67539c2 | 85 | |
7c673cae FG |
86 | BOOST_TEST(og0); |
87 | BOOST_TEST(og1); | |
88 | BOOST_TEST(og1_); | |
89 | BOOST_TEST(og2); | |
f67539c2 | 90 | |
7c673cae FG |
91 | BOOST_TEST(*og0 == g0); |
92 | BOOST_TEST(*og1 == g1); | |
93 | BOOST_TEST(*og1_ == g1); | |
94 | BOOST_TEST(*og2 == g2); | |
f67539c2 | 95 | |
7c673cae FG |
96 | BOOST_TEST(og1_ == og1); |
97 | BOOST_TEST(og1_ != og2); | |
98 | BOOST_TEST(og1_ != og0); | |
f67539c2 TL |
99 | |
100 | boost::optional<unsigned int> o; | |
101 | o = boost::in_place(5); | |
102 | BOOST_TEST(o); | |
103 | BOOST_TEST(*o == 5); | |
7c673cae FG |
104 | #endif |
105 | #endif | |
106 | } | |
107 | ||
108 | int main() | |
109 | { | |
110 | test_ctor(); | |
111 | test_assign(); | |
112 | return boost::report_errors(); | |
f67539c2 | 113 | } |