1 // Copyright (C) 2016 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:
13 #include "boost/optional/optional.hpp"
19 #include "boost/core/lightweight_test.hpp"
21 using boost::optional
;
27 explicit Value(int v
) : val(v
) {}
35 int val(Value
const& v
)
40 template <typename Tref
>
41 optional
<Tref
&> make_opt_ref(Tref
& v
)
43 return optional
<Tref
&>(v
);
46 template <typename Tval
, typename Tref
>
47 void test_construct_from_optional_ref()
51 optional
<Tref
&> opt_ref0
;
52 optional
<Tref
&> opt_ref1 (v1
);
54 optional
<Tval
> opt_val0 (opt_ref0
);
55 optional
<Tval
> opt_val1 (opt_ref1
);
56 optional
<Tval
> opt_val2 (make_opt_ref(v2
));
58 BOOST_TEST (!opt_val0
);
59 BOOST_TEST (opt_val1
);
60 BOOST_TEST (opt_val2
);
62 BOOST_TEST_EQ (1, val(*opt_val1
));
63 BOOST_TEST_EQ (2, val(*opt_val2
));
65 BOOST_TEST (boost::addressof(*opt_val1
) != boost::addressof(v1
));
66 BOOST_TEST (boost::addressof(*opt_val2
) != boost::addressof(v2
));
69 template <typename Tval
, typename Tref
>
70 void test_assign_from_optional_ref()
74 optional
<Tref
&> opt_ref0
;
75 optional
<Tref
&> opt_ref1 (v1
);
77 optional
<Tval
> opt_val0
;
78 optional
<Tval
> opt_val1
;
79 optional
<Tval
> opt_val2
;
83 opt_val2
= make_opt_ref(v2
);
85 BOOST_TEST (!opt_val0
);
86 BOOST_TEST (opt_val1
);
87 BOOST_TEST (opt_val2
);
89 BOOST_TEST_EQ (1, val(*opt_val1
));
90 BOOST_TEST_EQ (2, val(*opt_val2
));
92 BOOST_TEST (boost::addressof(*opt_val1
) != boost::addressof(v1
));
93 BOOST_TEST (boost::addressof(*opt_val2
) != boost::addressof(v2
));
99 test_construct_from_optional_ref
<int, int>();
100 test_construct_from_optional_ref
<int, int const>();
101 test_construct_from_optional_ref
<int const, int const>();
102 test_construct_from_optional_ref
<int const, int>();
104 test_construct_from_optional_ref
<Value
, Value
>();
105 test_construct_from_optional_ref
<Value
, Value
const>();
106 test_construct_from_optional_ref
<Value
const, Value
const>();
107 test_construct_from_optional_ref
<Value
const, Value
>();
109 test_assign_from_optional_ref
<int, int>();
110 test_assign_from_optional_ref
<int, int const>();
112 test_assign_from_optional_ref
<Value
, Value
>();
113 test_assign_from_optional_ref
<Value
, Value
const>();
115 return boost::report_errors();