Merge pull request #2222 from jwillemsen/jwi-dllexportwarning
[ACE_TAO.git] / TAO / tests / Sequence_Unit_Tests / unbounded_octet_sequence_ut.cpp
blob6fc708b37bd363d0d8e08e3631f7aab6b4e8ffef
1 /**
2 * @file
4 * @brief Unit test for unbounded sequences of octet types
6 * @author Johnny Willemsen
7 */
9 #include "testing_allocation_traits.hpp"
10 #include "testing_range_checking.hpp"
12 #include "tao/Unbounded_Value_Sequence_T.h"
14 #include "value_sequence_tester.hpp"
16 #include "test_macros.h"
18 #include "tao/Basic_Types.h"
19 #include "tao/CDR.h"
21 using namespace TAO_VERSIONED_NAMESPACE_NAME::TAO;
24 typedef unbounded_value_sequence<CORBA::Octet> tested_sequence;
25 typedef tested_sequence::element_traits tested_element_traits;
26 typedef tested_sequence::allocation_traits tested_allocation_traits;
27 typedef details::range_checking<CORBA::Octet,true> range;
29 struct Tester
31 typedef tested_sequence::value_type value_type;
33 int test_copy_constructor_from_ulong()
35 expected_calls a(tested_allocation_traits::allocbuf_calls);
36 expected_calls f(tested_allocation_traits::freebuf_calls);
38 tested_sequence x(16);
39 FAIL_RETURN_IF_NOT(a.expect(1), a);
40 x.length(8);
42 CHECK_EQUAL(CORBA::ULong(16), x.maximum());
43 CHECK_EQUAL(CORBA::ULong(8), x.length());
44 CHECK_EQUAL(true, x.release());
46 tested_sequence y(x);
47 FAIL_RETURN_IF_NOT(a.expect(1), a);
48 CHECK_EQUAL(CORBA::ULong(16), y.maximum());
49 CHECK_EQUAL(CORBA::ULong(8), y.length());
50 CHECK_EQUAL(true, y.release());
52 FAIL_RETURN_IF_NOT(f.expect(2), f);
53 return 0;
56 int test_assignment_from_ulong()
58 expected_calls a(tested_allocation_traits::allocbuf_calls);
59 expected_calls f(tested_allocation_traits::freebuf_calls);
61 tested_sequence x(16);
62 x.length(8);
63 FAIL_RETURN_IF_NOT(a.expect(1), a);
64 CHECK_EQUAL(CORBA::ULong(16), x.maximum());
65 CHECK_EQUAL(CORBA::ULong(8), x.length());
66 CHECK_EQUAL(true, x.release());
68 tested_sequence y;
69 FAIL_RETURN_IF_NOT(a.expect(0), a);
71 y = x;
72 FAIL_RETURN_IF_NOT(a.expect(1), a);
73 FAIL_RETURN_IF_NOT(f.expect(0), f);
74 CHECK_EQUAL(CORBA::ULong(16), y.maximum());
75 CHECK_EQUAL(CORBA::ULong(8), y.length());
76 CHECK_EQUAL(true, y.release());
78 FAIL_RETURN_IF_NOT(f.expect(2), f);
79 return 0;
82 int test_ulong_constructor()
84 expected_calls a(tested_allocation_traits::allocbuf_calls);
85 expected_calls f(tested_allocation_traits::freebuf_calls);
87 tested_sequence x(16);
89 CHECK_EQUAL(CORBA::ULong(16), x.maximum());
90 CHECK_EQUAL(CORBA::ULong(0), x.length());
91 CHECK_EQUAL(true, x.release());
93 FAIL_RETURN_IF_NOT(a.expect(1), a);
94 FAIL_RETURN_IF_NOT(f.expect(1), f);
95 return 0;
98 int test_exception_in_ulong_constructor()
100 expected_calls a(tested_allocation_traits::allocbuf_calls);
101 expected_calls f(tested_allocation_traits::freebuf_calls);
103 tested_allocation_traits::allocbuf_calls.failure_countdown(1);
104 CHECK_THROW(tested_sequence x(16), testing_exception);
105 FAIL_RETURN_IF_NOT(a.expect(1), a);
107 FAIL_RETURN_IF_NOT(f.expect(0), f);
108 return 0;
111 int test_set_length_less_than_maximum()
113 expected_calls a(tested_allocation_traits::allocbuf_calls);
114 expected_calls f(tested_allocation_traits::freebuf_calls);
116 tested_sequence x(16);
118 x.length(8);
119 CHECK_EQUAL(CORBA::ULong(16), x.maximum());
120 CHECK_EQUAL(CORBA::ULong(8), x.length());
121 CHECK_EQUAL(true, x.release());
123 FAIL_RETURN_IF_NOT(a.expect(1), a);
124 FAIL_RETURN_IF_NOT(f.expect(1), f);
125 return 0;
128 int test_set_length_more_than_maximum()
130 expected_calls a(tested_allocation_traits::allocbuf_calls);
131 expected_calls f(tested_allocation_traits::freebuf_calls);
133 tested_sequence x(16);
134 FAIL_RETURN_IF_NOT(a.expect(1), a);
136 x.length(32);
137 FAIL_RETURN_IF_NOT(a.expect(1), a);
138 FAIL_RETURN_IF_NOT(f.expect(1), f);
140 CHECK_EQUAL(CORBA::ULong(32), x.maximum());
141 CHECK_EQUAL(CORBA::ULong(32), x.length());
142 CHECK_EQUAL(true, x.release());
144 FAIL_RETURN_IF_NOT(f.expect(1), f);
145 return 0;
148 int test_exception_in_set_length()
150 expected_calls f(tested_allocation_traits::freebuf_calls);
152 tested_sequence x;
154 expected_calls a(tested_allocation_traits::allocbuf_calls);
155 tested_allocation_traits::allocbuf_calls.failure_countdown(1);
156 CHECK_THROW(x.length(8), testing_exception);
157 FAIL_RETURN_IF_NOT(a.expect(1), a);
159 FAIL_RETURN_IF_NOT(f.expect(0), f);
160 return 0;
163 value_type * alloc_and_init_buffer()
165 value_type * buf = tested_sequence::allocbuf(8);
166 buf[0] = 1; buf[1] = 4; buf[2] = 9; buf[3] = 16;
168 return buf;
171 int test_buffer_constructor_default()
173 value_type * buffer = alloc_and_init_buffer();
175 expected_calls a(tested_allocation_traits::allocbuf_calls);
176 expected_calls f(tested_allocation_traits::freebuf_calls);
178 tested_sequence a(8, 4, buffer);
179 CHECK_EQUAL(CORBA::ULong(8), a.maximum());
180 CHECK_EQUAL(CORBA::ULong(4), a.length());
181 CHECK_EQUAL(buffer, a.get_buffer());
182 CHECK_EQUAL(CORBA::Octet( 1), a[0]);
183 CHECK_EQUAL(CORBA::Octet( 4), a[1]);
184 CHECK_EQUAL(CORBA::Octet( 9), a[2]);
185 CHECK_EQUAL(CORBA::Octet(16), a[3]);
186 CHECK_EQUAL(false, a.release());
188 FAIL_RETURN_IF_NOT(a.expect(0), a);
189 FAIL_RETURN_IF_NOT(f.expect(0), f);
190 tested_sequence::freebuf(buffer);
191 return 0;
194 int test_buffer_constructor_false()
196 value_type * buffer = alloc_and_init_buffer();
197 expected_calls a(tested_allocation_traits::allocbuf_calls);
198 expected_calls f(tested_allocation_traits::freebuf_calls);
200 tested_sequence a(8, 4, buffer, false);
201 CHECK_EQUAL(CORBA::ULong(8), a.maximum());
202 CHECK_EQUAL(CORBA::ULong(4), a.length());
203 CHECK_EQUAL(buffer, a.get_buffer());
204 CHECK_EQUAL(CORBA::Octet( 1), a[0]);
205 CHECK_EQUAL(CORBA::Octet( 4), a[1]);
206 CHECK_EQUAL(CORBA::Octet( 9), a[2]);
207 CHECK_EQUAL(CORBA::Octet(16), a[3]);
208 CHECK_EQUAL(false, a.release());
210 FAIL_RETURN_IF_NOT(a.expect(0), a);
211 FAIL_RETURN_IF_NOT(f.expect(0), f);
212 tested_sequence::freebuf(buffer);
213 return 0;
216 int test_buffer_constructor_true()
218 value_type * buffer = alloc_and_init_buffer();
219 expected_calls a(tested_allocation_traits::allocbuf_calls);
220 expected_calls f(tested_allocation_traits::freebuf_calls);
222 tested_sequence a(8, 4, buffer, true);
223 CHECK_EQUAL(CORBA::ULong(8), a.maximum());
224 CHECK_EQUAL(CORBA::ULong(4), a.length());
225 CHECK_EQUAL(buffer, a.get_buffer());
226 CHECK_EQUAL(CORBA::Octet( 1), a[0]);
227 CHECK_EQUAL(CORBA::Octet( 4), a[1]);
228 CHECK_EQUAL(CORBA::Octet( 9), a[2]);
229 CHECK_EQUAL(CORBA::Octet(16), a[3]);
230 CHECK_EQUAL(true, a.release());
232 FAIL_RETURN_IF_NOT(a.expect(0), a);
233 FAIL_RETURN_IF_NOT(f.expect(1), f);
234 return 0;
237 int test_replace_default()
239 value_type * buffer = alloc_and_init_buffer();
241 expected_calls c(tested_allocation_traits::allocbuf_calls);
242 expected_calls f(tested_allocation_traits::freebuf_calls);
244 tested_sequence a;
245 a.replace(8, 4, buffer);
246 FAIL_RETURN_IF_NOT(c.expect(0), c);
247 FAIL_RETURN_IF_NOT(f.expect(0), f);
249 CHECK_EQUAL(CORBA::ULong(8), a.maximum());
250 CHECK_EQUAL(CORBA::ULong(4), a.length());
251 CHECK_EQUAL(buffer, a.get_buffer());
252 CHECK_EQUAL(CORBA::Octet( 1), a[0]);
253 CHECK_EQUAL(CORBA::Octet( 4), a[1]);
254 CHECK_EQUAL(CORBA::Octet( 9), a[2]);
255 CHECK_EQUAL(CORBA::Octet(16), a[3]);
256 CHECK_EQUAL(false, a.release());
258 FAIL_RETURN_IF_NOT(c.expect(0), c);
259 FAIL_RETURN_IF_NOT(f.expect(0), f);
260 tested_sequence::freebuf(buffer);
261 return 0;
264 int test_replace_false()
266 value_type * buffer = alloc_and_init_buffer();
267 expected_calls c(tested_allocation_traits::allocbuf_calls);
268 expected_calls f(tested_allocation_traits::freebuf_calls);
271 tested_sequence a;
272 a.replace(8, 4, buffer, false);
273 FAIL_RETURN_IF_NOT(c.expect(0), c);
274 FAIL_RETURN_IF_NOT(f.expect(0), f);
276 CHECK_EQUAL(CORBA::ULong(8), a.maximum());
277 CHECK_EQUAL(CORBA::ULong(4), a.length());
278 CHECK_EQUAL(buffer, a.get_buffer());
279 CHECK_EQUAL(CORBA::Octet( 1), a[0]);
280 CHECK_EQUAL(CORBA::Octet( 4), a[1]);
281 CHECK_EQUAL(CORBA::Octet( 9), a[2]);
282 CHECK_EQUAL(CORBA::Octet(16), a[3]);
283 CHECK_EQUAL(false, a.release());
285 FAIL_RETURN_IF_NOT(c.expect(0), c);
286 FAIL_RETURN_IF_NOT(f.expect(0), f);
287 tested_sequence::freebuf(buffer);
288 return 0;
291 int test_replace_true()
293 value_type * buffer = alloc_and_init_buffer();
294 expected_calls c(tested_allocation_traits::allocbuf_calls);
295 expected_calls f(tested_allocation_traits::freebuf_calls);
298 tested_sequence a;
299 a.replace(8, 4, buffer, true);
300 FAIL_RETURN_IF_NOT(c.expect(0), c);
301 FAIL_RETURN_IF_NOT(f.expect(0), f);
303 CHECK_EQUAL(CORBA::ULong(8), a.maximum());
304 CHECK_EQUAL(CORBA::ULong(4), a.length());
305 CHECK_EQUAL(buffer, a.get_buffer());
306 CHECK_EQUAL(CORBA::Octet( 1), a[0]);
307 CHECK_EQUAL(CORBA::Octet( 4), a[1]);
308 CHECK_EQUAL(CORBA::Octet( 9), a[2]);
309 CHECK_EQUAL(CORBA::Octet(16), a[3]);
310 CHECK_EQUAL(true, a.release());
312 FAIL_RETURN_IF_NOT(c.expect(0), c);
313 // Since we've given away the ownership the buffer is deallocated by
314 // the sequence.
315 FAIL_RETURN_IF_NOT(f.expect(1), f);
316 return 0;
319 int test_get_buffer_default()
321 value_type * buffer = alloc_and_init_buffer();
322 tested_sequence a(8, 4, buffer, true);
323 CHECK_EQUAL(a.get_buffer(), buffer);
324 return 0;
327 int test_get_buffer_false()
329 value_type * buffer = alloc_and_init_buffer();
330 tested_sequence a(8, 4, buffer, true);
331 CHECK_EQUAL(a.get_buffer(), buffer);
332 return 0;
335 int test_get_buffer_true_with_release_false()
337 value_type * buffer = alloc_and_init_buffer();
338 tested_sequence a(8, 4, buffer, false);
339 CHECK(0 == a.get_buffer(true));
340 tested_sequence::freebuf(buffer);
341 return 0;
344 int test_get_buffer_true_with_release_true()
346 value_type * buffer = alloc_and_init_buffer();
347 expected_calls c(tested_allocation_traits::allocbuf_calls);
348 expected_calls f(tested_allocation_traits::freebuf_calls);
350 tested_sequence a(8, 4, buffer, true);
351 CHECK_EQUAL(buffer, a.get_buffer(true));
353 tested_sequence const & b = a;
354 CHECK_EQUAL(0UL, b.maximum());
355 CHECK_EQUAL(0UL, b.length());
356 CHECK(0 != b.get_buffer());
357 CHECK_EQUAL(true, b.release());
359 FAIL_RETURN_IF_NOT(c.expect(1), c);
361 CHECK(buffer != b.get_buffer());
363 FAIL_RETURN_IF_NOT(c.expect(0), c);
364 FAIL_RETURN_IF_NOT(f.expect(1), f);
365 tested_sequence::freebuf(buffer);
366 return 0;
369 int test_all()
371 int status = 0;
373 status += this->test_ulong_constructor();
374 status += this->test_copy_constructor_from_ulong();
375 status += this->test_assignment_from_ulong();
376 status += this->test_exception_in_ulong_constructor();
377 status += this->test_set_length_less_than_maximum();
378 status += this->test_set_length_more_than_maximum();
379 status += this->test_exception_in_set_length();
380 status += this->test_buffer_constructor_default();
381 status += this->test_buffer_constructor_false();
382 status += this->test_buffer_constructor_true();
383 status += this->test_replace_default();
384 status += this->test_replace_false();
385 status += this->test_replace_true();
386 status += this->test_get_buffer_false();
387 status += this->test_get_buffer_true_with_release_false();
388 status += this->test_get_buffer_true_with_release_true();
389 return status;
392 Tester() {}
395 int ACE_TMAIN(int,ACE_TCHAR*[])
397 int status = 0;
401 Tester tester;
402 status += tester.test_all ();
406 typedef value_sequence_tester<tested_sequence,tested_allocation_traits> common;
407 common tester;
408 status += tester.test_all ();
411 catch (const ::CORBA::Exception &ex)
413 ex._tao_print_exception("ERROR : unexpected CORBA exception caugth :");
414 ++status;
417 return status;