1
0
forked from boostorg/core

Add more checked_delete tests, a visit_each test.

This commit is contained in:
Peter Dimov
2014-06-03 21:02:36 +03:00
parent d30b4b2ec3
commit 7ebcee675a
5 changed files with 154 additions and 1 deletions

View File

@ -16,7 +16,9 @@ test-suite "core"
[ run addressof_np_test.cpp ] [ run addressof_np_test.cpp ]
[ run addressof_fn_test.cpp ] [ run addressof_fn_test.cpp ]
[ run checked_delete_test.cpp ]
[ compile-fail checked_delete_fail.cpp ] [ compile-fail checked_delete_fail.cpp ]
[ compile-fail checked_delete_fail2.cpp ]
[ compile ref_ct_test.cpp ] [ compile ref_ct_test.cpp ]
[ run ref_test.cpp ] [ run ref_test.cpp ]
@ -43,4 +45,6 @@ test-suite "core"
<toolset>msvc:<cxxflags>"/we4100 /we4101" ] <toolset>msvc:<cxxflags>"/we4100 /we4101" ]
[ run sp_typeinfo_test.cpp ] [ run sp_typeinfo_test.cpp ]
[ run sp_typeinfo_test.cpp : : : <rtti>off : sp_typeinfo_test_no_rtti ] [ run sp_typeinfo_test.cpp : : : <rtti>off : sp_typeinfo_test_no_rtti ]
[ run visit_each_test.cpp ]
; ;

View File

@ -23,6 +23,5 @@ int main()
{ {
Incomplete * p = 0; Incomplete * p = 0;
boost::checked_delete(p); // should cause compile time error boost::checked_delete(p); // should cause compile time error
boost::checked_array_delete(p); // should cause compile time error
return 0; return 0;
} // main } // main

View File

@ -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 <boost/checked_delete.hpp> // 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

View File

@ -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 <boost/core/checked_delete.hpp>
#include <boost/core/lightweight_test.hpp>
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();
}

65
test/visit_each_test.cpp Normal file
View File

@ -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 <boost/visit_each.hpp>
#include <boost/core/lightweight_test.hpp>
#include <string>
struct X
{
int v;
std::string w;
};
template<class Visitor> 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();
}