• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2017 Andrzej Krzemienski.
2 //
3 // Use, modification, and distribution is subject to the Boost Software
4 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // See http://www.boost.org/lib/optional for documentation.
8 //
9 // You are welcome to contact the author at:
10 //  akrzemi1@gmail.com
11 
12 #include "boost/optional/optional.hpp"
13 
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17 
18 #include "boost/core/ignore_unused.hpp"
19 #include "boost/core/is_same.hpp"
20 #include "boost/core/lightweight_test.hpp"
21 #include "boost/core/lightweight_test_trait.hpp"
22 
23 
24 using boost::optional;
25 using boost::make_optional;
26 using boost::core::is_same;
27 
28 template <typename Expected, typename Deduced>
verify_type(Deduced)29 void verify_type(Deduced)
30 {
31   BOOST_TEST_TRAIT_TRUE(( is_same<Expected, Deduced> ));
32 }
33 
34 #if (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
35 struct MoveOnly
36 {
37   int value;
MoveOnlyMoveOnly38   explicit MoveOnly(int i) : value(i) {}
MoveOnlyMoveOnly39   MoveOnly(MoveOnly && r) : value(r.value) { r.value = 0; }
operator =MoveOnly40   MoveOnly& operator=(MoveOnly && r) { value = r.value; r.value = 0; return *this; }
41 
42 private:
43   MoveOnly(MoveOnly const&);
44   void operator=(MoveOnly const&);
45 };
46 
makeMoveOnly(int i)47 MoveOnly makeMoveOnly(int i)
48 {
49   return MoveOnly(i);
50 }
51 
test_make_optional_for_move_only_type()52 void test_make_optional_for_move_only_type()
53 {
54   verify_type< optional<MoveOnly> >(make_optional(makeMoveOnly(2)));
55   verify_type< optional<MoveOnly> >(make_optional(true, makeMoveOnly(2)));
56 
57   optional<MoveOnly> o1 = make_optional(makeMoveOnly(1));
58   BOOST_TEST (o1);
59   BOOST_TEST_EQ (1, o1->value);
60 
61   optional<MoveOnly> o2 = make_optional(true, makeMoveOnly(2));
62   BOOST_TEST (o2);
63   BOOST_TEST_EQ (2, o2->value);
64 
65   optional<MoveOnly> oN = make_optional(false, makeMoveOnly(2));
66   BOOST_TEST (!oN);
67 }
68 
69 #endif // !defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
70 
test_make_optional_for_optional()71 void test_make_optional_for_optional()
72 {
73   optional<int> oi;
74   verify_type< optional< optional<int> > >(make_optional(oi));
75   verify_type< optional< optional<int> > >(make_optional(true, oi));
76 
77   optional< optional<int> > ooi = make_optional(oi);
78   BOOST_TEST (ooi);
79   BOOST_TEST (!*ooi);
80 
81   optional< optional<int> > ooT = make_optional(true, oi);
82   BOOST_TEST (ooT);
83   BOOST_TEST (!*ooT);
84 
85   optional< optional<int> > ooF = make_optional(false, oi);
86   BOOST_TEST (!ooF);
87 }
88 
test_nested_make_optional()89 void test_nested_make_optional()
90 {
91   verify_type< optional< optional<int> > >(make_optional(make_optional(1)));
92   verify_type< optional< optional<int> > >(make_optional(true, make_optional(true, 2)));
93 
94   optional< optional<int> > oo1 = make_optional(make_optional(1));
95   BOOST_TEST (oo1);
96   BOOST_TEST (*oo1);
97   BOOST_TEST_EQ (1, **oo1);
98 
99   optional< optional<int> > oo2 = make_optional(true, make_optional(true, 2));
100   BOOST_TEST (oo2);
101   BOOST_TEST (*oo2);
102   BOOST_TEST_EQ (2, **oo2);
103 
104   optional< optional<int> > oo3 = make_optional(true, make_optional(false, 3));
105   BOOST_TEST (oo3);
106   BOOST_TEST (!*oo3);
107 
108   optional< optional<int> > oo4 = make_optional(false, make_optional(true, 4));
109   BOOST_TEST (!oo4);
110 }
111 
main()112 int main()
113 {
114 #if (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
115   test_make_optional_for_move_only_type();
116 #endif
117   test_make_optional_for_optional();
118   test_nested_make_optional();
119 
120   return boost::report_errors();
121 }
122