diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index aeaef2b..49d6df0 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -17,8 +17,10 @@ import testing ; { test-suite optional : [ run optional_test.cpp ] + [ run optional_test_conversions_from_U.cpp ] [ run optional_test_tie.cpp ] [ run optional_test_ref.cpp ] + [ run optional_test_ref_portable_minimum.cpp ] [ run optional_test_inplace.cpp ] [ run optional_test_io.cpp ] [ run optional_test_move.cpp ] diff --git a/test/optional_test.cpp b/test/optional_test.cpp index e34ce7e..1247965 100644 --- a/test/optional_test.cpp +++ b/test/optional_test.cpp @@ -906,41 +906,6 @@ void test_no_implicit_conversions() test_no_implicit_conversions_impl(p); } -struct A {} ; -void test_conversions1() -{ - TRACE( std::endl << BOOST_CURRENT_FUNCTION ); - -#ifndef BOOST_OPTIONAL_NO_CONVERTING_COPY_CTOR - char c = 20 ; - optional opt0(c); - optional opt1(opt0); - BOOST_CHECK(*opt1 == static_cast(c)); -#endif - -#ifndef BOOST_OPTIONAL_NO_CONVERTING_ASSIGNMENT - float f = 21.22f ; - double d = f ; - optional opt2(f) ; - optional opt3 ; - opt3 = opt2 ; - BOOST_CHECK(*opt3 == d); -#endif -} - -void test_conversions2() -{ - TRACE( std::endl << BOOST_CURRENT_FUNCTION ); - - char c = 20 ; - optional opt(c); - BOOST_CHECK( get(opt) == static_cast(c)); - - float f = 21.22f ; - optional opt1; - opt1 = f ; - BOOST_CHECK(*get(&opt1) == static_cast(f)); -} namespace optional_swap_test @@ -1307,8 +1272,6 @@ int test_main( int, char* [] ) test_with_class_type(); test_with_builtin_types(); test_no_implicit_conversions(); - test_conversions1(); - test_conversions2(); test_swap_tweaking(); test_custom_addressof_operator(); } diff --git a/test/optional_test_conversions_from_U.cpp b/test/optional_test_conversions_from_U.cpp new file mode 100644 index 0000000..57d8215 --- /dev/null +++ b/test/optional_test_conversions_from_U.cpp @@ -0,0 +1,111 @@ +// Copyright (C) 2014 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 author at: akrzemi1@gmail.com + + +#include "boost/optional/optional.hpp" + +#ifdef __BORLANDC__ +#pragma hdrstop +#endif + +#include "boost/core/lightweight_test.hpp" + +using boost::optional; + + +// testing types: +// X is convertible to Y +// ADeriv is convertible to ABase +struct X +{ + int val; + explicit X(int v) : val(v) {} +}; + +struct Y +{ + int yval; + Y(X const& x) : yval(x.val) {} + friend bool operator==(Y const& l, Y const& r) { return l.yval == r.yval; } +}; + +struct ABase +{ + int val; + explicit ABase(int v) : val(v) {} + friend bool operator==(ABase const& l, ABase const& r) { return l.val == r.val; } +}; + +struct ADeriv : ABase +{ + explicit ADeriv(int v) : ABase(v) {} +}; + + +template +void test_convert_optional_U_to_optional_T_for() +{ +#ifndef BOOST_OPTIONAL_NO_CONVERTING_COPY_CTOR + { + optional ou(U(8)); + optional ot1(ou); + BOOST_TEST(ot1); + BOOST_TEST(*ot1 == T(*ou)); + } +#endif + +#ifndef BOOST_OPTIONAL_NO_CONVERTING_ASSIGNMENT + { + optional ou(U(8)); + optional ot2; + ot2 = ou; + BOOST_TEST(ot2); + BOOST_TEST(*ot2 == T(*ou)); + } +#endif +} + +void test_convert_optional_U_to_optional_T() +{ + test_convert_optional_U_to_optional_T_for(); + test_convert_optional_U_to_optional_T_for(); + test_convert_optional_U_to_optional_T_for(); + test_convert_optional_U_to_optional_T_for(); +} + +template +void test_convert_U_to_optional_T_for() +{ + U u(8); + optional ot1(u); + BOOST_TEST(ot1); + BOOST_TEST(*ot1 == T(u)); + + optional ot2; + ot2 = u; + BOOST_TEST(ot2); + BOOST_TEST(*ot2 == T(u)); +} + +void test_convert_U_to_optional_T() +{ + test_convert_U_to_optional_T_for(); + test_convert_U_to_optional_T_for(); + test_convert_U_to_optional_T_for(); + test_convert_U_to_optional_T_for(); +} + +int main() +{ + test_convert_optional_U_to_optional_T(); + test_convert_U_to_optional_T(); + + return boost::report_errors(); +} diff --git a/test/optional_test_equals_none.cpp b/test/optional_test_equals_none.cpp index aed7771..1c8c5d8 100644 --- a/test/optional_test_equals_none.cpp +++ b/test/optional_test_equals_none.cpp @@ -8,18 +8,6 @@ // // You are welcome to contact the author at: // akrzemi1@gmail.com -// -// Revisions: -// -#include -#include -#include - -#define BOOST_ENABLE_ASSERT_HANDLER - -#include "boost/bind/apply.hpp" // Included just to test proper interaction with boost::apply<> as reported by Daniel Wallin -#include "boost/mpl/bool.hpp" -#include "boost/mpl/bool_fwd.hpp" // For mpl::true_ and mpl::false_ #include "boost/optional/optional.hpp" @@ -27,11 +15,9 @@ #pragma hdrstop #endif +#include "boost/core/lightweight_test.hpp" #include "boost/none.hpp" -#include "boost/test/minimal.hpp" - -#include "optional_test_common.cpp" struct SemiRegular // no operator== { @@ -41,28 +27,17 @@ private: void operator!=(SemiRegular const&) const {} void test_equal_to_none_of_noncomparable_T() { - optional i = SemiRegular(); - optional o; - - BOOST_CHECK(i != boost::none); - BOOST_CHECK(boost::none != i); - BOOST_CHECK(o == boost::none); - BOOST_CHECK(boost::none == o); + boost::optional i = SemiRegular(); + boost::optional o; + + BOOST_TEST(i != boost::none); + BOOST_TEST(boost::none != i); + BOOST_TEST(o == boost::none); + BOOST_TEST(boost::none == o); } -int test_main( int, char* [] ) +int main() { - try - { - test_equal_to_none_of_noncomparable_T(); - - } - catch ( ... ) - { - BOOST_ERROR("Unexpected Exception caught!"); - } - - return 0; + test_equal_to_none_of_noncomparable_T(); + return boost::report_errors(); } - - diff --git a/test/optional_test_ref_portable_minimum.cpp b/test/optional_test_ref_portable_minimum.cpp new file mode 100644 index 0000000..8555723 --- /dev/null +++ b/test/optional_test_ref_portable_minimum.cpp @@ -0,0 +1,147 @@ +// Copyright (C) 2014 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 author at: akrzemi1@gmail.com + + +#include "boost/optional/optional.hpp" + +#ifdef __BORLANDC__ +#pragma hdrstop +#endif + +#include "boost/core/addressof.hpp" +#include "boost/core/lightweight_test.hpp" +#include "boost/none.hpp" + +using boost::optional; +using boost::none; + +// testable classes +struct ScopeGuard // no copy/move ctor/assign +{ + int val; + explicit ScopeGuard(int v) : val(v) {} + +private: + ScopeGuard(ScopeGuard const&); + void operator=(ScopeGuard const&); +}; + +struct Abstract +{ + virtual int& val() = 0; + virtual ~Abstract() {} + Abstract(){} +private: + Abstract(Abstract const&); + void operator=(Abstract const&); +}; + +struct Impl : Abstract +{ + int val_; + Impl(int v) : val_(v) {} + int& val() { return val_; } +}; + +template +struct concrete_type_of +{ + typedef T type; +}; + +template <> +struct concrete_type_of +{ + typedef Impl type; +}; + +int& val(int& i) { return i; } +int& val(Abstract& a) { return a.val(); } +int& val(ScopeGuard& g) { return g.val; } + +template +void test_not_containing_value_for() +{ + optional o1; + optional o2 = none; + optional o3 = o1; + + BOOST_TEST(!o1); + BOOST_TEST(!o2); + BOOST_TEST(!o3); + + BOOST_TEST(o1 == none); + BOOST_TEST(o2 == none); + BOOST_TEST(o3 == none); +} + +template +void test_direct_init_for() +{ + typename concrete_type_of::type v(2); + optional o(v); + + BOOST_TEST(o); + BOOST_TEST(o != none); + BOOST_TEST(boost::addressof(*o) == boost::addressof(v)); + BOOST_TEST(val(*o) == val(v)); + + val(v) = 9; + BOOST_TEST(boost::addressof(*o) == boost::addressof(v)); + BOOST_TEST(val(*o) == val(v)); + BOOST_TEST(val(*o) == 9); + + val(*o) = 7; + BOOST_TEST(boost::addressof(*o) == boost::addressof(v)); + BOOST_TEST(val(*o) == val(v)); + BOOST_TEST(val(*o) == 7); +} + +template +void test_copy_assignment_for() +{ + typename concrete_type_of::type v(2); + optional o; + o = optional(v); + + BOOST_TEST(o); + BOOST_TEST(o != none); + BOOST_TEST(boost::addressof(*o) == boost::addressof(v)); + BOOST_TEST(val(*o) == val(v)); + BOOST_TEST(val(*o) == 2); + + val(v) = 9; + BOOST_TEST(boost::addressof(*o) == boost::addressof(v)); + BOOST_TEST(val(*o) == val(v)); + BOOST_TEST(val(*o) == 9); + + val(*o) = 7; + BOOST_TEST(boost::addressof(*o) == boost::addressof(v)); + BOOST_TEST(val(*o) == val(v)); + BOOST_TEST(val(*o) == 7); +} + + +template +void test_optional_ref() +{ + test_not_containing_value_for(); + test_direct_init_for(); + test_copy_assignment_for(); +} + +int main() +{ + test_optional_ref(); + test_optional_ref(); + test_optional_ref(); + + return boost::report_errors(); +}