| 1 | // Boost.Function library |
|---|
| 2 | |
|---|
| 3 | // Copyright Douglas Gregor 2001-2003. Use, modification and |
|---|
| 4 | // distribution is subject to the Boost Software License, Version |
|---|
| 5 | // 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
|---|
| 6 | // http://www.boost.org/LICENSE_1_0.txt) |
|---|
| 7 | |
|---|
| 8 | // For more information, see http://www.boost.org |
|---|
| 9 | |
|---|
| 10 | #include <boost/test/minimal.hpp> |
|---|
| 11 | #include <cassert> |
|---|
| 12 | #include <functional> |
|---|
| 13 | #include <boost/function.hpp> |
|---|
| 14 | |
|---|
| 15 | using namespace std; |
|---|
| 16 | using namespace boost; |
|---|
| 17 | |
|---|
| 18 | static int alloc_count = 0; |
|---|
| 19 | static int dealloc_count = 0; |
|---|
| 20 | |
|---|
| 21 | template<typename T> |
|---|
| 22 | struct counting_allocator : public std::allocator<T> |
|---|
| 23 | { |
|---|
| 24 | template<typename U> |
|---|
| 25 | struct rebind |
|---|
| 26 | { |
|---|
| 27 | typedef counting_allocator<U> other; |
|---|
| 28 | }; |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | T* allocate(std::size_t n) |
|---|
| 32 | { |
|---|
| 33 | alloc_count++; |
|---|
| 34 | return std::allocator<T>::allocate(n); |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | void deallocate(T* p, std::size_t n) |
|---|
| 38 | { |
|---|
| 39 | dealloc_count++; |
|---|
| 40 | std::allocator<T>::deallocate(p, n); |
|---|
| 41 | } |
|---|
| 42 | }; |
|---|
| 43 | |
|---|
| 44 | struct plus_int |
|---|
| 45 | { |
|---|
| 46 | int operator()(int x, int y) const { return x + y; } |
|---|
| 47 | |
|---|
| 48 | int unused_state_data[32]; |
|---|
| 49 | }; |
|---|
| 50 | |
|---|
| 51 | static int do_minus(int x, int y) { return x-y; } |
|---|
| 52 | |
|---|
| 53 | struct DoNothing |
|---|
| 54 | { |
|---|
| 55 | void operator()() const {} |
|---|
| 56 | |
|---|
| 57 | int unused_state_data[32]; |
|---|
| 58 | }; |
|---|
| 59 | |
|---|
| 60 | static void do_nothing() {} |
|---|
| 61 | |
|---|
| 62 | int |
|---|
| 63 | test_main(int, char*[]) |
|---|
| 64 | { |
|---|
| 65 | function2<int, int, int, counting_allocator<int> > f; |
|---|
| 66 | f = plus_int(); |
|---|
| 67 | f.clear(); |
|---|
| 68 | BOOST_CHECK(alloc_count == 1); |
|---|
| 69 | BOOST_CHECK(dealloc_count == 1); |
|---|
| 70 | |
|---|
| 71 | alloc_count = 0; |
|---|
| 72 | dealloc_count = 0; |
|---|
| 73 | f = &do_minus; |
|---|
| 74 | f.clear(); |
|---|
| 75 | BOOST_CHECK(alloc_count == 0); |
|---|
| 76 | BOOST_CHECK(dealloc_count == 0); |
|---|
| 77 | |
|---|
| 78 | function0<void, counting_allocator<int> > fv; |
|---|
| 79 | alloc_count = 0; |
|---|
| 80 | dealloc_count = 0; |
|---|
| 81 | fv = DoNothing(); |
|---|
| 82 | fv.clear(); |
|---|
| 83 | BOOST_CHECK(alloc_count == 1); |
|---|
| 84 | BOOST_CHECK(dealloc_count == 1); |
|---|
| 85 | |
|---|
| 86 | alloc_count = 0; |
|---|
| 87 | dealloc_count = 0; |
|---|
| 88 | fv = &do_nothing; |
|---|
| 89 | fv.clear(); |
|---|
| 90 | BOOST_CHECK(alloc_count == 0); |
|---|
| 91 | BOOST_CHECK(dealloc_count == 0); |
|---|
| 92 | |
|---|
| 93 | return 0; |
|---|
| 94 | } |
|---|