summaryrefslogtreecommitdiff
path: root/ACE/tests/Unbounded_Set_Test.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'ACE/tests/Unbounded_Set_Test.cpp')
-rw-r--r--ACE/tests/Unbounded_Set_Test.cpp135
1 files changed, 135 insertions, 0 deletions
diff --git a/ACE/tests/Unbounded_Set_Test.cpp b/ACE/tests/Unbounded_Set_Test.cpp
new file mode 100644
index 00000000000..dc4379b7720
--- /dev/null
+++ b/ACE/tests/Unbounded_Set_Test.cpp
@@ -0,0 +1,135 @@
+// $Id$
+
+// ============================================================================
+//
+// = LIBRARY
+// tests
+//
+// = FILENAME
+// Unbounded_Set_Test.cpp
+//
+// = DESCRIPTION
+// This test illustrates the use of ACE_Unbounded_Set.
+// No command line arguments are needed to run the test.
+//
+// = AUTHOR
+// Rudolf Weber <rfweber@tesionmail.de>,
+// ace/tests integration <Oliver.Kellogg@sysde.eads.net>
+//
+// ============================================================================
+
+#include "test_config.h"
+#include <ace/Unbounded_Set.h>
+#include <ace/Auto_Ptr.h>
+#include <ace/SString.h>
+
+ACE_RCSID(tests, Unbounded_Set_Test, "$Id$")
+
+struct MyNode
+{
+ unsigned k;
+ MyNode () : k (0) {}
+ MyNode (int pk) : k (pk) {}
+ MyNode (const MyNode& o) : k (o.k) {}
+ bool operator== (const MyNode& o) { return (k == o.k); }
+};
+
+size_t count_const_set (const ACE_Unbounded_Set<MyNode>& cubs)
+{
+ size_t number_of_elements = 0;
+ for (ACE_Unbounded_Set<MyNode>::const_iterator ci (cubs); !ci.done(); ci++)
+ number_of_elements++;
+ return number_of_elements;
+}
+
+int
+run_main (int, ACE_TCHAR *[])
+{
+ int r;
+ unsigned k;
+ MyNode node (1);
+
+ ACE_START_TEST (ACE_TEXT ("Unbounded_Set_Test"));
+
+ ACE_Unbounded_Set<MyNode> ubs;
+ ACE_ASSERT (ubs.size () == 0);
+
+ // Insert a value. Immediately remove it.
+ r = ubs.insert (node);
+ ACE_ASSERT (r == 0);
+ ACE_ASSERT (ubs.size () == 1);
+ r = ubs.remove (node);
+ ACE_ASSERT (r == 0);
+ ACE_ASSERT (ubs.size () == 0);
+
+ // Insert several different values.
+ for (node.k = 1; node.k <= 5; node.k++)
+ {
+ r = ubs.insert (node);
+ ACE_ASSERT (r == 0);
+ ACE_ASSERT (ubs.size () == node.k);
+ }
+
+ // Test assigment of sets.
+ // To do that, we also test some of the iterator methods.
+ typedef ACE_Unbounded_Set<MyNode> MySet;
+ MySet ubs2 = ubs; // Test a typedef of a set.
+ ACE_ASSERT (ubs2.size() == ubs.size());
+ {
+ MySet::ITERATOR it1 (ubs);
+ MySet::iterator it2 (ubs2);
+ for (k = 1; k <= 5; k++)
+ {
+ ACE_ASSERT (! it1.done ());
+ ACE_ASSERT (! it2.done ());
+ MyNode n1 = *it1;
+ MyNode n2 = *it2;
+ ACE_ASSERT (n1 == n2);
+ it1.advance ();
+ it2.advance ();
+ }
+ ACE_ASSERT (it1.done ());
+ ACE_ASSERT (it2.done ());
+ // Verify that a set may be emptied while an iterator on the set is
+ // in-scope but inactive:
+ ubs.reset ();
+ // Restore original set from ubs2
+ ubs = ubs2;
+ }
+
+ // Selective deletion of elements and element retrieval.
+ {
+ MySet::iterator it (ubs2);
+ int deleted = 0;
+ while (! it.done ())
+ {
+ MyNode n = *it;
+ it.advance (); /* Being friendly here: Move the iterator on
+ so that element removal does not interfere
+ with the current iterator position.
+ The less friendly case, removal under the
+ current iterator position, is below. */
+ if (n.k % 2 == 1)
+ {
+ r = ubs2.remove (n);
+ deleted++;
+ }
+ }
+ ACE_ASSERT (ubs2.size () + deleted == ubs.size());
+
+ MyNode node2 (2);
+ ACE_ASSERT (ubs2.find (node2) == 0);
+
+ MyNode node3 (3);
+ ACE_ASSERT (ubs2.find (node3) != 0);
+
+ ubs2.insert (node3);
+ }
+
+ size_t s = count_const_set (ubs);
+ ACE_ASSERT (s == ubs.size ());
+
+ ACE_END_TEST;
+ return 0;
+}
+