summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorokellogg <okellogg@ae88bc3d-4319-0410-8dbf-d08b4c9d3795>2003-03-25 16:19:17 +0000
committerokellogg <okellogg@ae88bc3d-4319-0410-8dbf-d08b4c9d3795>2003-03-25 16:19:17 +0000
commitbe8af93297e0005c3ab9a439bf6b252d575a7ac8 (patch)
treee341da596bda5d480f81159f550fe6aa2f1b06be
parent226f2f88da5f189d492efa4110c42e13bf5749e7 (diff)
downloadATCD-be8af93297e0005c3ab9a439bf6b252d575a7ac8.tar.gz
Tue Mar 25 17:09:22 CET 2003 Oliver Kellogg <oliver.kellogg@sysde.eads.net>
-rw-r--r--ChangeLog10
-rw-r--r--ace/Unbounded_Set.cpp41
-rw-r--r--tests/Unbounded_Set_Test.cpp97
3 files changed, 118 insertions, 30 deletions
diff --git a/ChangeLog b/ChangeLog
index ad77881cf52..665203bb86c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+Tue Mar 25 17:09:22 CET 2003 Oliver Kellogg <oliver.kellogg@sysde.eads.net>
+
+ From Rudolf Weber <rfweber@tesionmail.de>
+ * ace/Unbounded_Set.cpp:
+ Correction to the handling of the deleted_ flag in the iterators.
+
+ * tests/Unbounded_Set_Test.cpp:
+ Extended towards making it a general test of the ACE_Unbounded_Set.
+ Added test for the ACE_Unbounded_Set_Const_Iterator.
+
Tue Mar 25 10:44:23 CET 2003 Oliver Kellogg <oliver.kellogg@sysde.eads.net>
* ace/Unbounded_Set.{h,cpp}: Removed the `const' at the
diff --git a/ace/Unbounded_Set.cpp b/ace/Unbounded_Set.cpp
index d7a4ccebda2..7aa6291bb62 100644
--- a/ace/Unbounded_Set.cpp
+++ b/ace/Unbounded_Set.cpp
@@ -105,11 +105,19 @@ ACE_Unbounded_Set<T>::delete_nodes (void)
{
ACE_Node<T> *temp = curr;
curr = curr->next_;
- ACE_DES_FREE_TEMPLATE (temp,
- this->allocator_->free,
- ACE_Node,
- <T>);
- this->cur_size_--;
+
+ if (! temp->deleted_)
+ this->cur_size_--;
+
+ if (number_of_iterators_ == 0)
+ {
+ ACE_DES_FREE_TEMPLATE (temp,
+ this->allocator_->free,
+ ACE_Node,
+ <T>);
+ }
+ else
+ temp->deleted_ = true;
}
// Reset the list to be a circular list with just a dummy node.
@@ -242,10 +250,11 @@ ACE_Unbounded_Set<T>::remove (const T &item)
// Insert the item to be founded into the dummy node.
this->head_->item_ = item;
+ this->head_->deleted_ = false;
ACE_Node<T> *curr = this->head_;
- while (!(curr->next_->item_ == item))
+ while (!(curr->next_->item_ == item) || curr->next_->deleted_)
curr = curr->next_;
if (curr->next_ == this->head_)
@@ -256,13 +265,13 @@ ACE_Unbounded_Set<T>::remove (const T &item)
ACE_Node<T> *temp = curr->next_;
if(number_of_iterators_>0)
{
- temp->deleted_=true;
+ temp->deleted_ = true;
}
else
{
// Skip over the node that we're deleting.
curr->next_ = temp->next_;
-
+
ACE_DES_FREE_TEMPLATE (temp,
this->allocator_->free,
ACE_Node,
@@ -318,11 +327,14 @@ ACE_Unbounded_Set_Iterator<T>::dump (void) const
template <class T>
ACE_Unbounded_Set_Iterator<T>::ACE_Unbounded_Set_Iterator (ACE_Unbounded_Set<T> &s, int end)
- : current_ (end == 0 ? s.head_->next_ : s.head_ ),
+ : current_ (end == 0 ? s.head_->next_ : s.head_),
set_ (&s)
{
// ACE_TRACE ("ACE_Unbounded_Set_Iterator<T>::ACE_Unbounded_Set_Iterator");
- set_->iterator_add();
+ set_->iterator_add ();
+ // the first one may be deleted
+ while (this->current_->deleted_ && this->current_ != this->set_->head_)
+ this->current_ = this->current_->next_;
}
template <class T>
@@ -451,11 +463,14 @@ ACE_Unbounded_Set_Const_Iterator<T>::dump (void) const
template <class T>
ACE_Unbounded_Set_Const_Iterator<T>::ACE_Unbounded_Set_Const_Iterator (const ACE_Unbounded_Set<T> &s, int end)
- : current_ (end == 0 ? s.head_->next_ : s.head_ ),
+ : current_ (end == 0 ? s.head_->next_ : s.head_),
set_ (&s)
{
// ACE_TRACE ("ACE_Unbounded_Set_Const_Iterator<T>::ACE_Unbounded_Set_Const_Iterator");
set_->iterator_add ();
+ // the first one may be deleted
+ while (this->current_->deleted_ && this->current_ != this->set_->head_)
+ this->current_ = this->current_->next_;
}
template <class T>
@@ -486,7 +501,7 @@ ACE_Unbounded_Set_Const_Iterator<T>::advance (void)
{
// ACE_TRACE ("ACE_Unbounded_Set_Const_Iterator<T>::advance");
this->current_ = this->current_->next_;
- while(this->current_->deleted_ && this->current_ != this->set_->head_)
+ while (this->current_->deleted_ && this->current_ != this->set_->head_)
this->current_ = this->current_->next_;
return this->current_ != this->set_->head_;
}
@@ -496,7 +511,7 @@ ACE_Unbounded_Set_Const_Iterator<T>::first (void)
{
// ACE_TRACE ("ACE_Unbounded_Set_Const_Iterator<T>::first");
this->current_ = this->set_->head_->next_;
- while(this->current_->deleted_ && this->current_ != this->set_->head_)
+ while (this->current_->deleted_ && this->current_ != this->set_->head_)
this->current_ = this->current_->next_;
return this->current_ != this->set_->head_;
}
diff --git a/tests/Unbounded_Set_Test.cpp b/tests/Unbounded_Set_Test.cpp
index c7771b4d155..94db5d2dd5d 100644
--- a/tests/Unbounded_Set_Test.cpp
+++ b/tests/Unbounded_Set_Test.cpp
@@ -9,9 +9,8 @@
// Unbounded_Set_Test.cpp
//
// = DESCRIPTION
-// This test shall evolve to illustrate the use of ACE_Unbounded_Set.
-// For the moment it only tests some insertion and removal, and acts
-// as a regression test for Bugzilla bug 1460.
+// This test illustrates the use of ACE_Unbounded_Set.
+// In addition, it acts as a regression test for Bugzilla bug 1460.
// No command line arguments are needed to run the test.
//
// = AUTHOR
@@ -20,29 +19,38 @@
//
// ============================================================================
-#include "test_config.h"
+// #include "test_config.h"
#include <ace/Unbounded_Set.h>
ACE_RCSID(tests, Unbounded_Set_Test, "$Id$")
struct MyNode
{
- int k;
+ 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 main()
{
int r;
+ unsigned k;
MyNode node (1);
ACE_Unbounded_Set<MyNode> ubs;
ACE_ASSERT (ubs.size () == 0);
- // Insert a node. Immediately remove it.
+ // Insert a value. Immediately remove it.
r = ubs.insert (node);
ACE_ASSERT (r == 0);
ACE_ASSERT (ubs.size () == 1);
@@ -50,19 +58,74 @@ int main()
ACE_ASSERT (r == 0);
ACE_ASSERT (ubs.size () == 0);
- // Insert a node. Remove it within an iterator.
- int n_iterations = 0;
- r = ubs.insert (node);
- ACE_Unbounded_Set<MyNode>::ITERATOR end = ubs.end ();
- for (ACE_Unbounded_Set<MyNode>::ITERATOR i = ubs.begin (); i != end; i++)
+ // Insert several different values.
+ for (node.k = 1; node.k <= 5; node.k++)
{
- ACE_ASSERT (*i == node);
- r = ubs.remove (*i);
- n_iterations++;
+ r = ubs.insert (node);
+ ACE_ASSERT (r == 0);
+ ACE_ASSERT (ubs.size () == node.k);
}
- ACE_ASSERT (n_iterations == 1);
- ACE_ASSERT (r == 0);
- ACE_ASSERT (ubs.size () == 0);
+
+ // 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 ());
+ }
+
+ // Selective deletion of elements and element retrieval.
+ {
+ MySet::iterator end = ubs2.end();
+ int deleted = 0;
+ for (MySet::iterator i = ubs2.begin (); i != end; i++)
+ {
+ if ((*i).k % 2 == 1)
+ {
+ r = ubs2.remove (*i);
+ 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 ());
+
+ // Test deletion under the cursor.
+ // This is the regression test for Bugzilla bug 1460.
+ {
+ MySet::iterator end = ubs2.end ();
+ for (MySet::iterator i = ubs2.begin (); i != end; i++)
+ {
+ r = ubs2.remove (*i);
+ ACE_ASSERT (r == 0);
+ }
+ ACE_ASSERT (ubs2.size () == 0);
+ }
+ ACE_ASSERT (ubs2.is_empty ());
return 0;
}