Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / Sequence_Iterators / StringSeq.cpp
blob937c583c7aa372549a9686fa4a7ca446c9c75a09
1 /**
2 * @file StringSeq.cpp
4 * @brief test for STL iterator behaviour of CORBA unbounded string sequence
6 * @author Friedhelm Wolf (fwolf@dre.vanderbilt.edu)
7 */
9 #include "tao/StringSeqC.h"
10 #include "ace/Log_Msg.h"
11 #include "ace/OS_NS_string.h"
13 #include <iostream>
14 #include <iterator>
15 #include <sstream>
17 #define FAIL_RETURN_IF(CONDITION) \
18 if (CONDITION) \
19 { \
20 ACE_DEBUG ((LM_ERROR, ACE_TEXT ("\tFailed at %N:%l\n"))); \
21 return 1; \
24 //-----------------------------------------------------------------------------
26 #if defined TAO_HAS_SEQUENCE_ITERATORS && TAO_HAS_SEQUENCE_ITERATORS == 1
28 template <typename ITERATOR_T>
29 int test_sequence ()
31 ::CORBA::StringSeq a;
33 // test equality operator
34 FAIL_RETURN_IF (!(a.begin () == a.begin ()));
36 // test non-equality operator
37 FAIL_RETURN_IF (a.end () != a.end ());
39 // test for correct behaviour for empty sequence
41 FAIL_RETURN_IF (a.begin() != a.end ());
43 // setup of an example sequence
45 const char * elem0_cstr = "elem0";
46 const char * elem1_cstr = "elem1";
47 const char * elem2_cstr = "elem2";
48 const char * elem3_cstr = "elem3";
50 a.length (4);
51 // Create a case to test for memory leaks
52 // in the sequence.
53 a[0] = CORBA::string_dup (elem0_cstr);
54 a[0] = CORBA::string_dup (elem0_cstr);
55 a[1] = CORBA::string_dup (elem1_cstr);
56 a[2] = CORBA::string_dup (elem2_cstr);
57 a[3] = CORBA::string_dup (elem3_cstr);
59 // test iterator copy constructor
60 ITERATOR_T a_it (a.begin ());
61 FAIL_RETURN_IF (a_it != a.begin ());
63 // test assignment operator
64 a_it = a.begin ();
66 // Create a case to test for memory leaks
67 // in the iterator.
68 *a_it = CORBA::string_dup (elem0_cstr);
69 FAIL_RETURN_IF (a_it != a.begin ());
71 // test non const dereferencing
72 // I'm not sure this test makes sense since what's returned from
73 // dereferencing the iterator is an r value.
74 const char* value_seq = a[0];
75 FAIL_RETURN_IF (ACE_OS::strcmp (value_seq, elem0_cstr) != 0);
76 const char* value0 = *a_it;
77 FAIL_RETURN_IF (ACE_OS::strcmp (value0, elem0_cstr) != 0);
79 // test const dereferencing
80 const char* const value1 = *a_it;
81 FAIL_RETURN_IF (ACE_OS::strcmp (value1, elem0_cstr) != 0);
83 // test increment operation
84 a_it++;
85 FAIL_RETURN_IF (a_it == a.begin());
86 FAIL_RETURN_IF (ACE_OS::strcmp (*a_it, elem1_cstr) != 0);
88 // test < operator
89 FAIL_RETURN_IF (!(a.begin () < a_it));
90 FAIL_RETURN_IF (a_it < a.begin ());
92 // test difference type
93 int a_diff = a_it - a.begin ();
94 FAIL_RETURN_IF (a_diff != 1);
96 // test copy constructor
97 ITERATOR_T a_it1 (a_it);
98 FAIL_RETURN_IF (a_it1 != a_it);
100 // test preincrement operator
101 ++a_it1;
102 FAIL_RETURN_IF ((a_it1 - a_it) != 1);
104 // test = and += operator
105 ITERATOR_T a_it2 = a_it += 3;
106 FAIL_RETURN_IF (a_it2 != a_it);
107 FAIL_RETURN_IF ((a_it - a_it1) != 2);
109 // test + operator
110 a_it2 = a_it1 + 3;
111 FAIL_RETURN_IF ((a_it2 - a_it1) != 3);
113 // test post-decrement operation
114 a_it = a.end ();
115 a_it--;
116 FAIL_RETURN_IF (a_it == a.end ());
117 FAIL_RETURN_IF ((a.end () - a_it) != 1);
118 FAIL_RETURN_IF (ACE_OS::strcmp (*a_it, elem3_cstr) != 0);
120 // test pre-decrement operator
121 a_it = a.end ();
122 --a_it;
123 FAIL_RETURN_IF (a_it == a.end ());
124 FAIL_RETURN_IF ((a.end () - a_it) != 1);
125 FAIL_RETURN_IF (ACE_OS::strcmp (*a_it, elem3_cstr) != 0);
127 // test -= operator
128 a_it -= 3;
129 FAIL_RETURN_IF ((a_it1 - a_it) != 2);
131 // test - operator
132 a_it2 = a_it1 - 2;
133 FAIL_RETURN_IF ((a_it1 - a_it2) != 2);
135 // test operator[] read
136 a_it = a.begin ();
137 FAIL_RETURN_IF (ACE_OS::strcmp (a_it[0],a[0]) != 0);
138 a_it += 2;
139 FAIL_RETURN_IF (ACE_OS::strcmp (a_it[0],a[2]) != 0);
141 // test operator[] write
142 // NOTE: This assignment actually modifies the third element
143 // in the sequence since the iterator was previously positioned
144 // at the third element. I believe these are the proper semantics
145 // for using the bracket operator with an iterator. The number
146 // in the brackets is additive - not absolute.
147 a_it[0] = CORBA::string_dup (elem2_cstr);
148 FAIL_RETURN_IF (ACE_OS::strcmp (a[2], elem2_cstr) != 0);
150 // reset content of sequence a
151 a[1] = CORBA::string_dup (elem1_cstr);
153 // test for loop behaviour
154 ::CORBA::StringSeq b = a;
155 ITERATOR_T b_it = b.begin ();
157 for (a_it = a.begin ();
158 a_it != a.end ();
159 a_it++, b_it++)
161 FAIL_RETURN_IF (ACE_OS::strcmp (*a_it, *b_it) != 0);
164 ::CORBA::StringSeq test;
165 test.length (a.length ());
167 std::copy (a.begin (),
168 a.end (),
169 test.begin ());
171 FAIL_RETURN_IF (test.length () != a.length ());
173 ITERATOR_T copytest_iter = test.begin ();
174 for (ITERATOR_T copya_iter = a.begin ();
175 copya_iter != a.end ();
176 ++copya_iter, ++copytest_iter)
178 FAIL_RETURN_IF (ACE_OS::strcmp (*copya_iter, *copytest_iter) != 0);
181 /// Testing - using ostream_iterator
182 std::ostringstream ostream;
183 std::copy (a.begin (),
184 a.end (),
185 std::ostream_iterator<CORBA::StringSeq::const_value_type> (ostream,
186 "\n"));
188 // The third sequence element has been modified. Either we need to restore
189 // the original sequence values or modify the test here. I modified the
190 // test since it's easier.
191 FAIL_RETURN_IF (
192 ostream.str ().compare ("elem0\nelem1\nelem2\nelem3\n") != 0);
194 return 0;
197 //-----------------------------------------------------------------------------
199 template <typename ITERATOR_T>
200 int test_sequence_const_iterator ()
202 ::CORBA::StringSeq a;
204 // test equality operator
205 FAIL_RETURN_IF (!(a.begin () == a.begin ()));
207 // test non-equality operator
208 FAIL_RETURN_IF (a.end () != a.end ());
210 // test for correct behaviour for empty sequence
212 FAIL_RETURN_IF (a.begin() != a.end ());
214 // setup of an example sequence
216 const char * elem0_cstr = "elem0";
217 const char * elem1_cstr = "elem1";
218 const char * elem2_cstr = "elem2";
219 const char * elem3_cstr = "elem3";
221 a.length (4);
222 a[0] = CORBA::string_dup (elem0_cstr);
223 a[0] = CORBA::string_dup (elem0_cstr);
224 a[1] = CORBA::string_dup (elem1_cstr);
225 a[2] = CORBA::string_dup (elem2_cstr);
226 a[3] = CORBA::string_dup (elem3_cstr);
228 // test iterator copy constructor
229 ITERATOR_T a_it (a.begin ());
230 FAIL_RETURN_IF (a_it != a.begin ());
232 // test assignment operator
233 a_it = a.begin ();
234 FAIL_RETURN_IF (a_it != a.begin ());
236 // test non const dereferencing
237 // I'm not sure non-const deferencing makes sense since
238 // we are getting an r value.
239 const char * value0 = *a_it;
240 FAIL_RETURN_IF (ACE_OS::strcmp (value0, elem0_cstr) != 0);
242 // test const dereferencing
243 const char* const value1 = *a_it;
244 FAIL_RETURN_IF (ACE_OS::strcmp (value1, elem0_cstr) != 0);
246 // test increment operation
247 a_it++;
248 FAIL_RETURN_IF (a_it == a.begin());
249 FAIL_RETURN_IF (ACE_OS::strcmp (*a_it, elem1_cstr) != 0);
251 // test < operator
252 FAIL_RETURN_IF (!(a.begin () < a_it));
253 FAIL_RETURN_IF (a_it < a.begin ());
255 // test difference type
256 int a_diff = a_it - a.begin ();
257 FAIL_RETURN_IF (a_diff != 1);
259 // test copy constructor
260 ITERATOR_T a_it1 (a_it);
261 FAIL_RETURN_IF (a_it1 != a_it);
263 // test preincrement operator
264 ++a_it1;
265 FAIL_RETURN_IF ((a_it1 - a_it) != 1);
267 // test = and += operator
268 ITERATOR_T a_it2 = a_it += 3;
269 FAIL_RETURN_IF (a_it2 != a_it);
270 FAIL_RETURN_IF ((a_it - a_it1) != 2);
272 // test + operator
273 a_it2 = a_it1 + 3;
274 FAIL_RETURN_IF ((a_it2 - a_it1) != 3);
276 // test post-decrement operation
277 a_it = a.end ();
278 a_it--;
279 FAIL_RETURN_IF (a_it == a.end ());
280 FAIL_RETURN_IF ((a.end () - a_it) != 1);
281 FAIL_RETURN_IF (ACE_OS::strcmp (*a_it, elem3_cstr) != 0);
283 // test pre-decrement operator
284 a_it = a.end ();
285 --a_it;
286 FAIL_RETURN_IF (a_it == a.end ());
287 FAIL_RETURN_IF ((a.end () - a_it) != 1);
288 FAIL_RETURN_IF (ACE_OS::strcmp (*a_it, elem3_cstr) != 0);
290 // test -= operator
291 a_it -= 3;
292 FAIL_RETURN_IF ((a_it1 - a_it) != 2);
294 // test - operator
295 a_it2 = a_it1 - 2;
296 FAIL_RETURN_IF ((a_it1 - a_it2) != 2);
298 // test operator[] read
299 a_it = a.begin ();
300 FAIL_RETURN_IF (ACE_OS::strcmp (a_it[0],a[0]) != 0);
301 a_it += 2;
302 FAIL_RETURN_IF (ACE_OS::strcmp (a_it[0],a[2]) != 0);
304 // test for loop behaviour
305 ::CORBA::StringSeq b = a;
306 ITERATOR_T b_it = b.begin ();
308 for (a_it = a.begin ();
309 a_it != a.end ();
310 a_it++, b_it++)
312 FAIL_RETURN_IF (ACE_OS::strcmp (*a_it, *b_it) != 0);
315 ::CORBA::StringSeq test;
316 test.length (a.length ());
318 std::copy (a.begin (),
319 a.end (),
320 test.begin ());
322 FAIL_RETURN_IF (test.length () != a.length ());
324 ITERATOR_T copytest_iter = test.begin ();
325 for (ITERATOR_T copya_iter = a.begin ();
326 copya_iter != a.end ();
327 ++copya_iter, ++copytest_iter)
329 FAIL_RETURN_IF (ACE_OS::strcmp (*copya_iter, *copytest_iter) != 0);
332 /// Testing - using ostream_iterator
333 std::ostringstream ostream;
334 std::copy (a.begin (),
335 a.end (),
336 std::ostream_iterator<CORBA::StringSeq::const_value_type>(ostream,
337 "\n"));
339 FAIL_RETURN_IF (
340 ostream.str ().compare ("elem0\nelem1\nelem2\nelem3\n") != 0);
342 return 0;
345 //-----------------------------------------------------------------------------
347 template <typename ITERATOR_T>
348 int test_const_sequence ()
350 // setup of an example sequence
351 const char * elem0_cstr = "elem0";
352 const char * elem1_cstr = "elem1";
353 const char * elem2_cstr = "elem2";
354 const char * elem3_cstr = "elem3";
356 ::CORBA::StringSeq setup;
357 setup.length (4);
358 setup[0] = CORBA::string_dup (elem0_cstr);
359 setup[1] = CORBA::string_dup (elem1_cstr);
360 setup[2] = CORBA::string_dup (elem2_cstr);
361 setup[3] = CORBA::string_dup (elem3_cstr);
363 const ::CORBA::StringSeq a = setup;
365 // test equality operator
366 FAIL_RETURN_IF (!(a.begin () == a.begin ()));
367 ITERATOR_T c_iter1 (a.begin ());
368 ITERATOR_T c_iter2 = a.begin ();
369 FAIL_RETURN_IF (!(c_iter1 == c_iter2));
371 // test non-equality operator
372 FAIL_RETURN_IF (a.end () != a.end ());
374 // test iterator copy constructor
375 ITERATOR_T a_it (a.begin ());
376 FAIL_RETURN_IF (a_it != a.begin ());
378 // test non const dereferencing
379 // JWH2 - This test shouldn't be done when dealing with
380 // a const iterator (which we sometimes do with the current
381 // testing setup).
382 //typename ITERATOR_T::element_type value0 = *a_it;
383 //FAIL_RETURN_IF (ACE_OS::strcmp (value0, elem0_cstr) != 0);
385 // test const dereferencing
386 typename ITERATOR_T::const_element_type value1 = *a_it;
387 FAIL_RETURN_IF (ACE_OS::strcmp (value1, elem0_cstr) != 0);
389 // test increment operation
390 a_it++;
391 FAIL_RETURN_IF (a_it == a.begin());
392 FAIL_RETURN_IF (ACE_OS::strcmp (*a_it, elem1_cstr) != 0);
394 // test < operator
395 FAIL_RETURN_IF (!(a.begin () < a_it));
396 FAIL_RETURN_IF (a_it < a.begin ());
398 // test difference type
399 int a_diff = a_it - a.begin ();
400 FAIL_RETURN_IF (a_diff != 1);
402 // test copy constructor
403 ITERATOR_T a_it1 (a_it);
404 FAIL_RETURN_IF (a_it1 != a_it);
406 // test preincrement operator
407 ++a_it1;
408 FAIL_RETURN_IF ((a_it1 - a_it) != 1);
410 // test = and += operator
411 ITERATOR_T a_it2 = a_it += 3;
412 FAIL_RETURN_IF (a_it2 != a_it);
413 FAIL_RETURN_IF ((a_it - a_it1) != 2);
415 // test + operator
416 a_it2 = a_it1 + 3;
417 FAIL_RETURN_IF ((a_it2 - a_it1) != 3);
419 // test post-decrement operation
420 a_it = a.end ();
421 a_it--;
422 FAIL_RETURN_IF (a_it == a.end ());
423 FAIL_RETURN_IF ((a.end () - a_it) != 1);
424 FAIL_RETURN_IF (ACE_OS::strcmp (*a_it, elem3_cstr) != 0);
426 // test pre-decrement operator
427 a_it = a.end ();
428 --a_it;
429 FAIL_RETURN_IF (a_it == a.end ());
430 FAIL_RETURN_IF ((a.end () - a_it) != 1);
431 FAIL_RETURN_IF (ACE_OS::strcmp (*a_it, elem3_cstr) != 0);
433 // test -= operator
434 a_it -= 3;
435 FAIL_RETURN_IF ((a_it1 - a_it) != 2);
437 // test - operator
438 a_it2 = a_it1 - 2;
439 FAIL_RETURN_IF ((a_it1 - a_it2) != 2);
441 // test operator[] read
442 a_it = a.begin ();
443 FAIL_RETURN_IF (ACE_OS::strcmp (a_it[0],a[0]) != 0);
444 a_it += 2;
445 FAIL_RETURN_IF (ACE_OS::strcmp (a_it[0],a[2]) != 0);
447 // test for loop behaviour
448 const ::CORBA::StringSeq b = a;
449 ITERATOR_T b_it = b.begin ();
451 for (a_it = a.begin ();
452 a_it != a.end ();
453 a_it++, b_it++)
455 FAIL_RETURN_IF (ACE_OS::strcmp (*a_it, *b_it) != 0);
458 ::CORBA::StringSeq test;
459 test.length (a.length ());
461 std::copy (a.begin (),
462 a.end (),
463 test.begin ());
465 FAIL_RETURN_IF (test.length () != a.length ());
467 ITERATOR_T copytest_iter = test.begin ();
468 for (ITERATOR_T copya_iter = a.begin ();
469 copya_iter != a.end ();
470 ++copya_iter, ++copytest_iter)
472 FAIL_RETURN_IF (ACE_OS::strcmp (*copya_iter, *copytest_iter) != 0);
475 /// Testing - using ostream_iterator
476 std::ostringstream ostream;
477 std::copy (a.begin (),
478 a.end (),
479 std::ostream_iterator<CORBA::StringSeq::const_value_type> (ostream,
480 "\n"));
482 FAIL_RETURN_IF (
483 ostream.str ().compare ("elem0\nelem1\nelem2\nelem3\n") != 0);
485 return 0;
488 //-----------------------------------------------------------------------------
490 template <typename REVERSE_ITERATOR_T>
491 int test_sequence_reverse ()
493 ::CORBA::StringSeq a;
495 // test equality operator
496 FAIL_RETURN_IF (!(a.begin () == a.begin ()));
498 // test non-equality operator
499 FAIL_RETURN_IF (a.end () != a.end ());
501 // test for correct behaviour for empty sequence
503 FAIL_RETURN_IF (a.begin() != a.end ());
505 // setup of an example sequence
506 const char * elem0_cstr = "elem0";
507 const char * elem1_cstr = "elem1";
508 const char * elem2_cstr = "elem2";
509 const char * elem3_cstr = "elem3";
511 a.length (4);
512 a[0] = CORBA::string_dup (elem0_cstr);
513 a[1] = CORBA::string_dup (elem1_cstr);
514 a[2] = CORBA::string_dup (elem2_cstr);
515 a[3] = CORBA::string_dup (elem3_cstr);
517 // test iterator copy constructor
518 REVERSE_ITERATOR_T a_it (a.rbegin ());
519 FAIL_RETURN_IF (a_it != a.rbegin ());
521 // test assignment operator
522 a_it = a.rbegin ();
524 // Create a case to test for memory leaks
525 // in the iterator.
526 *a_it = CORBA::string_dup (elem3_cstr);
527 FAIL_RETURN_IF (a_it != a.rbegin ());
529 // test non const dereferencing
530 typename REVERSE_ITERATOR_T::element_type value0 = *a_it;
531 FAIL_RETURN_IF (ACE_OS::strcmp (value0, elem3_cstr) != 0);
533 // test const dereferencing
534 const char* const value1 = *a_it;
535 FAIL_RETURN_IF (ACE_OS::strcmp (value1, elem3_cstr) != 0);
537 // test increment operation
538 a_it++;
539 FAIL_RETURN_IF (a_it == a.rbegin());
540 FAIL_RETURN_IF (ACE_OS::strcmp (*a_it, elem2_cstr) != 0);
542 // test < operator
543 FAIL_RETURN_IF (!(a.rbegin () < a_it));
544 FAIL_RETURN_IF (a_it < a.rbegin ());
546 // test difference type
547 int a_diff = a_it - a.rbegin ();
548 FAIL_RETURN_IF (a_diff != 1);
550 // test copy constructor
551 REVERSE_ITERATOR_T a_it1 (a_it);
552 FAIL_RETURN_IF (a_it1 != a_it);
554 // test preincrement operator
555 ++a_it1;
556 FAIL_RETURN_IF ((a_it1 - a_it) != 1);
558 // test = and += operator
559 REVERSE_ITERATOR_T a_it2 = a_it += 3;
560 FAIL_RETURN_IF (a_it2 != a_it);
561 FAIL_RETURN_IF ((a_it - a_it1) != 2);
563 // test + operator
564 a_it2 = a_it1 + 3;
565 FAIL_RETURN_IF ((a_it2 - a_it1) != 3);
567 // test post-decrement operation
568 a_it = a.rend ();
569 a_it--;
570 FAIL_RETURN_IF (a_it == a.rend ());
571 FAIL_RETURN_IF ((a.rend () - a_it) != 1);
572 FAIL_RETURN_IF (ACE_OS::strcmp (*a_it, elem0_cstr) != 0);
574 // test pre-decrement operator
575 a_it = a.rend ();
576 --a_it;
577 FAIL_RETURN_IF (a_it == a.rend ());
578 FAIL_RETURN_IF ((a.rend () - a_it) != 1);
579 FAIL_RETURN_IF (ACE_OS::strcmp (*a_it, elem0_cstr) != 0);
581 // test -= operator
582 a_it -= 3;
583 FAIL_RETURN_IF ((a_it1 - a_it) != 2);
585 // test - operator
586 a_it2 = a_it1 - 2;
587 FAIL_RETURN_IF ((a_it1 - a_it2) != 2);
589 // test operator[] read
590 a_it = a.rbegin ();
591 FAIL_RETURN_IF (ACE_OS::strcmp (a_it[0],a[3]) != 0);
592 a_it += 2;
593 FAIL_RETURN_IF (ACE_OS::strcmp (a_it[0],a[1]) != 0);
595 // test operator[] write
596 a_it[0] = CORBA::string_dup (elem0_cstr);
597 FAIL_RETURN_IF (ACE_OS::strcmp (a[1],elem0_cstr) != 0);
599 // reset content of sequence a
600 a[1] = CORBA::string_dup (elem1_cstr);
602 // test for loop behaviour
603 ::CORBA::StringSeq b = a;
604 REVERSE_ITERATOR_T b_it = b.rbegin ();
606 for (a_it = a.rbegin ();
607 a_it != a.rend ();
608 a_it++, b_it++)
610 FAIL_RETURN_IF (ACE_OS::strcmp (*a_it, *b_it) != 0);
613 ::CORBA::StringSeq test;
614 test.length (a.length ());
616 std::copy (a.begin (),
617 a.end (),
618 test.begin ());
620 FAIL_RETURN_IF (test.length () != a.length ());
622 REVERSE_ITERATOR_T copytest_iter = test.rbegin ();
623 for (REVERSE_ITERATOR_T copya_iter = a.rbegin ();
624 copya_iter != a.rend ();
625 ++copya_iter, ++copytest_iter)
627 FAIL_RETURN_IF (ACE_OS::strcmp (*copya_iter, *copytest_iter) != 0);
630 /// Testing - using ostream_iterator
631 std::ostringstream ostream;
632 std::copy (a.rbegin (),
633 a.rend (),
634 std::ostream_iterator<CORBA::StringSeq::element_type> (ostream,
635 "\n"));
637 FAIL_RETURN_IF (
638 ostream.str ().compare ("elem3\nelem2\nelem1\nelem0\n") != 0);
640 return 0;
643 //-----------------------------------------------------------------------------
645 template <typename REVERSE_ITERATOR_T>
646 int test_sequence_reverse_const_iterator ()
648 ::CORBA::StringSeq a;
650 // test equality operator
651 FAIL_RETURN_IF (!(a.begin () == a.begin ()));
653 // test non-equality operator
654 FAIL_RETURN_IF (a.end () != a.end ());
656 // test for correct behaviour for empty sequence
658 FAIL_RETURN_IF (a.begin() != a.end ());
660 // setup of an example sequence
661 const char * elem0_cstr = "elem0";
662 const char * elem1_cstr = "elem1";
663 const char * elem2_cstr = "elem2";
664 const char * elem3_cstr = "elem3";
666 a.length (4);
667 a[0] = CORBA::string_dup (elem0_cstr);
668 a[1] = CORBA::string_dup (elem1_cstr);
669 a[2] = CORBA::string_dup (elem2_cstr);
670 a[3] = CORBA::string_dup (elem3_cstr);
672 // test iterator copy constructor
673 REVERSE_ITERATOR_T a_it (a.rbegin ());
674 FAIL_RETURN_IF (a_it != a.rbegin ());
676 // test assignment operator
677 a_it = a.rbegin ();
678 FAIL_RETURN_IF (a_it != a.rbegin ());
680 // test non const dereferencing
681 typename REVERSE_ITERATOR_T::const_element_type value0 = *a_it;
682 FAIL_RETURN_IF (ACE_OS::strcmp (value0, elem3_cstr) != 0);
684 // test const dereferencing
685 const char* const value1 = *a_it;
686 FAIL_RETURN_IF (ACE_OS::strcmp (value1, elem3_cstr) != 0);
688 // test increment operation
689 a_it++;
690 FAIL_RETURN_IF (a_it == a.rbegin());
691 FAIL_RETURN_IF (ACE_OS::strcmp (*a_it, elem2_cstr) != 0);
693 // test < operator
694 FAIL_RETURN_IF (!(a.rbegin () < a_it));
695 FAIL_RETURN_IF (a_it < a.rbegin ());
697 // test difference type
698 int a_diff = a_it - a.rbegin ();
699 FAIL_RETURN_IF (a_diff != 1);
701 // test copy constructor
702 REVERSE_ITERATOR_T a_it1 (a_it);
703 FAIL_RETURN_IF (a_it1 != a_it);
705 // test preincrement operator
706 ++a_it1;
707 FAIL_RETURN_IF ((a_it1 - a_it) != 1);
709 // test = and += operator
710 REVERSE_ITERATOR_T a_it2 = a_it += 3;
711 FAIL_RETURN_IF (a_it2 != a_it);
712 FAIL_RETURN_IF ((a_it - a_it1) != 2);
714 // test + operator
715 a_it2 = a_it1 + 3;
716 FAIL_RETURN_IF ((a_it2 - a_it1) != 3);
718 // test post-decrement operation
719 a_it = a.rend ();
720 a_it--;
721 FAIL_RETURN_IF (a_it == a.rend ());
722 FAIL_RETURN_IF ((a.rend () - a_it) != 1);
723 FAIL_RETURN_IF (ACE_OS::strcmp (*a_it, elem0_cstr) != 0);
725 // test pre-decrement operator
726 a_it = a.rend ();
727 --a_it;
728 FAIL_RETURN_IF (a_it == a.rend ());
729 FAIL_RETURN_IF ((a.rend () - a_it) != 1);
730 FAIL_RETURN_IF (ACE_OS::strcmp (*a_it, elem0_cstr) != 0);
732 // test -= operator
733 a_it -= 3;
734 FAIL_RETURN_IF ((a_it1 - a_it) != 2);
736 // test - operator
737 a_it2 = a_it1 - 2;
738 FAIL_RETURN_IF ((a_it1 - a_it2) != 2);
740 // test operator[] read
741 a_it = a.rbegin ();
742 FAIL_RETURN_IF (ACE_OS::strcmp (a_it[0],a[3]) != 0);
743 a_it += 2;
744 FAIL_RETURN_IF (ACE_OS::strcmp (a_it[0],a[1]) != 0);
746 // test for loop behaviour
747 ::CORBA::StringSeq b = a;
748 REVERSE_ITERATOR_T b_it = b.rbegin ();
750 for (a_it = a.rbegin ();
751 a_it != a.rend ();
752 a_it++, b_it++)
754 FAIL_RETURN_IF (ACE_OS::strcmp (*a_it, *b_it) != 0);
757 ::CORBA::StringSeq test;
758 test.length (a.length ());
760 std::copy (a.begin (),
761 a.end (),
762 test.begin ());
764 FAIL_RETURN_IF (test.length () != a.length ());
766 REVERSE_ITERATOR_T copytest_iter = test.rbegin ();
767 for (REVERSE_ITERATOR_T copya_iter = a.rbegin ();
768 copya_iter != a.rend ();
769 ++copya_iter, ++copytest_iter)
771 FAIL_RETURN_IF (ACE_OS::strcmp (*copya_iter, *copytest_iter) != 0);
774 /// Testing - using ostream_iterator
775 std::ostringstream ostream;
776 std::copy (a.rbegin (),
777 a.rend (),
778 std::ostream_iterator<CORBA::StringSeq::element_type> (ostream,
779 "\n"));
781 FAIL_RETURN_IF (
782 ostream.str ().compare ("elem3\nelem2\nelem1\nelem0\n") != 0);
784 return 0;
787 //-----------------------------------------------------------------------------
789 template <typename REVERSE_ITERATOR_T>
790 int test_const_sequence_reverse ()
792 // setup of an example sequence
793 const char * elem0_cstr = "elem0";
794 const char * elem1_cstr = "elem1";
795 const char * elem2_cstr = "elem2";
796 const char * elem3_cstr = "elem3";
798 ::CORBA::StringSeq setup;;
799 setup.length (4);
800 setup[0] = CORBA::string_dup (elem0_cstr);
801 setup[1] = CORBA::string_dup (elem1_cstr);
802 setup[2] = CORBA::string_dup (elem2_cstr);
803 setup[3] = CORBA::string_dup (elem3_cstr);
805 const ::CORBA::StringSeq a = setup;
807 // test equality operator
808 FAIL_RETURN_IF (!(a.begin () == a.begin ()));
810 // test non-equality operator
811 FAIL_RETURN_IF (a.end () != a.end ());
813 // test iterator copy constructor
814 REVERSE_ITERATOR_T a_it (a.rbegin ());
815 FAIL_RETURN_IF (a_it != a.rbegin ());
817 // test non const dereferencing
818 typename REVERSE_ITERATOR_T::const_element_type value0 = *a_it;
819 FAIL_RETURN_IF (ACE_OS::strcmp (value0, elem3_cstr) != 0);
821 // test const dereferencing
822 const char* const value1 = *a_it;
823 FAIL_RETURN_IF (ACE_OS::strcmp (value1, elem3_cstr) != 0);
825 // test increment operation
826 a_it++;
827 FAIL_RETURN_IF (a_it == a.rbegin());
828 FAIL_RETURN_IF (ACE_OS::strcmp (*a_it, elem2_cstr) != 0);
830 // test < operator
831 FAIL_RETURN_IF (!(a.rbegin () < a_it));
832 FAIL_RETURN_IF (a_it < a.rbegin ());
834 // test difference type
835 int a_diff = a_it - a.rbegin ();
836 FAIL_RETURN_IF (a_diff != 1);
838 // test copy constructor
839 REVERSE_ITERATOR_T a_it1 (a_it);
840 FAIL_RETURN_IF (a_it1 != a_it);
842 // test preincrement operator
843 ++a_it1;
844 FAIL_RETURN_IF ((a_it1 - a_it) != 1);
846 // test = and += operator
847 REVERSE_ITERATOR_T a_it2 = a_it += 3;
848 FAIL_RETURN_IF (a_it2 != a_it);
849 FAIL_RETURN_IF ((a_it - a_it1) != 2);
851 // test + operator
852 a_it2 = a_it1 + 3;
853 FAIL_RETURN_IF ((a_it2 - a_it1) != 3);
855 // test post-decrement operation
856 a_it = a.rend ();
857 a_it--;
858 FAIL_RETURN_IF (a_it == a.rend ());
859 FAIL_RETURN_IF ((a.rend () - a_it) != 1);
860 FAIL_RETURN_IF (ACE_OS::strcmp (*a_it, elem0_cstr) != 0);
862 // test pre-decrement operator
863 a_it = a.rend ();
864 --a_it;
865 FAIL_RETURN_IF (a_it == a.rend ());
866 FAIL_RETURN_IF ((a.rend () - a_it) != 1);
867 FAIL_RETURN_IF (ACE_OS::strcmp (*a_it, elem0_cstr) != 0);
869 // test -= operator
870 a_it -= 3;
871 FAIL_RETURN_IF ((a_it1 - a_it) != 2);
873 // test - operator
874 a_it2 = a_it1 - 2;
875 FAIL_RETURN_IF ((a_it1 - a_it2) != 2);
877 // test operator[] read
878 a_it = a.rbegin ();
879 FAIL_RETURN_IF (ACE_OS::strcmp (a_it[0],a[3]) != 0);
880 a_it += 2;
881 FAIL_RETURN_IF (ACE_OS::strcmp (a_it[0],a[1]) != 0);
883 // test for loop behaviour
884 ::CORBA::StringSeq b = a;
885 REVERSE_ITERATOR_T b_it = b.rbegin ();
887 for (a_it = a.rbegin ();
888 a_it != a.rend ();
889 a_it++, b_it++)
891 FAIL_RETURN_IF (ACE_OS::strcmp (*a_it, *b_it) != 0);
894 ::CORBA::StringSeq test;
895 test.length (a.length ());
897 std::copy (a.begin (),
898 a.end (),
899 test.begin ());
901 FAIL_RETURN_IF (test.length () != a.length ());
903 REVERSE_ITERATOR_T copytest_iter = test.rbegin ();
904 for (REVERSE_ITERATOR_T copya_iter = a.rbegin ();
905 copya_iter != a.rend ();
906 ++copya_iter, ++copytest_iter)
908 FAIL_RETURN_IF (ACE_OS::strcmp (*copya_iter, *copytest_iter) != 0);
911 /// Testing - using ostream_iterator
912 std::ostringstream ostream;
913 std::copy (a.rbegin (),
914 a.rend (),
915 std::ostream_iterator<CORBA::StringSeq::const_element_type> (ostream,
916 "\n"));
918 FAIL_RETURN_IF (
919 ostream.str ().compare ("elem3\nelem2\nelem1\nelem0\n") != 0);
921 return 0;
924 #endif
926 //-----------------------------------------------------------------------------
928 int ACE_TMAIN(int,ACE_TCHAR*[])
930 int status = 0;
932 #if defined TAO_HAS_SEQUENCE_ITERATORS && TAO_HAS_SEQUENCE_ITERATORS == 1
934 // Test Generic_Sequence_Iterator.
935 status += test_sequence< ::CORBA::StringSeq::iterator> ();
937 // g++ seems to make the conversion from iterator to const_iterator
938 // and Windows doesn't. Not sure why.
939 // Test Const_Generic_Sequence_Iterator with non-const sequence.
940 status += test_sequence_const_iterator <
941 ::CORBA::StringSeq::const_iterator> ();
943 // Test Const_Generic_Sequence_Iterator with const sequence.
944 status += test_const_sequence< ::CORBA::StringSeq::const_iterator> ();
946 // Test Generic_Sequence_Reverse_Iterator.
947 status += test_sequence_reverse< ::CORBA::StringSeq::reverse_iterator> ();
949 // Test Const_Generic_Sequence_Reverse_Iterator with non-const sequence.
950 status += test_sequence_reverse_const_iterator<
951 ::CORBA::StringSeq::const_reverse_iterator> ();
953 // Test Const_Generic_Sequence_Reverse_Iterator with const sequence.
954 status += test_const_sequence_reverse<
955 ::CORBA::StringSeq::const_reverse_iterator> ();
956 #endif
958 return status;