1 // Copyright (C) 2014 Andrzej Krzemienski.
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)
7 // See http://www.boost.org/lib/optional for documentation.
9 // You are welcome to contact the author at: akrzemi1@gmail.com
12 #include "boost/optional/optional.hpp"
18 #include "boost/core/addressof.hpp"
19 #include "boost/core/enable_if.hpp"
20 #include "boost/core/lightweight_test.hpp"
21 #include "testable_classes.hpp"
23 using boost::optional
;
27 void test_converting_ctor()
29 typename concrete_type_of
<T
>::type
v1(1), v2(2);
32 optional
<T
&> o1
= v1
, o1_
= v1
, o2
= v2
;
35 BOOST_TEST(boost::addressof(*o1
) == boost::addressof(v1
));
37 BOOST_TEST(boost::addressof(*o1_
) == boost::addressof(v1
));
38 BOOST_TEST(boost::addressof(*o1_
) == boost::addressof(*o1
));
41 BOOST_TEST(boost::addressof(*o2
) == boost::addressof(v2
));
42 BOOST_TEST(boost::addressof(*o2
) != boost::addressof(*o1
));
45 const optional
<T
&> o1
= v1
, o1_
= v1
, o2
= v2
;
48 BOOST_TEST(boost::addressof(*o1
) == boost::addressof(v1
));
50 BOOST_TEST(boost::addressof(*o1_
) == boost::addressof(v1
));
51 BOOST_TEST(boost::addressof(*o1_
) == boost::addressof(*o1
));
54 BOOST_TEST(boost::addressof(*o2
) == boost::addressof(v2
));
55 BOOST_TEST(boost::addressof(*o2
) != boost::addressof(*o1
));
60 void test_converting_ctor_for_noconst_const()
62 typename concrete_type_of
<T
>::type
v1(1), v2(2);
65 optional
<const T
&> o1
= v1
, o1_
= v1
, o2
= v2
;
68 BOOST_TEST(boost::addressof(*o1
) == boost::addressof(v1
));
70 BOOST_TEST(boost::addressof(*o1_
) == boost::addressof(v1
));
71 BOOST_TEST(boost::addressof(*o1_
) == boost::addressof(*o1
));
74 BOOST_TEST(boost::addressof(*o2
) == boost::addressof(v2
));
75 BOOST_TEST(boost::addressof(*o2
) != boost::addressof(*o1
));
78 const optional
<const T
&> o1
= v1
, o1_
= v1
, o2
= v2
;
81 BOOST_TEST(boost::addressof(*o1
) == boost::addressof(v1
));
83 BOOST_TEST(boost::addressof(*o1_
) == boost::addressof(v1
));
84 BOOST_TEST(boost::addressof(*o1_
) == boost::addressof(*o1
));
87 BOOST_TEST(boost::addressof(*o2
) == boost::addressof(v2
));
88 BOOST_TEST(boost::addressof(*o2
) != boost::addressof(*o1
));
93 void test_all_const_cases()
95 test_converting_ctor
<T
>();
96 #ifndef BOOST_OPTIONAL_CONFIG_NO_PROPER_CONVERT_FROM_CONST_INT
97 test_converting_ctor
<const T
>();
99 test_converting_ctor_for_noconst_const
<T
>();
104 test_all_const_cases
<int>();
105 test_all_const_cases
<ScopeGuard
>();
106 test_all_const_cases
<Abstract
>();
107 test_all_const_cases
< optional
<int> >();
109 return boost::report_errors();