• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/addressof.hpp"
19 #include "boost/core/enable_if.hpp"
20 #include "boost/core/lightweight_test.hpp"
21 #include "testable_classes.hpp"
22 
23 using boost::optional;
24 using boost::none;
25 
26 template <typename T>
test_converting_ctor()27 void test_converting_ctor()
28 {
29   typename concrete_type_of<T>::type v1(1), v2(2);
30 
31   {
32     optional<T&> o1 = v1, o1_ = v1, o2 = v2;
33 
34     BOOST_TEST(o1);
35     BOOST_TEST(boost::addressof(*o1) == boost::addressof(v1));
36     BOOST_TEST(o1_);
37     BOOST_TEST(boost::addressof(*o1_) == boost::addressof(v1));
38     BOOST_TEST(boost::addressof(*o1_) == boost::addressof(*o1));
39 
40     BOOST_TEST(o2);
41     BOOST_TEST(boost::addressof(*o2) == boost::addressof(v2));
42     BOOST_TEST(boost::addressof(*o2) != boost::addressof(*o1));
43   }
44   {
45     const optional<T&> o1 = v1, o1_ = v1, o2 = v2;
46 
47     BOOST_TEST(o1);
48     BOOST_TEST(boost::addressof(*o1) == boost::addressof(v1));
49     BOOST_TEST(o1_);
50     BOOST_TEST(boost::addressof(*o1_) == boost::addressof(v1));
51     BOOST_TEST(boost::addressof(*o1_) == boost::addressof(*o1));
52 
53     BOOST_TEST(o2);
54     BOOST_TEST(boost::addressof(*o2) == boost::addressof(v2));
55     BOOST_TEST(boost::addressof(*o2) != boost::addressof(*o1));
56   }
57 }
58 
59 template <typename T>
test_converting_ctor_for_noconst_const()60 void test_converting_ctor_for_noconst_const()
61 {
62   typename concrete_type_of<T>::type v1(1), v2(2);
63 
64   {
65     optional<const T&> o1 = v1, o1_ = v1, o2 = v2;
66 
67     BOOST_TEST(o1);
68     BOOST_TEST(boost::addressof(*o1) == boost::addressof(v1));
69     BOOST_TEST(o1_);
70     BOOST_TEST(boost::addressof(*o1_) == boost::addressof(v1));
71     BOOST_TEST(boost::addressof(*o1_) == boost::addressof(*o1));
72 
73     BOOST_TEST(o2);
74     BOOST_TEST(boost::addressof(*o2) == boost::addressof(v2));
75     BOOST_TEST(boost::addressof(*o2) != boost::addressof(*o1));
76   }
77   {
78     const optional<const T&> o1 = v1, o1_ = v1, o2 = v2;
79 
80     BOOST_TEST(o1);
81     BOOST_TEST(boost::addressof(*o1) == boost::addressof(v1));
82     BOOST_TEST(o1_);
83     BOOST_TEST(boost::addressof(*o1_) == boost::addressof(v1));
84     BOOST_TEST(boost::addressof(*o1_) == boost::addressof(*o1));
85 
86     BOOST_TEST(o2);
87     BOOST_TEST(boost::addressof(*o2) == boost::addressof(v2));
88     BOOST_TEST(boost::addressof(*o2) != boost::addressof(*o1));
89   }
90 }
91 
92 template <typename T>
test_all_const_cases()93 void test_all_const_cases()
94 {
95   test_converting_ctor<T>();
96 #ifndef BOOST_OPTIONAL_CONFIG_NO_PROPER_CONVERT_FROM_CONST_INT
97   test_converting_ctor<const T>();
98 #endif
99   test_converting_ctor_for_noconst_const<T>();
100 }
101 
main()102 int main()
103 {
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> >();
108 
109   return boost::report_errors();
110 }
111