File: allocator_test.cpp

package info (click to toggle)
boost1.35 1.35.0-5
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 203,856 kB
  • ctags: 337,867
  • sloc: cpp: 938,683; xml: 56,847; ansic: 41,589; python: 18,999; sh: 11,566; makefile: 664; perl: 494; yacc: 456; asm: 353; csh: 6
file content (94 lines) | stat: -rw-r--r-- 1,841 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Boost.Function library

//  Copyright Douglas Gregor 2001-2003. Use, modification and
//  distribution is subject to the Boost Software License, Version
//  1.0. (See accompanying file LICENSE_1_0.txt or copy at
//  http://www.boost.org/LICENSE_1_0.txt)

// For more information, see http://www.boost.org

#include <boost/test/minimal.hpp>
#include <cassert>
#include <functional>
#include <boost/function.hpp>

using namespace std;
using namespace boost;

static int alloc_count = 0;
static int dealloc_count = 0;

template<typename T>
struct counting_allocator : public std::allocator<T>
{
  template<typename U>
  struct rebind
  {
    typedef counting_allocator<U> other;
  };


  T* allocate(std::size_t n)
  {
    alloc_count++;
    return std::allocator<T>::allocate(n);
  }

  void deallocate(T* p, std::size_t n)
  {
    dealloc_count++;
    std::allocator<T>::deallocate(p, n);
  }
};

struct plus_int
{
  int operator()(int x, int y) const { return x + y; }

  int unused_state_data[32];
};

static int do_minus(int x, int y) { return x-y; }

struct DoNothing
{
  void operator()() const {}

  int unused_state_data[32];
};

static void do_nothing() {}

int
test_main(int, char*[])
{
  function2<int, int, int, counting_allocator<int> > f;
  f = plus_int();
  f.clear();
  BOOST_CHECK(alloc_count == 1);
  BOOST_CHECK(dealloc_count == 1);

  alloc_count = 0;
  dealloc_count = 0;
  f = &do_minus;
  f.clear();
  BOOST_CHECK(alloc_count == 0);
  BOOST_CHECK(dealloc_count == 0);

  function0<void, counting_allocator<int> > fv;
  alloc_count = 0;
  dealloc_count = 0;
  fv = DoNothing();
  fv.clear();
  BOOST_CHECK(alloc_count == 1);
  BOOST_CHECK(dealloc_count == 1);

  alloc_count = 0;
  dealloc_count = 0;
  fv = &do_nothing;
  fv.clear();
  BOOST_CHECK(alloc_count == 0);
  BOOST_CHECK(dealloc_count == 0);

  return 0;
}