mirror of
https://github.com/boostorg/optional.git
synced 2025-07-15 05:16:36 +02:00
more unit test for opt ref swap and abstract types
This commit is contained in:
@ -13,7 +13,12 @@
|
||||
|
||||
[heading Boost Release X.XX]
|
||||
|
||||
* Now `boost::optional` is specialized for reference parameters. This way the `sizeof` of optional reference is that of a pointer, and a number of bugs is avoided.
|
||||
* Now `boost::optional` is specialized for reference parameters. This addresses a couple of issues:
|
||||
* the `sizeof` of optional reference is that of a pointer,
|
||||
* some bugs connected to copying optional references are gone,
|
||||
* you can swap optional references: it is like swapping pointers: shalow, underlying objects are not affected,
|
||||
* optional references to abstract types work.
|
||||
* Documented nested typedefs ([@https://svn.boost.org/trac/boost/ticket/5193 Trac #5193]).
|
||||
|
||||
[heading Boost Release 1.60]
|
||||
|
||||
|
@ -31,11 +31,32 @@
|
||||
<span class="phrase"><a name="boost_optional.relnotes.boost_release_x_xx"></a></span><a class="link" href="relnotes.html#boost_optional.relnotes.boost_release_x_xx">Boost
|
||||
Release X.XX</a>
|
||||
</h4>
|
||||
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
|
||||
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
|
||||
<li class="listitem">
|
||||
Now <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">optional</span></code> is specialized for reference
|
||||
parameters. This way the <code class="computeroutput"><span class="keyword">sizeof</span></code>
|
||||
of optional reference is that of a pointer, and a number of bugs is avoided.
|
||||
</li></ul></div>
|
||||
parameters. This addresses a couple of issues:
|
||||
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: circle; ">
|
||||
<li class="listitem">
|
||||
the <code class="computeroutput"><span class="keyword">sizeof</span></code> of optional
|
||||
reference is that of a pointer,
|
||||
</li>
|
||||
<li class="listitem">
|
||||
some bugs connected to copying optional references are gone,
|
||||
</li>
|
||||
<li class="listitem">
|
||||
you can swap optional references: it is like swapping pointers: shalow,
|
||||
underlying objects are not affected,
|
||||
</li>
|
||||
<li class="listitem">
|
||||
optional references to abstract types work.
|
||||
</li>
|
||||
</ul></div>
|
||||
</li>
|
||||
<li class="listitem">
|
||||
Documented nested typedefs (<a href="https://svn.boost.org/trac/boost/ticket/5193" target="_top">Trac
|
||||
#5193</a>).
|
||||
</li>
|
||||
</ul></div>
|
||||
<h4>
|
||||
<a name="boost_optional.relnotes.h1"></a>
|
||||
<span class="phrase"><a name="boost_optional.relnotes.boost_release_1_60"></a></span><a class="link" href="relnotes.html#boost_optional.relnotes.boost_release_1_60">Boost
|
||||
|
@ -146,7 +146,7 @@
|
||||
</div>
|
||||
</div>
|
||||
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
|
||||
<td align="left"><p><small>Last revised: February 16, 2016 at 20:02:53 GMT</small></p></td>
|
||||
<td align="left"><p><small>Last revised: February 16, 2016 at 22:59:47 GMT</small></p></td>
|
||||
<td align="right"><div class="copyright-footer"></div></td>
|
||||
</tr></table>
|
||||
<hr>
|
||||
|
@ -30,7 +30,6 @@ import testing ;
|
||||
[ run optional_test_ref_convert_assign_const_int.cpp ]
|
||||
[ run optional_test_ref_portable_minimum.cpp ]
|
||||
[ run optional_test_ref_move.cpp ]
|
||||
[ run optional_test_ref_swap.cpp ]
|
||||
[ run optional_test_inplace_factory.cpp ]
|
||||
[ run optional_test_io.cpp ]
|
||||
[ run optional_test_move.cpp ]
|
||||
|
@ -38,7 +38,7 @@ void test_optional_ref_assignment()
|
||||
int main()
|
||||
{
|
||||
test_optional_ref_assignment<ScopeGuard>();
|
||||
//test_optional_ref_assignment<Abstract>();
|
||||
test_optional_ref_assignment<Abstract>();
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
||||
|
@ -33,7 +33,7 @@ void test_all_const_cases()
|
||||
int main()
|
||||
{
|
||||
test_all_const_cases<ScopeGuard>();
|
||||
//test_all_const_cases<Abstract>();
|
||||
test_all_const_cases<Abstract>();
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
||||
|
@ -101,7 +101,7 @@ int main()
|
||||
{
|
||||
test_all_const_cases<int>();
|
||||
test_all_const_cases<ScopeGuard>();
|
||||
//test_all_const_cases<Abstract>();
|
||||
test_all_const_cases<Abstract>();
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
||||
|
@ -256,11 +256,11 @@ void test_equality()
|
||||
BOOST_TEST( (o2 != oN));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
template <typename T, typename U = T>
|
||||
void test_order()
|
||||
{
|
||||
typename concrete_type_of<T>::type v1(1), v2(2), v2_(2), v3(3);
|
||||
optional<T&> o1(v1), o2(v2), o2_(v2_), o3(v3), o3_(v3), oN, oN_;
|
||||
optional<U&> o1(v1), o2(v2), o2_(v2_), o3(v3), o3_(v3), oN, oN_;
|
||||
// o2 and o2_ point to different objects; o3 and o3_ point to the same object
|
||||
|
||||
BOOST_TEST(!(oN < oN));
|
||||
@ -399,13 +399,35 @@ void test_order()
|
||||
BOOST_TEST(!(o3_ < oN_));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
template <typename T, typename U = T>
|
||||
void test_swap()
|
||||
{
|
||||
typename concrete_type_of<T>::type v1(1), v2(2);
|
||||
optional<T&> o1(v1), o1_(v1), o2(v2), o2_(v2), oN, oN_;
|
||||
optional<U&> o1(v1), o1_(v1), o2(v2), o2_(v2), oN, oN_;
|
||||
|
||||
// swap(o1, o1); DOESN'T WORK
|
||||
swap(o1, o1);
|
||||
BOOST_TEST(o1);
|
||||
BOOST_TEST(boost::addressof(*o1) == boost::addressof(v1));
|
||||
|
||||
swap(oN, oN_);
|
||||
BOOST_TEST(!oN);
|
||||
BOOST_TEST(!oN_);
|
||||
|
||||
swap(o1, oN);
|
||||
BOOST_TEST(!o1);
|
||||
BOOST_TEST(oN);
|
||||
BOOST_TEST(boost::addressof(*oN) == boost::addressof(v1));
|
||||
|
||||
swap(oN, o1);
|
||||
BOOST_TEST(!oN);
|
||||
BOOST_TEST(o1);
|
||||
BOOST_TEST(boost::addressof(*o1) == boost::addressof(v1));
|
||||
|
||||
swap(o1_, o2_);
|
||||
BOOST_TEST(o1_);
|
||||
BOOST_TEST(o2_);
|
||||
BOOST_TEST(boost::addressof(*o1_) == boost::addressof(v2));
|
||||
BOOST_TEST(boost::addressof(*o2_) == boost::addressof(v1));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
@ -431,18 +453,19 @@ void test_optional_const_ref()
|
||||
test_arrow_noconst_const<T>();
|
||||
test_equality<const T>();
|
||||
test_order<const T>();
|
||||
//test_swap<T>();
|
||||
test_swap<const T>();
|
||||
test_swap<T, const T>();
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
test_optional_ref<int>();
|
||||
test_optional_ref<ScopeGuard>();
|
||||
//test_optional_ref<Abstract>();
|
||||
test_optional_ref<Abstract>();
|
||||
|
||||
test_optional_const_ref<int>();
|
||||
test_optional_const_ref<ScopeGuard>();
|
||||
//test_optional_const_ref<Abstract>();
|
||||
test_optional_const_ref<Abstract>();
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
||||
|
@ -1,106 +0,0 @@
|
||||
// Copyright (C) 2016 Andrzej Krzemienski.
|
||||
//
|
||||
// Use, modification, and distribution is subject to the Boost Software
|
||||
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
// See http://www.boost.org/lib/optional for documentation.
|
||||
//
|
||||
// You are welcome to contact the maintainer at: akrzemi1@gmail.com
|
||||
|
||||
|
||||
#include "boost/optional/optional.hpp"
|
||||
|
||||
#ifdef __BORLANDC__
|
||||
#pragma hdrstop
|
||||
#endif
|
||||
|
||||
#include <string>
|
||||
#include "boost/core/addressof.hpp"
|
||||
#include "boost/core/lightweight_test.hpp"
|
||||
|
||||
using boost::optional;
|
||||
|
||||
struct Iface
|
||||
{
|
||||
virtual void fun() = 0;
|
||||
protected: ~Iface() {}
|
||||
};
|
||||
|
||||
struct Impl : Iface
|
||||
{
|
||||
void fun() {}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
void test_swap_empty_with_empty()
|
||||
{
|
||||
boost::optional<T&> o1, o2;
|
||||
swap(o1, o2);
|
||||
|
||||
BOOST_TEST(!o1);
|
||||
BOOST_TEST(!o2);
|
||||
}
|
||||
|
||||
template <typename T, typename U>
|
||||
void test_swap_value_with_empty(U v1)
|
||||
{
|
||||
boost::optional<T&> o1 (v1), o2;
|
||||
swap(o1, o2);
|
||||
|
||||
BOOST_TEST(!o1);
|
||||
BOOST_TEST(o2);
|
||||
BOOST_TEST(boost::addressof(*o2) == boost::addressof(v1));
|
||||
}
|
||||
|
||||
template <typename T, typename U>
|
||||
void test_swap_empty_with_value(U v2)
|
||||
{
|
||||
boost::optional<T&> o1, o2(v2);
|
||||
swap(o1, o2);
|
||||
|
||||
BOOST_TEST(o1);
|
||||
BOOST_TEST(boost::addressof(*o1) == boost::addressof(v2));
|
||||
BOOST_TEST(!o2);
|
||||
}
|
||||
|
||||
template <typename T, typename U>
|
||||
void test_swap_value_with_value(U v1, U v2)
|
||||
{
|
||||
boost::optional<T&> o1(v1), o2(v2);
|
||||
swap(o1, o2);
|
||||
|
||||
BOOST_TEST(o1);
|
||||
BOOST_TEST(o2);
|
||||
BOOST_TEST(boost::addressof(*o1) == boost::addressof(v2));
|
||||
BOOST_TEST(boost::addressof(*o2) == boost::addressof(v1));
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
std::string s1 ("AAA"), s2 ("BB");
|
||||
int i1 = 1, i2 = 2;
|
||||
Impl p1, p2;
|
||||
|
||||
test_swap_empty_with_empty<std::string>( );
|
||||
test_swap_value_with_empty<std::string>(s1 );
|
||||
test_swap_empty_with_value<std::string>( s2);
|
||||
test_swap_value_with_value<std::string>(s1, s2);
|
||||
|
||||
test_swap_empty_with_empty<int>( );
|
||||
test_swap_value_with_empty<int>(i1 );
|
||||
test_swap_empty_with_value<int>( i2);
|
||||
test_swap_value_with_value<int>(i1, i2);
|
||||
|
||||
test_swap_empty_with_empty<Iface>( );
|
||||
test_swap_value_with_empty<Iface>(p1 );
|
||||
test_swap_empty_with_value<Iface>( p2);
|
||||
test_swap_value_with_value<Iface>(p1, p2);
|
||||
|
||||
test_swap_empty_with_empty<const Iface>( );
|
||||
test_swap_value_with_empty<const Iface>(p1 );
|
||||
test_swap_empty_with_value<const Iface>( p2);
|
||||
test_swap_value_with_value<const Iface>(p1, p2);
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
Reference in New Issue
Block a user