Merge pull request #2303 from jwillemsen/jwi-803
[ACE_TAO.git] / TAO / tests / Sequence_Unit_Tests / string_sequence_element_ut.cpp
blob4ae6b243b7219056504659df0a17bd9efd57f7b5
1 /**
2 * @file
4 * @brief Unit test for string_sequence_element, this is the type
5 * returned by operator[] from a string sequence.
7 * @author Carlos O'Ryan
8 */
9 #include "testing_string_traits.hpp"
10 #include "tao/String_Sequence_Element_T.h"
11 #include "tao/String_Manager_T.h"
12 #include "tao/CORBA_String.h"
13 #include "tao/SystemException.h"
15 #include "ace/OS_NS_string.h"
17 #include "test_macros.h"
19 using namespace TAO_VERSIONED_NAMESPACE_NAME::TAO::details;
22 template<typename charT>
23 struct helper
27 template<>
28 struct helper <char>
30 static char const *
31 empty ()
33 return "";
35 static char const *
36 sample0 ()
38 return "Hello";
40 static char const *
41 sample1 ()
43 return "World";
45 static char *
46 dup_sample0 ()
48 return string_traits <char, true>::duplicate (sample0 ());
50 static char *
51 dup_sample1 ()
53 return string_traits <char, true>::duplicate (sample1 ());
55 static bool
56 equal (char const *lhs, char const *rhs)
58 return ACE_OS::strcmp (lhs, rhs) == 0;
62 #if defined(ACE_HAS_WCHAR)
63 template<>
64 struct helper <CORBA::WChar>
66 static CORBA::WChar const *
67 empty ()
69 return L"";
71 static CORBA::WChar const *
72 sample0 ()
74 return L"Hello";
76 static CORBA::WChar const *
77 sample1 ()
79 return L"World";
81 static CORBA::WChar *
82 dup_sample0 ()
84 return string_traits <CORBA::WChar, true>::duplicate (sample0 ());
86 static CORBA::WChar *
87 dup_sample1 ()
89 return string_traits <CORBA::WChar, true>::duplicate (sample1 ());
91 static bool
92 equal (CORBA::WChar const *lhs, CORBA::WChar const *rhs)
94 return ACE_OS::strcmp (lhs, rhs) == 0;
97 #endif
99 template<class charT>
100 struct Tester
102 typedef string_traits <charT, true> tested_element_traits;
103 typedef string_sequence_element <tested_element_traits> tested_element;
104 typedef charT *string_type;
105 typedef charT const *const_string_type;
106 typedef typename tested_element_traits::string_var string_var;
107 typedef typename tested_element_traits::string_mgr string_mgr;
110 test_assignment_from_const_string ()
112 expected_calls d (tested_element_traits::duplicate_calls);
113 expected_calls r (tested_element_traits::release_calls);
116 string_type xe = helper <charT>::dup_sample0 ();
117 const_string_type y = helper <charT>::sample1 ();
118 d.reset ();
119 r.reset ();
121 tested_element x(xe, true);
122 FAIL_RETURN_IF_NOT(d.expect(0), d);
123 FAIL_RETURN_IF_NOT(r.expect(0), r);
125 x = y;
127 FAIL_RETURN_IF_NOT(d.expect(0), d);
128 FAIL_RETURN_IF_NOT(r.expect(1), r);
130 FAIL_RETURN_IF_NOT(
131 helper<charT>::equal(helper<charT>::sample1(), xe),
132 "Mismatch after assignment from const. expected="
133 << helper<charT>::sample0()
134 << ", got=" << x);
135 tested_element_traits::release (xe);
136 FAIL_RETURN_IF_NOT(r.expect(1), r);
138 FAIL_RETURN_IF_NOT(d.expect(0), d);
139 FAIL_RETURN_IF_NOT(r.expect(0), r);
140 return 0;
144 test_assignment_from_element ()
146 expected_calls d (tested_element_traits::duplicate_calls);
147 expected_calls r (tested_element_traits::release_calls);
149 string_type xe = helper <charT>::dup_sample0 ();
150 tested_element x(xe, true);
152 string_type ye = helper <charT>::dup_sample1 ();
153 tested_element y(ye, true);
155 d.reset ();
156 r.reset ();
158 x = y;
160 FAIL_RETURN_IF_NOT(d.expect(0), d);
161 FAIL_RETURN_IF_NOT(r.expect(1), r);
163 FAIL_RETURN_IF_NOT(
164 helper<charT>::equal(helper<charT>::sample1(), xe),
165 "Mismatch after assignment from element. expected="
166 << helper<charT>::sample1()
167 << ", got=" << xe);
169 tested_element_traits::release (xe);
170 tested_element_traits::release (ye);
171 FAIL_RETURN_IF_NOT(r.expect(2), r);
173 FAIL_RETURN_IF_NOT(d.expect(0), d);
174 FAIL_RETURN_IF_NOT(r.expect(0), r);
175 return 0;
179 test_self_assignment ()
181 expected_calls d (tested_element_traits::duplicate_calls);
182 expected_calls r (tested_element_traits::release_calls);
184 string_type xe = helper <charT>::dup_sample0 ();
186 tested_element x(xe, true);
188 d.reset ();
189 r.reset ();
191 x = x;
193 FAIL_RETURN_IF_NOT(d.expect(0), d);
194 FAIL_RETURN_IF_NOT(r.expect(1), r);
196 FAIL_RETURN_IF_NOT(
197 helper<charT>::equal(helper<charT>::sample0(), xe),
198 "Mismatch after self assignment. expected="
199 << helper<charT>::sample0()
200 << ", got=" << xe);
202 tested_element_traits::release (xe);
203 FAIL_RETURN_IF_NOT(r.expect(1), r);
205 FAIL_RETURN_IF_NOT(d.expect(0), d);
206 FAIL_RETURN_IF_NOT(r.expect(0), r);
207 return 0;
211 test_assignment_from_non_const_string ()
213 expected_calls d (tested_element_traits::duplicate_calls);
214 expected_calls r (tested_element_traits::release_calls);
217 string_type xe = 0;
218 tested_element x(xe, true);
220 string_type y =
221 tested_element_traits::duplicate (helper <charT>::sample0 ());
222 FAIL_RETURN_IF_NOT(d.expect(1), d);
223 FAIL_RETURN_IF_NOT(r.expect(0), r);
225 x = y;
227 FAIL_RETURN_IF_NOT(d.expect(0), d);
228 FAIL_RETURN_IF_NOT(r.expect(1), r);
230 FAIL_RETURN_IF_NOT(
231 helper<charT>::equal(helper<charT>::sample0(), xe),
232 "Mismatch after assignment from non-const. expected="
233 << helper<charT>::sample0()
234 << ", got=" << x);
235 tested_element_traits::release (xe);
236 FAIL_RETURN_IF_NOT(r.expect(1), r);
238 FAIL_RETURN_IF_NOT(d.expect(0), d);
239 FAIL_RETURN_IF_NOT(r.expect(0), r);
240 return 0;
244 test_copy_constructor ()
246 expected_calls d (tested_element_traits::duplicate_calls);
247 expected_calls r (tested_element_traits::release_calls);
250 string_type xe =
251 tested_element_traits::duplicate (helper <charT>::sample0 ());
252 tested_element x(xe, true);
254 d.reset ();
255 r.reset ();
257 tested_element y (x);
259 FAIL_RETURN_IF_NOT(d.expect(0), d);
260 FAIL_RETURN_IF_NOT(r.expect(0), r);
262 FAIL_RETURN_IF_NOT(
263 helper<charT>::equal(helper<charT>::sample0(), y),
264 "Mismatch after copy constructor. expected="
265 << helper<charT>::sample0()
266 << ", got=" << y);
268 tested_element_traits::release (xe);
269 FAIL_RETURN_IF_NOT(r.expect(1), r);
271 FAIL_RETURN_IF_NOT(d.expect(0), d);
272 FAIL_RETURN_IF_NOT(r.expect(0), r);
273 return 0;
277 test_assignment_from_copy ()
279 expected_calls d (tested_element_traits::duplicate_calls);
280 expected_calls r (tested_element_traits::release_calls);
282 string_type xe = helper <charT>::dup_sample0 ();
283 tested_element x(xe, true);
285 d.reset ();
286 r.reset ();
288 tested_element y (x);
290 x = y;
292 FAIL_RETURN_IF_NOT(r.expect(1), r);
294 FAIL_RETURN_IF_NOT(
295 helper<charT>::equal(helper<charT>::sample0(), xe),
296 "Mismatch after assignment. expected="
297 << helper<charT>::sample0()
298 << ", got=" << xe);
300 FAIL_RETURN_IF_NOT(
301 helper<charT>::equal(helper<charT>::sample0(), y),
302 "Mismatch after assignment. expected="
303 << helper<charT>::sample0()
304 << ", got=" << y);
306 tested_element_traits::release (xe);
307 FAIL_RETURN_IF_NOT(r.expect(1), r);
309 FAIL_RETURN_IF_NOT(d.expect(0), d);
310 FAIL_RETURN_IF_NOT(r.expect(0), r);
311 return 0;
315 test_assignment_from_var ()
317 expected_calls d (tested_element_traits::duplicate_calls);
318 expected_calls r (tested_element_traits::release_calls);
321 string_type xe = helper <charT>::dup_sample1 ();
322 tested_element x(xe, true);
323 FAIL_RETURN_IF_NOT(d.expect(1), d);
325 string_var y (helper <charT>::sample0());
327 FAIL_RETURN_IF_NOT(d.expect(0), d);
328 FAIL_RETURN_IF_NOT(r.expect(0), r);
330 x = y;
332 FAIL_RETURN_IF_NOT(r.expect(1), r);
334 FAIL_RETURN_IF_NOT(
335 helper<charT>::equal(helper<charT>::sample0(), xe),
336 "Mismatch after assignment from var. expected="
337 << helper<charT>::sample0()
338 << ", got=" << x);
340 tested_element_traits::release (xe);
341 FAIL_RETURN_IF_NOT(r.expect(1), r);
343 FAIL_RETURN_IF_NOT(d.expect(0), d);
344 FAIL_RETURN_IF_NOT(r.expect(0), r);
345 return 0;
349 test_assignment_from_mgr ()
351 expected_calls d (tested_element_traits::duplicate_calls);
352 expected_calls r (tested_element_traits::release_calls);
355 string_type xe = helper <charT>::dup_sample1 ();
356 tested_element x(xe, true);
357 FAIL_RETURN_IF_NOT(d.expect(1), d);
359 string_mgr y;
360 y = helper <charT>::sample0 ();
362 FAIL_RETURN_IF_NOT(d.expect(0), d);
363 FAIL_RETURN_IF_NOT(r.expect(0), r);
365 x = y;
367 FAIL_RETURN_IF_NOT(r.expect(1), r);
369 FAIL_RETURN_IF_NOT(
370 helper<charT>::equal(helper<charT>::sample0(), xe),
371 "Mismatch after assignment from mgr. expected="
372 << helper<charT>::sample0()
373 << ", got=" << x);
375 tested_element_traits::release (xe);
376 FAIL_RETURN_IF_NOT(r.expect(1), r);
378 FAIL_RETURN_IF_NOT(d.expect(0), d);
379 FAIL_RETURN_IF_NOT(r.expect(0), r);
380 return 0;
383 test_all ()
385 int status = 0;
386 status += this->test_assignment_from_const_string ();
387 status += this->test_assignment_from_element ();
388 status += this->test_self_assignment ();
389 status += this->test_assignment_from_non_const_string ();
390 status += this->test_copy_constructor ();
391 status += this->test_assignment_from_copy ();
392 status += this->test_assignment_from_var ();
393 status += this->test_assignment_from_mgr ();
394 return status;
398 int ACE_TMAIN (int, ACE_TCHAR*[])
400 int status = 0;
404 Tester <char> char_tester;
405 status += char_tester.test_all ();
406 Tester <char> wchar_tester;
407 status += wchar_tester.test_all ();
409 catch (const ::CORBA::Exception& ex)
411 ex._tao_print_exception("ERROR : unexpected CORBA exception caugth :");
412 ++status;
415 return status;