From 7ebcee675aca0f0523f0d8a571fdaa5bc7c7b061 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Tue, 3 Jun 2014 21:02:36 +0300 Subject: [PATCH] Add more checked_delete tests, a visit_each test. --- test/Jamfile.v2 | 4 +++ test/checked_delete_fail.cpp | 1 - test/checked_delete_fail2.cpp | 27 +++++++++++++++ test/checked_delete_test.cpp | 58 +++++++++++++++++++++++++++++++ test/visit_each_test.cpp | 65 +++++++++++++++++++++++++++++++++++ 5 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 test/checked_delete_fail2.cpp create mode 100644 test/checked_delete_test.cpp create mode 100644 test/visit_each_test.cpp diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 509e60f..e7ddce2 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -16,7 +16,9 @@ test-suite "core" [ run addressof_np_test.cpp ] [ run addressof_fn_test.cpp ] + [ run checked_delete_test.cpp ] [ compile-fail checked_delete_fail.cpp ] + [ compile-fail checked_delete_fail2.cpp ] [ compile ref_ct_test.cpp ] [ run ref_test.cpp ] @@ -43,4 +45,6 @@ test-suite "core" msvc:"/we4100 /we4101" ] [ run sp_typeinfo_test.cpp ] [ run sp_typeinfo_test.cpp : : : off : sp_typeinfo_test_no_rtti ] + + [ run visit_each_test.cpp ] ; diff --git a/test/checked_delete_fail.cpp b/test/checked_delete_fail.cpp index 41fdc9a..76418db 100644 --- a/test/checked_delete_fail.cpp +++ b/test/checked_delete_fail.cpp @@ -23,6 +23,5 @@ int main() { Incomplete * p = 0; boost::checked_delete(p); // should cause compile time error - boost::checked_array_delete(p); // should cause compile time error return 0; } // main diff --git a/test/checked_delete_fail2.cpp b/test/checked_delete_fail2.cpp new file mode 100644 index 0000000..f7362a9 --- /dev/null +++ b/test/checked_delete_fail2.cpp @@ -0,0 +1,27 @@ +// Boost checked_delete test program ---------------------------------------// + +// Copyright Beman Dawes 2001. 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) + +// See http://www.boost.org/libs/utility for documentation. + +// Revision History +// 21 May 01 Initial version (Beman Dawes) + +#include // for checked_delete + +// This program demonstrates compiler errors when trying to delete an +// incomplete type. + +namespace +{ + class Incomplete; +} + +int main() +{ + Incomplete * p = 0; + boost::checked_array_delete(p); // should cause compile time error + return 0; +} // main diff --git a/test/checked_delete_test.cpp b/test/checked_delete_test.cpp new file mode 100644 index 0000000..46c2aed --- /dev/null +++ b/test/checked_delete_test.cpp @@ -0,0 +1,58 @@ +// Boost checked_delete test program + +// Copyright Beman Dawes 2001. 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 +#include + +struct X +{ + static int instances; + + X() + { + ++instances; + } + + ~X() + { + --instances; + } + +private: + + X( X const & ); + X & operator=( X const & ); +}; + +int X::instances = 0; + +int main() +{ + BOOST_TEST( X::instances == 0 ); + + { + X * p = new X; + + BOOST_TEST( X::instances == 1 ); + + boost::checked_delete( p ); + + BOOST_TEST( X::instances == 0 ); + } + + { + X * p = new X[ 3 ]; + + BOOST_TEST( X::instances == 3 ); + + boost::checked_array_delete( p ); + + BOOST_TEST( X::instances == 0 ); + } + + return boost::report_errors(); +} diff --git a/test/visit_each_test.cpp b/test/visit_each_test.cpp new file mode 100644 index 0000000..726188e --- /dev/null +++ b/test/visit_each_test.cpp @@ -0,0 +1,65 @@ +// +// visit_each_test.cpp +// +// 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 +#include +#include + +struct X +{ + int v; + std::string w; +}; + +template inline void visit_each( Visitor & visitor, X const & x ) +{ + using boost::visit_each; + + visit_each( visitor, x.v ); + visit_each( visitor, x.w ); +} + +struct V +{ + int s_; + + V(): s_( 0 ) + { + } + + template< class T > void operator()( T const & t ) + { + } + + void operator()( int const & v ) + { + s_ = s_ * 10 + v; + } + + void operator()( std::string const & w ) + { + s_ = s_ * 10 + w.size(); + } +}; + +int main() +{ + X x = { 5, "test" }; + V v; + + { + using boost::visit_each; + visit_each( v, x ); + } + + BOOST_TEST( v.s_ == 54 ); + + return boost::report_errors(); +}