Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / ACE / tests / OrdMultiSet_Test.cpp
blobf62bf4ada6fcc2fb2b65b70d3335b49e3f9995cd
2 //=============================================================================
3 /**
4 * @file OrdMultiSet_Test.cpp
6 * This is a simple test of the <ACE_Ordered_MultiSet> and
7 * <ACE_Ordered_MultiSet_Iterator> class templates, instantiating
8 * them with type int. No command line arguments are needed to run
9 * the test.
11 * @author Chris Gill <cdgill@cs.wustl.edu>
13 //=============================================================================
16 // Note, for this test the config.h file *must* come first!
17 #include "ace/config-all.h"
19 #include "test_config.h"
20 #include "ace/Containers.h"
24 int
25 run_main (int, ACE_TCHAR *[])
27 int ret = 0;
28 int *ptr = 0;
30 ACE_START_TEST (ACE_TEXT ("OrdMultiSet_Test"));
32 // make an empty set of int and an iterator
33 ACE_Ordered_MultiSet<int> set;
34 ACE_Ordered_MultiSet_Iterator<int> iter(set);
36 // Put in a range of odd ints, without an iterator.
37 int i;
38 for (i = -10; i < 10; ++i)
39 set.insert (2 * i + 1);
41 // Put in an interleaved range of even ints, using an iterator.
42 for (i = -10; i <= 10; ++i)
43 set.insert (2 * i, iter);
45 // Remove the first and last elements of range.
46 while (set.remove (-20) == 0)
48 // No action.
51 while (set.remove (20) == 0)
53 // No action.
56 // Should still have 39 elements in the set.
57 ACE_TEST_ASSERT (set.is_empty () == 0);
58 ACE_TEST_ASSERT (set.size () == 39);
60 // Iterate forward through the range we created: should be one of
61 // each.
62 iter.first ();
64 for (i = -19; i <= 19; ++i)
66 // we should still be in the set
67 ACE_TEST_ASSERT (iter.done () == 0);
69 // make sure the current element is what we expect
70 iter.next (ptr);
71 ACE_TEST_ASSERT (ptr != 0);
72 ACE_TEST_ASSERT (*ptr == i);
74 // move to the next element in the set
75 iter.advance ();
78 // We should have iterated through the entire set.
79 ACE_TEST_ASSERT (iter.done () != 0);
81 // Iterate backward through the range we created: should be one of
82 // each.
83 iter.last ();
85 for (i = 19; i >= -19; --i)
87 // We should still be in the set.
88 ACE_TEST_ASSERT (iter.done () == 0);
90 // Make sure the current element is what we expect.
91 int *ptr = 0;
92 iter.next (ptr);
93 ACE_TEST_ASSERT (ptr != 0);
94 ACE_TEST_ASSERT (*ptr == i);
96 // Move to the previous element in the set.
97 iter.retreat ();
100 // We should have iterated through the entire set.
101 ACE_TEST_ASSERT (iter.done () != 0);
103 // Iterate through the set and use the operator* to get the element
104 iter.first ();
106 for (i = -19; i <= 19; ++i)
108 // we should still be in the set
109 ACE_TEST_ASSERT (iter.done () == 0);
111 // make sure the current element is what we expect
112 int& l = *iter;
113 ACE_TEST_ASSERT (l == i);
115 // move to the next element in the set
116 iter.advance ();
119 // We should have iterated through the entire set.
120 ACE_TEST_ASSERT (iter.done () != 0);
122 // Clear the set, restart the iterator, and make sure the iterator
123 // is out of range at both ends, the set is empty, and a subsequent
124 // advance or retreat on an out of range iterator does not cause
125 // problems
126 set.reset ();
127 ACE_TEST_ASSERT (set.is_empty () != 0);
128 iter.first ();
129 ACE_TEST_ASSERT (iter.done () != 0);
130 iter.retreat ();
131 iter.last ();
132 ACE_TEST_ASSERT (iter.done () != 0);
133 iter.advance ();
135 // Put in a bunch of ints in various relative positions, using an
136 // iterator for the odds and no iterator for the evens.
137 set.insert (203, iter);
138 set.insert (202);
139 set.insert (204);
140 set.insert (201, iter);
141 set.insert (205, iter);
143 set.insert (203, iter);
144 set.insert (203, iter);
146 set.insert (204);
147 set.insert (204);
148 set.insert (204);
149 set.insert (205, iter);
150 set.insert (205, iter);
151 set.insert (205, iter);
152 set.insert (205, iter);
153 set.insert (202);
155 // remove the middle elements
156 while (set.remove (204) == 0)
158 // No action.
161 while (set.remove (202) == 0)
163 // No action.
166 while (set.remove (203) == 0)
168 // No action.
171 // Put the iterator out of range and make sure it stays
172 // that way for finds on the missing elements.
173 iter.last ();
174 iter.advance ();
175 set.find (203, iter);
176 ACE_TEST_ASSERT (iter.done () != 0);
177 set.find (202, iter);
178 ACE_TEST_ASSERT (iter.done () != 0);
179 set.find (204, iter);
180 ACE_TEST_ASSERT (iter.done () != 0);
182 // Make sure the other elements can be found.
183 set.find (205, iter);
184 ACE_TEST_ASSERT (iter.done () == 0);
185 iter.next (ptr);
186 ACE_TEST_ASSERT (ptr != 0);
187 ACE_TEST_ASSERT (*ptr == 205);
188 set.find (201, iter);
189 ACE_TEST_ASSERT (iter.done () == 0);
190 iter.next (ptr);
191 ACE_TEST_ASSERT (ptr != 0);
192 ACE_TEST_ASSERT (*ptr == 201);
194 // Finally, iterate through the set and make sure its contents are
195 // correct (one 201 and five 205s).
196 iter.first ();
197 ACE_TEST_ASSERT (iter.done () == 0);
198 iter.next (ptr);
199 ACE_TEST_ASSERT (ptr != 0);
200 ACE_TEST_ASSERT (*ptr == 201);
201 iter.advance ();
203 for (i = 1; i <= 5; ++i)
205 // Should be in the set, able to access the element, value
206 // should be 205
207 ACE_TEST_ASSERT (iter.done () == 0);
208 iter.next (ptr);
209 ACE_TEST_ASSERT (ptr != 0);
210 ACE_TEST_ASSERT (*ptr == 205);
212 // Move to the next element in the set.
213 iter.advance ();
216 // Should not be anything else in the set.
217 ACE_TEST_ASSERT (iter.done () != 0);
219 // remove the rest
220 while (set.remove (205) == 0)
222 // No action.
225 while (set.remove (201) == 0)
227 // No action.
230 // Should have no more elements in the set.
231 ACE_TEST_ASSERT (set.is_empty () != 0);
232 ACE_TEST_ASSERT (set.size () == 0);
233 iter.first ();
234 ACE_TEST_ASSERT (iter.done () != 0);
235 iter.last ();
236 ACE_TEST_ASSERT (iter.done () != 0);
238 ACE_END_TEST;
240 return ret;