Merge pull request #2301 from sonndinh/remove-dup-reactor-functions
[ACE_TAO.git] / TAO / tests / Sequence_Unit_Tests / bounded_string_sequence_ut.cpp
blob32723f71a70d4e3d70970a97159ae3b439576f5d
1 /**
2 * @file
4 * @brief Unit test for bounded string sequences.
6 * @author Carlos O'Ryan
7 */
8 #define TAO_USER_DEFINED_SEQUENCE_RANGE_CHECKING_INCLUDE \
9 "testing_range_checking.hpp"
11 #include "testing_string_traits.hpp"
12 #include "tao/String_Traits_T.h"
13 #include "testing_allocation_traits.hpp"
15 #include "tao/Bounded_Basic_String_Sequence_T.h"
16 #include "tao/CORBA_String.h"
18 #include "string_sequence_tester.hpp"
20 #include "ace/OS_NS_string.h"
22 #include "test_macros.h"
25 using namespace TAO_VERSIONED_NAMESPACE_NAME::TAO;
27 CORBA::ULong const MAXIMUM = 32;
29 template<class tested_sequence>
30 struct Tester
32 typedef typename tested_sequence::character_type char_type;
33 typedef string_sequence_test_helpers<char_type> helper;
34 typedef typename tested_sequence::value_type value_type;
35 typedef typename tested_sequence::const_value_type const_value_type;
37 typedef typename tested_sequence::element_traits tested_element_traits;
38 typedef typename tested_sequence::allocation_traits tested_allocation_traits;
39 typedef TAO::details::range_checking<value_type,true> range;
41 int test_set_length_less_than_maximum()
43 expected_calls a(tested_allocation_traits::allocbuf_calls);
44 expected_calls f(tested_allocation_traits::freebuf_calls);
45 expected_calls i(tested_element_traits::default_initializer_calls);
47 tested_sequence x;
49 x.length(8);
50 // length() after default constructed sequence leads to
51 // buffer allocation.
52 FAIL_RETURN_IF_NOT(a.expect(1), a);
53 CHECK_EQUAL(CORBA::ULong(MAXIMUM), x.maximum());
54 CHECK_EQUAL(CORBA::ULong(8), x.length());
55 CHECK_EQUAL(true, x.release());
57 // 32 is here because allocbuf for bounded sequences
58 // initializes all elements.
59 FAIL_RETURN_IF_NOT(i.expect(32), i);
61 FAIL_RETURN_IF_NOT(f.expect(1), f);
62 return 0;
65 int test_set_length_more_than_maximum()
67 tested_sequence x;
69 CHECK_THROW(x.length(64), std::runtime_error);
70 return 0;
73 value_type * alloc_and_init_buffer()
75 value_type * buf = tested_sequence::allocbuf();
76 tested_element_traits::release (buf[0]);
77 buf[0] = helper::to_string(1);
78 tested_element_traits::release (buf[1]);
79 buf[1] = helper::to_string(4);
80 tested_element_traits::release (buf[2]);
81 buf[2] = helper::to_string(9);
82 tested_element_traits::release (buf[3]);
83 buf[3] = helper::to_string(16);
85 return buf;
88 int check_values(tested_sequence const & a)
90 CHECK(helper::compare(1, a[0]));
91 CHECK(helper::compare(4, a[1]));
92 CHECK(helper::compare(9, a[2]));
93 CHECK(helper::compare(16, a[3]));
94 return 0;
97 int test_regression_2201()
99 value_type * buffer = alloc_and_init_buffer();
100 expected_calls a(tested_allocation_traits::allocbuf_calls);
101 expected_calls f(tested_allocation_traits::freebuf_calls);
102 expected_calls r(tested_element_traits::release_calls);
104 tested_sequence x(4, buffer, true);
105 CHECK_EQUAL(CORBA::ULong(MAXIMUM), x.maximum());
106 CHECK_EQUAL(CORBA::ULong(4), x.length());
107 CHECK_EQUAL(buffer, x.get_buffer());
108 CHECK_EQUAL(true, x.release());
109 x.length (3);
110 CHECK_EQUAL(CORBA::ULong(3), x.length());
111 x.length (4);
112 CHECK_EQUAL(CORBA::ULong(4), x.length());
113 CHECK(helper::compare_empty(x[3]));
115 FAIL_RETURN_IF_NOT(a.expect(0), a);
116 FAIL_RETURN_IF_NOT(f.expect(1), f);
117 // 1 additional release call happens when we shrink
118 // the sequence to length 3.
119 FAIL_RETURN_IF_NOT(r.expect(MAXIMUM + 1), r);
120 return 0;
123 int test_buffer_constructor_default()
125 value_type * buffer = alloc_and_init_buffer();
126 expected_calls a(tested_allocation_traits::allocbuf_calls);
127 expected_calls f(tested_allocation_traits::freebuf_calls);
128 expected_calls r(tested_element_traits::release_calls);
130 tested_sequence a(4, buffer, false);
131 CHECK_EQUAL(CORBA::ULong(MAXIMUM), a.maximum());
132 CHECK_EQUAL(CORBA::ULong(4), a.length());
133 CHECK_EQUAL(buffer, a.get_buffer());
134 CHECK_EQUAL(false, a.release());
135 check_values(a);
137 FAIL_RETURN_IF_NOT(a.expect(0), a);
138 FAIL_RETURN_IF_NOT(f.expect(0), f);
139 tested_sequence::freebuf(buffer);
140 FAIL_RETURN_IF_NOT(r.expect(MAXIMUM), r);
141 return 0;
144 int test_buffer_constructor_false()
146 value_type * buffer = alloc_and_init_buffer();
147 expected_calls a(tested_allocation_traits::allocbuf_calls);
148 expected_calls f(tested_allocation_traits::freebuf_calls);
149 expected_calls r(tested_element_traits::release_calls);
151 tested_sequence a(4, buffer, false);
152 CHECK_EQUAL(CORBA::ULong(MAXIMUM), a.maximum());
153 CHECK_EQUAL(CORBA::ULong(4), a.length());
154 CHECK_EQUAL(buffer, a.get_buffer());
155 CHECK_EQUAL(false, a.release());
156 check_values(a);
158 FAIL_RETURN_IF_NOT(a.expect(0), a);
159 FAIL_RETURN_IF_NOT(f.expect(0), f);
160 tested_sequence::freebuf(buffer);
161 FAIL_RETURN_IF_NOT(r.expect(MAXIMUM), r);
162 return 0;
165 int test_buffer_constructor_true()
167 value_type * buffer = alloc_and_init_buffer();
168 expected_calls a(tested_allocation_traits::allocbuf_calls);
169 expected_calls f(tested_allocation_traits::freebuf_calls);
170 expected_calls r(tested_element_traits::release_calls);
172 tested_sequence a(4, buffer, true);
173 CHECK_EQUAL(CORBA::ULong(MAXIMUM), a.maximum());
174 CHECK_EQUAL(CORBA::ULong(4), a.length());
175 CHECK_EQUAL(buffer, a.get_buffer());
176 CHECK_EQUAL(true, a.release());
177 check_values(a);
179 FAIL_RETURN_IF_NOT(a.expect(0), a);
180 FAIL_RETURN_IF_NOT(f.expect(1), f);
181 FAIL_RETURN_IF_NOT(r.expect(MAXIMUM), r);
182 return 0;
185 int test_replace_default()
187 value_type * buffer = alloc_and_init_buffer();
189 expected_calls c(tested_allocation_traits::allocbuf_calls);
190 expected_calls f(tested_allocation_traits::freebuf_calls);
191 expected_calls r(tested_element_traits::release_calls);
193 tested_sequence a;
194 a.replace(4, buffer);
195 FAIL_RETURN_IF_NOT(c.expect(0), c);
196 // Default constructed sequence doesn't allocate a buffer
197 // thus nothing to release and free.
198 FAIL_RETURN_IF_NOT(f.expect(0), f);
199 FAIL_RETURN_IF_NOT(r.expect(0), r);
201 CHECK_EQUAL(CORBA::ULong(MAXIMUM), a.maximum());
202 CHECK_EQUAL(CORBA::ULong(4), a.length());
203 CHECK_EQUAL(buffer, a.get_buffer());
204 CHECK_EQUAL(false, a.release());
205 check_values(a);
207 FAIL_RETURN_IF_NOT(c.expect(0), c);
208 FAIL_RETURN_IF_NOT(f.expect(0), f);
209 tested_sequence::freebuf(buffer);
210 FAIL_RETURN_IF_NOT(r.expect(MAXIMUM), r);
211 return 0;
214 int test_replace_false()
216 value_type * buffer = alloc_and_init_buffer();
218 expected_calls c(tested_allocation_traits::allocbuf_calls);
219 expected_calls f(tested_allocation_traits::freebuf_calls);
220 expected_calls r(tested_element_traits::release_calls);
222 tested_sequence a;
223 a.replace(4, buffer, false);
224 FAIL_RETURN_IF_NOT(c.expect(0), c);
225 // Default constructed sequence doesn't allocate a buffer.
226 FAIL_RETURN_IF_NOT(f.expect(0), f);
227 FAIL_RETURN_IF_NOT(r.expect(0), r);
229 CHECK_EQUAL(CORBA::ULong(MAXIMUM), a.maximum());
230 CHECK_EQUAL(CORBA::ULong(4), a.length());
231 CHECK_EQUAL(buffer, a.get_buffer());
232 CHECK_EQUAL(false, a.release());
233 check_values(a);
235 FAIL_RETURN_IF_NOT(c.expect(0), c);
236 FAIL_RETURN_IF_NOT(f.expect(0), f);
237 tested_sequence::freebuf(buffer);
238 FAIL_RETURN_IF_NOT(r.expect(MAXIMUM), r);
239 return 0;
242 int test_replace_true()
244 value_type * buffer = alloc_and_init_buffer();
246 expected_calls c(tested_allocation_traits::allocbuf_calls);
247 expected_calls f(tested_allocation_traits::freebuf_calls);
248 expected_calls r(tested_element_traits::release_calls);
250 tested_sequence a;
251 a.replace(4, buffer, true);
252 FAIL_RETURN_IF_NOT(c.expect(0), c);
253 // Default constructed sequence doesn't allocate a buffer.
254 FAIL_RETURN_IF_NOT(f.expect(0), f);
255 FAIL_RETURN_IF_NOT(r.expect(0), r);
257 CHECK_EQUAL(CORBA::ULong(MAXIMUM), a.maximum());
258 CHECK_EQUAL(CORBA::ULong(4), a.length());
259 CHECK_EQUAL(buffer, a.get_buffer());
260 CHECK_EQUAL(true, a.release());
261 check_values(a);
263 FAIL_RETURN_IF_NOT(c.expect(0), c);
264 // Since we've given away the ownership the buffer is deallocated by
265 // the sequence.
266 FAIL_RETURN_IF_NOT(f.expect(1), f);
267 FAIL_RETURN_IF_NOT(r.expect(MAXIMUM), r);
268 return 0;
271 int test_get_buffer_default()
273 value_type * buffer = alloc_and_init_buffer();
274 tested_sequence a(4, buffer, true);
275 CHECK_EQUAL(a.get_buffer(), buffer);
276 return 0;
279 int test_get_buffer_false()
281 value_type * buffer = alloc_and_init_buffer();
282 tested_sequence a(4, buffer, true);
283 CHECK_EQUAL(a.get_buffer(), buffer);
284 return 0;
287 int test_get_buffer_true_with_release_false()
289 value_type * buffer = alloc_and_init_buffer();
290 tested_sequence a(4, buffer, false);
291 CHECK(0 == a.get_buffer(true));
292 tested_sequence::freebuf(buffer);
293 return 0;
296 int test_get_buffer_true_with_release_true()
298 value_type * buffer = alloc_and_init_buffer();
299 expected_calls c(tested_allocation_traits::allocbuf_calls);
300 expected_calls f(tested_allocation_traits::freebuf_calls);
301 expected_calls r(tested_element_traits::release_calls);
303 tested_sequence a(4, buffer, true);
304 CHECK_EQUAL(buffer, a.get_buffer(true));
306 tested_sequence const & b = a;
307 CHECK_EQUAL(CORBA::ULong(MAXIMUM), b.maximum());
308 CHECK_EQUAL(CORBA::ULong(0), b.length());
309 CHECK(0 != b.get_buffer());
310 CHECK_EQUAL(true, b.release());
312 // Ownership was taken by get_buffer(true) and later get_buffer call
313 // allocated a new buffer.
314 FAIL_RETURN_IF_NOT(c.expect(1), c);
316 CHECK(buffer != b.get_buffer());
318 FAIL_RETURN_IF_NOT(f.expect(1), f);
319 FAIL_RETURN_IF_NOT(r.expect(MAXIMUM), r);
320 FAIL_RETURN_IF_NOT(c.expect(0), c);
322 tested_sequence::freebuf(buffer);
323 FAIL_RETURN_IF_NOT(f.expect(1), f);
324 FAIL_RETURN_IF_NOT(r.expect(MAXIMUM), r);
325 return 0;
328 int test_all ()
330 int status = 0;
332 typedef string_sequence_tester<tested_sequence> common;
333 common X;
334 status += X.test_all ();
336 status += this->test_set_length_less_than_maximum();
337 status += this->test_set_length_more_than_maximum();
338 status += this->test_regression_2201();
339 status += this->test_buffer_constructor_default();
340 status += this->test_buffer_constructor_false();
341 status += this->test_buffer_constructor_true();
342 status += this->test_replace_default();
343 status += this->test_replace_false();
344 status += this->test_replace_true();
345 status += this->test_get_buffer_default();
346 status += this->test_get_buffer_false();
347 status += this->test_get_buffer_true_with_release_false();
348 status += this->test_get_buffer_true_with_release_true();
349 return status;
353 int ACE_TMAIN(int,ACE_TCHAR*[])
355 int status = 0;
359 typedef TAO::bounded_basic_string_sequence<char, MAXIMUM> s_sequence;
360 typedef Tester<s_sequence> nTester;
361 nTester myntester;
363 status += myntester.test_all();
365 #if defined(ACE_HAS_WCHAR) && !defined(TAO_LACKS_WCHAR_CXX_STDLIB)
366 typedef TAO::bounded_basic_string_sequence<CORBA::WChar, MAXIMUM> w_sequence;
367 typedef Tester<w_sequence> wTester;
368 wTester mywtester;
369 status += mywtester.test_all();
370 #endif
372 catch (const ::CORBA::Exception &ex)
374 ex._tao_print_exception("ERROR : unexpected CORBA exception caugth :");
375 ++status;
378 return status;