Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / ACE / tests / Unbounded_Set_Test.cpp
blob4d1b7b1adb3a4a2a6acc14b40d2c575e733c3377
2 //=============================================================================
3 /**
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>
18 struct MyNode
20 unsigned k;
21 MyNode () : k (0) {}
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++)
35 number_of_elements++;
36 return number_of_elements;
39 int
40 run_main (int, ACE_TCHAR *[])
42 int r;
43 int retval = 0;
44 unsigned k;
45 MyNode node (1);
47 ACE_START_TEST (ACE_TEXT ("Unbounded_Set_Test"));
49 ACE_Unbounded_Set<MyNode> ubs;
50 if (ubs.size () != 0)
52 ACE_ERROR ((LM_ERROR, "Error: ubs.size () != 0\n"));
53 retval = -1;
55 if (!ubs.is_empty ())
57 ACE_ERROR ((LM_ERROR, "Error: !ubs.is_empty ()\n"));
58 retval = -1;
61 // Insert a value. Immediately remove it.
62 r = ubs.insert (node);
63 if (r != 0)
65 ACE_ERROR ((LM_ERROR, "Error: r != 0\n"));
66 retval = -1;
68 if (ubs.size () != 1)
70 ACE_ERROR ((LM_ERROR, "Error: ubs.size () != 1\n"));
71 retval = -1;
73 r = ubs.remove (node);
74 if (r != 0)
76 ACE_ERROR ((LM_ERROR, "Error: r != 0\n"));
77 retval = -1;
79 if (ubs.size () != 0)
81 ACE_ERROR ((LM_ERROR, "Error: ubs.size () != 0\n"));
82 retval = -1;
85 // Insert several different values.
86 for (node.k = 1; node.k <= 5; node.k++)
88 r = ubs.insert (node);
89 if (r != 0)
91 ACE_ERROR ((LM_ERROR, "Error: r != 0\n"));
92 retval = -1;
94 if (ubs.size () != node.k)
96 ACE_ERROR ((LM_ERROR, "Error: ubs.size () != node.k\n"));
97 retval = -1;
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"));
108 retval = -1;
111 MySet::ITERATOR it1 (ubs);
112 MySet::iterator it2 (ubs2);
113 for (k = 1; k <= 5; k++)
115 if (it1.done ())
117 ACE_ERROR ((LM_ERROR, "Error: it1.done ()\n"));
118 retval = -1;
120 if (it2.done ())
122 ACE_ERROR ((LM_ERROR, "Error: it2.done ()\n"));
123 retval = -1;
125 MyNode n1 = *it1;
126 MyNode n2 = *it2;
127 if (!(n1 == n2))
129 ACE_ERROR ((LM_ERROR, "Error: !(n1 == n2)\n"));
130 retval = -1;
132 it1.advance ();
133 it2.advance ();
135 if (!it1.done ())
137 ACE_ERROR ((LM_ERROR, "Error: !it1.done ()\n"));
138 retval = -1;
140 if (!it2.done ())
142 ACE_ERROR ((LM_ERROR, "Error: !it2.done ()\n"));
143 retval = -1;
145 // Verify that a set may be emptied while an iterator on the set is
146 // in-scope but inactive:
147 ubs.reset ();
148 // Restore original set from ubs2
149 ubs = ubs2;
152 // Selective deletion of elements and element retrieval.
154 MySet::iterator it (ubs2);
155 int deleted = 0;
156 while (! it.done ())
158 MyNode n = *it;
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. */
164 if (n.k % 2 == 1)
166 r = ubs2.remove (n);
167 deleted++;
170 if (ubs2.size () + deleted != ubs.size())
172 ACE_ERROR ((LM_ERROR, "Error: ubs2.size () + deleted != ubs.size()\n"));
173 retval = -1;
176 MyNode node2 (2);
177 if (ubs2.find (node2) != 0)
179 ACE_ERROR ((LM_ERROR, "Error: ubs2.find (node2) != 0\n"));
180 retval = -1;
183 MyNode node3 (3);
184 if (ubs2.find (node3) == 0)
186 ACE_ERROR ((LM_ERROR, "Error: ubs2.find (node3) == 0\n"));
187 retval = -1;
190 ubs2.insert (node3);
193 size_t s = count_const_set (ubs);
194 if (s != ubs.size ())
196 ACE_ERROR ((LM_ERROR, "Error: s != ubs.size ()\n"));
197 retval = -1;
200 ACE_Unbounded_Set<MyNode> ubs_insert;
201 MyNode node4 (4);
202 if (ubs_insert.insert (node4) != 0)
204 ACE_ERROR ((LM_ERROR, "Error: insert node4 failed\n"));
205 retval = -1;
207 if (ubs_insert.insert (node4) != 1)
209 ACE_ERROR ((LM_ERROR, "Error: insert node4 didn't return 1\n"));
210 retval = -1;
213 // Test iterating through a set and deleting elements.
215 ACE_Unbounded_Set<MyNode*> ubs3;
216 MyNode *n = 0;
217 for (int i = 0; i < 10; ++i)
219 n = new MyNode (i);
220 ubs3.insert (n);
222 MyNode **i_n = 0;
223 ACE_Unbounded_Set_Iterator<MyNode*> ubs3_iter (ubs3.begin ());
224 for (; ubs3_iter.next (i_n); ++ubs3_iter)
225 delete *i_n;
227 ACE_END_TEST;
228 return retval;