Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/function/test/allocator_test.cpp @ 12

Last change on this file since 12 was 12, checked in by landauf, 18 years ago

added boost

File size: 1.8 KB
Line 
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
15using namespace std;
16using namespace boost;
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
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
44struct plus_int
45{
46  int operator()(int x, int y) const { return x + y; }
47
48  int unused_state_data;
49};
50
51static int do_minus(int x, int y) { return x-y; }
52
53struct DoNothing
54{
55  void operator()() const {}
56
57  int unused_state_data;
58};
59
60static void do_nothing() {}
61
62int
63test_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}
Note: See TracBrowser for help on using the repository browser.