[Aptitude-svn-commit] r3982 - in branches/aptitude-0.3/aptitude: src/generic tests

Daniel Burrows dburrows at costa.debian.org
Mon Aug 29 22:10:25 UTC 2005


Author: dburrows
Date: Mon Aug 29 22:10:25 2005
New Revision: 3982

Modified:
   branches/aptitude-0.3/aptitude/src/generic/immset.h
   branches/aptitude-0.3/aptitude/tests/test_wtree.cc
Log:
Add a node typedef to imm::map; write comparison operators.


Modified: branches/aptitude-0.3/aptitude/src/generic/immset.h
==============================================================================
--- branches/aptitude-0.3/aptitude/src/generic/immset.h	(original)
+++ branches/aptitude-0.3/aptitude/src/generic/immset.h	Mon Aug 29 22:10:25 2005
@@ -30,6 +30,8 @@
 
 #include <assert.h>
 
+#include <vector>
+
 namespace imm
 {
   /** A generic node in a weighted tree a described in "Implementing
@@ -603,7 +605,63 @@
     }
   };
 
-  template<typename Key, typename Val, typename Compare = std::less<Val> >
+  /** Compare two sets.  Will produce strange results unless the two
+   *  sets have the same comparison *object*; you are responsible for
+   *  ensuring that this is the case. (i.e., if the comparator has
+   *  associated data, it should be identical in the two sets)
+   */
+  template<typename Val, typename Compare, int w>
+  inline bool operator<(const set<Val, Compare, w> &s1,
+			const set<Val, Compare, w> &s2)
+  {
+    typename set<Val, Compare, w>::const_iterator
+      i1 = s1.begin(), i2 = s2.begin();
+
+    while(i1 != s1.end() && i2 != s2.end())
+      {
+	if(*i1 < *i2)
+	  return true;
+	else if(*i2 < *i1)
+	  return false;
+	else
+	  {
+	    ++i1;
+	    ++i2;
+	  }
+      }
+
+    if(i1 != s1.end())
+      return false;
+    else if(i2 != s2.end())
+      return true;
+    else
+      return false;
+  }
+
+  /** Compare two sets for equality, with the same caveat as operator<. */
+  template<typename Val, typename Compare, int w>
+  inline bool operator==(const set<Val, Compare, w> &s1,
+			 const set<Val, Compare, w> &s2)
+  {
+    typename set<Val, Compare, w>::const_iterator
+      i1 = s1.begin(), i2 = s2.begin();
+
+    while(i1 != s1.end() && i2 != s2.end())
+      {
+	if(!(*i1 == *i2))
+	  return false;
+	else
+	  {
+	    ++i1;
+	    ++i2;
+	  }
+      }
+
+    return i1 == s1.end() && i2 == s2.end();
+  }
+
+
+  template<typename Key, typename Val, typename Compare = std::less<Key> >
   class map
   {
   public:
@@ -626,6 +684,7 @@
     typedef set<binding_type, key_compare> mapping_type;
     typedef typename mapping_type::const_iterator const_iterator;
     typedef typename mapping_type::size_type size_type;
+    typedef typename mapping_type::node node;
 
   private:
     mapping_type contents;
@@ -671,7 +730,7 @@
     /** \return either the node corresponding to the given key,
      *  or an empty tree.
      */
-    typename mapping_type::node lookup(const Key &k)
+    node lookup(const Key &k) const
     {
       return contents.find_node(binding_type(k, Val()));
     }
@@ -679,9 +738,9 @@
     /** \return either the value of the mapping at k, or dflt if k is
      *  unbound.
      */
-    Val get(const Key &k, const Val &dflt)
+    Val get(const Key &k, const Val &dflt) const
     {
-      typename mapping_type::node found = contents.find_node(binding_type(k, Val()));
+      node found = contents.find_node(binding_type(k, Val()));
 
       if(found.isValid())
 	return found.getVal().second;
@@ -720,6 +779,16 @@
     {
       return contents.contains(binding_type(k, Val()));
     }
+
+    bool operator<(const map &other) const
+    {
+      return contents < other.contents;
+    }
+
+    bool operator==(const map &other) const
+    {
+      return contents == other.contents;
+    }
   };
 };
 

Modified: branches/aptitude-0.3/aptitude/tests/test_wtree.cc
==============================================================================
--- branches/aptitude-0.3/aptitude/tests/test_wtree.cc	(original)
+++ branches/aptitude-0.3/aptitude/tests/test_wtree.cc	Mon Aug 29 22:10:25 2005
@@ -39,6 +39,8 @@
   CPPUNIT_TEST(testRotateRight);
   CPPUNIT_TEST(testDoubleRotateLeft);
   CPPUNIT_TEST(testDoubleRotateRight);
+  CPPUNIT_TEST(testEquality);
+  CPPUNIT_TEST(testLessThan);
   CPPUNIT_TEST(generalWTreeTest);
   CPPUNIT_TEST(mapTest);
 
@@ -403,6 +405,95 @@
       CPPUNIT_FAIL(out.str()); \
     } } while(0)
 
+  void testEquality()
+  {
+    set<int> a;
+    set<int> b;
+
+    CPPUNIT_ASSERT(a == b);
+
+    a.insert(1);
+
+    CPPUNIT_ASSERT(!(a == b));
+
+    a.insert(6);
+    a.insert(4);
+    a.insert(5);
+    a.insert(2);
+    a.insert(3);
+
+    CPPUNIT_ASSERT(!(a == b));
+
+    b.insert(1);
+    b.insert(2);
+    b.insert(3);
+    b.insert(4);
+    b.insert(5);
+    b.insert(6);
+
+    int tmp[] = {1, 2, 3, 4, 5, 6};
+    assertWTreeValues(a, tmp, 6);
+    assertWTreeValues(b, tmp, 6);
+
+    CPPUNIT_ASSERT(a == b);
+
+    a.erase(4);
+
+    CPPUNIT_ASSERT(!(a == b));
+
+    a.insert(4);
+
+    CPPUNIT_ASSERT(a == b);
+
+    b.insert(10);
+
+    CPPUNIT_ASSERT(!(a == b));
+
+    b.erase(5);
+
+    CPPUNIT_ASSERT(!(a == b));
+
+    a.erase(5);
+
+    CPPUNIT_ASSERT(!(a == b));
+
+    a.insert(10);
+
+    CPPUNIT_ASSERT(a == b);
+  }
+
+  void testLessThan()
+  {
+    set<int> a;
+    set<int> b;
+
+    a.insert(1);
+    a.insert(2);
+    a.insert(3);
+    a.insert(4);
+    a.insert(5);
+    a.insert(6);
+
+    b.insert(2);
+    b.insert(3);
+    b.insert(4);
+    b.insert(5);
+    b.insert(6);
+
+    CPPUNIT_ASSERT(a < b);
+    CPPUNIT_ASSERT(!(b < a));
+
+    b.insert(1);
+
+    CPPUNIT_ASSERT(!(a < b));
+    CPPUNIT_ASSERT(!(b < a));
+
+    a.erase(1);
+
+    CPPUNIT_ASSERT(!(a < b));
+    CPPUNIT_ASSERT(b < a);
+  }
+
   void generalWTreeTest()
   {
     set<int> t;



More information about the Aptitude-svn-commit mailing list