blob: de91e6b4c061f6106b280f5d41cf71eb0b059847 [file] [log] [blame]
Jeff Thompsona28eed82013-08-22 16:21:10 -07001// 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
15using namespace std;
16using namespace ndnboost;
17
18static int alloc_count = 0;
19static int dealloc_count = 0;
20
21template<typename T>
22struct counting_allocator : public std::allocator<T>
23{
24 template<typename U>
25 struct rebind
26 {
27 typedef counting_allocator<U> other;
28 };
29
30 counting_allocator()
31 {
32 }
33
34 template<typename U>
35 counting_allocator( counting_allocator<U> )
36 {
37 }
38
39 T* allocate(std::size_t n)
40 {
41 alloc_count++;
42 return std::allocator<T>::allocate(n);
43 }
44
45 void deallocate(T* p, std::size_t n)
46 {
47 dealloc_count++;
48 std::allocator<T>::deallocate(p, n);
49 }
50};
51
52struct enable_small_object_optimization
53{
54};
55
56struct disable_small_object_optimization
57{
58 int unused_state_data[32];
59};
60
61template <typename base>
62struct plus_int: base
63{
64 int operator()(int x, int y) const { return x + y; }
65};
66
67static int do_minus(int x, int y) { return x-y; }
68
69template <typename base>
70struct DoNothing: base
71{
72 void operator()() const {}
73};
74
75static void do_nothing() {}
76
77int
78test_main(int, char*[])
79{
80 function2<int, int, int> f;
81 f.assign( plus_int<disable_small_object_optimization>(), counting_allocator<int>() );
82 f.clear();
83 BOOST_CHECK(alloc_count == 1);
84 BOOST_CHECK(dealloc_count == 1);
85 alloc_count = 0;
86 dealloc_count = 0;
87 f.assign( plus_int<enable_small_object_optimization>(), counting_allocator<int>() );
88 f.clear();
89 BOOST_CHECK(alloc_count == 0);
90 BOOST_CHECK(dealloc_count == 0);
91 f.assign( plus_int<disable_small_object_optimization>(), std::allocator<int>() );
92 f.clear();
93 f.assign( plus_int<enable_small_object_optimization>(), std::allocator<int>() );
94 f.clear();
95
96 alloc_count = 0;
97 dealloc_count = 0;
98 f.assign( &do_minus, counting_allocator<int>() );
99 f.clear();
100 BOOST_CHECK(alloc_count == 0);
101 BOOST_CHECK(dealloc_count == 0);
102 f.assign( &do_minus, std::allocator<int>() );
103 f.clear();
104
105 function0<void> fv;
106 alloc_count = 0;
107 dealloc_count = 0;
108 fv.assign( DoNothing<disable_small_object_optimization>(), counting_allocator<int>() );
109 fv.clear();
110 BOOST_CHECK(alloc_count == 1);
111 BOOST_CHECK(dealloc_count == 1);
112 alloc_count = 0;
113 dealloc_count = 0;
114 fv.assign( DoNothing<enable_small_object_optimization>(), counting_allocator<int>() );
115 fv.clear();
116 BOOST_CHECK(alloc_count == 0);
117 BOOST_CHECK(dealloc_count == 0);
118 fv.assign( DoNothing<disable_small_object_optimization>(), std::allocator<int>() );
119 fv.clear();
120 fv.assign( DoNothing<enable_small_object_optimization>(), std::allocator<int>() );
121 fv.clear();
122
123 alloc_count = 0;
124 dealloc_count = 0;
125 fv.assign( &do_nothing, counting_allocator<int>() );
126 fv.clear();
127 BOOST_CHECK(alloc_count == 0);
128 BOOST_CHECK(dealloc_count == 0);
129 fv.assign( &do_nothing, std::allocator<int>() );
130 fv.clear();
131
132 function0<void> fv2;
133 fv.assign(&do_nothing, std::allocator<int>() );
134 fv2.assign(fv, std::allocator<int>() );
135
136 return 0;
137}