More tests update
[ACE_TAO.git] / TAO / tests / Sequence_Iterators / Unbounded_Objectref.cpp
blobc951b6e90a1b1f79cf1cdee501e0d1569958494c
1 /**
2 * @file Unbounded_Objectref.cpp
4 * @brief test for STL iterator behaviour of CORBA unbounded object reference
5 * sequence
7 * @author Friedhelm Wolf (fwolf@dre.vanderbilt.edu)
8 */
10 #include "ace/Log_Msg.h"
12 #include <iostream>
13 #include <iterator>
14 #include <sstream>
16 #include "tao/Object_Reference_Traits_T.h"
17 #include "mock_reference.hpp"
18 #include "tao/Unbounded_Object_Reference_Sequence_T.h"
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 #if defined TAO_HAS_SEQUENCE_ITERATORS && TAO_HAS_SEQUENCE_ITERATORS == 1
29 typedef TAO::unbounded_object_reference_sequence<mock_reference, mock_reference_var> tested_sequence;
31 template <typename ITERATOR_T>
32 int test_sequence ()
34 tested_sequence a;
36 // test equality operator
37 FAIL_RETURN_IF (!(a.begin () == a.begin ()));
39 // test non-equality operator
40 FAIL_RETURN_IF (a.end () != a.end ());
42 // test for correct behaviour for empty sequence
44 FAIL_RETURN_IF (a.begin() != a.end ());
46 mock_reference* elem0 = mock_reference::allocate (0);
47 mock_reference* elem1 = mock_reference::allocate (1);
48 mock_reference* elem2 = mock_reference::allocate (2);
49 mock_reference* elem3 = mock_reference::allocate (3);
51 // setup of an example sequence
52 a.length (4);
54 a[0] = elem0;
55 a[1] = elem1;
56 a[2] = elem2;
57 a[3] = elem3;
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 ();
65 FAIL_RETURN_IF (a_it != a.begin ());
67 // test non const dereferencing
68 mock_reference* value0 = *a_it;
69 FAIL_RETURN_IF (value0->id () != elem0->id ());
71 // test const dereferencing
72 const mock_reference* const value1 = *a_it;
73 FAIL_RETURN_IF (value1->id () != elem0->id ());
75 // test increment operation
76 a_it++;
77 FAIL_RETURN_IF (a_it == a.begin());
78 FAIL_RETURN_IF ((*a_it)->id () != elem1->id ());
80 // test < operator
81 FAIL_RETURN_IF (!(a.begin () < a_it));
82 FAIL_RETURN_IF (a_it < a.begin ());
84 // test difference type
85 int a_diff = a_it - a.begin ();
86 FAIL_RETURN_IF (a_diff != 1);
88 // test copy constructor
89 ITERATOR_T a_it1 (a_it);
90 FAIL_RETURN_IF (a_it1 != a_it);
92 // test preincrement operator
93 ++a_it1;
94 FAIL_RETURN_IF ((a_it1 - a_it) != 1);
96 // test = and += operator
97 ITERATOR_T a_it2 = a_it += 3;
98 FAIL_RETURN_IF (a_it2 != a_it);
99 FAIL_RETURN_IF ((a_it - a_it1) != 2);
101 // test + operator
102 a_it2 = a_it1 + 3;
103 FAIL_RETURN_IF ((a_it2 - a_it1) != 3);
105 // test post-decrement operation
106 a_it = a.end ();
107 a_it--;
108 FAIL_RETURN_IF (a_it == a.end ());
109 FAIL_RETURN_IF ((a.end () - a_it) != 1);
110 FAIL_RETURN_IF ((*a_it)->id () != elem3->id ());
112 // test pre-decrement operator
113 a_it = a.end ();
114 --a_it;
115 FAIL_RETURN_IF (a_it == a.end ());
116 FAIL_RETURN_IF ((a.end () - a_it) != 1);
117 FAIL_RETURN_IF ((*a_it)->id () != elem3->id ());
119 // test -= operator
120 a_it -= 3;
121 FAIL_RETURN_IF ((a_it1 - a_it) != 2);
123 // test - operator
124 a_it2 = a_it1 - 2;
125 FAIL_RETURN_IF ((a_it1 - a_it2) != 2);
127 // test operator[] read
128 a_it = a.begin ();
129 FAIL_RETURN_IF (a_it[0]->id () != a[0]->id ());
130 a_it += 2;
131 FAIL_RETURN_IF (a_it[0]->id () != a[2]->id ());
133 // test for loop behaviour
134 tested_sequence b = a;
135 ITERATOR_T b_it = b.begin ();
137 for (a_it = a.begin ();
138 a_it != a.end ();
139 a_it++, b_it++)
141 FAIL_RETURN_IF ((*a_it)->id () != (*b_it)->id ());
144 tested_sequence test;
145 test.length (a.length ());
147 std::copy (a.begin (),
148 a.end (),
149 test.begin ());
151 FAIL_RETURN_IF (test.length () != a.length ());
153 ITERATOR_T copytest_iter = test.begin ();
154 for (ITERATOR_T copya_iter = a.begin ();
155 copya_iter != a.end ();
156 ++copya_iter, ++copytest_iter)
158 FAIL_RETURN_IF ((*copya_iter)->id () != (*copytest_iter)->id ());
161 /// Testing - using ostream_iterator
162 /// JWH2 - I don't think the ostream test makes sense for object references.
164 std::ostringstream ostream;
165 std::copy (a.begin (),
166 a.end (),
167 std::ostream_iterator<tested_sequence::value_type> (ostream,
168 "\n"));
170 FAIL_RETURN_IF (
171 ostream.str ().compare ("elem0\nelem1\nelem2\nelem3\n") != 0);
174 return 0;
177 //-----------------------------------------------------------------------------
179 template <typename ITERATOR_T>
180 int test_const_sequence ()
182 // setup of an example sequence
183 tested_sequence setup;
184 setup.length (4);
186 mock_reference* elem0 = mock_reference::allocate (0);
187 mock_reference* elem1 = mock_reference::allocate (1);
188 mock_reference* elem2 = mock_reference::allocate (2);
189 mock_reference* elem3 = mock_reference::allocate (3);
191 // setup of an example sequence
192 setup[0] = elem0;
193 setup[1] = elem1;
194 setup[2] = elem2;
195 setup[3] = elem3;
197 const tested_sequence a = setup;
199 // test equality operator
200 FAIL_RETURN_IF (!(a.begin () == a.begin ()));
202 // test non-equality operator
203 FAIL_RETURN_IF (a.end () != a.end ());
205 // test iterator copy constructor
206 ITERATOR_T a_it (a.begin ());
207 FAIL_RETURN_IF (a_it != a.begin ());
209 // test assignment operator
210 a_it = a.begin ();
211 FAIL_RETURN_IF (a_it != a.begin ());
213 // test non const dereferencing
214 mock_reference* value0 = *a_it;
215 FAIL_RETURN_IF (value0->id () != elem0->id ());
217 // test const dereferencing
218 const mock_reference* const value1 = *a_it;
219 FAIL_RETURN_IF (value1->id () != elem0->id ());
221 // test increment operation
222 a_it++;
223 FAIL_RETURN_IF (a_it == a.begin());
224 FAIL_RETURN_IF ((*a_it)->id () != elem1->id ());
226 // test < operator
227 FAIL_RETURN_IF (!(a.begin () < a_it));
228 FAIL_RETURN_IF (a_it < a.begin ());
230 // test difference type
231 int a_diff = a_it - a.begin ();
232 FAIL_RETURN_IF (a_diff != 1);
234 // test copy constructor
235 ITERATOR_T a_it1 (a_it);
236 FAIL_RETURN_IF (a_it1 != a_it);
238 // test preincrement operator
239 ++a_it1;
240 FAIL_RETURN_IF ((a_it1 - a_it) != 1);
242 // test = and += operator
243 ITERATOR_T a_it2 = a_it += 3;
244 FAIL_RETURN_IF (a_it2 != a_it);
245 FAIL_RETURN_IF ((a_it - a_it1) != 2);
247 // test + operator
248 a_it2 = a_it1 + 3;
249 FAIL_RETURN_IF ((a_it2 - a_it1) != 3);
251 // test post-decrement operation
252 a_it = a.end ();
253 a_it--;
254 FAIL_RETURN_IF (a_it == a.end ());
255 FAIL_RETURN_IF ((a.end () - a_it) != 1);
256 FAIL_RETURN_IF ((*a_it)->id () != elem3->id ());
258 // test pre-decrement operator
259 a_it = a.end ();
260 --a_it;
261 FAIL_RETURN_IF (a_it == a.end ());
262 FAIL_RETURN_IF ((a.end () - a_it) != 1);
263 FAIL_RETURN_IF ((*a_it)->id () != elem3->id ());
265 // test -= operator
266 a_it -= 3;
267 FAIL_RETURN_IF ((a_it1 - a_it) != 2);
269 // test - operator
270 a_it2 = a_it1 - 2;
271 FAIL_RETURN_IF ((a_it1 - a_it2) != 2);
273 // test operator[] read
274 a_it = a.begin ();
275 FAIL_RETURN_IF ((a_it[0])->id () != a[0]->id ());
276 a_it += 2;
277 FAIL_RETURN_IF ((a_it[0])->id () != a[2]->id ());
279 // test for loop behaviour
280 tested_sequence b = a;
281 ITERATOR_T b_it = b.begin ();
283 for (a_it = a.begin ();
284 a_it != a.end ();
285 a_it++, b_it++)
287 FAIL_RETURN_IF ((*a_it)->id () != (*b_it)->id ());
290 tested_sequence test;
291 test.length (4);
294 * The copy call below causes double deletes and seg faults.
296 std::copy (a.begin (),
297 a.end (),
298 test.begin ());
300 FAIL_RETURN_IF (test.length () != a.length ());
302 ITERATOR_T copytest_iter = test.begin ();
303 for (ITERATOR_T copya_iter = a.begin ();
304 copya_iter != a.end ();
305 ++copya_iter, ++copytest_iter)
307 FAIL_RETURN_IF ((*copya_iter)->id () != (*copytest_iter)->id ());
310 /// Testing - using ostream_iterator
311 /// JWH2 - I don't think the ostream test makes sense for object references.
313 std::ostringstream ostream;
314 std::copy (a.begin (),
315 a.end (),
316 std::ostream_iterator<tested_sequence::value_type> (ostream,
317 "\n"));
319 FAIL_RETURN_IF (
320 ostream.str ().compare ("elem0\nelem1\nelem2\nelem3\n") != 0);
323 return 0;
326 //-----------------------------------------------------------------------------
328 template <typename REVERSE_ITERATOR_T>
329 int test_sequence_reverse ()
331 tested_sequence a;
333 // test equality operator
334 FAIL_RETURN_IF (!(a.begin () == a.begin ()));
336 // test non-equality operator
337 FAIL_RETURN_IF (a.end () != a.end ());
339 // test for correct behaviour for empty sequence
341 FAIL_RETURN_IF (a.begin() != a.end ());
343 // setup of an example sequence
344 a.length (4);
346 mock_reference* elem0 = mock_reference::allocate (0);
347 mock_reference* elem1 = mock_reference::allocate (1);
348 mock_reference* elem2 = mock_reference::allocate (2);
349 mock_reference* elem3 = mock_reference::allocate (3);
351 // setup of an example sequence
352 a.length (4);
353 a[0] = elem0;
354 a[1] = elem1;
355 a[2] = elem2;
356 a[3] = elem3;
358 // test iterator copy constructor
359 REVERSE_ITERATOR_T a_it (a.rbegin ());
360 FAIL_RETURN_IF (a_it != a.rbegin ());
362 // test assignment operator
363 a_it = a.rbegin ();
364 FAIL_RETURN_IF (a_it != a.rbegin ());
366 // test non const dereferencing
367 mock_reference* value0 = *a_it;
368 FAIL_RETURN_IF (value0->id () != elem3->id ());
370 // test const dereferencing
371 const mock_reference* const value1 = *a_it;
372 FAIL_RETURN_IF (value1->id () != elem3->id ());
374 // test increment operation
375 a_it++;
376 FAIL_RETURN_IF (a_it == a.rbegin());
377 FAIL_RETURN_IF ((*a_it)->id () != elem2->id ());
379 // test < operator
380 FAIL_RETURN_IF (!(a.rbegin () < a_it));
381 FAIL_RETURN_IF (a_it < a.rbegin ());
383 // test difference type
384 int a_diff = a_it - a.rbegin ();
385 FAIL_RETURN_IF (a_diff != 1);
387 // test copy constructor
388 REVERSE_ITERATOR_T a_it1 (a_it);
389 FAIL_RETURN_IF (a_it1 != a_it);
391 // test preincrement operator
392 ++a_it1;
393 FAIL_RETURN_IF ((a_it1 - a_it) != 1);
395 // test = and += operator
396 REVERSE_ITERATOR_T a_it2 = a_it += 3;
397 FAIL_RETURN_IF (a_it2 != a_it);
398 FAIL_RETURN_IF ((a_it - a_it1) != 2);
400 // test + operator
401 a_it2 = a_it1 + 3;
402 FAIL_RETURN_IF ((a_it2 - a_it1) != 3);
404 // test post-decrement operation
405 a_it = a.rend ();
406 a_it--;
407 FAIL_RETURN_IF (a_it == a.rend ());
408 FAIL_RETURN_IF ((a.rend () - a_it) != 1);
409 FAIL_RETURN_IF ((*a_it)->id () != elem0->id ());
411 // test pre-decrement operator
412 a_it = a.rend ();
413 --a_it;
414 FAIL_RETURN_IF (a_it == a.rend ());
415 FAIL_RETURN_IF ((a.rend () - a_it) != 1);
416 FAIL_RETURN_IF ((*a_it)->id () != elem0->id ());
418 // test -= operator
419 a_it -= 3;
420 FAIL_RETURN_IF ((a_it1 - a_it) != 2);
422 // test - operator
423 a_it2 = a_it1 - 2;
424 FAIL_RETURN_IF ((a_it1 - a_it2) != 2);
426 // test operator[] read
427 a_it = a.rbegin ();
428 FAIL_RETURN_IF ((a_it[0])->id () != a[3]->id ());
429 a_it += 2;
430 FAIL_RETURN_IF ((a_it[0])->id () != a[1]->id ());
432 // test for loop behaviour
433 tested_sequence b = a;
434 REVERSE_ITERATOR_T b_it = b.rbegin ();
436 for (a_it = a.rbegin ();
437 a_it != a.rend ();
438 a_it++, b_it++)
440 FAIL_RETURN_IF ((*a_it)->id () != (*b_it)->id ());
443 tested_sequence test;
444 test.length (a.length ());
446 std::copy (a.begin (),
447 a.end (),
448 test.begin ());
450 FAIL_RETURN_IF (test.length () != a.length ());
452 REVERSE_ITERATOR_T copytest_iter = test.rbegin ();
453 for (REVERSE_ITERATOR_T copya_iter = a.rbegin ();
454 copya_iter != a.rend ();
455 ++copya_iter, ++copytest_iter)
457 FAIL_RETURN_IF ((*copya_iter)->id () != (*copytest_iter)->id ());
460 /// Testing - using ostream_iterator
461 /// JWH2 - I don't think the ostream test makes sense for object references.
463 std::ostringstream ostream;
464 std::copy (a.rbegin (),
465 a.rend (),
466 std::ostream_iterator<tested_sequence::value_type> (ostream,
467 "\n"));
469 FAIL_RETURN_IF (
470 ostream.str ().compare ("elem3\nelem2\nelem1\nelem0\n") != 0);
473 return 0;
476 //-----------------------------------------------------------------------------
478 template <typename REVERSE_ITERATOR_T>
479 int test_const_sequence_reverse ()
481 // setup of an example sequence
482 tested_sequence setup;
483 setup.length (4);
485 mock_reference* elem0 = mock_reference::allocate (0);
486 mock_reference* elem1 = mock_reference::allocate (1);
487 mock_reference* elem2 = mock_reference::allocate (2);
488 mock_reference* elem3 = mock_reference::allocate (3);
490 // setup of an example sequence
491 setup[0] = elem0;
492 setup[1] = elem1;
493 setup[2] = elem2;
494 setup[3] = elem3;
496 const tested_sequence a = setup;
498 // test equality operator
499 FAIL_RETURN_IF (!(a.begin () == a.begin ()));
501 // test non-equality operator
502 FAIL_RETURN_IF (a.end () != a.end ());
504 // test iterator copy constructor
505 REVERSE_ITERATOR_T a_it (a.rbegin ());
506 FAIL_RETURN_IF (a_it != a.rbegin ());
508 // test assignment operator
509 a_it = a.rbegin ();
510 FAIL_RETURN_IF (a_it != a.rbegin ());
512 // test non const dereferencing
513 mock_reference* value0 = *a_it;
514 FAIL_RETURN_IF (value0->id () != elem3->id ());
516 // test const dereferencing
517 const mock_reference* const value1 = *a_it;
518 FAIL_RETURN_IF (value1->id () != elem3->id ());
520 // test increment operation
521 a_it++;
522 FAIL_RETURN_IF (a_it == a.rbegin());
523 FAIL_RETURN_IF ((*a_it)->id () != elem2->id ());
525 // test < operator
526 FAIL_RETURN_IF (!(a.rbegin () < a_it));
527 FAIL_RETURN_IF (a_it < a.rbegin ());
529 // test difference type
530 int a_diff = a_it - a.rbegin ();
531 FAIL_RETURN_IF (a_diff != 1);
533 // test copy constructor
534 REVERSE_ITERATOR_T a_it1 (a_it);
535 FAIL_RETURN_IF (a_it1 != a_it);
537 // test preincrement operator
538 ++a_it1;
539 FAIL_RETURN_IF ((a_it1 - a_it) != 1);
541 // test = and += operator
542 REVERSE_ITERATOR_T a_it2 = a_it += 3;
543 FAIL_RETURN_IF (a_it2 != a_it);
544 FAIL_RETURN_IF ((a_it - a_it1) != 2);
546 // test + operator
547 a_it2 = a_it1 + 3;
548 FAIL_RETURN_IF ((a_it2 - a_it1) != 3);
550 // test post-decrement operation
551 a_it = a.rend ();
552 a_it--;
553 FAIL_RETURN_IF (a_it == a.rend ());
554 FAIL_RETURN_IF ((a.rend () - a_it) != 1);
555 FAIL_RETURN_IF ((*a_it)->id () != elem0->id ());
557 // test pre-decrement operator
558 a_it = a.rend ();
559 --a_it;
560 FAIL_RETURN_IF (a_it == a.rend ());
561 FAIL_RETURN_IF ((a.rend () - a_it) != 1);
562 FAIL_RETURN_IF ((*a_it)->id () != elem0->id ());
564 // test -= operator
565 a_it -= 3;
566 FAIL_RETURN_IF ((a_it1 - a_it) != 2);
568 // test - operator
569 a_it2 = a_it1 - 2;
570 FAIL_RETURN_IF ((a_it1 - a_it2) != 2);
572 // test operator[] read
573 a_it = a.rbegin ();
574 FAIL_RETURN_IF ((a_it[0])->id () != a[3]->id ());
575 a_it += 2;
576 FAIL_RETURN_IF ((a_it[0])->id () != a[1]->id ());
578 // test operator[] write
579 // NOTE: This now changes the sequence a.
580 // this is not possible for const iterators
581 // a_it[0] = CORBA::string_dup (elem0_cstr);
582 // FAIL_RETURN_IF (ACE_OS::strcmp (a[1],elem0_cstr) != 0);
584 // reset content of sequence a
585 //a[1] = CORBA::string_dup (elem1_cstr);
587 // test for loop behaviour
588 tested_sequence b = a;
589 REVERSE_ITERATOR_T b_it = b.rbegin ();
591 for (a_it = a.rbegin ();
592 a_it != a.rend ();
593 a_it++, b_it++)
595 FAIL_RETURN_IF ((*a_it)->id () != (*b_it)->id ());
598 tested_sequence test;
599 test.length (a.length ());
602 * The copy call below causes double deletes and seg faults.
604 std::copy (a.begin (),
605 a.end (),
606 test.begin ());
608 FAIL_RETURN_IF (test.length () != a.length ());
610 REVERSE_ITERATOR_T copytest_iter = test.rbegin ();
611 for (REVERSE_ITERATOR_T copya_iter = a.rbegin ();
612 copya_iter != a.rend ();
613 ++copya_iter, ++copytest_iter)
615 FAIL_RETURN_IF ((*copya_iter)->id () != (*copytest_iter)->id ());
618 /// Testing - using ostream_iterator
619 /// JWH2 - I don't think the ostream test makes sense for object references.
621 std::ostringstream ostream;
622 std::copy (a.rbegin (),
623 a.rend (),
624 std::ostream_iterator<tested_sequence::value_type> (ostream,
625 "\n"));
627 FAIL_RETURN_IF (
628 ostream.str ().compare ("elem3\nelem2\nelem1\nelem0\n") != 0);
631 return 0;
634 #endif
636 //-----------------------------------------------------------------------------
638 int ACE_TMAIN(int,ACE_TCHAR*[])
640 int status = 0;
642 #if defined TAO_HAS_SEQUENCE_ITERATORS && TAO_HAS_SEQUENCE_ITERATORS == 1
644 // Test Generic_Sequence_Iterator.
645 status += test_sequence< tested_sequence::iterator> ();
647 // g++ seems to make the conversion from iterator to const_iterator
648 // and Windows doesn't. Not sure why.
649 // Test Const_Generic_Sequence_Iterator with non-const sequence.
650 status += test_sequence< tested_sequence::const_iterator> ();
652 // Test Const_Generic_Sequence_Iterator with const sequence.
653 status += test_const_sequence< tested_sequence::const_iterator> ();
655 // Test Generic_Sequence_Reverse_Iterator.
656 status += test_sequence_reverse< tested_sequence::reverse_iterator> ();
658 // Test Const_Generic_Sequence_Reverse_Iterator with non-const sequence.
659 status += test_sequence_reverse< tested_sequence::const_reverse_iterator> ();
661 // Test Const_Generic_Sequence_Reverse_Iterator with const sequence.
662 status += test_const_sequence_reverse< tested_sequence::const_reverse_iterator> ();
664 #endif /* TAO_HAS_SEQUENCE_ITERATORS == 1 */
666 return status;