diff --git a/test/optional_test_common.cpp b/test/optional_test_common.cpp new file mode 100644 index 0000000..ad865ee --- /dev/null +++ b/test/optional_test_common.cpp @@ -0,0 +1,246 @@ +// (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 +// + + +#ifdef ENABLE_TRACE +#define TRACE(msg) std::cout << msg << std::endl ; +#else +#define TRACE(msg) +#endif + +namespace boost { + +void assertion_failed (char const * expr, char const * func, char const * file, long ) +{ + using std::string ; + string msg = string("Boost assertion failure for \"") + + string(expr) + + string("\" at file \"") + + string(file) + + string("\" function \"") + + string(func) + + string("\"") ; + + TRACE(msg); + + throw std::logic_error(msg); +} + +} + +using boost::optional ; + +template inline void unused_variable ( T ) {} + +#ifdef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP +using boost::swap ; +using boost::get ; +using boost::get_pointer ; +#endif + +// MSVC6.0 does not support comparisons of optional against a literal null pointer value (0) +// via the safe_bool operator. +#if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1300) ) // 1300 == VC++ 7.1 +#define BOOST_OPTIONAL_NO_NULL_COMPARE +#endif + +#define ARG(T) (static_cast< T const* >(0)) + +// +// Helper class used to verify the lifetime managment of the values held by optional +// +class X +{ + public : + + X ( int av ) : v(av) + { + ++ count ; + + TRACE ( "X::X(" << av << "). this=" << this ) ; + } + + X ( X const& rhs ) : v(rhs.v) + { + pending_copy = false ; + + TRACE ( "X::X( X const& rhs). this=" << this << " rhs.v=" << rhs.v ) ; + + if ( throw_on_copy ) + { + TRACE ( "throwing exception in X's copy ctor" ) ; + throw 0 ; + } + + ++ count ; + } + + ~X() + { + pending_dtor = false ; + + -- count ; + + TRACE ( "X::~X(). v=" << v << " this=" << this ); + } + + X& operator= ( X const& rhs ) + { + v = rhs.v ; + + TRACE ( "X::operator =( X const& rhs). this=" << this << " rhs.v=" << rhs.v ) ; + + return *this ; + } + + friend bool operator == ( X const& a, X const& b ) + { return a.v == b.v ; } + + friend bool operator != ( X const& a, X const& b ) + { return a.v != b.v ; } + + friend bool operator < ( X const& a, X const& b ) + { return a.v < b.v ; } + + int V() const { return v ; } + int& V() { return v ; } + + static int count ; + static bool pending_copy ; + static bool pending_dtor ; + static bool throw_on_copy ; + + private : + + int v ; + + private : + + X() ; +} ; + + +int X::count = 0 ; +bool X::pending_copy = false ; +bool X::pending_dtor = false ; +bool X::throw_on_copy = false ; + +inline void set_pending_copy ( X const* x ) { X::pending_copy = true ; } +inline void set_pending_dtor ( X const* x ) { X::pending_dtor = true ; } +inline void set_throw_on_copy ( X const* x ) { X::throw_on_copy = true ; } +inline void reset_throw_on_copy ( X const* x ) { X::throw_on_copy = false ; } +inline void check_is_pending_copy ( X const* x ) { BOOST_CHECK( X::pending_copy ) ; } +inline void check_is_pending_dtor ( X const* x ) { BOOST_CHECK( X::pending_dtor ) ; } +inline void check_is_not_pending_copy( X const* x ) { BOOST_CHECK( !X::pending_copy ) ; } +inline void check_is_not_pending_dtor( X const* x ) { BOOST_CHECK( !X::pending_dtor ) ; } +inline void check_instance_count ( int c, X const* x ) { BOOST_CHECK( X::count == c ) ; } +inline int get_instance_count ( X const* x ) { return X::count ; } + +inline void set_pending_copy (...) {} +inline void set_pending_dtor (...) {} +inline void set_throw_on_copy (...) {} +inline void reset_throw_on_copy (...) {} +inline void check_is_pending_copy (...) {} +inline void check_is_pending_dtor (...) {} +inline void check_is_not_pending_copy(...) {} +inline void check_is_not_pending_dtor(...) {} +inline void check_instance_count (...) {} +inline int get_instance_count (...) { return 0 ; } + + +template +inline void check_uninitialized_const ( optional const& opt ) +{ +#ifndef BOOST_OPTIONAL_NO_NULL_COMPARE + BOOST_CHECK( opt == 0 ) ; +#endif + BOOST_CHECK( !opt ) ; + BOOST_CHECK( !get_pointer(opt) ) ; + BOOST_CHECK( !opt.get_ptr() ) ; +} +template +inline void check_uninitialized ( optional& opt ) +{ +#ifndef BOOST_OPTIONAL_NO_NULL_COMPARE + BOOST_CHECK( opt == 0 ) ; +#endif + BOOST_CHECK( !opt ) ; + BOOST_CHECK( !get_pointer(opt) ) ; + BOOST_CHECK( !opt.get_ptr() ) ; + + check_uninitialized_const(opt); +} + +template +inline void check_initialized_const ( optional const& opt ) +{ + BOOST_CHECK( opt ) ; + +#ifndef BOOST_OPTIONAL_NO_NULL_COMPARE + BOOST_CHECK( opt != 0 ) ; +#endif + + BOOST_CHECK ( !!opt ) ; + BOOST_CHECK ( get_pointer(opt) ) ; + BOOST_CHECK ( opt.get_ptr() ) ; +} + +template +inline void check_initialized ( optional& opt ) +{ + BOOST_CHECK( opt ) ; + +#ifndef BOOST_OPTIONAL_NO_NULL_COMPARE + BOOST_CHECK( opt != 0 ) ; +#endif + + BOOST_CHECK ( !!opt ) ; + BOOST_CHECK ( get_pointer(opt) ) ; + BOOST_CHECK ( opt.get_ptr() ) ; + + check_initialized_const(opt); +} + +template +inline void check_value_const ( optional const& opt, T const& v, T const& z ) +{ + BOOST_CHECK( *opt == v ) ; + BOOST_CHECK( *opt != z ) ; + BOOST_CHECK( opt.get() == v ) ; + BOOST_CHECK( opt.get() != z ) ; + BOOST_CHECK( (*(opt.operator->()) == v) ) ; + BOOST_CHECK( *get_pointer(opt) == v ) ; +} + +template +inline void check_value ( optional& opt, T const& v, T const& z ) +{ +#if BOOST_WORKAROUND(BOOST_MSVC, <= 1200) // 1200 == VC++ 6.0 + // For some reason, VC6.0 is creating a temporary while evaluating (*opt == v), + // so we need to turn throw on copy off first. + reset_throw_on_copy( ARG(T) ) ; +#endif + + BOOST_CHECK( *opt == v ) ; + BOOST_CHECK( *opt != z ) ; + BOOST_CHECK( opt.get() == v ) ; + BOOST_CHECK( opt.get() != z ) ; + BOOST_CHECK( (*(opt.operator->()) == v) ) ; + BOOST_CHECK( *get_pointer(opt) == v ) ; + + check_value_const(opt,v,z); +} + + diff --git a/test/optional_test_fail2.cpp b/test/optional_test_fail2.cpp index 2ab17c6..c242315 100644 --- a/test/optional_test_fail2.cpp +++ b/test/optional_test_fail2.cpp @@ -17,10 +17,13 @@ // // THIS TEST SHOULD FAIL TO COMPILE // -void test_no_direct_value_assignment() +void test_no_implicit_conversion() { - boost::optional opt(3) ; - opt = 4 ; // Cannot assign "int" to "optional" + boost::optional opt(1) ; + + // You can compare against 0 or against another optional<>, + // but not against another value + if ( opt == 1 ) ; } diff --git a/test/optional_test_fail3a.cpp b/test/optional_test_fail3a.cpp new file mode 100644 index 0000000..771ef73 --- /dev/null +++ b/test/optional_test_fail3a.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 + +#include "boost/optional.hpp" + +// +// THIS TEST SHOULD FAIL TO COMPILE +// +void test_no_unsupported_conversion() +{ + boost::optional opt1(1) ; + boost::optional< std::string > opt2( opt1 ) ; // Cannot convert from "int" to "std::string" +} + + diff --git a/test/optional_test_fail3b.cpp b/test/optional_test_fail3b.cpp new file mode 100644 index 0000000..1d00d2b --- /dev/null +++ b/test/optional_test_fail3b.cpp @@ -0,0 +1,29 @@ +// (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 + +#include "boost/optional.hpp" + +// +// THIS TEST SHOULD FAIL TO COMPILE +// +void test_no_unsupported_conversion() +{ + boost::optional opt1(1) ; + boost::optional< std::string > opt2 ; + opt2 = opt1 ; // Cannot convert from "int" to "std::string" +} + + diff --git a/test/optional_test_inplace.cpp b/test/optional_test_inplace.cpp new file mode 100644 index 0000000..858045e --- /dev/null +++ b/test/optional_test_inplace.cpp @@ -0,0 +1,57 @@ +// (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 +#include +#include + +#define BOOST_ENABLE_ASSERT_HANDLER + +#include "boost/optional.hpp" +#include "boost/utility/in_place_factory.hpp" + +#ifdef __BORLANDC__ +#pragma hdrstop +#endif + +#include "boost/test/minimal.hpp" + +#include "optional_test_common.cpp" + +struct A +{ + A ( double a0, std::string a1 ) : m_a0(a0), m_a1(a1) {} + + friend bool operator == ( A const& x, A const& y ) + { return x.m_a0 == y.m_a0 && x.m_a1 == y.m_a1 ; } + + double m_a0 ; + std::string m_a1 ; +} ; + +int test_main( int, char* [] ) +{ + A a(3.14,"pi"); + + boost::optional opt1(a); + + boost::optional opt2 ( boost::in_place(3.14,"pi") ) ; + + BOOST_CHECK( opt1 == opt2 ) ; + BOOST_CHECK( *opt2 == a ) ; + + return 0; +} + + diff --git a/test/optional_test_inplace_fail.cpp b/test/optional_test_inplace_fail.cpp new file mode 100644 index 0000000..7a88365 --- /dev/null +++ b/test/optional_test_inplace_fail.cpp @@ -0,0 +1,51 @@ +// (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 +#include +#include + +#define BOOST_ENABLE_ASSERT_HANDLER + +#include "boost/optional.hpp" +#include "boost/utility/in_place_factory.hpp" + +#ifdef __BORLANDC__ +#pragma hdrstop +#endif + +#include "boost/test/minimal.hpp" + +#include "optional_test_common.cpp" + +struct A +{ + A ( double a0, std::string a1 ) : m_a0(a0), m_a1(a1) {} + + friend bool operator == ( A const& x, A const& y ) + { return x.m_a0 == y.m_a0 && x.m_a1 == y.m_a1 ; } + + double m_a0 ; + std::string m_a1 ; +} ; + +int test_main( int, char* [] ) +{ + int invalid_extra_parameter ; + boost::optional opt2 ( boost::in_place(3.14,"pi",invalid_extra_parameter) ) ; + + return 0; +} + + diff --git a/test/optional_test_references.cpp b/test/optional_test_references.cpp new file mode 100644 index 0000000..6acd930 --- /dev/null +++ b/test/optional_test_references.cpp @@ -0,0 +1,251 @@ +// (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 +#include +#include + +#define BOOST_ENABLE_ASSERT_HANDLER + +#include "boost/optional.hpp" + +#ifdef __BORLANDC__ +#pragma hdrstop +#endif + +#include "boost/test/minimal.hpp" + +#include "optional_test_common.cpp" + +template +inline void check_ref_uninitialized_const ( optional const& opt ) +{ +#ifndef BOOST_OPTIONAL_NO_NULL_COMPARE + BOOST_CHECK( opt == 0 ) ; +#endif + BOOST_CHECK( !opt ) ; +} +template +inline void check_ref_uninitialized ( optional& opt ) +{ +#ifndef BOOST_OPTIONAL_NO_NULL_COMPARE + BOOST_CHECK( opt == 0 ) ; +#endif + BOOST_CHECK( !opt ) ; + + check_ref_uninitialized_const(opt); +} + +template +inline void check_ref_initialized_const ( optional const& opt ) +{ + BOOST_CHECK( opt ) ; + +#ifndef BOOST_OPTIONAL_NO_NULL_COMPARE + BOOST_CHECK( opt != 0 ) ; +#endif + + BOOST_CHECK ( !!opt ) ; +} + +template +inline void check_ref_initialized ( optional& opt ) +{ + BOOST_CHECK( opt ) ; + +#ifndef BOOST_OPTIONAL_NO_NULL_COMPARE + BOOST_CHECK( opt != 0 ) ; +#endif + + BOOST_CHECK ( !!opt ) ; + + check_ref_initialized_const(opt); +} + +template +inline void check_ref_value_const ( optional const& opt, T const& v, T const& z ) +{ + BOOST_CHECK( *opt == v ) ; + BOOST_CHECK( *opt != z ) ; + BOOST_CHECK( opt.get() == v ) ; + BOOST_CHECK( opt.get() != z ) ; +} + +template +inline void check_ref_value ( optional& opt, T const& v, T const& z ) +{ + BOOST_CHECK( *opt == v ) ; + BOOST_CHECK( *opt != z ) ; + BOOST_CHECK( opt.get() == v ) ; + BOOST_CHECK( opt.get() != z ) ; + + check_ref_value_const(opt,v,z); +} + +// +// Basic test. +// Check ordinary functionality: +// Initialization, assignment, comparison and value-accessing. +// +template +void test_basics( T const* ) +{ + TRACE( std::endl << BOOST_CURRENT_FUNCTION ); + + T z(0); + + T a(1); + + T& aref = a ; + + // Default construction. + // 'def' state is Uninitialized. + // T::T() is not called + optional def ; + check_ref_uninitialized(def); + + // Direct initialization. + // 'oa' state is Initialized with 'a' + // T::T( T const& x ) is NOT used becasue the optional holds a reference. + set_pending_copy( ARG(T) ) ; + optional oa ( aref ) ; + check_is_pending_copy( ARG(T) ); + check_ref_initialized(oa); + check_ref_value(oa,a,z); + + // Copy initialization. + // T::T ( T const& x ) is NOT used becasue the optional holds a reference. + set_pending_copy( ARG(T) ) ; + optional const oa2 ( oa ) ; + check_is_pending_copy( ARG(T) ) ; + check_ref_initialized_const(oa2); + check_ref_value_const(oa2,a,z); + + T b(2); + // Assignment initialization. + // T::T ( T const& x ) is NOT used becasue the optional holds a reference. + set_pending_copy( ARG(T) ) ; + optional ob = b ; + check_is_pending_copy( ARG(T) ) ; + check_ref_initialized_const(ob); + check_ref_value_const(ob,b,z); + + // Deinitialization of Initialized Optional + // T::~T() is NOT used becasue the optional holds a reference. + set_pending_dtor( ARG(T) ) ; + ob.reset(); + check_is_pending_dtor( ARG(T) ) ; + check_ref_uninitialized(ob); + + // Deinitialization of Uninitialized Optional + // T::~T() is not called this time + set_pending_dtor( ARG(T) ) ; + ob.reset(); + check_is_pending_dtor( ARG(T) ) ; + check_ref_uninitialized(ob); +} + +// +// This verifies relational operators. +// +template +void test_relops( T const* ) +{ + TRACE( std::endl << BOOST_CURRENT_FUNCTION ); + + reset_throw_on_copy( ARG(T) ) ; + + T v0(18); + T v1(19); + T v2(19); + + optional def0 ; + optional def1 ; + optional opt0(v0); + optional opt1(v1); + optional opt2(v2); + + // Check identity + BOOST_CHECK ( def0 == def0 ) ; + BOOST_CHECK ( opt0 == opt0 ) ; + BOOST_CHECK ( !(def0 != def0) ) ; + BOOST_CHECK ( !(opt0 != opt0) ) ; + + // 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) ) ; + BOOST_CHECK ( def0 <= def1 ) ; + BOOST_CHECK ( def0 >= def1 ) ; + + // Check when only lhs is uninitialized. + BOOST_CHECK ( def0 != opt0 ) ; // uninitialized is never equal to initialized + BOOST_CHECK ( !(def0 == opt0) ) ; + BOOST_CHECK ( def0 < opt0 ) ; // uninitialized is always less than initialized + BOOST_CHECK ( !(def0 > opt0) ) ; + BOOST_CHECK ( def0 <= opt0 ) ; + BOOST_CHECK ( !(def0 >= opt0) ) ; + + // Check when only rhs is uninitialized. + BOOST_CHECK ( opt0 != def0 ) ; // initialized is never equal to uninitialized + BOOST_CHECK ( !(opt0 == def0) ) ; + BOOST_CHECK ( !(opt0 < def0) ) ; // initialized is never less than uninitialized + 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 ) ; + BOOST_CHECK ( opt0 < opt1 ) ; + BOOST_CHECK ( opt1 > opt0 ) ; + BOOST_CHECK ( opt1 <= opt2 ) ; + BOOST_CHECK ( opt1 >= opt0 ) ; +} + +void test_with_builtin_types() +{ + TRACE( std::endl << BOOST_CURRENT_FUNCTION ); + + test_basics( ARG(double) ); + test_relops( ARG(double) ) ; +} + +void test_with_class_type() +{ + TRACE( std::endl << BOOST_CURRENT_FUNCTION ); + + test_basics( ARG(X) ); + test_relops( ARG(X) ) ; + + BOOST_CHECK ( X::count == 0 ) ; +} + +int test_main( int, char* [] ) +{ + try + { + test_with_class_type(); + test_with_builtin_types(); + } + catch ( ... ) + { + BOOST_ERROR("Unexpected Exception caught!"); + } + + return 0; +} + + diff --git a/test/optional_test_references_fail1a.cpp b/test/optional_test_references_fail1a.cpp new file mode 100644 index 0000000..f60935b --- /dev/null +++ b/test/optional_test_references_fail1a.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_assignment1() +{ + boost::optional opt ; + int v = 2 ; + int& r = v ; + opt = r ; +} + + diff --git a/test/optional_test_references_fail1b.cpp b/test/optional_test_references_fail1b.cpp new file mode 100644 index 0000000..fbb2c0f --- /dev/null +++ b/test/optional_test_references_fail1b.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_assignment2() +{ + boost::optional opt ; + int v = 2 ; + int& r = v ; + opt.reset ( r ) ; +} + + diff --git a/test/optional_test_references_fail1c.cpp b/test/optional_test_references_fail1c.cpp new file mode 100644 index 0000000..27ec6e3 --- /dev/null +++ b/test/optional_test_references_fail1c.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_fail2.cpp b/test/optional_test_references_fail2.cpp new file mode 100644 index 0000000..53a7e5d --- /dev/null +++ b/test/optional_test_references_fail2.cpp @@ -0,0 +1,26 @@ +// (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_ptr_access() +{ + boost::optional opt ; + opt.get_ptr(); +} + + diff --git a/test/optional_test_references_fail3.cpp b/test/optional_test_references_fail3.cpp new file mode 100644 index 0000000..f2f5b5d --- /dev/null +++ b/test/optional_test_references_fail3.cpp @@ -0,0 +1,26 @@ +// (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_ctor() +{ + boost::optional opt1 ; + boost::optional opt2 = opt1 ; +} + + diff --git a/test/optional_test_references_fail4.cpp b/test/optional_test_references_fail4.cpp new file mode 100644 index 0000000..51f748b --- /dev/null +++ b/test/optional_test_references_fail4.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) ; +} + + diff --git a/test/optional_test_tie.cpp b/test/optional_test_tie.cpp new file mode 100644 index 0000000..0925667 --- /dev/null +++ b/test/optional_test_tie.cpp @@ -0,0 +1,67 @@ +// (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 +#include +#include + +#define BOOST_ENABLE_ASSERT_HANDLER + +#include "boost/optional.hpp" +#include "boost/tuple/tuple.hpp" + +#ifdef __BORLANDC__ +#pragma hdrstop +#endif + +#include "boost/test/minimal.hpp" + +#include "optional_test_common.cpp" + +// Test boost::tie() interoperabiliy. +int test_main( int, char* [] ) +{ + typedef X T ; + + try + { + TRACE( std::endl << BOOST_CURRENT_FUNCTION ); + + T z(0); + T a(1); + T b(2); + + optional oa, ob ; + + // T::T( T const& x ) is used + set_pending_dtor( ARG(T) ) ; + set_pending_copy( ARG(T) ) ; + boost::tie(oa,ob) = std::make_pair(a,b) ; + check_is_not_pending_dtor( ARG(T) ) ; + check_is_not_pending_copy( ARG(T) ) ; + check_initialized(oa); + check_initialized(ob); + check_value(oa,a,z); + check_value(ob,b,z); + + } + catch ( ... ) + { + BOOST_ERROR("Unexpected Exception caught!"); + } + + return 0; +} + +