2 * @file Unbounded_Value.cpp
4 * @brief test for STL iterator behaviour of CORBA bounded string sequence
6 * @author Friedhelm Wolf (fwolf@dre.vanderbilt.edu)
9 #include "tao/Unbounded_Value_Sequence_T.h"
10 #include "ace/Log_Msg.h"
16 #if defined TAO_HAS_SEQUENCE_ITERATORS && TAO_HAS_SEQUENCE_ITERATORS == 1
18 typedef TAO::unbounded_value_sequence
<int> v_sequence
;
20 #define FAIL_RETURN_IF(CONDITION) \
23 ACE_DEBUG ((LM_ERROR, ACE_TEXT ("\tFailed at %N:%l\n"))); \
27 template <typename ITERATOR_T
>
32 // test equality operator
33 FAIL_RETURN_IF (!(a
.begin () == a
.begin ()));
35 // test non-equality operator
36 FAIL_RETURN_IF (a
.end () != a
.end ());
38 // test for correct behaviour for empty sequence
39 FAIL_RETURN_IF (a
.begin() != a
.end ());
41 // setup of an example sequence
54 // test iterator copy constructor
55 ITERATOR_T
a_it (a
.begin ());
56 FAIL_RETURN_IF (a_it
!= a
.begin ());
58 // test assignment operator
60 FAIL_RETURN_IF (a_it
!= a
.begin ());
62 // test non const dereferencing
63 // JWH2 - I don't think this test makes sense. I believe the compiler
64 // will always return a const value since the dereference is on
65 // the right hand side of the assignment (i.e., r value).
67 //FAIL_RETURN_IF (value0 != elem0);
69 // test const dereferencing
71 FAIL_RETURN_IF (value1
!= elem0
);
73 // test increment operation
75 FAIL_RETURN_IF (a_it
== a
.begin());
76 FAIL_RETURN_IF (*a_it
!= elem1
);
79 FAIL_RETURN_IF (!(a
.begin () < a_it
));
80 FAIL_RETURN_IF (a_it
< a
.begin ());
82 // test difference type
83 int a_diff
= a_it
- a
.begin ();
84 FAIL_RETURN_IF (a_diff
!= 1);
86 // test copy constructor
87 ITERATOR_T
a_it1 (a_it
);
88 FAIL_RETURN_IF (a_it1
!= a_it
);
90 // test preincrement operator
92 FAIL_RETURN_IF ((a_it1
- a_it
) != 1);
94 // test = and += operator
95 ITERATOR_T a_it2
= a_it
+= 3;
96 FAIL_RETURN_IF (a_it2
!= a_it
);
97 FAIL_RETURN_IF ((a_it
- a_it1
) != 2);
101 FAIL_RETURN_IF ((a_it2
- a_it1
) != 3);
103 // test post-decrement operation
106 FAIL_RETURN_IF (a_it
== a
.end ());
107 FAIL_RETURN_IF ((a
.end () - a_it
) != 1);
108 FAIL_RETURN_IF (*a_it
!= elem3
);
110 // test pre-decrement operator
113 FAIL_RETURN_IF (a_it
== a
.end ());
114 FAIL_RETURN_IF ((a
.end () - a_it
) != 1);
115 FAIL_RETURN_IF (*a_it
!= elem3
);
119 FAIL_RETURN_IF ((a_it1
- a_it
) != 2);
123 FAIL_RETURN_IF ((a_it1
- a_it2
) != 2);
125 // test operator[] read
127 FAIL_RETURN_IF (a_it
[0] != a
[0]);
129 FAIL_RETURN_IF (a_it
[0] != a
[2]);
131 // test operator[] write
132 // NOTE: This now changes the sequence a.
133 // NOTE: This does not work for const_iterators
135 // FAIL_RETURN_IF (a[2] != elem0);
137 // reset content of sequence a
140 // test for loop behaviour
142 ITERATOR_T b_it
= b
.begin ();
144 for (a_it
= a
.begin ();
148 FAIL_RETURN_IF (*a_it
!= *b_it
);
154 std::copy (a
.begin (),
158 FAIL_RETURN_IF (test
.length () != a
.length ());
160 ITERATOR_T copytest_iter
= test
.begin ();
161 for (ITERATOR_T copya_iter
= a
.begin ();
162 copya_iter
!= a
.end ();
163 ++copya_iter
, ++copytest_iter
)
165 FAIL_RETURN_IF (*copya_iter
!= *copytest_iter
);
168 /// Testing - using ostream_iterator
170 std::ostringstream ostream
;
171 std::copy (a
.begin (),
173 // JWH2 - I changed value_type to const_value_type. Is that
174 // the correct approach?
175 std::ostream_iterator
<v_sequence::const_value_type
> (ostream
,
179 ostream
.str ().compare ("0\n1\n2\n3\n") != 0);
184 //-----------------------------------------------------------------------------
186 template <typename ITERATOR_T
>
187 int test_const_sequence ()
189 // setup of an example sequence
203 const v_sequence a
= setup
;
205 // test equality operator
206 FAIL_RETURN_IF (!(a
.begin () == a
.begin ()));
208 // test non-equality operator
209 FAIL_RETURN_IF (a
.end () != a
.end ());
211 // test iterator copy constructor
212 ITERATOR_T
a_it (a
.begin ());
213 FAIL_RETURN_IF (a_it
!= a
.begin ());
215 // test assignment operator
217 FAIL_RETURN_IF (a_it
!= a
.begin ());
219 // test non const dereferencing
220 // JWH2 - I don't think this test makes sense. I believe the compiler
221 // will always return a const value since the dereference is on
222 // the right hand side of the assignment (i.e., r value).
223 //char* value0 = *a_it;
224 //FAIL_RETURN_IF (value0 != elem0);
226 // test const dereferencing
228 FAIL_RETURN_IF (value1
!= elem0
);
230 // test increment operation
232 FAIL_RETURN_IF (a_it
== a
.begin());
233 FAIL_RETURN_IF (*a_it
!= elem1
);
236 FAIL_RETURN_IF (!(a
.begin () < a_it
));
237 FAIL_RETURN_IF (a_it
< a
.begin ());
239 // test difference type
240 int a_diff
= a_it
- a
.begin ();
241 FAIL_RETURN_IF (a_diff
!= 1);
243 // test copy constructor
244 ITERATOR_T
a_it1 (a_it
);
245 FAIL_RETURN_IF (a_it1
!= a_it
);
247 // test preincrement operator
249 FAIL_RETURN_IF ((a_it1
- a_it
) != 1);
251 // test = and += operator
252 ITERATOR_T a_it2
= a_it
+= 3;
253 FAIL_RETURN_IF (a_it2
!= a_it
);
254 FAIL_RETURN_IF ((a_it
- a_it1
) != 2);
258 FAIL_RETURN_IF ((a_it2
- a_it1
) != 3);
260 // test post-decrement operation
263 FAIL_RETURN_IF (a_it
== a
.end ());
264 FAIL_RETURN_IF ((a
.end () - a_it
) != 1);
265 FAIL_RETURN_IF (*a_it
!= elem3
);
267 // test pre-decrement operator
270 FAIL_RETURN_IF (a_it
== a
.end ());
271 FAIL_RETURN_IF ((a
.end () - a_it
) != 1);
272 FAIL_RETURN_IF (*a_it
!= elem3
);
276 FAIL_RETURN_IF ((a_it1
- a_it
) != 2);
280 FAIL_RETURN_IF ((a_it1
- a_it2
) != 2);
282 // test operator[] read
284 FAIL_RETURN_IF (a_it
[0] != a
[0]);
286 FAIL_RETURN_IF (a_it
[0] != a
[2]);
288 // test operator[] write
289 // NOTE: This now changes the sequence a.
290 // NOTE: This does not work for const_iterators
292 // FAIL_RETURN_IF (a[2] != elem0);
294 // reset content of sequence a
297 // test for loop behaviour
299 ITERATOR_T b_it
= b
.begin ();
301 for (a_it
= a
.begin ();
305 FAIL_RETURN_IF (*a_it
!= *b_it
);
311 std::copy (a
.begin (),
315 FAIL_RETURN_IF (test
.length () != a
.length ());
317 ITERATOR_T copytest_iter
= test
.begin ();
318 for (ITERATOR_T copya_iter
= a
.begin ();
319 copya_iter
!= a
.end ();
320 ++copya_iter
, ++copytest_iter
)
322 FAIL_RETURN_IF (*copya_iter
!= *copytest_iter
);
325 /// Testing - using ostream_iterator
327 std::ostringstream ostream
;
328 std::copy (a
.begin (),
330 // JWH2 - I changed value_type to const_value_type. Is that
331 // the correct approach?
332 std::ostream_iterator
<v_sequence::const_value_type
> (ostream
,
336 ostream
.str ().compare ("0\n1\n2\n3\n") != 0);
341 //-----------------------------------------------------------------------------
343 template <typename REVERSE_ITERATOR_T
>
344 int test_sequence_reverse ()
348 // test equality operator
349 FAIL_RETURN_IF (!(a
.begin () == a
.begin ()));
351 // test non-equality operator
352 FAIL_RETURN_IF (a
.end () != a
.end ());
354 // test for correct behaviour for empty sequence
356 FAIL_RETURN_IF (a
.begin() != a
.end ());
358 // setup of an example sequence
371 // test iterator copy constructor
372 REVERSE_ITERATOR_T
a_it (a
.rbegin ());
373 FAIL_RETURN_IF (a_it
!= a
.rbegin ());
375 // test assignment operator
377 FAIL_RETURN_IF (a_it
!= a
.rbegin ());
379 // test non const dereferencing
380 // JWH2 - I don't think this test makes sense. I believe the compiler
381 // will always return a const value since the dereference is on
382 // the right hand side of the assignment (i.e., r value).
383 //int value0 = *a_it;
384 //FAIL_RETURN_IF (value0 != elem3);
386 // test const dereferencing
388 FAIL_RETURN_IF (value1
!= elem3
);
390 // test increment operation
392 FAIL_RETURN_IF (a_it
== a
.rbegin());
393 FAIL_RETURN_IF (*a_it
!= elem2
);
396 FAIL_RETURN_IF (!(a
.rbegin () < a_it
));
397 FAIL_RETURN_IF (a_it
< a
.rbegin ());
399 // test difference type
400 int a_diff
= a_it
- a
.rbegin ();
401 FAIL_RETURN_IF (a_diff
!= 1);
403 // test copy constructor
404 REVERSE_ITERATOR_T
a_it1 (a_it
);
405 FAIL_RETURN_IF (a_it1
!= a_it
);
407 // test preincrement operator
409 FAIL_RETURN_IF ((a_it1
- a_it
) != 1);
411 // test = and += operator
412 REVERSE_ITERATOR_T a_it2
= a_it
+= 3;
413 FAIL_RETURN_IF (a_it2
!= a_it
);
414 FAIL_RETURN_IF ((a_it
- a_it1
) != 2);
418 FAIL_RETURN_IF ((a_it2
- a_it1
) != 3);
420 // test post-decrement operation
423 FAIL_RETURN_IF (a_it
== a
.rend ());
424 FAIL_RETURN_IF ((a
.rend () - a_it
) != 1);
425 FAIL_RETURN_IF (*a_it
!= elem0
);
427 // test pre-decrement operator
430 FAIL_RETURN_IF (a_it
== a
.rend ());
431 FAIL_RETURN_IF ((a
.rend () - a_it
) != 1);
432 FAIL_RETURN_IF (*a_it
!= elem0
);
436 FAIL_RETURN_IF ((a_it1
- a_it
) != 2);
440 FAIL_RETURN_IF ((a_it1
- a_it2
) != 2);
442 // test operator[] read
444 FAIL_RETURN_IF (a_it
[0] != a
[3]);
446 FAIL_RETURN_IF (a_it
[0] != a
[1]);
448 // test operator[] write
449 // NOTE: This now changes the sequence a.
450 // this is not possible for const iterators
452 // FAIL_RETURN_IF (a[1] != elem0);
454 // reset content of sequence a
457 // test for loop behaviour
459 REVERSE_ITERATOR_T b_it
= b
.rbegin ();
461 for (a_it
= a
.rbegin ();
465 FAIL_RETURN_IF (*a_it
!= *b_it
);
469 test
.length (a
.length ());
471 std::copy (a
.begin (),
475 FAIL_RETURN_IF (test
.length () != a
.length ());
477 REVERSE_ITERATOR_T copytest_iter
= test
.rbegin ();
478 for (REVERSE_ITERATOR_T copya_iter
= a
.rbegin ();
479 copya_iter
!= a
.rend ();
480 ++copya_iter
, ++copytest_iter
)
482 FAIL_RETURN_IF (*copya_iter
!= *copytest_iter
);
485 /// Testing - using ostream_iterator
487 std::ostringstream ostream
;
488 std::copy (a
.rbegin (),
490 // JWH2 - I changed value_type to const_value_type. Is that
491 // the correct approach?
492 std::ostream_iterator
<v_sequence::const_value_type
> (ostream
,
496 ostream
.str ().compare ("3\n2\n1\n0\n") != 0);
501 //-----------------------------------------------------------------------------
503 template <typename REVERSE_ITERATOR_T
>
504 int test_const_sequence_reverse ()
506 // setup of an example sequence
520 const v_sequence a
= setup
;
522 // test equality operator
523 FAIL_RETURN_IF (!(a
.begin () == a
.begin ()));
525 // test non-equality operator
526 FAIL_RETURN_IF (a
.end () != a
.end ());
528 // test iterator copy constructor
529 REVERSE_ITERATOR_T
a_it (a
.rbegin ());
530 FAIL_RETURN_IF (a_it
!= a
.rbegin ());
532 // test assignment operator
534 FAIL_RETURN_IF (a_it
!= a
.rbegin ());
536 // test non const dereferencing
537 // JWH2 - I don't think this test makes sense. I believe the compiler
538 // will always return a const value since the dereference is on
539 // the right hand side of the assignment (i.e., r value).
540 //int value0 = *a_it;
541 //FAIL_RETURN_IF (value0 != elem3);
543 // test const dereferencing
545 FAIL_RETURN_IF (value1
!= elem3
);
547 // test increment operation
549 FAIL_RETURN_IF (a_it
== a
.rbegin());
550 FAIL_RETURN_IF (*a_it
!= elem2
);
553 FAIL_RETURN_IF (!(a
.rbegin () < a_it
));
554 FAIL_RETURN_IF (a_it
< a
.rbegin ());
556 // test difference type
557 int a_diff
= a_it
- a
.rbegin ();
558 FAIL_RETURN_IF (a_diff
!= 1);
560 // test copy constructor
561 REVERSE_ITERATOR_T
a_it1 (a_it
);
562 FAIL_RETURN_IF (a_it1
!= a_it
);
564 // test preincrement operator
566 FAIL_RETURN_IF ((a_it1
- a_it
) != 1);
568 // test = and += operator
569 REVERSE_ITERATOR_T a_it2
= a_it
+= 3;
570 FAIL_RETURN_IF (a_it2
!= a_it
);
571 FAIL_RETURN_IF ((a_it
- a_it1
) != 2);
575 FAIL_RETURN_IF ((a_it2
- a_it1
) != 3);
577 // test post-decrement operation
580 FAIL_RETURN_IF (a_it
== a
.rend ());
581 FAIL_RETURN_IF ((a
.rend () - a_it
) != 1);
582 FAIL_RETURN_IF (*a_it
!= elem0
);
584 // test pre-decrement operator
587 FAIL_RETURN_IF (a_it
== a
.rend ());
588 FAIL_RETURN_IF ((a
.rend () - a_it
) != 1);
589 FAIL_RETURN_IF (*a_it
!= elem0
);
593 FAIL_RETURN_IF ((a_it1
- a_it
) != 2);
597 FAIL_RETURN_IF ((a_it1
- a_it2
) != 2);
599 // test operator[] read
601 FAIL_RETURN_IF (a_it
[0] != a
[3]);
603 FAIL_RETURN_IF (a_it
[0] != a
[1]);
605 // test operator[] write
606 // NOTE: This now changes the sequence a.
607 // this is not possible for const iterators
609 // FAIL_RETURN_IF (a[1] != elem0);
611 // reset content of sequence a
614 // test for loop behaviour
616 REVERSE_ITERATOR_T b_it
= b
.rbegin ();
618 for (a_it
= a
.rbegin ();
622 FAIL_RETURN_IF (*a_it
!= *b_it
);
626 test
.length (a
.length ());
628 std::copy (a
.begin (),
632 FAIL_RETURN_IF (test
.length () != a
.length ());
634 REVERSE_ITERATOR_T copytest_iter
= test
.rbegin ();
635 for (REVERSE_ITERATOR_T copya_iter
= a
.rbegin ();
636 copya_iter
!= a
.rend ();
637 ++copya_iter
, ++copytest_iter
)
639 FAIL_RETURN_IF (*copya_iter
!= *copytest_iter
);
642 /// Testing - using ostream_iterator
644 std::ostringstream ostream
;
645 std::copy (a
.rbegin (),
647 // JWH2 - I changed value_type to const_value_type. Is that
648 // the correct approach?
649 std::ostream_iterator
<v_sequence::const_value_type
> (ostream
,
653 ostream
.str ().compare ("3\n2\n1\n0\n") != 0);
660 //-----------------------------------------------------------------------------
662 int ACE_TMAIN(int,ACE_TCHAR
*[])
666 #if defined TAO_HAS_SEQUENCE_ITERATORS && TAO_HAS_SEQUENCE_ITERATORS == 1
668 // Test Generic_Sequence_Iterator.
669 status
+= test_sequence
<v_sequence::iterator
> ();
671 // g++ seems to make the conversion from iterator to const_iterator
672 // and Windows doesn't. Not sure why.
673 // Test Const_Generic_Sequence_Iterator with non-const sequence.
674 status
+= test_sequence
<v_sequence::const_iterator
> ();
676 // Test Const_Generic_Sequence_Iterator with const sequence.
677 status
+= test_const_sequence
<v_sequence::const_iterator
> ();
679 // Test Generic_Sequence_Reverse_Iterator.
680 status
+= test_sequence_reverse
<v_sequence::reverse_iterator
> ();
682 // Test Const_Generic_Sequence_Reverse_Iterator with non-const sequence.
683 status
+= test_sequence_reverse
<v_sequence::const_reverse_iterator
> ();
685 // Test Const_Generic_Sequence_Reverse_Iterator with const sequence.
686 status
+= test_const_sequence_reverse
<v_sequence::const_reverse_iterator
> ();