Changes to attempt to silence bcc64x
[ACE_TAO.git] / TAO / tests / Sequence_Unit_Tests / bounded_value_sequence_ut.cpp
blobed94624487c1125835e34153d85ae0a48158f687
1 /**
2 * @file
4 * @brief Unit test for bounded sequences of value types (integers,
5 * structures, etc.)
7 * @author Carlos O'Ryan
8 */
9 #include "testing_allocation_traits.hpp"
10 #include "testing_range_checking.hpp"
12 #include "tao/Bounded_Value_Sequence_T.h"
14 #include "value_sequence_tester.hpp"
16 #include "test_macros.h"
19 using namespace TAO_VERSIONED_NAMESPACE_NAME::TAO;
21 CORBA::ULong const MAXIMUM = 32;
23 typedef bounded_value_sequence<int,MAXIMUM> tested_sequence;
24 typedef tested_sequence::element_traits tested_element_traits;
25 typedef tested_sequence::allocation_traits tested_allocation_traits;
26 typedef details::range_checking<int,true> range;
28 struct Tester
30 typedef tested_sequence::value_type value_type;
32 int test_set_length_less_than_maximum()
34 expected_calls a(tested_allocation_traits::allocbuf_calls);
35 expected_calls f(tested_allocation_traits::freebuf_calls);
37 tested_sequence x;
39 x.length(8);
40 CHECK_EQUAL(CORBA::ULong(MAXIMUM), x.maximum());
41 CHECK_EQUAL(CORBA::ULong(8), x.length());
42 CHECK_EQUAL(true, x.release());
44 // Naturally buffer in x is allocated after length() was called.
45 FAIL_RETURN_IF_NOT(a.expect(1), a);
46 FAIL_RETURN_IF_NOT(f.expect(1), f);
47 return 0;
50 int test_set_length_more_than_maximum()
52 tested_sequence x;
54 CHECK_THROW(x.length(64), std::runtime_error);
55 return 0;
58 value_type * alloc_and_init_buffer()
60 value_type * buf = tested_sequence::allocbuf();
61 buf[0] = 1; buf[1] = 4; buf[2] = 9; buf[3] = 16;
63 return buf;
66 int test_regression_2201 ()
68 value_type * buffer = alloc_and_init_buffer();
70 expected_calls a(tested_allocation_traits::allocbuf_calls);
71 expected_calls f(tested_allocation_traits::freebuf_calls);
73 tested_sequence x(32, buffer, true);
74 CHECK_EQUAL(CORBA::ULong(32), x.maximum());
75 CHECK_EQUAL(CORBA::ULong(32), x.length());
76 CHECK_EQUAL(buffer, x.get_buffer());
77 CHECK_EQUAL(int( 1), x[0]);
78 CHECK_EQUAL(int( 4), x[1]);
79 CHECK_EQUAL(int( 9), x[2]);
80 CHECK_EQUAL(int(16), x[3]);
81 CHECK_EQUAL(true, x.release());
82 x.length (3);
83 CHECK_EQUAL(CORBA::ULong(32), x.maximum());
84 CHECK_EQUAL(CORBA::ULong(3), x.length());
85 x.length (4);
86 CHECK_EQUAL(CORBA::ULong(32), x.maximum());
87 CHECK_EQUAL(CORBA::ULong(4), x.length());
88 CHECK_EQUAL(int( 0), x[3]);
90 FAIL_RETURN_IF_NOT(a.expect(0), a);
91 FAIL_RETURN_IF_NOT(f.expect(1), f);
92 return 0;
95 int test_buffer_constructor_default()
97 value_type * buffer = alloc_and_init_buffer();
99 expected_calls a(tested_allocation_traits::allocbuf_calls);
100 expected_calls f(tested_allocation_traits::freebuf_calls);
102 tested_sequence a(4, buffer);
103 CHECK_EQUAL(CORBA::ULong(32), a.maximum());
104 CHECK_EQUAL(CORBA::ULong(4), a.length());
105 CHECK_EQUAL(buffer, a.get_buffer());
106 CHECK_EQUAL(int( 1), a[0]);
107 CHECK_EQUAL(int( 4), a[1]);
108 CHECK_EQUAL(int( 9), a[2]);
109 CHECK_EQUAL(int(16), a[3]);
110 CHECK_EQUAL(false, a.release());
112 FAIL_RETURN_IF_NOT(a.expect(0), a);
113 FAIL_RETURN_IF_NOT(f.expect(0), f);
114 tested_sequence::freebuf(buffer);
115 return 0;
118 int test_buffer_constructor_false()
120 value_type * buffer = alloc_and_init_buffer();
121 expected_calls a(tested_allocation_traits::allocbuf_calls);
122 expected_calls f(tested_allocation_traits::freebuf_calls);
124 tested_sequence a(4, buffer, false);
125 CHECK_EQUAL(CORBA::ULong(32), a.maximum());
126 CHECK_EQUAL(CORBA::ULong(4), a.length());
127 CHECK_EQUAL(buffer, a.get_buffer());
128 CHECK_EQUAL(int( 1), a[0]);
129 CHECK_EQUAL(int( 4), a[1]);
130 CHECK_EQUAL(int( 9), a[2]);
131 CHECK_EQUAL(int(16), a[3]);
132 CHECK_EQUAL(false, a.release());
134 FAIL_RETURN_IF_NOT(a.expect(0), a);
135 FAIL_RETURN_IF_NOT(f.expect(0), f);
136 tested_sequence::freebuf(buffer);
137 return 0;
140 int test_buffer_constructor_true()
142 value_type * buffer = alloc_and_init_buffer();
143 expected_calls a(tested_allocation_traits::allocbuf_calls);
144 expected_calls f(tested_allocation_traits::freebuf_calls);
146 tested_sequence a(4, buffer, true);
147 CHECK_EQUAL(CORBA::ULong(32), a.maximum());
148 CHECK_EQUAL(CORBA::ULong(4), a.length());
149 CHECK_EQUAL(buffer, a.get_buffer());
150 CHECK_EQUAL(int( 1), a[0]);
151 CHECK_EQUAL(int( 4), a[1]);
152 CHECK_EQUAL(int( 9), a[2]);
153 CHECK_EQUAL(int(16), a[3]);
154 CHECK_EQUAL(true, a.release());
156 FAIL_RETURN_IF_NOT(a.expect(0), a);
157 FAIL_RETURN_IF_NOT(f.expect(1), f);
158 return 0;
161 int test_replace_default()
163 value_type * buffer = alloc_and_init_buffer();
165 expected_calls c(tested_allocation_traits::allocbuf_calls);
166 expected_calls f(tested_allocation_traits::freebuf_calls);
168 tested_sequence a;
169 a.replace(4, buffer);
170 FAIL_RETURN_IF_NOT(c.expect(0), c);
171 // Default constructed sequence doesn't allocate a buffer.
172 FAIL_RETURN_IF_NOT(f.expect(0), f);
174 CHECK_EQUAL(CORBA::ULong(32), a.maximum());
175 CHECK_EQUAL(CORBA::ULong(4), a.length());
176 CHECK_EQUAL(buffer, a.get_buffer());
177 CHECK_EQUAL(int( 1), a[0]);
178 CHECK_EQUAL(int( 4), a[1]);
179 CHECK_EQUAL(int( 9), a[2]);
180 CHECK_EQUAL(int(16), a[3]);
181 CHECK_EQUAL(false, a.release());
183 FAIL_RETURN_IF_NOT(c.expect(0), c);
184 FAIL_RETURN_IF_NOT(f.expect(0), f);
185 tested_sequence::freebuf(buffer);
186 return 0;
189 int test_replace_false()
191 value_type * buffer = alloc_and_init_buffer();
192 expected_calls c(tested_allocation_traits::allocbuf_calls);
193 expected_calls f(tested_allocation_traits::freebuf_calls);
196 tested_sequence a;
197 a.replace(4, buffer, false);
198 FAIL_RETURN_IF_NOT(c.expect(0), c);
199 // Default constructed sequence doesn't allocate a buffer.
200 FAIL_RETURN_IF_NOT(f.expect(0), f);
202 CHECK_EQUAL(CORBA::ULong(32), a.maximum());
203 CHECK_EQUAL(CORBA::ULong(4), a.length());
204 CHECK_EQUAL(buffer, a.get_buffer());
205 CHECK_EQUAL(int( 1), a[0]);
206 CHECK_EQUAL(int( 4), a[1]);
207 CHECK_EQUAL(int( 9), a[2]);
208 CHECK_EQUAL(int(16), a[3]);
209 CHECK_EQUAL(false, a.release());
211 FAIL_RETURN_IF_NOT(c.expect(0), c);
212 FAIL_RETURN_IF_NOT(f.expect(0), f);
213 tested_sequence::freebuf(buffer);
214 return 0;
217 int test_replace_true()
219 value_type * buffer = alloc_and_init_buffer();
220 expected_calls c(tested_allocation_traits::allocbuf_calls);
221 expected_calls f(tested_allocation_traits::freebuf_calls);
224 tested_sequence a;
225 a.replace(4, buffer, true);
226 FAIL_RETURN_IF_NOT(c.expect(0), c);
227 // Default constructed sequence doesn't allocate a buffer.
228 FAIL_RETURN_IF_NOT(f.expect(0), f);
230 CHECK_EQUAL(CORBA::ULong(32), a.maximum());
231 CHECK_EQUAL(CORBA::ULong(4), a.length());
232 CHECK_EQUAL(buffer, a.get_buffer());
233 CHECK_EQUAL(int( 1), a[0]);
234 CHECK_EQUAL(int( 4), a[1]);
235 CHECK_EQUAL(int( 9), a[2]);
236 CHECK_EQUAL(int(16), a[3]);
237 CHECK_EQUAL(true, a.release());
239 FAIL_RETURN_IF_NOT(c.expect(0), c);
240 FAIL_RETURN_IF_NOT(f.expect(1), f);
241 return 0;
244 int test_get_buffer_default()
246 value_type * buffer = alloc_and_init_buffer();
247 tested_sequence a(4, buffer, true);
248 CHECK_EQUAL(a.get_buffer(), buffer);
249 return 0;
252 int test_get_buffer_false()
254 value_type * buffer = alloc_and_init_buffer();
255 tested_sequence a(4, buffer, true);
256 CHECK_EQUAL(a.get_buffer(), buffer);
257 return 0;
260 int test_get_buffer_true_with_release_false()
262 value_type * buffer = alloc_and_init_buffer();
263 tested_sequence a(4, buffer, false);
264 CHECK(0 == a.get_buffer(true));
265 tested_sequence::freebuf(buffer);
266 return 0;
269 int test_get_buffer_true_with_release_true()
271 value_type * buffer = alloc_and_init_buffer();
272 expected_calls c(tested_allocation_traits::default_buffer_allocation_calls);
273 expected_calls f(tested_allocation_traits::freebuf_calls);
275 tested_sequence a(4, buffer, true);
276 CHECK_EQUAL(buffer, a.get_buffer(true));
278 tested_sequence const & b = a;
279 CHECK_EQUAL(MAXIMUM, b.maximum());
280 CHECK_EQUAL(0UL, b.length());
281 CHECK(0 != b.get_buffer());
282 CHECK_EQUAL(true, b.release());
284 FAIL_RETURN_IF_NOT(c.expect(1), c);
286 CHECK(buffer != b.get_buffer());
288 FAIL_RETURN_IF_NOT(c.expect(0), c);
289 FAIL_RETURN_IF_NOT(f.expect(1), c);
290 tested_sequence::freebuf(buffer);
291 return 0;
294 int test_all()
296 int status = 0;
297 status += this->test_set_length_less_than_maximum();
298 status += this->test_set_length_more_than_maximum();
299 status += this->test_regression_2201();
300 status += this->test_buffer_constructor_default();
301 status += this->test_buffer_constructor_false();
302 status += this->test_buffer_constructor_true();
303 status += this->test_replace_default();
304 status += this->test_replace_false();
305 status += this->test_replace_true();
306 status += this->test_get_buffer_false();
307 status += this->test_get_buffer_true_with_release_false();
308 status += this->test_get_buffer_true_with_release_true();
309 return status;
311 Tester() {}
314 int ACE_TMAIN(int,ACE_TCHAR*[])
316 int status = 0;
320 Tester tester;
321 status += tester.test_all ();
325 typedef value_sequence_tester<tested_sequence,tested_allocation_traits> common;
326 common tester;
327 status += tester.test_all ();
330 catch (const ::CORBA::Exception &ex)
332 ex._tao_print_exception("ERROR : unexpected CORBA exception caugth :");
333 ++status;
336 return status;