From 8a03282736c447d549d6e02d002c0e05daf2140b Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Wed, 11 Jun 2014 14:35:27 +0300 Subject: [PATCH 1/5] Add additional tests. --- test/Jamfile.v2 | 3 +- test/implicit_cast_fail2.cpp | 19 +++++ test/polymorphic_cast_test.cpp | 150 +++++++++++++++++++++++++++++++++ 3 files changed, 171 insertions(+), 1 deletion(-) create mode 100644 test/implicit_cast_fail2.cpp create mode 100644 test/polymorphic_cast_test.cpp diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index f79e724..69f8b3c 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -27,5 +27,6 @@ test-suite conversion [ compile-fail implicit_cast_fail.cpp ] [ run cast_test.cpp ] [ run numeric_cast_test.cpp ] + [ run polymorphic_cast_test.cpp ] + [ compile-fail implicit_cast_fail2.cpp ] ; - diff --git a/test/implicit_cast_fail2.cpp b/test/implicit_cast_fail2.cpp new file mode 100644 index 0000000..97cc2d8 --- /dev/null +++ b/test/implicit_cast_fail2.cpp @@ -0,0 +1,19 @@ +// +// Test that implicit_cast requires a template argument +// +// Copyright 2014 Peter Dimov +// +// Distributed under 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 +// + +#include + +int main() +{ + int x = boost::implicit_cast( 1 ); + (void)x; + + return 0; +} diff --git a/test/polymorphic_cast_test.cpp b/test/polymorphic_cast_test.cpp new file mode 100644 index 0000000..b3f1397 --- /dev/null +++ b/test/polymorphic_cast_test.cpp @@ -0,0 +1,150 @@ +// +// Test boost::polymorphic_cast, boost::polymorphic_downcast +// +// Copyright 1999 Beman Dawes +// Copyright 1999 Dave Abrahams +// Copyright 2014 Peter Dimov +// +// Distributed under 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 +// + +#define BOOST_ENABLE_ASSERT_HANDLER +#include +#include +#include + +static bool expect_assertion = false; +static int assertion_failed_count = 0; + +// BOOST_ASSERT custom handler +void boost::assertion_failed( char const * expr, char const * function, char const * file, long line ) +{ + if( expect_assertion ) + { + ++assertion_failed_count; + } + else + { + BOOST_ERROR( "unexpected assertion" ); + + BOOST_LIGHTWEIGHT_TEST_OSTREAM + << file << "(" << line << "): assertion '" << expr << "' failed in function '" + << function << "'" << std::endl; + } +} + +// + +struct Base +{ + virtual ~Base() {} + virtual std::string kind() { return "Base"; } +}; + +struct Base2 +{ + virtual ~Base2() {} + virtual std::string kind2() { return "Base2"; } +}; + +struct Derived : public Base, Base2 +{ + virtual std::string kind() { return "Derived"; } +}; + +static void test_polymorphic_cast() +{ + Base * base = new Derived; + + Derived * derived; + + try + { + derived = boost::polymorphic_cast( base ); + + BOOST_TEST( derived != 0 ); + + if( derived != 0 ) + { + BOOST_TEST_EQ( derived->kind(), "Derived" ); + } + } + catch( std::bad_cast const& ) + { + BOOST_ERROR( "boost::polymorphic_cast( base ) threw std::bad_cast" ); + } + + Base2 * base2; + + try + { + base2 = boost::polymorphic_cast( base ); // crosscast + + BOOST_TEST( base2 != 0 ); + + if( base2 != 0 ) + { + BOOST_TEST_EQ( base2->kind2(), "Base2" ); + } + } + catch( std::bad_cast const& ) + { + BOOST_ERROR( "boost::polymorphic_cast( base ) threw std::bad_cast" ); + } + + delete base; +} + +static void test_polymorphic_downcast() +{ + Base * base = new Derived; + + Derived * derived = boost::polymorphic_downcast( base ); + + BOOST_TEST( derived != 0 ); + + if( derived != 0 ) + { + BOOST_TEST_EQ( derived->kind(), "Derived" ); + } + + // polymorphic_downcast can't do crosscasts + + delete base; +} + +static void test_polymorphic_cast_fail() +{ + Base * base = new Base; + + BOOST_TEST_THROWS( boost::polymorphic_cast( base ), std::bad_cast ); + + delete base; +} + +static void test_polymorphic_downcast_fail() +{ + Base * base = new Base; + + int old_count = assertion_failed_count; + expect_assertion = true; + + boost::polymorphic_downcast( base ); // should assert + + BOOST_TEST_EQ( assertion_failed_count, old_count + 1 ); + expect_assertion = false; + + delete base; +} + +int main() +{ + test_polymorphic_cast(); + test_polymorphic_downcast(); + test_polymorphic_cast_fail(); + test_polymorphic_downcast_fail(); + + return boost::report_errors(); +} From 24d6ac1e87d309fb6ff36608155a0df346022904 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Wed, 11 Jun 2014 14:38:48 +0300 Subject: [PATCH 2/5] Remove unnecessary includes from polymorphic_cast.hpp. --- include/boost/polymorphic_cast.hpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/include/boost/polymorphic_cast.hpp b/include/boost/polymorphic_cast.hpp index f02d7ea..03013c7 100644 --- a/include/boost/polymorphic_cast.hpp +++ b/include/boost/polymorphic_cast.hpp @@ -48,9 +48,6 @@ # include # include # include -# include -# include -# include namespace boost { From f0caf56d0b4ab7a97c4ebda07dbdeedb453c3e2c Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Wed, 11 Jun 2014 14:40:50 +0300 Subject: [PATCH 3/5] Remove unnecessary includes from implicit_cast_fail.cpp. --- test/implicit_cast_fail.cpp | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/test/implicit_cast_fail.cpp b/test/implicit_cast_fail.cpp index 442fa4c..86ccbb2 100644 --- a/test/implicit_cast_fail.cpp +++ b/test/implicit_cast_fail.cpp @@ -4,10 +4,6 @@ // http://www.boost.org/LICENSE_1_0.txt) #include -#include - -#define BOOST_INCLUDE_MAIN -#include using boost::implicit_cast; @@ -16,11 +12,9 @@ struct foo explicit foo(char const*) {} }; -int test_main(int, char*[]) +int main() { foo x = implicit_cast("foobar"); (void)x; // warning suppression. - BOOST_CHECK(false); // suppressing warning about 'boost::unit_test::{anonymous}::unit_test_log' defined but not used return 0; } - From df57f258076b427de788f5517a39c33756367f9b Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Wed, 11 Jun 2014 14:41:55 +0300 Subject: [PATCH 4/5] Remove unnecessary includes from cast_test.cpp. --- test/cast_test.cpp | 6 ------ 1 file changed, 6 deletions(-) diff --git a/test/cast_test.cpp b/test/cast_test.cpp index fa30df8..5295e32 100644 --- a/test/cast_test.cpp +++ b/test/cast_test.cpp @@ -14,14 +14,8 @@ // 3 Aug 99 Initial Version #include -#include -#include // for DBL_MAX (Peter Schmid) #include -# if SCHAR_MAX == LONG_MAX -# error "This test program doesn't work if SCHAR_MAX == LONG_MAX" -# endif - using namespace boost; using std::cout; From 800c10a4eb5053f4c7e9aa0084d9272de1fe5cd3 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Wed, 11 Jun 2014 14:45:37 +0300 Subject: [PATCH 5/5] Remove use of mpl::identity from implicit_cast.hpp. --- include/boost/implicit_cast.hpp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/include/boost/implicit_cast.hpp b/include/boost/implicit_cast.hpp index 5b1cd92..d82db76 100644 --- a/include/boost/implicit_cast.hpp +++ b/include/boost/implicit_cast.hpp @@ -5,17 +5,24 @@ #ifndef IMPLICIT_CAST_DWA200356_HPP # define IMPLICIT_CAST_DWA200356_HPP -# include - namespace boost { +namespace detail { + +template struct icast_identity +{ + typedef T type; +}; + +} // namespace detail + // implementation originally suggested by C. Green in // http://lists.boost.org/MailArchives/boost/msg00886.php // The use of identity creates a non-deduced form, so that the // explicit template argument must be supplied template -inline T implicit_cast (typename mpl::identity::type x) { +inline T implicit_cast (typename boost::detail::icast_identity::type x) { return x; }