Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / Sequence_Iterators / Unbounded_Value.cpp
blob682c27bd025d735ce513c39c50f1eb501a66d4be
1 /**
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)
7 */
9 #include "tao/Unbounded_Value_Sequence_T.h"
10 #include "ace/Log_Msg.h"
12 #include <iostream>
13 #include <iterator>
14 #include <sstream>
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) \
21 if (CONDITION) \
22 { \
23 ACE_DEBUG ((LM_ERROR, ACE_TEXT ("\tFailed at %N:%l\n"))); \
24 return 1; \
27 template <typename ITERATOR_T>
28 int test_sequence ()
30 v_sequence a;
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
42 a.length (4);
44 int elem0 = 0;
45 int elem1 = 1;
46 int elem2 = 2;
47 int elem3 = 3;
49 a[0] = elem0;
50 a[1] = elem1;
51 a[2] = elem2;
52 a[3] = elem3;
54 // test iterator copy constructor
55 ITERATOR_T a_it (a.begin ());
56 FAIL_RETURN_IF (a_it != a.begin ());
58 // test assignment operator
59 a_it = a.begin ();
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).
66 //int value0 = *a_it;
67 //FAIL_RETURN_IF (value0 != elem0);
69 // test const dereferencing
70 int value1 = *a_it;
71 FAIL_RETURN_IF (value1 != elem0);
73 // test increment operation
74 a_it++;
75 FAIL_RETURN_IF (a_it == a.begin());
76 FAIL_RETURN_IF (*a_it != elem1);
78 // test < operator
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
91 ++a_it1;
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);
99 // test + operator
100 a_it2 = a_it1 + 3;
101 FAIL_RETURN_IF ((a_it2 - a_it1) != 3);
103 // test post-decrement operation
104 a_it = a.end ();
105 a_it--;
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
111 a_it = a.end ();
112 --a_it;
113 FAIL_RETURN_IF (a_it == a.end ());
114 FAIL_RETURN_IF ((a.end () - a_it) != 1);
115 FAIL_RETURN_IF (*a_it != elem3);
117 // test -= operator
118 a_it -= 3;
119 FAIL_RETURN_IF ((a_it1 - a_it) != 2);
121 // test - operator
122 a_it2 = a_it1 - 2;
123 FAIL_RETURN_IF ((a_it1 - a_it2) != 2);
125 // test operator[] read
126 a_it = a.begin ();
127 FAIL_RETURN_IF (a_it[0] != a[0]);
128 a_it += 2;
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
134 // a_it[0] = elem0;
135 // FAIL_RETURN_IF (a[2] != elem0);
137 // reset content of sequence a
138 //a[2] = elem2;
140 // test for loop behaviour
141 v_sequence b = a;
142 ITERATOR_T b_it = b.begin ();
144 for (a_it = a.begin ();
145 a_it != a.end ();
146 a_it++, b_it++)
148 FAIL_RETURN_IF (*a_it != *b_it);
151 v_sequence test;
152 test.length (4);
154 std::copy (a.begin (),
155 a.end (),
156 test.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 (),
172 a.end (),
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,
176 "\n"));
178 FAIL_RETURN_IF (
179 ostream.str ().compare ("0\n1\n2\n3\n") != 0);
181 return 0;
184 //-----------------------------------------------------------------------------
186 template <typename ITERATOR_T>
187 int test_const_sequence ()
189 // setup of an example sequence
190 v_sequence setup;
191 setup.length (4);
193 int elem0 = 0;
194 int elem1 = 1;
195 int elem2 = 2;
196 int elem3 = 3;
198 setup[0] = elem0;
199 setup[1] = elem1;
200 setup[2] = elem2;
201 setup[3] = elem3;
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
216 a_it = a.begin ();
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
227 int value1 = *a_it;
228 FAIL_RETURN_IF (value1 != elem0);
230 // test increment operation
231 a_it++;
232 FAIL_RETURN_IF (a_it == a.begin());
233 FAIL_RETURN_IF (*a_it != elem1);
235 // test < operator
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
248 ++a_it1;
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);
256 // test + operator
257 a_it2 = a_it1 + 3;
258 FAIL_RETURN_IF ((a_it2 - a_it1) != 3);
260 // test post-decrement operation
261 a_it = a.end ();
262 a_it--;
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
268 a_it = a.end ();
269 --a_it;
270 FAIL_RETURN_IF (a_it == a.end ());
271 FAIL_RETURN_IF ((a.end () - a_it) != 1);
272 FAIL_RETURN_IF (*a_it != elem3);
274 // test -= operator
275 a_it -= 3;
276 FAIL_RETURN_IF ((a_it1 - a_it) != 2);
278 // test - operator
279 a_it2 = a_it1 - 2;
280 FAIL_RETURN_IF ((a_it1 - a_it2) != 2);
282 // test operator[] read
283 a_it = a.begin ();
284 FAIL_RETURN_IF (a_it[0] != a[0]);
285 a_it += 2;
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
291 // a_it[0] = elem0;
292 // FAIL_RETURN_IF (a[2] != elem0);
294 // reset content of sequence a
295 //a[2] = elem2;
297 // test for loop behaviour
298 v_sequence b = a;
299 ITERATOR_T b_it = b.begin ();
301 for (a_it = a.begin ();
302 a_it != a.end ();
303 a_it++, b_it++)
305 FAIL_RETURN_IF (*a_it != *b_it);
308 v_sequence test;
309 test.length (4);
311 std::copy (a.begin (),
312 a.end (),
313 test.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 (),
329 a.end (),
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,
333 "\n"));
335 FAIL_RETURN_IF (
336 ostream.str ().compare ("0\n1\n2\n3\n") != 0);
338 return 0;
341 //-----------------------------------------------------------------------------
343 template <typename REVERSE_ITERATOR_T>
344 int test_sequence_reverse ()
346 v_sequence a;
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
359 a.length (4);
361 int elem0 = 0;
362 int elem1 = 1;
363 int elem2 = 2;
364 int elem3 = 3;
366 a[0] = elem0;
367 a[1] = elem1;
368 a[2] = elem2;
369 a[3] = elem3;
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
376 a_it = a.rbegin ();
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
387 int value1 = *a_it;
388 FAIL_RETURN_IF (value1 != elem3);
390 // test increment operation
391 a_it++;
392 FAIL_RETURN_IF (a_it == a.rbegin());
393 FAIL_RETURN_IF (*a_it != elem2);
395 // test < operator
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
408 ++a_it1;
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);
416 // test + operator
417 a_it2 = a_it1 + 3;
418 FAIL_RETURN_IF ((a_it2 - a_it1) != 3);
420 // test post-decrement operation
421 a_it = a.rend ();
422 a_it--;
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
428 a_it = a.rend ();
429 --a_it;
430 FAIL_RETURN_IF (a_it == a.rend ());
431 FAIL_RETURN_IF ((a.rend () - a_it) != 1);
432 FAIL_RETURN_IF (*a_it != elem0);
434 // test -= operator
435 a_it -= 3;
436 FAIL_RETURN_IF ((a_it1 - a_it) != 2);
438 // test - operator
439 a_it2 = a_it1 - 2;
440 FAIL_RETURN_IF ((a_it1 - a_it2) != 2);
442 // test operator[] read
443 a_it = a.rbegin ();
444 FAIL_RETURN_IF (a_it[0] != a[3]);
445 a_it += 2;
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
451 // a_it[0] = elem0;
452 // FAIL_RETURN_IF (a[1] != elem0);
454 // reset content of sequence a
455 //a[1] = elem1;
457 // test for loop behaviour
458 v_sequence b = a;
459 REVERSE_ITERATOR_T b_it = b.rbegin ();
461 for (a_it = a.rbegin ();
462 a_it != a.rend ();
463 a_it++, b_it++)
465 FAIL_RETURN_IF (*a_it != *b_it);
468 v_sequence test;
469 test.length (a.length ());
471 std::copy (a.begin (),
472 a.end (),
473 test.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 (),
489 a.rend (),
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,
493 "\n"));
495 FAIL_RETURN_IF (
496 ostream.str ().compare ("3\n2\n1\n0\n") != 0);
498 return 0;
501 //-----------------------------------------------------------------------------
503 template <typename REVERSE_ITERATOR_T>
504 int test_const_sequence_reverse ()
506 // setup of an example sequence
507 v_sequence setup;
508 setup.length (4);
510 int elem0 = 0;
511 int elem1 = 1;
512 int elem2 = 2;
513 int elem3 = 3;
515 setup[0] = elem0;
516 setup[1] = elem1;
517 setup[2] = elem2;
518 setup[3] = elem3;
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
533 a_it = a.rbegin ();
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
544 int value1 = *a_it;
545 FAIL_RETURN_IF (value1 != elem3);
547 // test increment operation
548 a_it++;
549 FAIL_RETURN_IF (a_it == a.rbegin());
550 FAIL_RETURN_IF (*a_it != elem2);
552 // test < operator
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
565 ++a_it1;
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);
573 // test + operator
574 a_it2 = a_it1 + 3;
575 FAIL_RETURN_IF ((a_it2 - a_it1) != 3);
577 // test post-decrement operation
578 a_it = a.rend ();
579 a_it--;
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
585 a_it = a.rend ();
586 --a_it;
587 FAIL_RETURN_IF (a_it == a.rend ());
588 FAIL_RETURN_IF ((a.rend () - a_it) != 1);
589 FAIL_RETURN_IF (*a_it != elem0);
591 // test -= operator
592 a_it -= 3;
593 FAIL_RETURN_IF ((a_it1 - a_it) != 2);
595 // test - operator
596 a_it2 = a_it1 - 2;
597 FAIL_RETURN_IF ((a_it1 - a_it2) != 2);
599 // test operator[] read
600 a_it = a.rbegin ();
601 FAIL_RETURN_IF (a_it[0] != a[3]);
602 a_it += 2;
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
608 // a_it[0] = elem0;
609 // FAIL_RETURN_IF (a[1] != elem0);
611 // reset content of sequence a
612 //a[1] = elem1;
614 // test for loop behaviour
615 v_sequence b = a;
616 REVERSE_ITERATOR_T b_it = b.rbegin ();
618 for (a_it = a.rbegin ();
619 a_it != a.rend ();
620 a_it++, b_it++)
622 FAIL_RETURN_IF (*a_it != *b_it);
625 v_sequence test;
626 test.length (a.length ());
628 std::copy (a.begin (),
629 a.end (),
630 test.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 (),
646 a.rend (),
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,
650 "\n"));
652 FAIL_RETURN_IF (
653 ostream.str ().compare ("3\n2\n1\n0\n") != 0);
655 return 0;
658 #endif
660 //-----------------------------------------------------------------------------
662 int ACE_TMAIN(int,ACE_TCHAR*[])
664 int status = 0;
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> ();
687 #endif
689 return status;