summaryrefslogtreecommitdiff
path: root/TAO/tests/Sequence_Unit_Tests/testing_allocation_traits_ut.cpp
blob: de4426c4f168f618944508112c867253dbd3cf47 (plain)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/**
 * @file
 *
 * @brief Unit test for the testing_allocation_traits.
 *
 * $Id$
 *
 * @author Carlos O'Ryan
 */
#include "testing_allocation_traits.hpp"

#include <boost/test/unit_test.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>

#define CHECK_NO_THROW(statement) \
try { statement; } catch(...) { \
  BOOST_ERROR("unexpected exception raised"); }

using namespace TAO_VERSIONED_NAMESPACE_NAME::TAO::details;

using namespace boost::unit_test_framework;

CORBA::ULong const MAXIMUM = 32;

template<class value_type>
struct Tester
{
  typedef unbounded_value_allocation_traits<value_type,true> unbounded;
  typedef bounded_value_allocation_traits<value_type,MAXIMUM,true> bounded;

  template<class aspect>
  void test_allocbuf()
  {
    expected_calls c(aspect::allocbuf_calls);

    aspect::allocbuf_calls.failure_countdown(2);
    value_type * s = 0;
    CHECK_NO_THROW(s = aspect::allocbuf(4));
    aspect::freebuf(s);
    BOOST_CHECK_THROW(s = aspect::allocbuf(4), testing_exception);
    CHECK_NO_THROW(s = aspect::allocbuf(4));
    aspect::freebuf(s);

    BOOST_CHECK_MESSAGE(c.expect(3), c);
  }

  template<class aspect>
  void test_freebuf()
  {
    expected_calls c(aspect::freebuf_calls);

    aspect::freebuf_calls.failure_countdown(2);
    value_type * s = aspect::allocbuf(4);
    CHECK_NO_THROW(aspect::freebuf(s));
    s = aspect::allocbuf(4);
    BOOST_CHECK_THROW(aspect::freebuf(s), testing_exception);
    aspect::freebuf(s);
    s = aspect::allocbuf(4);
    CHECK_NO_THROW(aspect::freebuf(s));

    BOOST_CHECK_MESSAGE(c.expect(4), c);
  }

  void test_default_buffer_allocation_value()
  {
    expected_calls u(unbounded::default_buffer_allocation_calls);
    expected_calls b(bounded::default_buffer_allocation_calls);

    value_type * s = unbounded::default_buffer_allocation();
    BOOST_CHECK_MESSAGE(u.expect(1), u);
    BOOST_CHECK_MESSAGE(b.expect(0), b);
    BOOST_CHECK_EQUAL(static_cast<value_type*>(0), s);
    bounded::freebuf(s);

    s = bounded::default_buffer_allocation();
    BOOST_CHECK_MESSAGE(u.expect(0), u);
    BOOST_CHECK_MESSAGE(b.expect(1), b);
    BOOST_CHECK(static_cast<value_type*>(0) != s);
    bounded::freebuf(s);
  }

  template<class aspect>
  void test_default_buffer_allocation()
  {
    expected_calls c(aspect::default_buffer_allocation_calls);

    aspect::default_buffer_allocation_calls.failure_countdown(2);
    value_type * s = 0;
    CHECK_NO_THROW(
        s = aspect::default_buffer_allocation());
    aspect::freebuf(s);
    BOOST_CHECK_THROW(
        s = aspect::default_buffer_allocation(), testing_exception);
    CHECK_NO_THROW(
        s = aspect::default_buffer_allocation());
    aspect::freebuf(s);

    BOOST_CHECK_MESSAGE(c.expect(3), c);
  }

  void test_default_buffer_allocation_unbounded()
  {
    test_default_buffer_allocation<unbounded>();
  }

  void test_default_buffer_allocation_bounded()
  {
    test_default_buffer_allocation<bounded>();
  }

  void test_allocbuf_unbounded()
  {
    test_allocbuf<unbounded>();
  }

  void test_allocbuf_bounded()
  {
    test_allocbuf<bounded>();
  }

  void test_freebuf_unbounded()
  {
    test_freebuf<unbounded>();
  }

  void test_freebuf_bounded()
  {
    test_freebuf<bounded>();
  }

  void add_all(test_suite * ts)
  {
    boost::shared_ptr<Tester> shared_this(self_);

    ts->add(BOOST_CLASS_TEST_CASE(
                &Tester::test_default_buffer_allocation_value,
                shared_this));
    ts->add(BOOST_CLASS_TEST_CASE(
                &Tester::test_default_buffer_allocation_unbounded,
                shared_this));
    ts->add(BOOST_CLASS_TEST_CASE(
                &Tester::test_default_buffer_allocation_bounded,
                shared_this));
    ts->add(BOOST_CLASS_TEST_CASE(
                &Tester::test_allocbuf_unbounded,
                shared_this));
    ts->add(BOOST_CLASS_TEST_CASE(
                &Tester::test_allocbuf_bounded,
                shared_this));
    ts->add(BOOST_CLASS_TEST_CASE(
                &Tester::test_freebuf_unbounded,
                shared_this));
    ts->add(BOOST_CLASS_TEST_CASE(
                &Tester::test_freebuf_bounded,
                shared_this));
  }

  static boost::shared_ptr<Tester> allocate()
  {
    boost::shared_ptr<Tester> ptr(new Tester);
    ptr->self_ = ptr;

    return ptr;
  }

private:
  Tester() {}

  boost::weak_ptr<Tester> self_;
};

struct Foo { int y; };

ACE_Proper_Export_Flag test_suite *
init_unit_test_suite(int, char*[])
{
  test_suite * ts =
      BOOST_TEST_SUITE("testing allocation traits unit test");

  {
    boost::shared_ptr<Tester<int> > tester(
        Tester<int>::allocate());
    tester->add_all(ts);
  }

  {
    boost::shared_ptr<Tester<Foo> > tester(
        Tester<Foo>::allocate());
    tester->add_all(ts);
  }

  {
    boost::shared_ptr<Tester<char*> > tester(
        Tester<char*>::allocate());
    tester->add_all(ts);
  }

  return ts;
}