[Lemon-commits] Peter Kovacs: Bug fix in (di)graphCopy() (#371)

Lemon HG hg at lemon.cs.elte.hu
Fri Jun 25 06:51:43 CEST 2010


details:   http://lemon.cs.elte.hu/hg/lemon/rev/bb871cb8ac06
changeset: 980:bb871cb8ac06
user:      Peter Kovacs <kpeter [at] inf.elte.hu>
date:      Tue Jun 22 15:39:26 2010 +0200
description:
	Bug fix in (di)graphCopy() (#371)

	The target graph is cleared before adding nodes and arcs/edges.

diffstat:

 lemon/core.h            |   2 ++
 test/graph_copy_test.cc |  24 ++++++++++++++++++++++++
 2 files changed, 26 insertions(+), 0 deletions(-)

diffs (100 lines):

diff --git a/lemon/core.h b/lemon/core.h
--- a/lemon/core.h
+++ b/lemon/core.h
@@ -384,6 +384,7 @@
       template <typename From, typename NodeRefMap, typename ArcRefMap>
       static void copy(const From& from, Digraph &to,
                        NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
+        to.clear();
         for (typename From::NodeIt it(from); it != INVALID; ++it) {
           nodeRefMap[it] = to.addNode();
         }
@@ -411,6 +412,7 @@
       template <typename From, typename NodeRefMap, typename EdgeRefMap>
       static void copy(const From& from, Graph &to,
                        NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
+        to.clear();
         for (typename From::NodeIt it(from); it != INVALID; ++it) {
           nodeRefMap[it] = to.addNode();
         }
diff --git a/test/graph_copy_test.cc b/test/graph_copy_test.cc
--- a/test/graph_copy_test.cc
+++ b/test/graph_copy_test.cc
@@ -29,6 +29,7 @@
 void digraph_copy_test() {
   const int nn = 10;
 
+  // Build a digraph
   SmartDigraph from;
   SmartDigraph::NodeMap<int> fnm(from);
   SmartDigraph::ArcMap<int> fam(from);
@@ -51,6 +52,7 @@
     }
   }
 
+  // Test digraph copy
   ListDigraph to;
   ListDigraph::NodeMap<int> tnm(to);
   ListDigraph::ArcMap<int> tam(to);
@@ -68,6 +70,9 @@
     nodeRef(nr).arcRef(er).
     nodeCrossRef(ncr).arcCrossRef(ecr).
     node(fn, tn).arc(fa, ta).run();
+  
+  check(countNodes(from) == countNodes(to), "Wrong copy.");
+  check(countArcs(from) == countArcs(to), "Wrong copy.");
 
   for (SmartDigraph::NodeIt it(from); it != INVALID; ++it) {
     check(ncr[nr[it]] == it, "Wrong copy.");
@@ -90,11 +95,18 @@
   }
   check(tn == nr[fn], "Wrong copy.");
   check(ta == er[fa], "Wrong copy.");
+
+  // Test repeated copy
+  digraphCopy(from, to).run();
+  
+  check(countNodes(from) == countNodes(to), "Wrong copy.");
+  check(countArcs(from) == countArcs(to), "Wrong copy.");
 }
 
 void graph_copy_test() {
   const int nn = 10;
 
+  // Build a graph
   SmartGraph from;
   SmartGraph::NodeMap<int> fnm(from);
   SmartGraph::ArcMap<int> fam(from);
@@ -122,6 +134,7 @@
     }
   }
 
+  // Test graph copy
   ListGraph to;
   ListGraph::NodeMap<int> tnm(to);
   ListGraph::ArcMap<int> tam(to);
@@ -144,6 +157,10 @@
     nodeCrossRef(ncr).arcCrossRef(acr).edgeCrossRef(ecr).
     node(fn, tn).arc(fa, ta).edge(fe, te).run();
 
+  check(countNodes(from) == countNodes(to), "Wrong copy.");
+  check(countEdges(from) == countEdges(to), "Wrong copy.");
+  check(countArcs(from) == countArcs(to), "Wrong copy.");
+
   for (SmartGraph::NodeIt it(from); it != INVALID; ++it) {
     check(ncr[nr[it]] == it, "Wrong copy.");
     check(fnm[it] == tnm[nr[it]], "Wrong copy.");
@@ -180,6 +197,13 @@
   check(tn == nr[fn], "Wrong copy.");
   check(ta == ar[fa], "Wrong copy.");
   check(te == er[fe], "Wrong copy.");
+
+  // Test repeated copy
+  graphCopy(from, to).run();
+  
+  check(countNodes(from) == countNodes(to), "Wrong copy.");
+  check(countEdges(from) == countEdges(to), "Wrong copy.");
+  check(countArcs(from) == countArcs(to), "Wrong copy.");
 }
 
 



More information about the Lemon-commits mailing list