1 // Copyright (C) 2014 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: akrzemi1@gmail.com
10
11
12 #include "boost/optional/optional.hpp"
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 #include "boost/core/lightweight_test.hpp"
19
20 using boost::optional;
21
22
23 // testing types:
24 // X is convertible to Y
25 // ADeriv is convertible to ABase
26 struct X
27 {
28 int val;
XX29 explicit X(int v) : val(v) {}
30 };
31
32 struct Y
33 {
34 int yval;
YY35 Y(X const& x) : yval(x.val) {}
operator ==(Y const & l,Y const & r)36 friend bool operator==(Y const& l, Y const& r) { return l.yval == r.yval; }
37 };
38
39 struct ABase
40 {
41 int val;
ABaseABase42 explicit ABase(int v) : val(v) {}
operator ==(ABase const & l,ABase const & r)43 friend bool operator==(ABase const& l, ABase const& r) { return l.val == r.val; }
44 };
45
46 struct ADeriv : ABase
47 {
ADerivADeriv48 explicit ADeriv(int v) : ABase(v) {}
49 };
50
51
52 template <typename T, typename U>
test_convert_optional_U_to_optional_T_for()53 void test_convert_optional_U_to_optional_T_for()
54 {
55 #ifndef BOOST_OPTIONAL_NO_CONVERTING_COPY_CTOR
56 {
57 optional<U> ou(U(8));
58 optional<T> ot1(ou);
59 BOOST_TEST(ot1);
60 BOOST_TEST(*ot1 == T(*ou));
61 }
62 #endif
63
64 #ifndef BOOST_OPTIONAL_NO_CONVERTING_ASSIGNMENT
65 {
66 optional<U> ou(U(8));
67 optional<T> ot2;
68 ot2 = ou;
69 BOOST_TEST(ot2);
70 BOOST_TEST(*ot2 == T(*ou));
71 }
72 #endif
73 }
74
test_convert_optional_U_to_optional_T()75 void test_convert_optional_U_to_optional_T()
76 {
77 test_convert_optional_U_to_optional_T_for<Y, X>();
78 test_convert_optional_U_to_optional_T_for<ABase, ADeriv>();
79 test_convert_optional_U_to_optional_T_for<long, short>();
80 test_convert_optional_U_to_optional_T_for<double, float>();
81 }
82
83 template <typename T, typename U>
test_convert_U_to_optional_T_for()84 void test_convert_U_to_optional_T_for()
85 {
86 U u(8);
87 optional<T> ot1(u);
88 BOOST_TEST(ot1);
89 BOOST_TEST(*ot1 == T(u));
90
91 optional<T> ot2;
92 ot2 = u;
93 BOOST_TEST(ot2);
94 BOOST_TEST(*ot2 == T(u));
95 }
96
test_convert_U_to_optional_T()97 void test_convert_U_to_optional_T()
98 {
99 test_convert_U_to_optional_T_for<Y, X>();
100 test_convert_U_to_optional_T_for<ABase, ADeriv>();
101 test_convert_U_to_optional_T_for<long, short>();
102 test_convert_U_to_optional_T_for<double, float>();
103 }
104
main()105 int main()
106 {
107 test_convert_optional_U_to_optional_T();
108 test_convert_U_to_optional_T();
109
110 return boost::report_errors();
111 }
112