| 1 | // Boost checked_delete test program |
|---|---|
| 2 | |
| 3 | // Copyright Beman Dawes 2001. Copyright 2014 Peter Dimov. |
| 4 | // Distributed under the Boost Software License, Version 1.0. |
| 5 | // See accompanying file LICENSE_1_0.txt or copy at |
| 6 | // http://www.boost.org/LICENSE_1_0.txt |
| 7 | |
| 8 | #include <boost/core/checked_delete.hpp> |
| 9 | #include <boost/core/lightweight_test.hpp> |
| 10 | |
| 11 | struct X |
| 12 | { |
| 13 | static int instances; |
| 14 | |
| 15 | X() |
| 16 | { |
| 17 | ++instances; |
| 18 | } |
| 19 | |
| 20 | ~X() |
| 21 | { |
| 22 | --instances; |
| 23 | } |
| 24 | |
| 25 | private: |
| 26 | |
| 27 | X( X const & ); |
| 28 | X & operator=( X const & ); |
| 29 | }; |
| 30 | |
| 31 | int X::instances = 0; |
| 32 | |
| 33 | int main() |
| 34 | { |
| 35 | BOOST_TEST( X::instances == 0 ); |
| 36 | |
| 37 | { |
| 38 | X * p = new X; |
| 39 | |
| 40 | BOOST_TEST( X::instances == 1 ); |
| 41 | |
| 42 | boost::checked_delete( x: p ); |
| 43 | |
| 44 | BOOST_TEST( X::instances == 0 ); |
| 45 | } |
| 46 | |
| 47 | { |
| 48 | X * p = new X[ 3 ]; |
| 49 | |
| 50 | BOOST_TEST( X::instances == 3 ); |
| 51 | |
| 52 | boost::checked_array_delete( x: p ); |
| 53 | |
| 54 | BOOST_TEST( X::instances == 0 ); |
| 55 | } |
| 56 | |
| 57 | return boost::report_errors(); |
| 58 | } |
| 59 |
