summaryrefslogtreecommitdiff
path: root/tests/Sequence_Unit_Tests/testing_allocation_traits_ut.cpp
blob: ae7445e53b42971ad7913de39b7e0e7d5896bc74 (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
/**
 * @file
 *
 * @brief Unit test for the testing_allocation_traits.
 *
 * $Id$
 *
 * @author Carlos O'Ryan
 */
#include "testing_allocation_traits.hpp"
#include "test_macros.h"

#define CHECK_NO_THROW(statement) \
try { statement; } catch(...) { \
  return 1; }

using namespace TAO_VERSIONED_NAMESPACE_NAME::TAO::details;



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>
  int 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);
    CHECK_THROW(s = aspect::allocbuf(4), testing_exception);
    CHECK_NO_THROW(s = aspect::allocbuf(4));
    aspect::freebuf(s);

    FAIL_RETURN_IF_NOT(c.expect(3), c);
    return 0;
  }

  template<class aspect>
  int 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);
    CHECK_THROW(aspect::freebuf(s), testing_exception);
    aspect::freebuf(s);
    s = aspect::allocbuf(4);
    CHECK_NO_THROW(aspect::freebuf(s));

    FAIL_RETURN_IF_NOT(c.expect(4), c);
    return 0;
  }

  int 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();
    FAIL_RETURN_IF_NOT(u.expect(1), u);
    FAIL_RETURN_IF_NOT(b.expect(0), b);
    CHECK_EQUAL(static_cast<value_type*>(0), s);
    bounded::freebuf(s);

    s = bounded::default_buffer_allocation();
    FAIL_RETURN_IF_NOT(u.expect(0), u);
    FAIL_RETURN_IF_NOT(b.expect(1), b);
    // default_buffer_allocation doesn't allocate a buffer for
    // bounded sequences (see bug 3042).
    CHECK_EQUAL(static_cast<value_type*>(0), s);
    bounded::freebuf(s);
    return 0;
  }

  template<class aspect>
  int 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);
    CHECK_THROW(
        s = aspect::default_buffer_allocation(), testing_exception);
    CHECK_NO_THROW(
        s = aspect::default_buffer_allocation());
    aspect::freebuf(s);

    FAIL_RETURN_IF_NOT(c.expect(3), c);
    return 0;
  }

  int test_default_buffer_allocation_unbounded()
  {
    return test_default_buffer_allocation<unbounded>();
  }

  int test_default_buffer_allocation_bounded()
  {
    return test_default_buffer_allocation<bounded>();
  }

  int test_allocbuf_unbounded()
  {
    return test_allocbuf<unbounded>();
  }

  int test_allocbuf_bounded()
  {
    return test_allocbuf<bounded>();
  }

  int test_freebuf_unbounded()
  {
    return test_freebuf<unbounded>();
  }

  int test_freebuf_bounded()
  {
    return test_freebuf<bounded>();
  }

  int test_all()
  {
    int status = 0;
    status += this->test_default_buffer_allocation_value();
    status += this->test_default_buffer_allocation_unbounded();
    status += this->test_default_buffer_allocation_bounded();
    status += this->test_allocbuf_unbounded();
    status += this->test_allocbuf_bounded();
    status += this->test_freebuf_unbounded();
    status += this->test_freebuf_bounded();
     return status;
  }
  Tester() {}
};

struct Foo { int y; };

int ACE_TMAIN(int,ACE_TCHAR*[])
{
  int status = 0;
  {
    Tester<int>  tester;
    status += tester.test_all ();
  }

  {
    Tester<Foo> tester;
    status += tester.test_all ();
  }

  {
    Tester<char*>  tester;
    status += tester.test_all ();
  }

  return status;
}