Merge pull request #2303 from jwillemsen/jwi-803
[ACE_TAO.git] / TAO / tests / Sequence_Unit_Tests / testing_allocation_traits_ut.cpp
blob00be9a2f8e175c5cf0883bcd454332023d5bf6ab
1 /**
2 * @file
4 * @brief Unit test for the testing_allocation_traits.
6 * @author Carlos O'Ryan
7 */
8 #include "testing_allocation_traits.hpp"
9 #include "test_macros.h"
11 #include "tao/SystemException.h"
13 #define CHECK_NO_THROW(statement) \
14 try { statement; } catch(...) { \
15 return 1; }
17 using namespace TAO_VERSIONED_NAMESPACE_NAME::TAO::details;
20 CORBA::ULong const MAXIMUM = 32;
22 template<class value_type>
23 struct Tester
25 typedef unbounded_value_allocation_traits<value_type,true> unbounded;
26 typedef bounded_value_allocation_traits<value_type,MAXIMUM,true> bounded;
28 template<class aspect>
29 int test_allocbuf()
31 expected_calls c(aspect::allocbuf_calls);
33 aspect::allocbuf_calls.failure_countdown(2);
34 value_type * s = 0;
35 CHECK_NO_THROW(s = aspect::allocbuf(4));
36 aspect::freebuf(s);
37 CHECK_THROW(s = aspect::allocbuf(4), testing_exception);
38 CHECK_NO_THROW(s = aspect::allocbuf(4));
39 aspect::freebuf(s);
41 FAIL_RETURN_IF_NOT(c.expect(3), c);
42 return 0;
45 template<class aspect>
46 int test_freebuf()
48 expected_calls c(aspect::freebuf_calls);
50 aspect::freebuf_calls.failure_countdown(2);
51 value_type * s = aspect::allocbuf(4);
52 CHECK_NO_THROW(aspect::freebuf(s));
53 s = aspect::allocbuf(4);
54 CHECK_THROW(aspect::freebuf(s), testing_exception);
55 aspect::freebuf(s);
56 s = aspect::allocbuf(4);
57 CHECK_NO_THROW(aspect::freebuf(s));
59 FAIL_RETURN_IF_NOT(c.expect(4), c);
60 return 0;
63 int test_default_buffer_allocation_value()
65 expected_calls u(unbounded::default_buffer_allocation_calls);
66 expected_calls b(bounded::default_buffer_allocation_calls);
68 value_type * s = unbounded::default_buffer_allocation();
69 FAIL_RETURN_IF_NOT(u.expect(1), u);
70 FAIL_RETURN_IF_NOT(b.expect(0), b);
71 CHECK_EQUAL(static_cast<value_type*>(0), s);
72 bounded::freebuf(s);
74 s = bounded::default_buffer_allocation();
75 FAIL_RETURN_IF_NOT(u.expect(0), u);
76 FAIL_RETURN_IF_NOT(b.expect(1), b);
77 // default_buffer_allocation doesn't allocate a buffer for
78 // bounded sequences (see bug 3042).
79 CHECK_EQUAL(static_cast<value_type*>(0), s);
80 bounded::freebuf(s);
81 return 0;
84 template<class aspect>
85 int test_default_buffer_allocation()
87 expected_calls c(aspect::default_buffer_allocation_calls);
89 aspect::default_buffer_allocation_calls.failure_countdown(2);
90 value_type * s = 0;
91 CHECK_NO_THROW(
92 s = aspect::default_buffer_allocation());
93 aspect::freebuf(s);
94 CHECK_THROW(
95 s = aspect::default_buffer_allocation(), testing_exception);
96 CHECK_NO_THROW(
97 s = aspect::default_buffer_allocation());
98 aspect::freebuf(s);
100 FAIL_RETURN_IF_NOT(c.expect(3), c);
101 return 0;
104 int test_default_buffer_allocation_unbounded()
106 return test_default_buffer_allocation<unbounded>();
109 int test_default_buffer_allocation_bounded()
111 return test_default_buffer_allocation<bounded>();
114 int test_allocbuf_unbounded()
116 return test_allocbuf<unbounded>();
119 int test_allocbuf_bounded()
121 return test_allocbuf<bounded>();
124 int test_freebuf_unbounded()
126 return test_freebuf<unbounded>();
129 int test_freebuf_bounded()
131 return test_freebuf<bounded>();
134 int test_all()
136 int status = 0;
137 status += this->test_default_buffer_allocation_value();
138 status += this->test_default_buffer_allocation_unbounded();
139 status += this->test_default_buffer_allocation_bounded();
140 status += this->test_allocbuf_unbounded();
141 status += this->test_allocbuf_bounded();
142 status += this->test_freebuf_unbounded();
143 status += this->test_freebuf_bounded();
144 return status;
146 Tester() {}
149 struct Foo { int y; };
151 int ACE_TMAIN(int,ACE_TCHAR*[])
153 int status = 0;
158 Tester<int> tester;
159 status += tester.test_all ();
163 Tester<Foo> tester;
164 status += tester.test_all ();
168 Tester<char*> tester;
169 status += tester.test_all ();
172 catch (const ::CORBA::Exception &ex)
174 ex._tao_print_exception("ERROR : unexpected CORBA exception caugth :");
175 ++status;
178 return status;