diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index d20cd84..da08dfe 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -26,7 +26,6 @@ import testing ; [ compile-fail optional_test_fail3a.cpp ] [ compile-fail optional_test_fail3b.cpp ] [ compile-fail optional_test_ref_fail1.cpp ] - [ compile-fail optional_test_ref_fail2.cpp ] [ compile-fail optional_test_ref_fail3.cpp ] [ compile-fail optional_test_ref_fail4.cpp ] [ compile-fail optional_test_inplace_fail.cpp ] diff --git a/test/optional_test.cpp b/test/optional_test.cpp index 12dda2a..3050639 100644 --- a/test/optional_test.cpp +++ b/test/optional_test.cpp @@ -15,7 +15,9 @@ #define BOOST_ENABLE_ASSERT_HANDLER -#include "boost/optional.hpp" +#include "boost/bind/apply.hpp" // Included just to test proper interaction with boost::apply<> as reported by Daniel Wallin + +#include "boost/optional/optional.hpp" #ifdef __BORLANDC__ #pragma hdrstop @@ -151,6 +153,95 @@ void test_basics( T const* ) ob.reset(); check_is_pending_dtor( ARG(T) ) ; check_uninitialized(ob); + +} + +template +void test_conditional_ctor_and_get_valur_or ( T const* ) +{ + TRACE( std::endl << BOOST_CURRENT_FUNCTION ); + + T a(321); + + T z(123); + + optional const cdef0(false,a); + + optional def0(false,a); + optional def1 = boost::make_optional(false,a); // T is not within boost so ADL won't find make_optional unqualified + check_uninitialized(def0); + check_uninitialized(def1); + + optional const co0(true,a); + + optional o0(true,a); + optional o1 = boost::make_optional(true,a); // T is not within boost so ADL won't find make_optional unqualified + + check_initialized(o0); + check_initialized(o1); + check_value(o0,a,z); + check_value(o1,a,z); + + T b = def0.get_value_or(z); + BOOST_CHECK( b == z ) ; + + b = get_optional_value_or(def0,z); + BOOST_CHECK( b == z ) ; + + b = o0.get_value_or(z); + BOOST_CHECK( b == a ) ; + + b = get_optional_value_or(o0,z); + BOOST_CHECK( b == a ) ; + + + T const& crz = z ; + T& rz = z ; + + T const& crzz = def0.get_value_or(crz); + BOOST_CHECK( crzz == crz ) ; + + T& rzz = def0.get_value_or(rz); + BOOST_CHECK( rzz == rz ) ; + + T const& crzzz = get_optional_value_or(cdef0,crz); + BOOST_CHECK( crzzz == crz ) ; + + T& rzzz = get_optional_value_or(def0,rz); + BOOST_CHECK( rzzz == rz ) ; + + T const& crb = o0.get_value_or(crz); + BOOST_CHECK( crb == a ) ; + + T& rb = o0.get_value_or(rz); + BOOST_CHECK( rb == b ) ; + + T const& crbb = get_optional_value_or(co0,crz); + BOOST_CHECK( crbb == b ) ; + + T const& crbbb = get_optional_value_or(o0,crz); + BOOST_CHECK( crbbb == b ) ; + + T& rbb = get_optional_value_or(o0,rz); + BOOST_CHECK( rbb == b ) ; + + T& ra = a ; + + optional defref(false,ra); + BOOST_CHECK(!defref); + + optional ref(true,ra); + BOOST_CHECK(!!ref); + + a = T(432); + + BOOST_CHECK( *ref == a ) ; + + T& r1 = defref.get_value_or(z); + BOOST_CHECK( r1 == z ) ; + + T& r2 = ref.get_value_or(z); + BOOST_CHECK( r2 == a ) ; } // @@ -683,11 +774,30 @@ void test_none( T const* ) test_default_implicit_construction(T(1),none); } +template +void test_arrow( T const* ) +{ + TRACE( std::endl << BOOST_CURRENT_FUNCTION ); + + T a(1234); + + optional oa(a) ; + optional const coa(a) ; + + BOOST_CHECK ( coa->V() == 1234 ) ; + + oa->V() = 4321 ; + + BOOST_CHECK ( a.V() = 1234 ) ; + BOOST_CHECK ( (*oa).V() = 4321 ) ; +} + void test_with_builtin_types() { TRACE( std::endl << BOOST_CURRENT_FUNCTION ); test_basics( ARG(double) ); + test_conditional_ctor_and_get_valur_or( ARG(double) ); test_uninitialized_access( ARG(double) ); test_no_throwing_swap( ARG(double) ); test_relops( ARG(double) ) ; @@ -699,6 +809,7 @@ void test_with_class_type() TRACE( std::endl << BOOST_CURRENT_FUNCTION ); test_basics( ARG(X) ); + test_conditional_ctor_and_get_valur_or( ARG(X) ); test_direct_value_manip( ARG(X) ); test_uninitialized_access( ARG(X) ); test_throwing_direct_init( ARG(X) ); @@ -711,6 +822,7 @@ void test_with_class_type() test_throwing_swap( ARG(X) ); test_relops( ARG(X) ) ; test_none( ARG(X) ) ; + test_arrow( ARG(X) ) ; BOOST_CHECK ( X::count == 0 ) ; } diff --git a/test/optional_test_ref.cpp b/test/optional_test_ref.cpp index 1ccaeb4..91149ba 100644 --- a/test/optional_test_ref.cpp +++ b/test/optional_test_ref.cpp @@ -299,6 +299,23 @@ void test_none( T const* ) BOOST_CHECK ( !non_def ) ; } +template +void test_arrow( T const* ) +{ + TRACE( std::endl << BOOST_CURRENT_FUNCTION ); + + T a(1234); + + optional oa(a) ; + optional const coa(a) ; + + BOOST_CHECK ( coa->V() == 1234 ) ; + + oa->V() = 4321 ; + + BOOST_CHECK ( a.V() = 4321 ) ; +} + void test_with_builtin_types() { TRACE( std::endl << BOOST_CURRENT_FUNCTION ); @@ -315,6 +332,7 @@ void test_with_class_type() test_basics( ARG(X) ); test_relops( ARG(X) ) ; test_none ( ARG(X) ) ; + test_arrow ( ARG(X) ) ; BOOST_CHECK ( X::count == 0 ) ; } diff --git a/test/optional_test_ref_fail2.cpp b/test/optional_test_ref_fail2.cpp deleted file mode 100644 index 732c995..0000000 --- a/test/optional_test_ref_fail2.cpp +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright (C) 2003, Fernando Luis Cacciola Carballal. -// -// 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 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(); -} - -