forked from boostorg/optional
Tests updated to reflect 1.15 changes
[SVN r20512]
This commit is contained in:
@ -34,12 +34,11 @@ DEPENDS all : test ;
|
|||||||
[ compile-fail libs/optional/test/optional_test_fail2.cpp ]
|
[ compile-fail libs/optional/test/optional_test_fail2.cpp ]
|
||||||
[ compile-fail libs/optional/test/optional_test_fail3a.cpp ]
|
[ compile-fail libs/optional/test/optional_test_fail3a.cpp ]
|
||||||
[ compile-fail libs/optional/test/optional_test_fail3b.cpp ]
|
[ compile-fail libs/optional/test/optional_test_fail3b.cpp ]
|
||||||
[ compile-fail libs/optional/test/optional_test_references_fail1a.cpp ]
|
[ compile-fail libs/optional/test/optional_test_references_fail1.cpp ]
|
||||||
[ compile-fail libs/optional/test/optional_test_references_fail1b.cpp ]
|
|
||||||
[ compile-fail libs/optional/test/optional_test_references_fail1c.cpp ]
|
|
||||||
[ compile-fail libs/optional/test/optional_test_references_fail2.cpp ]
|
[ compile-fail libs/optional/test/optional_test_references_fail2.cpp ]
|
||||||
[ compile-fail libs/optional/test/optional_test_references_fail3.cpp ]
|
[ compile-fail libs/optional/test/optional_test_references_fail3.cpp ]
|
||||||
[ compile-fail libs/optional/test/optional_test_references_fail4.cpp ]
|
[ compile-fail libs/optional/test/optional_test_references_fail4.cpp ]
|
||||||
|
[ compile-fail libs/optional/test/optional_test_references_fail5.cpp ]
|
||||||
[ compile-fail libs/optional/test/optional_test_inplace_fail.cpp ]
|
[ compile-fail libs/optional/test/optional_test_inplace_fail.cpp ]
|
||||||
[ compile-fail libs/optional/test/optional_test_inplace_fail2.cpp ]
|
[ compile-fail libs/optional/test/optional_test_inplace_fail2.cpp ]
|
||||||
;
|
;
|
||||||
|
@ -24,9 +24,11 @@
|
|||||||
#pragma hdrstop
|
#pragma hdrstop
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include "boost/utility/none.hpp"
|
||||||
|
|
||||||
#include "boost/test/minimal.hpp"
|
#include "boost/test/minimal.hpp"
|
||||||
|
|
||||||
#include "optional_test_common.cpp"
|
#include "optional_test_common.cpp"
|
||||||
|
|
||||||
void test_implicit_construction ( optional<double> opt, double v, double z )
|
void test_implicit_construction ( optional<double> opt, double v, double z )
|
||||||
{
|
{
|
||||||
@ -35,7 +37,17 @@ void test_implicit_construction ( optional<double> opt, double v, double z )
|
|||||||
|
|
||||||
void test_implicit_construction ( optional<X> opt, X const& v, X const& z )
|
void test_implicit_construction ( optional<X> opt, X const& v, X const& z )
|
||||||
{
|
{
|
||||||
check_value(opt,v,z);
|
check_value(opt,v,z);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_default_implicit_construction ( double, optional<double> opt )
|
||||||
|
{
|
||||||
|
BOOST_CHECK(!opt);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_default_implicit_construction ( X const&, optional<X> opt )
|
||||||
|
{
|
||||||
|
BOOST_CHECK(!opt);
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
@ -619,7 +631,7 @@ void test_relops( T const* )
|
|||||||
// Check when both are uininitalized.
|
// Check when both are uininitalized.
|
||||||
BOOST_CHECK ( def0 == def1 ) ; // both uninitialized compare equal
|
BOOST_CHECK ( def0 == def1 ) ; // both uninitialized compare equal
|
||||||
BOOST_CHECK ( !(def0 < def1) ) ; // uninitialized is never less than uninitialized
|
BOOST_CHECK ( !(def0 < def1) ) ; // uninitialized is never less than uninitialized
|
||||||
BOOST_CHECK ( !(def0 > def1) ) ; // uninitialized is never greater than uninitialized
|
BOOST_CHECK ( !(def0 > def1) ) ; // uninitialized is never greater than uninitialized
|
||||||
BOOST_CHECK ( !(def0 != def1) ) ;
|
BOOST_CHECK ( !(def0 != def1) ) ;
|
||||||
BOOST_CHECK ( def0 <= def1 ) ;
|
BOOST_CHECK ( def0 <= def1 ) ;
|
||||||
BOOST_CHECK ( def0 >= def1 ) ;
|
BOOST_CHECK ( def0 >= def1 ) ;
|
||||||
@ -639,7 +651,7 @@ void test_relops( T const* )
|
|||||||
BOOST_CHECK ( opt0 > def0 ) ;
|
BOOST_CHECK ( opt0 > def0 ) ;
|
||||||
BOOST_CHECK ( !(opt0 <= def0) ) ;
|
BOOST_CHECK ( !(opt0 <= def0) ) ;
|
||||||
BOOST_CHECK ( opt0 >= opt0 ) ;
|
BOOST_CHECK ( opt0 >= opt0 ) ;
|
||||||
|
|
||||||
// If both are initialized, values are compared
|
// If both are initialized, values are compared
|
||||||
BOOST_CHECK ( opt0 != opt1 ) ;
|
BOOST_CHECK ( opt0 != opt1 ) ;
|
||||||
BOOST_CHECK ( opt1 == opt2 ) ;
|
BOOST_CHECK ( opt1 == opt2 ) ;
|
||||||
@ -649,6 +661,27 @@ void test_relops( T const* )
|
|||||||
BOOST_CHECK ( opt1 >= opt0 ) ;
|
BOOST_CHECK ( opt1 >= opt0 ) ;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
void test_none( T const* )
|
||||||
|
{
|
||||||
|
TRACE( std::endl << BOOST_CURRENT_FUNCTION );
|
||||||
|
|
||||||
|
using boost::none ;
|
||||||
|
|
||||||
|
optional<T> def0 ;
|
||||||
|
optional<T> def1(none) ;
|
||||||
|
optional<T> non_def( T(1234) ) ;
|
||||||
|
|
||||||
|
BOOST_CHECK ( def0 == none ) ;
|
||||||
|
BOOST_CHECK ( non_def != none ) ;
|
||||||
|
BOOST_CHECK ( !def1 ) ;
|
||||||
|
|
||||||
|
non_def = none ;
|
||||||
|
BOOST_CHECK ( !non_def ) ;
|
||||||
|
|
||||||
|
test_default_implicit_construction(T(1),none);
|
||||||
|
}
|
||||||
|
|
||||||
void test_with_builtin_types()
|
void test_with_builtin_types()
|
||||||
{
|
{
|
||||||
TRACE( std::endl << BOOST_CURRENT_FUNCTION );
|
TRACE( std::endl << BOOST_CURRENT_FUNCTION );
|
||||||
@ -657,6 +690,7 @@ void test_with_builtin_types()
|
|||||||
test_uninitialized_access( ARG(double) );
|
test_uninitialized_access( ARG(double) );
|
||||||
test_no_throwing_swap( ARG(double) );
|
test_no_throwing_swap( ARG(double) );
|
||||||
test_relops( ARG(double) ) ;
|
test_relops( ARG(double) ) ;
|
||||||
|
test_none( ARG(double) ) ;
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_with_class_type()
|
void test_with_class_type()
|
||||||
@ -675,9 +709,11 @@ void test_with_class_type()
|
|||||||
test_no_throwing_swap( ARG(X) );
|
test_no_throwing_swap( ARG(X) );
|
||||||
test_throwing_swap( ARG(X) );
|
test_throwing_swap( ARG(X) );
|
||||||
test_relops( ARG(X) ) ;
|
test_relops( ARG(X) ) ;
|
||||||
|
test_none( ARG(X) ) ;
|
||||||
BOOST_CHECK ( X::count == 0 ) ;
|
BOOST_CHECK ( X::count == 0 ) ;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int eat ( bool ) { return 1 ; }
|
||||||
int eat ( char ) { return 1 ; }
|
int eat ( char ) { return 1 ; }
|
||||||
int eat ( int ) { return 1 ; }
|
int eat ( int ) { return 1 ; }
|
||||||
int eat ( void const* ) { return 1 ; }
|
int eat ( void const* ) { return 1 ; }
|
||||||
@ -700,10 +736,12 @@ void test_no_implicit_conversions()
|
|||||||
{
|
{
|
||||||
TRACE( std::endl << BOOST_CURRENT_FUNCTION );
|
TRACE( std::endl << BOOST_CURRENT_FUNCTION );
|
||||||
|
|
||||||
|
bool b = false ;
|
||||||
char c = 0 ;
|
char c = 0 ;
|
||||||
int i = 0 ;
|
int i = 0 ;
|
||||||
void const* p = 0 ;
|
void const* p = 0 ;
|
||||||
|
|
||||||
|
test_no_implicit_conversions_impl(b);
|
||||||
test_no_implicit_conversions_impl(c);
|
test_no_implicit_conversions_impl(c);
|
||||||
test_no_implicit_conversions_impl(i);
|
test_no_implicit_conversions_impl(i);
|
||||||
test_no_implicit_conversions_impl(p);
|
test_no_implicit_conversions_impl(p);
|
||||||
|
@ -43,21 +43,28 @@ struct A
|
|||||||
|
|
||||||
int test_main( int, char* [] )
|
int test_main( int, char* [] )
|
||||||
{
|
{
|
||||||
double a0 = 3.14 ;
|
double a00 = 3.14, a10 = 6.02e-23;
|
||||||
std::string a1("pi");
|
std::string a01("pi"), a11("mol");
|
||||||
|
|
||||||
A a(a0,a1);
|
A a0(a00,a01);
|
||||||
|
A a1(a10,a11);
|
||||||
|
|
||||||
|
boost::optional<A> opt1(a0);
|
||||||
|
|
||||||
boost::optional<A> opt1(a);
|
boost::optional<A> opt2 ( boost::in_place(a00,a01) ) ;
|
||||||
|
|
||||||
boost::optional<A> opt2 ( boost::in_place(a0,a1) ) ;
|
boost::optional<A> opt3 ( boost::in_place<A>(a00,a01) ) ;
|
||||||
|
|
||||||
boost::optional<A> opt3 ( boost::in_place<A>(a0,a1) ) ;
|
|
||||||
|
|
||||||
BOOST_CHECK( opt1 == opt2 ) ;
|
BOOST_CHECK( opt1 == opt2 ) ;
|
||||||
BOOST_CHECK( opt2 == opt2 ) ;
|
BOOST_CHECK( opt2 == opt2 ) ;
|
||||||
BOOST_CHECK( *opt2 == a ) ;
|
BOOST_CHECK( *opt2 == a0 ) ;
|
||||||
|
|
||||||
|
opt2 = boost::in_place(a10,a11);
|
||||||
|
BOOST_CHECK( *opt2 == a1 ) ;
|
||||||
|
|
||||||
|
opt3 = boost::in_place<A>(a10,a11);
|
||||||
|
BOOST_CHECK( *opt3 == a1 ) ;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,6 +24,8 @@
|
|||||||
#pragma hdrstop
|
#pragma hdrstop
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include "boost/utility/none.hpp"
|
||||||
|
|
||||||
#include "boost/test/minimal.hpp"
|
#include "boost/test/minimal.hpp"
|
||||||
|
|
||||||
#include "optional_test_common.cpp"
|
#include "optional_test_common.cpp"
|
||||||
@ -106,7 +108,7 @@ void test_basics( T const* )
|
|||||||
T z(0);
|
T z(0);
|
||||||
|
|
||||||
T a(1);
|
T a(1);
|
||||||
|
|
||||||
T& aref = a ;
|
T& aref = a ;
|
||||||
|
|
||||||
// Default construction.
|
// Default construction.
|
||||||
@ -133,13 +135,63 @@ void test_basics( T const* )
|
|||||||
check_ref_value_const(oa2,a,z);
|
check_ref_value_const(oa2,a,z);
|
||||||
|
|
||||||
T b(2);
|
T b(2);
|
||||||
|
optional<T&> ob ;
|
||||||
|
|
||||||
|
// Value-Assignment upon Uninitialized optional.
|
||||||
|
// T::T ( T const& x ) is NOT used becasue the optional holds a reference.
|
||||||
|
set_pending_copy( ARG(T) ) ;
|
||||||
|
ob = a ;
|
||||||
|
check_is_pending_copy( ARG(T) ) ;
|
||||||
|
check_ref_initialized(ob);
|
||||||
|
check_ref_value(ob,a,z);
|
||||||
|
|
||||||
|
// Value-Assignment upon Initialized optional.
|
||||||
|
// T::T ( T const& x ) is NOT used becasue the optional holds a reference.
|
||||||
|
set_pending_dtor( ARG(T) ) ;
|
||||||
|
set_pending_copy( ARG(T) ) ;
|
||||||
|
ob = b ;
|
||||||
|
check_is_pending_dtor( ARG(T) ) ;
|
||||||
|
check_is_pending_copy( ARG(T) ) ;
|
||||||
|
check_ref_initialized(ob);
|
||||||
|
check_ref_value(ob,b,z);
|
||||||
|
|
||||||
// Assignment initialization.
|
// Assignment initialization.
|
||||||
// T::T ( T const& x ) is NOT used becasue the optional holds a reference.
|
// T::T ( T const& x ) is NOT used becasue the optional holds a reference.
|
||||||
set_pending_copy( ARG(T) ) ;
|
set_pending_copy( ARG(T) ) ;
|
||||||
optional<T&> ob = b ;
|
optional<T&> const oa3 = b ;
|
||||||
check_is_pending_copy( ARG(T) ) ;
|
check_is_pending_copy( ARG(T) ) ;
|
||||||
check_ref_initialized_const(ob);
|
check_ref_initialized_const(oa3);
|
||||||
check_ref_value_const(ob,b,z);
|
check_ref_value_const(oa3,b,z);
|
||||||
|
|
||||||
|
|
||||||
|
// Assignment
|
||||||
|
// T::~T() is used to destroy previous value in ob.
|
||||||
|
// T::T ( T const& x ) is NOT used becasue the optional holds a reference.
|
||||||
|
set_pending_dtor( ARG(T) ) ;
|
||||||
|
set_pending_copy( ARG(T) ) ;
|
||||||
|
oa = ob ;
|
||||||
|
check_is_pending_dtor( ARG(T) ) ;
|
||||||
|
check_is_pending_copy( ARG(T) ) ;
|
||||||
|
check_ref_initialized(oa);
|
||||||
|
check_ref_value(oa,b,z);
|
||||||
|
|
||||||
|
// Uninitializing Assignment upon Initialized Optional
|
||||||
|
// T::~T() is NOT used becasue the optional holds a reference.
|
||||||
|
set_pending_dtor( ARG(T) ) ;
|
||||||
|
set_pending_copy( ARG(T) ) ;
|
||||||
|
oa = def ;
|
||||||
|
check_is_pending_dtor( ARG(T) ) ;
|
||||||
|
check_is_pending_copy( ARG(T) ) ;
|
||||||
|
check_ref_uninitialized(oa);
|
||||||
|
|
||||||
|
// Uninitializing Assignment upon Uninitialized Optional
|
||||||
|
// (Dtor is not called this time)
|
||||||
|
set_pending_dtor( ARG(T) ) ;
|
||||||
|
set_pending_copy( ARG(T) ) ;
|
||||||
|
oa = def ;
|
||||||
|
check_is_pending_dtor( ARG(T) ) ;
|
||||||
|
check_is_pending_copy( ARG(T) ) ;
|
||||||
|
check_ref_uninitialized(oa);
|
||||||
|
|
||||||
// Deinitialization of Initialized Optional
|
// Deinitialization of Initialized Optional
|
||||||
// T::~T() is NOT used becasue the optional holds a reference.
|
// T::~T() is NOT used becasue the optional holds a reference.
|
||||||
@ -215,12 +267,34 @@ void test_relops( T const* )
|
|||||||
BOOST_CHECK ( opt1 >= opt0 ) ;
|
BOOST_CHECK ( opt1 >= opt0 ) ;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
void test_none( T const* )
|
||||||
|
{
|
||||||
|
TRACE( std::endl << BOOST_CURRENT_FUNCTION );
|
||||||
|
|
||||||
|
using boost::none ;
|
||||||
|
|
||||||
|
T a(1234);
|
||||||
|
|
||||||
|
optional<T&> def0 ;
|
||||||
|
optional<T&> def1(none) ;
|
||||||
|
optional<T&> non_def(a) ;
|
||||||
|
|
||||||
|
BOOST_CHECK ( def0 == none ) ;
|
||||||
|
BOOST_CHECK ( non_def != none ) ;
|
||||||
|
BOOST_CHECK ( !def1 ) ;
|
||||||
|
|
||||||
|
non_def = none ;
|
||||||
|
BOOST_CHECK ( !non_def ) ;
|
||||||
|
}
|
||||||
|
|
||||||
void test_with_builtin_types()
|
void test_with_builtin_types()
|
||||||
{
|
{
|
||||||
TRACE( std::endl << BOOST_CURRENT_FUNCTION );
|
TRACE( std::endl << BOOST_CURRENT_FUNCTION );
|
||||||
|
|
||||||
test_basics( ARG(double) );
|
test_basics( ARG(double) );
|
||||||
test_relops( ARG(double) ) ;
|
test_relops( ARG(double) ) ;
|
||||||
|
test_none ( ARG(double) ) ;
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_with_class_type()
|
void test_with_class_type()
|
||||||
@ -229,6 +303,7 @@ void test_with_class_type()
|
|||||||
|
|
||||||
test_basics( ARG(X) );
|
test_basics( ARG(X) );
|
||||||
test_relops( ARG(X) ) ;
|
test_relops( ARG(X) ) ;
|
||||||
|
test_none ( ARG(X) ) ;
|
||||||
|
|
||||||
BOOST_CHECK ( X::count == 0 ) ;
|
BOOST_CHECK ( X::count == 0 ) ;
|
||||||
}
|
}
|
||||||
|
28
test/optional_test_references_fail1.cpp
Normal file
28
test/optional_test_references_fail1.cpp
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
// (C) 2003, Fernando Luis Cacciola Carballal.
|
||||||
|
//
|
||||||
|
// This material is provided "as is", with absolutely no warranty expressed
|
||||||
|
// or implied. Any use is at your own risk.
|
||||||
|
//
|
||||||
|
// Permission to use or copy this software for any purpose is hereby granted
|
||||||
|
// without fee, provided the above notices are retained on all copies.
|
||||||
|
// Permission to modify the code and to distribute modified code is granted,
|
||||||
|
// provided the above notices are retained, and a notice that the code was
|
||||||
|
// modified is included with the above copyright notice.
|
||||||
|
//
|
||||||
|
// You are welcome to contact the author at:
|
||||||
|
// fernando_cacciola@hotmail.com
|
||||||
|
//
|
||||||
|
#include "boost/optional.hpp"
|
||||||
|
|
||||||
|
//
|
||||||
|
// THIS TEST SHOULD FAIL TO COMPILE
|
||||||
|
//
|
||||||
|
void optional_reference__test_no_converting_assignment()
|
||||||
|
{
|
||||||
|
boost::optional<int&> opt ;
|
||||||
|
short v = 1 ;
|
||||||
|
short& r = v ;
|
||||||
|
opt = r ;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
27
test/optional_test_references_fail5.cpp
Normal file
27
test/optional_test_references_fail5.cpp
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
// (C) 2003, Fernando Luis Cacciola Carballal.
|
||||||
|
//
|
||||||
|
// This material is provided "as is", with absolutely no warranty expressed
|
||||||
|
// or implied. Any use is at your own risk.
|
||||||
|
//
|
||||||
|
// Permission to use or copy this software for any purpose is hereby granted
|
||||||
|
// without fee, provided the above notices are retained on all copies.
|
||||||
|
// Permission to modify the code and to distribute modified code is granted,
|
||||||
|
// provided the above notices are retained, and a notice that the code was
|
||||||
|
// modified is included with the above copyright notice.
|
||||||
|
//
|
||||||
|
// You are welcome to contact the author at:
|
||||||
|
// fernando_cacciola@hotmail.com
|
||||||
|
//
|
||||||
|
#include "boost/optional.hpp"
|
||||||
|
|
||||||
|
//
|
||||||
|
// THIS TEST SHOULD FAIL TO COMPILE
|
||||||
|
//
|
||||||
|
void optional_reference__test_no_converting_initialization()
|
||||||
|
{
|
||||||
|
short v = 1 ;
|
||||||
|
short& r = v;
|
||||||
|
boost::optional<int&> opt(r) ;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
Reference in New Issue
Block a user