1 // Boost.Function library
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)
8 // For more information, see http://www.boost.org
10 #include <boost/function.hpp>
11 #include <boost/core/lightweight_test.hpp>
16 using namespace boost
;
18 static int alloc_count
= 0;
19 static int dealloc_count
= 0;
22 struct counting_allocator
: public std::allocator
<T
>
27 typedef counting_allocator
<U
> other
;
35 counting_allocator( counting_allocator
<U
> )
39 T
* allocate(std::size_t n
)
42 return std::allocator
<T
>::allocate(n
);
45 void deallocate(T
* p
, std::size_t n
)
48 std::allocator
<T
>::deallocate(p
, n
);
52 struct enable_small_object_optimization
56 struct disable_small_object_optimization
58 int unused_state_data
[32];
61 template <typename base
>
64 int operator()(int x
, int y
) const { return x
+ y
; }
67 static int do_minus(int x
, int y
) { return x
-y
; }
69 template <typename base
>
70 struct DoNothing
: base
72 void operator()() const {}
75 static void do_nothing() {}
79 function2
<int, int, int> f
;
80 f
.assign( plus_int
<disable_small_object_optimization
>(), counting_allocator
<int>() );
82 BOOST_TEST_EQ( alloc_count
, 1 );
83 BOOST_TEST_EQ( dealloc_count
, 1 );
86 f
.assign( plus_int
<enable_small_object_optimization
>(), counting_allocator
<int>() );
88 BOOST_TEST_EQ( alloc_count
, 0 );
89 BOOST_TEST_EQ( dealloc_count
, 0 );
90 f
.assign( plus_int
<disable_small_object_optimization
>(), std::allocator
<int>() );
92 f
.assign( plus_int
<enable_small_object_optimization
>(), std::allocator
<int>() );
97 f
.assign( &do_minus
, counting_allocator
<int>() );
99 BOOST_TEST_EQ( alloc_count
, 0 );
100 BOOST_TEST_EQ( dealloc_count
, 0 );
101 f
.assign( &do_minus
, std::allocator
<int>() );
107 fv
.assign( DoNothing
<disable_small_object_optimization
>(), counting_allocator
<int>() );
109 BOOST_TEST_EQ( alloc_count
, 1 );
110 BOOST_TEST_EQ( dealloc_count
, 1 );
113 fv
.assign( DoNothing
<enable_small_object_optimization
>(), counting_allocator
<int>() );
115 BOOST_TEST_EQ( alloc_count
, 0 );
116 BOOST_TEST_EQ( dealloc_count
, 0 );
117 fv
.assign( DoNothing
<disable_small_object_optimization
>(), std::allocator
<int>() );
119 fv
.assign( DoNothing
<enable_small_object_optimization
>(), std::allocator
<int>() );
124 fv
.assign( &do_nothing
, counting_allocator
<int>() );
126 BOOST_TEST_EQ( alloc_count
, 0 );
127 BOOST_TEST_EQ( dealloc_count
, 0 );
128 fv
.assign( &do_nothing
, std::allocator
<int>() );
132 fv
.assign(&do_nothing
, std::allocator
<int>() );
133 fv2
.assign(fv
, std::allocator
<int>() );
135 return boost::report_errors();