diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 46ed39c..dc29fea 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -20,6 +20,9 @@ import testing ; [ run optional_test_conversions_from_U.cpp ] [ run optional_test_tie.cpp ] [ run optional_test_ref.cpp ] + [ run optional_test_ref_assign_portable_minimum.cpp ] + [ run optional_test_ref_assign_mutable_int.cpp ] + [ run optional_test_ref_assign_const_int.cpp ] [ run optional_test_ref_portable_minimum.cpp ] [ run optional_test_inplace.cpp ] [ run optional_test_io.cpp ] diff --git a/test/optional_ref_assign_test_defs.hpp b/test/optional_ref_assign_test_defs.hpp new file mode 100644 index 0000000..1dc9643 --- /dev/null +++ b/test/optional_ref_assign_test_defs.hpp @@ -0,0 +1,166 @@ +// 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 + +#ifndef BOOST_OPTIONAL_TEST_OPTIONAL_REF_ASSIGN_TEST_DEFS_AK_07JAN2015_HPP +#define BOOST_OPTIONAL_TEST_OPTIONAL_REF_ASSIGN_TEST_DEFS_AK_07JAN2015_HPP + +#include "boost/optional/optional.hpp" + +#ifdef __BORLANDC__ +#pragma hdrstop +#endif + +#include "boost/core/addressof.hpp" +#include "testable_classes.hpp" + +using boost::optional; +using boost::none; + +template +void test_copy_assignment_for_const() +{ + const 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); +} + +template +void test_copy_assignment_for_noconst_const() +{ + 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_EQ(val(*o), val(v)); + BOOST_TEST_EQ(val(*o), 9); + BOOST_TEST_EQ(val(v), 9); +} + +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_EQ(val(*o), val(v)); + BOOST_TEST_EQ(val(*o), 9); + BOOST_TEST_EQ(val(v), 9); + + val(*o) = 7; + BOOST_TEST(boost::addressof(*o) == boost::addressof(v)); + BOOST_TEST_EQ(val(*o), val(v)); + BOOST_TEST_EQ(val(*o), 7); + BOOST_TEST_EQ(val(v), 7); +} + +template +void test_rebinding_assignment_semantics_const() +{ + const typename concrete_type_of::type v(2), w(7); + optional o(v); + + BOOST_TEST(o); + BOOST_TEST(boost::addressof(*o) == boost::addressof(v)); + BOOST_TEST_EQ(val(*o), val(v)); + BOOST_TEST_EQ(val(*o), 2); + + o = optional(w); + BOOST_TEST_EQ(val(v), 2); + + BOOST_TEST(o); + BOOST_TEST(boost::addressof(*o) != boost::addressof(v)); + BOOST_TEST_NE(val(*o), val(v)); + BOOST_TEST_NE(val(*o), 2); + + BOOST_TEST(boost::addressof(*o) == boost::addressof(w)); + BOOST_TEST_EQ(val(*o), val(w)); + BOOST_TEST_EQ(val(*o), 7); +} + +template +void test_rebinding_assignment_semantics_noconst_const() +{ + typename concrete_type_of::type v(2), w(7); + optional o(v); + + BOOST_TEST(o); + BOOST_TEST(boost::addressof(*o) == boost::addressof(v)); + BOOST_TEST_EQ(val(*o), val(v)); + BOOST_TEST_EQ(val(*o), 2); + + o = optional(w); + BOOST_TEST_EQ(val(v), 2); + + BOOST_TEST(o); + BOOST_TEST(boost::addressof(*o) != boost::addressof(v)); + BOOST_TEST_NE(val(*o), val(v)); + BOOST_TEST_NE(val(*o), 2); + + BOOST_TEST(boost::addressof(*o) == boost::addressof(w)); + BOOST_TEST_EQ(val(*o), val(w)); + BOOST_TEST_EQ(val(*o), 7); +} + +template +void test_rebinding_assignment_semantics() +{ + typename concrete_type_of::type v(2), w(7); + optional o(v); + + BOOST_TEST(o); + BOOST_TEST(boost::addressof(*o) == boost::addressof(v)); + BOOST_TEST_EQ(val(*o), val(v)); + BOOST_TEST_EQ(val(*o), 2); + + o = optional(w); + BOOST_TEST_EQ(val(v), 2); + + BOOST_TEST(o); + BOOST_TEST(boost::addressof(*o) != boost::addressof(v)); + BOOST_TEST_NE(val(*o), val(v)); + BOOST_TEST_NE(val(*o), 2); + + BOOST_TEST(boost::addressof(*o) == boost::addressof(w)); + BOOST_TEST_EQ(val(*o), val(w)); + BOOST_TEST_EQ(val(*o), 7); + + val(*o) = 8; + BOOST_TEST(boost::addressof(*o) == boost::addressof(w)); + BOOST_TEST_EQ(val(*o), val(w)); + BOOST_TEST_EQ(val(*o), 8); + BOOST_TEST_EQ(val(w), 8); + BOOST_TEST_EQ(val(v), 2); +} + +#endif //BOOST_OPTIONAL_TEST_OPTIONAL_REF_ASSIGN_TEST_DEFS_AK_07JAN2015_HPP + diff --git a/test/optional_test_ref_assign_const_int.cpp b/test/optional_test_ref_assign_const_int.cpp new file mode 100644 index 0000000..a14ef6d --- /dev/null +++ b/test/optional_test_ref_assign_const_int.cpp @@ -0,0 +1,34 @@ +// 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" +#include "testable_classes.hpp" +#include "optional_ref_assign_test_defs.hpp" + +using boost::optional; +using boost::none; + + +int main() +{ + test_copy_assignment_for_const(); + test_copy_assignment_for_noconst_const(); + test_rebinding_assignment_semantics_const(); + test_rebinding_assignment_semantics_noconst_const(); + + return boost::report_errors(); +} diff --git a/test/optional_test_ref_assign_mutable_int.cpp b/test/optional_test_ref_assign_mutable_int.cpp new file mode 100644 index 0000000..1c674da --- /dev/null +++ b/test/optional_test_ref_assign_mutable_int.cpp @@ -0,0 +1,31 @@ +// 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" +#include "testable_classes.hpp" +#include "optional_ref_assign_test_defs.hpp" + +using boost::optional; +using boost::none; + +int main() +{ + test_copy_assignment_for(); + test_rebinding_assignment_semantics(); + + return boost::report_errors(); +} diff --git a/test/optional_test_ref_assign_portable_minimum.cpp b/test/optional_test_ref_assign_portable_minimum.cpp new file mode 100644 index 0000000..2a548ab --- /dev/null +++ b/test/optional_test_ref_assign_portable_minimum.cpp @@ -0,0 +1,44 @@ +// 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" +#include "testable_classes.hpp" +#include "optional_ref_assign_test_defs.hpp" + +using boost::optional; +using boost::none; + + +template +void test_optional_ref_assignment() +{ + test_copy_assignment_for(); + test_rebinding_assignment_semantics(); + + test_copy_assignment_for_const(); + test_copy_assignment_for_noconst_const(); + test_rebinding_assignment_semantics_const(); + test_rebinding_assignment_semantics_noconst_const(); +} + +int main() +{ + test_optional_ref_assignment(); + //test_optional_ref_assignment(); + + return boost::report_errors(); +} diff --git a/test/optional_test_ref_portable_minimum.cpp b/test/optional_test_ref_portable_minimum.cpp index a4da751..ddcffc9 100644 --- a/test/optional_test_ref_portable_minimum.cpp +++ b/test/optional_test_ref_portable_minimum.cpp @@ -18,87 +18,11 @@ #include "boost/core/addressof.hpp" #include "boost/core/enable_if.hpp" #include "boost/core/lightweight_test.hpp" +#include "testable_classes.hpp" using boost::optional; using boost::none; -// testable classes -struct ScopeGuard // no copy/move ctor/assign -{ - int val_; - explicit ScopeGuard(int v) : val_(v) {} - int& val() { return val_; } - const int& val() const { return val_; } - -private: - ScopeGuard(ScopeGuard const&); - void operator=(ScopeGuard const&); -}; - -struct Abstract -{ - virtual int& val() = 0; - virtual const int& val() const = 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_; } - const int& val() const { return val_; } -}; - -template -struct concrete_type_of -{ - typedef T type; -}; - -template <> -struct concrete_type_of -{ - typedef Impl type; -}; - -template <> -struct concrete_type_of -{ - typedef const Impl type; -}; - -template -struct has_arrow -{ - static const bool value = true; -}; - -template <> -struct has_arrow -{ - static const bool value = false; -}; - -int& val(int& i) { return i; } -int& val(Abstract& a) { return a.val(); } -int& val(ScopeGuard& g) { return g.val(); } - -const int& val(const int& i) { return i; } -const int& val(const Abstract& a) { return a.val(); } -const int& val(const ScopeGuard& g) { return g.val(); } - -bool operator==(const Abstract& l, const Abstract& r) { return l.val() == r.val(); } -bool operator==(const ScopeGuard& l, const ScopeGuard& r) { return l.val() == r.val(); } - -bool operator<(const Abstract& l, const Abstract& r) { return l.val() < r.val(); } -bool operator<(const ScopeGuard& l, const ScopeGuard& r) { return l.val() < r.val(); } -// end testable classes template typename boost::enable_if< has_arrow >::type @@ -264,145 +188,6 @@ void test_clearing_the_value() BOOST_TEST_EQ(val(v), 2); } -template -void test_copy_assignment_for_const() -{ - const 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); -} - -template -void test_copy_assignment_for_noconst_const() -{ - 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_EQ(val(*o), val(v)); - BOOST_TEST_EQ(val(*o), 9); - BOOST_TEST_EQ(val(v), 9); -} - -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_EQ(val(*o), val(v)); - BOOST_TEST_EQ(val(*o), 9); - BOOST_TEST_EQ(val(v), 9); - - val(*o) = 7; - BOOST_TEST(boost::addressof(*o) == boost::addressof(v)); - BOOST_TEST_EQ(val(*o), val(v)); - BOOST_TEST_EQ(val(*o), 7); - BOOST_TEST_EQ(val(v), 7); -} - -template -void test_rebinding_assignment_semantics_const() -{ - const typename concrete_type_of::type v(2), w(7); - optional o(v); - - BOOST_TEST(o); - BOOST_TEST(boost::addressof(*o) == boost::addressof(v)); - BOOST_TEST_EQ(val(*o), val(v)); - BOOST_TEST_EQ(val(*o), 2); - - o = optional(w); - BOOST_TEST_EQ(val(v), 2); - - BOOST_TEST(o); - BOOST_TEST(boost::addressof(*o) != boost::addressof(v)); - BOOST_TEST_NE(val(*o), val(v)); - BOOST_TEST_NE(val(*o), 2); - - BOOST_TEST(boost::addressof(*o) == boost::addressof(w)); - BOOST_TEST_EQ(val(*o), val(w)); - BOOST_TEST_EQ(val(*o), 7); -} - -template -void test_rebinding_assignment_semantics_noconst_const() -{ - typename concrete_type_of::type v(2), w(7); - optional o(v); - - BOOST_TEST(o); - BOOST_TEST(boost::addressof(*o) == boost::addressof(v)); - BOOST_TEST_EQ(val(*o), val(v)); - BOOST_TEST_EQ(val(*o), 2); - - o = optional(w); - BOOST_TEST_EQ(val(v), 2); - - BOOST_TEST(o); - BOOST_TEST(boost::addressof(*o) != boost::addressof(v)); - BOOST_TEST_NE(val(*o), val(v)); - BOOST_TEST_NE(val(*o), 2); - - BOOST_TEST(boost::addressof(*o) == boost::addressof(w)); - BOOST_TEST_EQ(val(*o), val(w)); - BOOST_TEST_EQ(val(*o), 7); -} - -template -void test_rebinding_assignment_semantics() -{ - typename concrete_type_of::type v(2), w(7); - optional o(v); - - BOOST_TEST(o); - BOOST_TEST(boost::addressof(*o) == boost::addressof(v)); - BOOST_TEST_EQ(val(*o), val(v)); - BOOST_TEST_EQ(val(*o), 2); - - o = optional(w); - BOOST_TEST_EQ(val(v), 2); - - BOOST_TEST(o); - BOOST_TEST(boost::addressof(*o) != boost::addressof(v)); - BOOST_TEST_NE(val(*o), val(v)); - BOOST_TEST_NE(val(*o), 2); - - BOOST_TEST(boost::addressof(*o) == boost::addressof(w)); - BOOST_TEST_EQ(val(*o), val(w)); - BOOST_TEST_EQ(val(*o), 7); - - val(*o) = 8; - BOOST_TEST(boost::addressof(*o) == boost::addressof(w)); - BOOST_TEST_EQ(val(*o), val(w)); - BOOST_TEST_EQ(val(*o), 8); - BOOST_TEST_EQ(val(w), 8); - BOOST_TEST_EQ(val(v), 2); -} - template void test_equality() { @@ -596,8 +381,6 @@ void test_optional_ref() { test_not_containing_value_for(); test_direct_init_for(); - test_copy_assignment_for(); - test_rebinding_assignment_semantics(); test_clearing_the_value(); test_arrow(); test_equality(); @@ -611,10 +394,6 @@ void test_optional_const_ref() test_not_containing_value_for(); test_direct_init_for_const(); test_direct_init_for_noconst_const(); - test_copy_assignment_for_const(); - test_copy_assignment_for_noconst_const(); - test_rebinding_assignment_semantics_const(); - test_rebinding_assignment_semantics_noconst_const(); test_clearing_the_value(); test_arrow_const(); test_arrow_noconst_const(); diff --git a/test/testable_classes.hpp b/test/testable_classes.hpp new file mode 100644 index 0000000..e18359f --- /dev/null +++ b/test/testable_classes.hpp @@ -0,0 +1,91 @@ +// 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/libs/optional for documentation. +// +// You are welcome to contact the author at: +// akrzemi1@gmail.com + +#ifndef BOOST_OPTIONAL_TEST_TESTABKE_CLASSES_AK_07JAN2015_HPP +#define BOOST_OPTIONAL_TEST_TESTABKE_CLASSES_AK_07JAN2015_HPP + +struct ScopeGuard // no copy/move ctor/assign +{ + int val_; + explicit ScopeGuard(int v) : val_(v) {} + int& val() { return val_; } + const int& val() const { return val_; } + +private: + ScopeGuard(ScopeGuard const&); + void operator=(ScopeGuard const&); +}; + +struct Abstract +{ + virtual int& val() = 0; + virtual const int& val() const = 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_; } + const int& val() const { return val_; } +}; + +template +struct concrete_type_of +{ + typedef T type; +}; + +template <> +struct concrete_type_of +{ + typedef Impl type; +}; + +template <> +struct concrete_type_of +{ + typedef const Impl type; +}; + +template +struct has_arrow +{ + static const bool value = true; +}; + +template <> +struct has_arrow +{ + static const bool value = false; +}; + +int& val(int& i) { return i; } +int& val(Abstract& a) { return a.val(); } +int& val(ScopeGuard& g) { return g.val(); } + +const int& val(const int& i) { return i; } +const int& val(const Abstract& a) { return a.val(); } +const int& val(const ScopeGuard& g) { return g.val(); } + +bool operator==(const Abstract& l, const Abstract& r) { return l.val() == r.val(); } +bool operator==(const ScopeGuard& l, const ScopeGuard& r) { return l.val() == r.val(); } + +bool operator<(const Abstract& l, const Abstract& r) { return l.val() < r.val(); } +bool operator<(const ScopeGuard& l, const ScopeGuard& r) { return l.val() < r.val(); } + +#endif //BOOST_OPTIONAL_TEST_TESTABKE_CLASSES_AK_07JAN2015_HPP