More tests update
[ACE_TAO.git] / TAO / tests / Sequence_Unit_Tests / string_sequence_element_ut.cpp
blob56985021e43f2c597109455cdd072ce4d6feda0d
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;
23 template<typename charT>
24 struct helper
28 template<>
29 struct helper <char>
31 static char const *
32 empty ()
34 return "";
36 static char const *
37 sample0 ()
39 return "Hello";
41 static char const *
42 sample1 ()
44 return "World";
46 static char *
47 dup_sample0 ()
49 return string_traits <char, true>::duplicate (sample0 ());
51 static char *
52 dup_sample1 ()
54 return string_traits <char, true>::duplicate (sample1 ());
56 static bool
57 equal (char const *lhs, char const *rhs)
59 return ACE_OS::strcmp (lhs, rhs) == 0;
63 #if defined(ACE_HAS_WCHAR)
64 template<>
65 struct helper <CORBA::WChar>
67 static CORBA::WChar const *
68 empty ()
70 return L"";
72 static CORBA::WChar const *
73 sample0 ()
75 return L"Hello";
77 static CORBA::WChar const *
78 sample1 ()
80 return L"World";
82 static CORBA::WChar *
83 dup_sample0 ()
85 return string_traits <CORBA::WChar, true>::duplicate (sample0 ());
87 static CORBA::WChar *
88 dup_sample1 ()
90 return string_traits <CORBA::WChar, true>::duplicate (sample1 ());
92 static bool
93 equal (CORBA::WChar const *lhs, CORBA::WChar const *rhs)
95 return ACE_OS::strcmp (lhs, rhs) == 0;
98 #endif
100 template<class charT>
101 struct Tester
103 typedef string_traits <charT, true> tested_element_traits;
104 typedef string_sequence_element <tested_element_traits> tested_element;
105 typedef charT *string_type;
106 typedef charT const *const_string_type;
107 typedef typename tested_element_traits::string_var string_var;
108 typedef typename tested_element_traits::string_mgr string_mgr;
111 test_assignment_from_const_string ()
113 expected_calls d (tested_element_traits::duplicate_calls);
114 expected_calls r (tested_element_traits::release_calls);
117 string_type xe = helper <charT>::dup_sample0 ();
118 const_string_type y = helper <charT>::sample1 ();
119 d.reset ();
120 r.reset ();
122 tested_element x(xe, true);
123 FAIL_RETURN_IF_NOT(d.expect(0), d);
124 FAIL_RETURN_IF_NOT(r.expect(0), r);
126 x = y;
128 FAIL_RETURN_IF_NOT(d.expect(0), d);
129 FAIL_RETURN_IF_NOT(r.expect(1), r);
131 FAIL_RETURN_IF_NOT(
132 helper<charT>::equal(helper<charT>::sample1(), xe),
133 "Mismatch after assignment from const. expected="
134 << helper<charT>::sample0()
135 << ", got=" << x);
136 tested_element_traits::release (xe);
137 FAIL_RETURN_IF_NOT(r.expect(1), r);
139 FAIL_RETURN_IF_NOT(d.expect(0), d);
140 FAIL_RETURN_IF_NOT(r.expect(0), r);
141 return 0;
145 test_assignment_from_element ()
147 expected_calls d (tested_element_traits::duplicate_calls);
148 expected_calls r (tested_element_traits::release_calls);
150 string_type xe = helper <charT>::dup_sample0 ();
151 tested_element x(xe, true);
153 string_type ye = helper <charT>::dup_sample1 ();
154 tested_element y(ye, true);
156 d.reset ();
157 r.reset ();
159 x = y;
161 FAIL_RETURN_IF_NOT(d.expect(0), d);
162 FAIL_RETURN_IF_NOT(r.expect(1), r);
164 FAIL_RETURN_IF_NOT(
165 helper<charT>::equal(helper<charT>::sample1(), xe),
166 "Mismatch after assignment from element. expected="
167 << helper<charT>::sample1()
168 << ", got=" << xe);
170 tested_element_traits::release (xe);
171 tested_element_traits::release (ye);
172 FAIL_RETURN_IF_NOT(r.expect(2), r);
174 FAIL_RETURN_IF_NOT(d.expect(0), d);
175 FAIL_RETURN_IF_NOT(r.expect(0), r);
176 return 0;
180 test_self_assignment ()
182 expected_calls d (tested_element_traits::duplicate_calls);
183 expected_calls r (tested_element_traits::release_calls);
185 string_type xe = helper <charT>::dup_sample0 ();
187 tested_element x(xe, true);
189 d.reset ();
190 r.reset ();
192 x = x;
194 FAIL_RETURN_IF_NOT(d.expect(0), d);
195 FAIL_RETURN_IF_NOT(r.expect(1), r);
197 FAIL_RETURN_IF_NOT(
198 helper<charT>::equal(helper<charT>::sample0(), xe),
199 "Mismatch after self assignment. expected="
200 << helper<charT>::sample0()
201 << ", got=" << xe);
203 tested_element_traits::release (xe);
204 FAIL_RETURN_IF_NOT(r.expect(1), r);
206 FAIL_RETURN_IF_NOT(d.expect(0), d);
207 FAIL_RETURN_IF_NOT(r.expect(0), r);
208 return 0;
212 test_assignment_from_non_const_string ()
214 expected_calls d (tested_element_traits::duplicate_calls);
215 expected_calls r (tested_element_traits::release_calls);
218 string_type xe = 0;
219 tested_element x(xe, true);
221 string_type y =
222 tested_element_traits::duplicate (helper <charT>::sample0 ());
223 FAIL_RETURN_IF_NOT(d.expect(1), d);
224 FAIL_RETURN_IF_NOT(r.expect(0), r);
226 x = y;
228 FAIL_RETURN_IF_NOT(d.expect(0), d);
229 FAIL_RETURN_IF_NOT(r.expect(1), r);
231 FAIL_RETURN_IF_NOT(
232 helper<charT>::equal(helper<charT>::sample0(), xe),
233 "Mismatch after assignment from non-const. expected="
234 << helper<charT>::sample0()
235 << ", got=" << x);
236 tested_element_traits::release (xe);
237 FAIL_RETURN_IF_NOT(r.expect(1), r);
239 FAIL_RETURN_IF_NOT(d.expect(0), d);
240 FAIL_RETURN_IF_NOT(r.expect(0), r);
241 return 0;
245 test_copy_constructor ()
247 expected_calls d (tested_element_traits::duplicate_calls);
248 expected_calls r (tested_element_traits::release_calls);
251 string_type xe =
252 tested_element_traits::duplicate (helper <charT>::sample0 ());
253 tested_element x(xe, true);
255 d.reset ();
256 r.reset ();
258 tested_element y (x);
260 FAIL_RETURN_IF_NOT(d.expect(0), d);
261 FAIL_RETURN_IF_NOT(r.expect(0), r);
263 FAIL_RETURN_IF_NOT(
264 helper<charT>::equal(helper<charT>::sample0(), y),
265 "Mismatch after copy constructor. expected="
266 << helper<charT>::sample0()
267 << ", got=" << y);
269 tested_element_traits::release (xe);
270 FAIL_RETURN_IF_NOT(r.expect(1), r);
272 FAIL_RETURN_IF_NOT(d.expect(0), d);
273 FAIL_RETURN_IF_NOT(r.expect(0), r);
274 return 0;
278 test_assignment_from_copy ()
280 expected_calls d (tested_element_traits::duplicate_calls);
281 expected_calls r (tested_element_traits::release_calls);
283 string_type xe = helper <charT>::dup_sample0 ();
284 tested_element x(xe, true);
286 d.reset ();
287 r.reset ();
289 tested_element y (x);
291 x = y;
293 FAIL_RETURN_IF_NOT(r.expect(1), r);
295 FAIL_RETURN_IF_NOT(
296 helper<charT>::equal(helper<charT>::sample0(), xe),
297 "Mismatch after assignment. expected="
298 << helper<charT>::sample0()
299 << ", got=" << xe);
301 FAIL_RETURN_IF_NOT(
302 helper<charT>::equal(helper<charT>::sample0(), y),
303 "Mismatch after assignment. expected="
304 << helper<charT>::sample0()
305 << ", got=" << y);
307 tested_element_traits::release (xe);
308 FAIL_RETURN_IF_NOT(r.expect(1), r);
310 FAIL_RETURN_IF_NOT(d.expect(0), d);
311 FAIL_RETURN_IF_NOT(r.expect(0), r);
312 return 0;
316 test_assignment_from_var ()
318 expected_calls d (tested_element_traits::duplicate_calls);
319 expected_calls r (tested_element_traits::release_calls);
322 string_type xe = helper <charT>::dup_sample1 ();
323 tested_element x(xe, true);
324 FAIL_RETURN_IF_NOT(d.expect(1), d);
326 string_var y (helper <charT>::sample0());
328 FAIL_RETURN_IF_NOT(d.expect(0), d);
329 FAIL_RETURN_IF_NOT(r.expect(0), r);
331 x = y;
333 FAIL_RETURN_IF_NOT(r.expect(1), r);
335 FAIL_RETURN_IF_NOT(
336 helper<charT>::equal(helper<charT>::sample0(), xe),
337 "Mismatch after assignment from var. expected="
338 << helper<charT>::sample0()
339 << ", got=" << x);
341 tested_element_traits::release (xe);
342 FAIL_RETURN_IF_NOT(r.expect(1), r);
344 FAIL_RETURN_IF_NOT(d.expect(0), d);
345 FAIL_RETURN_IF_NOT(r.expect(0), r);
346 return 0;
350 test_assignment_from_mgr ()
352 expected_calls d (tested_element_traits::duplicate_calls);
353 expected_calls r (tested_element_traits::release_calls);
356 string_type xe = helper <charT>::dup_sample1 ();
357 tested_element x(xe, true);
358 FAIL_RETURN_IF_NOT(d.expect(1), d);
360 string_mgr y;
361 y = helper <charT>::sample0 ();
363 FAIL_RETURN_IF_NOT(d.expect(0), d);
364 FAIL_RETURN_IF_NOT(r.expect(0), r);
366 x = y;
368 FAIL_RETURN_IF_NOT(r.expect(1), r);
370 FAIL_RETURN_IF_NOT(
371 helper<charT>::equal(helper<charT>::sample0(), xe),
372 "Mismatch after assignment from mgr. expected="
373 << helper<charT>::sample0()
374 << ", got=" << x);
376 tested_element_traits::release (xe);
377 FAIL_RETURN_IF_NOT(r.expect(1), r);
379 FAIL_RETURN_IF_NOT(d.expect(0), d);
380 FAIL_RETURN_IF_NOT(r.expect(0), r);
381 return 0;
384 test_all ()
386 int status = 0;
387 status += this->test_assignment_from_const_string ();
388 status += this->test_assignment_from_element ();
389 status += this->test_self_assignment ();
390 status += this->test_assignment_from_non_const_string ();
391 status += this->test_copy_constructor ();
392 status += this->test_assignment_from_copy ();
393 status += this->test_assignment_from_var ();
394 status += this->test_assignment_from_mgr ();
395 return status;
399 int ACE_TMAIN (int, ACE_TCHAR*[])
401 int status = 0;
405 Tester <char> char_tester;
406 status += char_tester.test_all ();
407 Tester <char> wchar_tester;
408 status += wchar_tester.test_all ();
410 catch (const ::CORBA::Exception& ex)
412 ex._tao_print_exception("ERROR : unexpected CORBA exception caugth :");
413 ++status;
416 return status;