add comparing to PoolQuery
authorJosef Reidinger <jreidinger@suse.cz>
Tue, 8 Apr 2008 13:47:27 +0000 (13:47 +0000)
committerJosef Reidinger <jreidinger@suse.cz>
Tue, 8 Apr 2008 13:47:27 +0000 (13:47 +0000)
tests/zypp/PoolQuery_test.cc
zypp/PoolQuery.cc
zypp/PoolQuery.h

index e41e4e0..8769e77 100644 (file)
@@ -266,6 +266,33 @@ BOOST_AUTO_TEST_CASE(pool_query_11)
 }
 */
 
+// test matching
+BOOST_AUTO_TEST_CASE(pool_query_equal)
+{
+  cout << "****equal****"  << endl;
+  PoolQuery q;
+  q.addString("zypp");
+  q.addAttribute(sat::SolvAttr::name);
+  q.setMatchGlob();
+  PoolQuery q2;
+  q2.addString("zypp");
+  q2.addAttribute(sat::SolvAttr::name);
+  q2.setMatchGlob();
+  PoolQuery q3;
+  q3.addString("zypp");
+  q3.addAttribute(sat::SolvAttr::name);
+  q3.setMatchGlob();
+  q3.setRequireAll(true);
+  PoolQuery q4;
+  q4.addAttribute(sat::SolvAttr::name,"zypp");
+  q4.setMatchGlob();
+
+  BOOST_CHECK(equal(q,q2));
+  BOOST_CHECK(!equal(q,q3));
+  //only exact equal! \TODO maybe change
+  BOOST_CHECK(!equal(q,q4));
+  BOOST_CHECK(!equal(q4,q3));
+}
 
 // save/load query
 BOOST_AUTO_TEST_CASE(pool_query_save_restore)
index 78de090..13a6dd9 100644 (file)
@@ -944,6 +944,53 @@ attremptycheckend:
   ostream & operator<<( ostream & str, const PoolQuery & obj )
   { return str << obj.asString(); }
 
+  bool operator==(const PoolQuery& a, const PoolQuery& b)
+  {
+    return equal(a,b);
+  }
+
+  //internal matching two containers O(n^2)
+  template <class Container>
+  bool equalContainers(const Container& a, const Container& b)
+  {
+    for_(it,a.begin(),a.end())
+    {
+      bool finded = false;
+      for_( it2, b.begin(),b.end() )
+      {
+        if (*it==*it2)
+        {
+          finded = true;
+          break;
+        }
+      }
+
+      if (!finded)
+        return false;
+    }
+    return true;
+  }
+
+  bool equal(const PoolQuery& a, const PoolQuery& b)
+  {
+    if( a.matchType()!=b.matchType() )
+      return false;
+    if( a.matchWord()!=b.matchWord())
+      return false;
+    if( a.requireAll()!=b.requireAll() )
+      return false;
+    if(!equalContainers(a.strings(), b.strings()))
+      return false;
+    if(!equalContainers(a.kinds(), b.kinds()))
+      return false;
+    if(!equalContainers(a.repos(), b.repos()))
+      return false;
+    if(!equalContainers(a.attributes(), b.attributes()))
+      return false;
+
+    return true;
+  }
+
 
   /////////////////////////////////////////////////////////////////
 } // namespace zypp
index cd96802..3812d9b 100644 (file)
@@ -270,6 +270,9 @@ namespace zypp
   /** \relates PoolQuery Stream output. */
   std::ostream & operator<<( std::ostream & str, const PoolQuery & obj );
 
+  bool equal(const PoolQuery& a, const PoolQuery& b);
+  bool operator==(const PoolQuery& a, const PoolQuery& b);
+
   ///////////////////////////////////////////////////////////////////
   //
   //  CLASS NAME : PoolQuery::ResultIterator