diff --git a/test/Jamfile b/test/Jamfile index 1fd8060..dcb4391 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -34,12 +34,11 @@ DEPENDS all : test ; [ 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_fail3b.cpp ] - [ compile-fail libs/optional/test/optional_test_references_fail1a.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_fail1.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_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_fail2.cpp ] ; diff --git a/test/optional_test.cpp b/test/optional_test.cpp index ced8a47..c102975 100644 --- a/test/optional_test.cpp +++ b/test/optional_test.cpp @@ -24,9 +24,11 @@ #pragma hdrstop #endif +#include "boost/utility/none.hpp" + #include "boost/test/minimal.hpp" -#include "optional_test_common.cpp" +#include "optional_test_common.cpp" void test_implicit_construction ( optional opt, double v, double z ) { @@ -35,7 +37,17 @@ void test_implicit_construction ( optional opt, double v, double z ) void test_implicit_construction ( optional opt, X const& v, X const& z ) { - check_value(opt,v,z); + check_value(opt,v,z); +} + +void test_default_implicit_construction ( double, optional opt ) +{ + BOOST_CHECK(!opt); +} + +void test_default_implicit_construction ( X const&, optional opt ) +{ + BOOST_CHECK(!opt); } // @@ -619,7 +631,7 @@ void test_relops( T const* ) // Check when both are uininitalized. 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 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 ) ; @@ -639,7 +651,7 @@ void test_relops( T const* ) BOOST_CHECK ( opt0 > def0 ) ; BOOST_CHECK ( !(opt0 <= def0) ) ; BOOST_CHECK ( opt0 >= opt0 ) ; - + // If both are initialized, values are compared BOOST_CHECK ( opt0 != opt1 ) ; BOOST_CHECK ( opt1 == opt2 ) ; @@ -649,6 +661,27 @@ void test_relops( T const* ) BOOST_CHECK ( opt1 >= opt0 ) ; } +template +void test_none( T const* ) +{ + TRACE( std::endl << BOOST_CURRENT_FUNCTION ); + + using boost::none ; + + optional def0 ; + optional def1(none) ; + optional 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() { TRACE( std::endl << BOOST_CURRENT_FUNCTION ); @@ -657,6 +690,7 @@ void test_with_builtin_types() test_uninitialized_access( ARG(double) ); test_no_throwing_swap( ARG(double) ); test_relops( ARG(double) ) ; + test_none( ARG(double) ) ; } void test_with_class_type() @@ -675,9 +709,11 @@ void test_with_class_type() test_no_throwing_swap( ARG(X) ); test_throwing_swap( ARG(X) ); test_relops( ARG(X) ) ; + test_none( ARG(X) ) ; BOOST_CHECK ( X::count == 0 ) ; } +int eat ( bool ) { return 1 ; } int eat ( char ) { return 1 ; } int eat ( int ) { return 1 ; } int eat ( void const* ) { return 1 ; } @@ -700,10 +736,12 @@ void test_no_implicit_conversions() { TRACE( std::endl << BOOST_CURRENT_FUNCTION ); + bool b = false ; char c = 0 ; int i = 0 ; void const* p = 0 ; + test_no_implicit_conversions_impl(b); test_no_implicit_conversions_impl(c); test_no_implicit_conversions_impl(i); test_no_implicit_conversions_impl(p); diff --git a/test/optional_test_inplace.cpp b/test/optional_test_inplace.cpp index 23b7a9d..2eb0113 100644 --- a/test/optional_test_inplace.cpp +++ b/test/optional_test_inplace.cpp @@ -43,21 +43,28 @@ struct A int test_main( int, char* [] ) { - double a0 = 3.14 ; - std::string a1("pi"); + double a00 = 3.14, a10 = 6.02e-23; + std::string a01("pi"), a11("mol"); - A a(a0,a1); + A a0(a00,a01); + A a1(a10,a11); + + boost::optional opt1(a0); - boost::optional opt1(a); + boost::optional opt2 ( boost::in_place(a00,a01) ) ; - boost::optional opt2 ( boost::in_place(a0,a1) ) ; - - boost::optional opt3 ( boost::in_place(a0,a1) ) ; + boost::optional opt3 ( boost::in_place(a00,a01) ) ; BOOST_CHECK( opt1 == 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(a10,a11); + BOOST_CHECK( *opt3 == a1 ) ; + return 0; } diff --git a/test/optional_test_references.cpp b/test/optional_test_references.cpp index 6acd930..510446b 100644 --- a/test/optional_test_references.cpp +++ b/test/optional_test_references.cpp @@ -24,6 +24,8 @@ #pragma hdrstop #endif +#include "boost/utility/none.hpp" + #include "boost/test/minimal.hpp" #include "optional_test_common.cpp" @@ -106,7 +108,7 @@ void test_basics( T const* ) T z(0); T a(1); - + T& aref = a ; // Default construction. @@ -133,13 +135,63 @@ void test_basics( T const* ) check_ref_value_const(oa2,a,z); T b(2); + optional 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. // T::T ( T const& x ) is NOT used becasue the optional holds a reference. set_pending_copy( ARG(T) ) ; - optional ob = b ; + optional const oa3 = b ; check_is_pending_copy( ARG(T) ) ; - check_ref_initialized_const(ob); - check_ref_value_const(ob,b,z); + check_ref_initialized_const(oa3); + 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 // T::~T() is NOT used becasue the optional holds a reference. @@ -215,12 +267,34 @@ void test_relops( T const* ) BOOST_CHECK ( opt1 >= opt0 ) ; } +template +void test_none( T const* ) +{ + TRACE( std::endl << BOOST_CURRENT_FUNCTION ); + + using boost::none ; + + T a(1234); + + optional def0 ; + optional def1(none) ; + optional 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() { TRACE( std::endl << BOOST_CURRENT_FUNCTION ); test_basics( ARG(double) ); test_relops( ARG(double) ) ; + test_none ( ARG(double) ) ; } void test_with_class_type() @@ -229,6 +303,7 @@ void test_with_class_type() test_basics( ARG(X) ); test_relops( ARG(X) ) ; + test_none ( ARG(X) ) ; BOOST_CHECK ( X::count == 0 ) ; } diff --git a/test/optional_test_references_fail1.cpp b/test/optional_test_references_fail1.cpp new file mode 100644 index 0000000..27ec6e3 --- /dev/null +++ b/test/optional_test_references_fail1.cpp @@ -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 opt ; + short v = 1 ; + short& r = v ; + opt = r ; +} + + diff --git a/test/optional_test_references_fail5.cpp b/test/optional_test_references_fail5.cpp new file mode 100644 index 0000000..51f748b --- /dev/null +++ b/test/optional_test_references_fail5.cpp @@ -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 opt(r) ; +} + +