2 //=============================================================================
4 * @file Unbounded_Set_Test.cpp
6 * This test illustrates the use of ACE_Unbounded_Set.
7 * No command line arguments are needed to run the test.
9 * @author Rudolf Weber <rfweber@tesionmail.de>
10 * @author ace/tests integration <Oliver.Kellogg@sysde.eads.net>
12 //=============================================================================
14 #include "test_config.h"
15 #include <ace/Unbounded_Set.h>
16 #include <ace/SString.h>
22 MyNode (int pk
) : k (pk
) {}
23 MyNode (const MyNode
& o
) : k (o
.k
) {}
24 MyNode (MyNode
&& o
) : k(o
.k
) {}
25 MyNode
& operator=(const MyNode
& o
) { k
= o
.k
; return *this; }
26 MyNode
& operator=(MyNode
&& o
) { k
= o
.k
; return *this; }
27 bool operator== (const MyNode
& o
) const { return (k
== o
.k
); }
28 bool operator!= (const MyNode
& o
) const { return (k
!= o
.k
); }
31 size_t count_const_set (const ACE_Unbounded_Set
<MyNode
>& cubs
)
33 size_t number_of_elements
= 0;
34 for (ACE_Unbounded_Set
<MyNode
>::const_iterator
ci (cubs
); !ci
.done(); ci
++)
36 return number_of_elements
;
40 run_main (int, ACE_TCHAR
*[])
47 ACE_START_TEST (ACE_TEXT ("Unbounded_Set_Test"));
49 ACE_Unbounded_Set
<MyNode
> ubs
;
52 ACE_ERROR ((LM_ERROR
, "Error: ubs.size () != 0\n"));
57 ACE_ERROR ((LM_ERROR
, "Error: !ubs.is_empty ()\n"));
61 // Insert a value. Immediately remove it.
62 r
= ubs
.insert (node
);
65 ACE_ERROR ((LM_ERROR
, "Error: r != 0\n"));
70 ACE_ERROR ((LM_ERROR
, "Error: ubs.size () != 1\n"));
73 r
= ubs
.remove (node
);
76 ACE_ERROR ((LM_ERROR
, "Error: r != 0\n"));
81 ACE_ERROR ((LM_ERROR
, "Error: ubs.size () != 0\n"));
85 // Insert several different values.
86 for (node
.k
= 1; node
.k
<= 5; node
.k
++)
88 r
= ubs
.insert (node
);
91 ACE_ERROR ((LM_ERROR
, "Error: r != 0\n"));
94 if (ubs
.size () != node
.k
)
96 ACE_ERROR ((LM_ERROR
, "Error: ubs.size () != node.k\n"));
101 // Test assigment of sets.
102 // To do that, we also test some of the iterator methods.
103 using MySet
= ACE_Unbounded_Set
<MyNode
>;
104 MySet ubs2
= ubs
; // Test a typedef of a set.
105 if (ubs2
.size() != ubs
.size())
107 ACE_ERROR ((LM_ERROR
, "Error: ubs2.size() != ubs.size()\n"));
111 MySet::ITERATOR
it1 (ubs
);
112 MySet::iterator
it2 (ubs2
);
113 for (k
= 1; k
<= 5; k
++)
117 ACE_ERROR ((LM_ERROR
, "Error: it1.done ()\n"));
122 ACE_ERROR ((LM_ERROR
, "Error: it2.done ()\n"));
129 ACE_ERROR ((LM_ERROR
, "Error: !(n1 == n2)\n"));
137 ACE_ERROR ((LM_ERROR
, "Error: !it1.done ()\n"));
142 ACE_ERROR ((LM_ERROR
, "Error: !it2.done ()\n"));
145 // Verify that a set may be emptied while an iterator on the set is
146 // in-scope but inactive:
148 // Restore original set from ubs2
152 // Selective deletion of elements and element retrieval.
154 MySet::iterator
it (ubs2
);
159 it
.advance (); /* Being friendly here: Move the iterator on
160 so that element removal does not interfere
161 with the current iterator position.
162 The less friendly case, removal under the
163 current iterator position, is below. */
170 if (ubs2
.size () + deleted
!= ubs
.size())
172 ACE_ERROR ((LM_ERROR
, "Error: ubs2.size () + deleted != ubs.size()\n"));
177 if (ubs2
.find (node2
) != 0)
179 ACE_ERROR ((LM_ERROR
, "Error: ubs2.find (node2) != 0\n"));
184 if (ubs2
.find (node3
) == 0)
186 ACE_ERROR ((LM_ERROR
, "Error: ubs2.find (node3) == 0\n"));
193 size_t s
= count_const_set (ubs
);
194 if (s
!= ubs
.size ())
196 ACE_ERROR ((LM_ERROR
, "Error: s != ubs.size ()\n"));
200 ACE_Unbounded_Set
<MyNode
> ubs_insert
;
202 if (ubs_insert
.insert (node4
) != 0)
204 ACE_ERROR ((LM_ERROR
, "Error: insert node4 failed\n"));
207 if (ubs_insert
.insert (node4
) != 1)
209 ACE_ERROR ((LM_ERROR
, "Error: insert node4 didn't return 1\n"));
213 // Test iterating through a set and deleting elements.
215 ACE_Unbounded_Set
<MyNode
*> ubs3
;
217 for (int i
= 0; i
< 10; ++i
)
223 ACE_Unbounded_Set_Iterator
<MyNode
*> ubs3_iter (ubs3
.begin ());
224 for (; ubs3_iter
.next (i_n
); ++ubs3_iter
)