• 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 #ifndef BOOST_OPTIONAL_TEST_OPTIONAL_REF_ASSIGN_TEST_DEFS_AK_07JAN2015_HPP
12 #define BOOST_OPTIONAL_TEST_OPTIONAL_REF_ASSIGN_TEST_DEFS_AK_07JAN2015_HPP
13 
14 #include "boost/optional/optional.hpp"
15 
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19 
20 #include "boost/core/addressof.hpp"
21 #include "testable_classes.hpp"
22 
23 using boost::optional;
24 using boost::none;
25 using boost::addressof;
26 
27 template <typename T>
test_copy_assignment_for_const()28 void test_copy_assignment_for_const()
29 {
30   const typename concrete_type_of<T>::type v(2);
31   optional<const T&> o;
32   o = optional<const T&>(v);
33 
34   BOOST_TEST(o);
35   BOOST_TEST(o != none);
36   BOOST_TEST(addressof(*o) == addressof(v));
37   BOOST_TEST(val(*o) == val(v));
38   BOOST_TEST(val(*o) == 2);
39 }
40 
41 template <typename T>
test_copy_assignment_for_noconst_const()42 void test_copy_assignment_for_noconst_const()
43 {
44   typename concrete_type_of<T>::type v(2);
45   optional<const T&> o;
46   o = optional<const T&>(v);
47 
48   BOOST_TEST(o);
49   BOOST_TEST(o != none);
50   BOOST_TEST(addressof(*o) == addressof(v));
51   BOOST_TEST(val(*o) == val(v));
52   BOOST_TEST(val(*o) == 2);
53 
54   val(v) = 9;
55   BOOST_TEST(addressof(*o) == addressof(v));
56   BOOST_TEST_EQ(val(*o), val(v));
57   BOOST_TEST_EQ(val(*o), 9);
58   BOOST_TEST_EQ(val(v), 9);
59 }
60 
61 template <typename T>
test_copy_assignment_for()62 void test_copy_assignment_for()
63 {
64   typename concrete_type_of<T>::type v(2);
65   optional<T&> o;
66   o = optional<T&>(v);
67 
68   BOOST_TEST(o);
69   BOOST_TEST(o != none);
70   BOOST_TEST(addressof(*o) == addressof(v));
71   BOOST_TEST(val(*o) == val(v));
72   BOOST_TEST(val(*o) == 2);
73 
74   val(v) = 9;
75   BOOST_TEST(addressof(*o) == addressof(v));
76   BOOST_TEST_EQ(val(*o), val(v));
77   BOOST_TEST_EQ(val(*o), 9);
78   BOOST_TEST_EQ(val(v), 9);
79 
80   val(*o) = 7;
81   BOOST_TEST(addressof(*o) == addressof(v));
82   BOOST_TEST_EQ(val(*o), val(v));
83   BOOST_TEST_EQ(val(*o), 7);
84   BOOST_TEST_EQ(val(v), 7);
85 }
86 
87 template <typename T>
test_rebinding_assignment_semantics_const()88 void test_rebinding_assignment_semantics_const()
89 {
90   const typename concrete_type_of<T>::type v(2), w(7);
91   optional<const T&> o(v);
92 
93   BOOST_TEST(o);
94   BOOST_TEST(addressof(*o) == addressof(v));
95   BOOST_TEST_EQ(val(*o), val(v));
96   BOOST_TEST_EQ(val(*o), 2);
97 
98   o = optional<const T&>(w);
99   BOOST_TEST_EQ(val(v), 2);
100 
101   BOOST_TEST(o);
102   BOOST_TEST(addressof(*o) != addressof(v));
103   BOOST_TEST_NE(val(*o), val(v));
104   BOOST_TEST_NE(val(*o), 2);
105 
106   BOOST_TEST(addressof(*o) == addressof(w));
107   BOOST_TEST_EQ(val(*o), val(w));
108   BOOST_TEST_EQ(val(*o), 7);
109 }
110 
111 template <typename T>
test_rebinding_assignment_semantics_noconst_const()112 void test_rebinding_assignment_semantics_noconst_const()
113 {
114   typename concrete_type_of<T>::type v(2), w(7);
115   optional<const T&> o(v);
116 
117   BOOST_TEST(o);
118   BOOST_TEST(addressof(*o) == addressof(v));
119   BOOST_TEST_EQ(val(*o), val(v));
120   BOOST_TEST_EQ(val(*o), 2);
121 
122   o = optional<const T&>(w);
123   BOOST_TEST_EQ(val(v), 2);
124 
125   BOOST_TEST(o);
126   BOOST_TEST(addressof(*o) != addressof(v));
127   BOOST_TEST_NE(val(*o), val(v));
128   BOOST_TEST_NE(val(*o), 2);
129 
130   BOOST_TEST(addressof(*o) == addressof(w));
131   BOOST_TEST_EQ(val(*o), val(w));
132   BOOST_TEST_EQ(val(*o), 7);
133 }
134 
135 template <typename T>
test_rebinding_assignment_semantics()136 void test_rebinding_assignment_semantics()
137 {
138   typename concrete_type_of<T>::type v(2), w(7);
139   optional<T&> o(v);
140 
141   BOOST_TEST(o);
142   BOOST_TEST(addressof(*o) == addressof(v));
143   BOOST_TEST_EQ(val(*o), val(v));
144   BOOST_TEST_EQ(val(*o), 2);
145 
146   o = optional<T&>(w);
147   BOOST_TEST_EQ(val(v), 2);
148 
149   BOOST_TEST(o);
150   BOOST_TEST(addressof(*o) != addressof(v));
151   BOOST_TEST_NE(val(*o), val(v));
152   BOOST_TEST_NE(val(*o), 2);
153 
154   BOOST_TEST(addressof(*o) == addressof(w));
155   BOOST_TEST_EQ(val(*o), val(w));
156   BOOST_TEST_EQ(val(*o), 7);
157 
158   val(*o) = 8;
159   BOOST_TEST(addressof(*o) == addressof(w));
160   BOOST_TEST_EQ(val(*o), val(w));
161   BOOST_TEST_EQ(val(*o), 8);
162   BOOST_TEST_EQ(val(w), 8);
163   BOOST_TEST_EQ(val(v), 2);
164 }
165 
166 template <typename T, typename U>
test_converting_assignment()167 void test_converting_assignment()
168 {
169   typename concrete_type_of<T>::type v1(1), v2(2), v3(3);
170   optional<U&> oA(v1), oB(none);
171 
172   oA = v2;
173   BOOST_TEST(oA);
174   BOOST_TEST(addressof(*oA) == addressof(v2));
175 
176   oB = v3;
177   BOOST_TEST(oB);
178   BOOST_TEST(addressof(*oB) == addressof(v3));
179 }
180 
181 #endif //BOOST_OPTIONAL_TEST_OPTIONAL_REF_ASSIGN_TEST_DEFS_AK_07JAN2015_HPP
182