]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/optional/test/optional_test_emplace.cpp
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:
12 #include "boost/optional/optional.hpp"
19 #include "boost/core/lightweight_test.hpp"
20 #include "boost/none.hpp"
22 //#ifndef BOOST_OPTIONAL_NO_CONVERTING_ASSIGNMENT
23 //#ifndef BOOST_OPTIONAL_NO_CONVERTING_COPY_CTOR
25 using boost::optional
;
27 using boost::in_place_init
;
28 using boost::in_place_init_if
;
30 #if (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES) && (!defined BOOST_NO_CXX11_VARIADIC_TEMPLATES)
36 Guard () : which_ctor(0) { }
37 Guard (int&, double&&) : which_ctor(1) { }
38 Guard (int&&, double&) : which_ctor(2) { }
39 Guard (int&&, double&&) : which_ctor(3) { }
40 Guard (int&, double&) : which_ctor(4) { }
41 Guard (std::string
const&) : which_ctor(5) { }
42 Guard (std::string
&) : which_ctor(6) { }
43 Guard (std::string
&&) : which_ctor(7) { }
47 void operator=(Guard
&&);
48 void operator=(Guard
const&);
62 BOOST_TEST(0 == o
->which_ctor
);
66 BOOST_TEST(1 == o
->which_ctor
);
70 BOOST_TEST(2 == o
->which_ctor
);
74 BOOST_TEST(3 == o
->which_ctor
);
78 BOOST_TEST(4 == o
->which_ctor
);
82 BOOST_TEST(5 == o
->which_ctor
);
86 BOOST_TEST(6 == o
->which_ctor
);
88 o
.emplace(std::string());
90 BOOST_TEST(7 == o
->which_ctor
);
93 void test_in_place_ctor()
101 optional
<Guard
> o (in_place_init
);
103 BOOST_TEST(0 == o
->which_ctor
);
106 optional
<Guard
> o (in_place_init
, i
, 2.0);
108 BOOST_TEST(1 == o
->which_ctor
);
111 optional
<Guard
> o (in_place_init
, 1, d
);
113 BOOST_TEST(2 == o
->which_ctor
);
116 optional
<Guard
> o (in_place_init
, 1, 2.0);
118 BOOST_TEST(3 == o
->which_ctor
);
121 optional
<Guard
> o (in_place_init
, i
, d
);
123 BOOST_TEST(4 == o
->which_ctor
);
126 optional
<Guard
> o (in_place_init
, cs
);
128 BOOST_TEST(5 == o
->which_ctor
);
131 optional
<Guard
> o (in_place_init
, ms
);
133 BOOST_TEST(6 == o
->which_ctor
);
136 optional
<Guard
> o (in_place_init
, std::string());
138 BOOST_TEST(7 == o
->which_ctor
);
142 void test_in_place_if_ctor()
146 const std::string cs
;
150 optional
<Guard
> o (in_place_init_if
, true);
152 BOOST_TEST(0 == o
->which_ctor
);
155 optional
<Guard
> o (in_place_init_if
, true, i
, 2.0);
157 BOOST_TEST(1 == o
->which_ctor
);
160 optional
<Guard
> o (in_place_init_if
, true, 1, d
);
162 BOOST_TEST(2 == o
->which_ctor
);
165 optional
<Guard
> o (in_place_init_if
, true, 1, 2.0);
167 BOOST_TEST(3 == o
->which_ctor
);
170 optional
<Guard
> o (in_place_init_if
, true, i
, d
);
172 BOOST_TEST(4 == o
->which_ctor
);
175 optional
<Guard
> o (in_place_init_if
, true, cs
);
177 BOOST_TEST(5 == o
->which_ctor
);
180 optional
<Guard
> o (in_place_init_if
, true, ms
);
182 BOOST_TEST(6 == o
->which_ctor
);
185 optional
<Guard
> o (in_place_init_if
, true, std::string());
187 BOOST_TEST(7 == o
->which_ctor
);
191 optional
<Guard
> o (in_place_init_if
, false, 1, 2.0);
199 #if (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
204 ThrowOnMove(ThrowOnMove
&&) { throw int(); }
205 ThrowOnMove(ThrowOnMove
const&) { throw int(); }
210 void test_no_moves_on_emplacement()
213 optional
<ThrowOnMove
> o
;
222 void test_no_moves_on_in_place_ctor()
225 optional
<ThrowOnMove
> o (in_place_init
, 1);
228 optional
<ThrowOnMove
> p (in_place_init_if
, true, 1);
231 optional
<ThrowOnMove
> q (in_place_init_if
, false, 1);
243 Thrower(bool throw_
) { if (throw_
) throw int(); }
246 Thrower(Thrower
const&);
247 #if (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
252 void test_clear_on_throw()
254 optional
<Thrower
> ot
;
270 void test_no_assignment_on_emplacement()
272 optional
<const std::string
> os
, ot
;
276 BOOST_TEST_EQ(*os
, "wow");
281 BOOST_TEST_EQ(*ot
, "");
284 namespace no_rvalue_refs
{
290 Guard () : which_ctor(0) { }
291 Guard (std::string
const&) : which_ctor(5) { }
292 Guard (std::string
&) : which_ctor(6) { }
295 void operator=(Guard
const&);
300 const std::string cs
;
306 BOOST_TEST(0 == o
->which_ctor
);
310 BOOST_TEST(5 == o
->which_ctor
);
314 BOOST_TEST(6 == o
->which_ctor
);
317 void test_in_place_ctor()
319 const std::string cs
;
323 optional
<Guard
> o (in_place_init
);
325 BOOST_TEST(0 == o
->which_ctor
);
328 optional
<Guard
> o (in_place_init
, cs
);
330 BOOST_TEST(5 == o
->which_ctor
);
333 optional
<Guard
> o (in_place_init
, ms
);
335 BOOST_TEST(6 == o
->which_ctor
);
339 void test_in_place_if_ctor()
341 const std::string cs
;
345 optional
<Guard
> n (in_place_init_if
, false);
348 optional
<Guard
> o (in_place_init_if
, true);
350 BOOST_TEST(0 == o
->which_ctor
);
353 optional
<Guard
> n (in_place_init_if
, false, cs
);
356 optional
<Guard
> o (in_place_init_if
, true, cs
);
358 BOOST_TEST(5 == o
->which_ctor
);
361 optional
<Guard
> n (in_place_init_if
, false, ms
);
364 optional
<Guard
> o (in_place_init_if
, true, ms
);
366 BOOST_TEST(6 == o
->which_ctor
);
370 } // namespace no_rvalue_ref
374 #if (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES) && (!defined BOOST_NO_CXX11_VARIADIC_TEMPLATES)
376 test_in_place_ctor();
377 test_in_place_if_ctor();
379 #if (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
380 test_no_moves_on_emplacement();
381 test_no_moves_on_in_place_ctor();
383 test_clear_on_throw();
384 test_no_assignment_on_emplacement();
385 no_rvalue_refs::test_emplace();
386 no_rvalue_refs::test_in_place_ctor();
387 no_rvalue_refs::test_in_place_if_ctor();
389 return boost::report_errors();