test/graph_copy_test.cc
changeset 1402 3c00344f49c9
parent 1194 699c7eac2c6d
     1.1 --- a/test/graph_copy_test.cc	Mon Jul 16 16:21:40 2018 +0200
     1.2 +++ b/test/graph_copy_test.cc	Wed Oct 17 19:14:07 2018 +0200
     1.3 @@ -2,7 +2,7 @@
     1.4   *
     1.5   * This file is a part of LEMON, a generic C++ optimization library.
     1.6   *
     1.7 - * Copyright (C) 2003-2009
     1.8 + * Copyright (C) 2003-2013
     1.9   * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    1.10   * (Egervary Research Group on Combinatorial Optimization, EGRES).
    1.11   *
    1.12 @@ -18,6 +18,7 @@
    1.13  
    1.14  #include <lemon/smart_graph.h>
    1.15  #include <lemon/list_graph.h>
    1.16 +#include <lemon/static_graph.h>
    1.17  #include <lemon/lgf_reader.h>
    1.18  #include <lemon/error.h>
    1.19  
    1.20 @@ -26,6 +27,7 @@
    1.21  using namespace std;
    1.22  using namespace lemon;
    1.23  
    1.24 +template <typename GR>
    1.25  void digraph_copy_test() {
    1.26    const int nn = 10;
    1.27  
    1.28 @@ -53,24 +55,24 @@
    1.29    }
    1.30  
    1.31    // Test digraph copy
    1.32 -  ListDigraph to;
    1.33 -  ListDigraph::NodeMap<int> tnm(to);
    1.34 -  ListDigraph::ArcMap<int> tam(to);
    1.35 -  ListDigraph::Node tn;
    1.36 -  ListDigraph::Arc ta;
    1.37 +  GR to;
    1.38 +  typename GR::template NodeMap<int> tnm(to);
    1.39 +  typename GR::template ArcMap<int> tam(to);
    1.40 +  typename GR::Node tn;
    1.41 +  typename GR::Arc ta;
    1.42  
    1.43 -  SmartDigraph::NodeMap<ListDigraph::Node> nr(from);
    1.44 -  SmartDigraph::ArcMap<ListDigraph::Arc> er(from);
    1.45 +  SmartDigraph::NodeMap<typename GR::Node> nr(from);
    1.46 +  SmartDigraph::ArcMap<typename GR::Arc> er(from);
    1.47  
    1.48 -  ListDigraph::NodeMap<SmartDigraph::Node> ncr(to);
    1.49 -  ListDigraph::ArcMap<SmartDigraph::Arc> ecr(to);
    1.50 +  typename GR::template NodeMap<SmartDigraph::Node> ncr(to);
    1.51 +  typename GR::template ArcMap<SmartDigraph::Arc> ecr(to);
    1.52  
    1.53    digraphCopy(from, to).
    1.54      nodeMap(fnm, tnm).arcMap(fam, tam).
    1.55      nodeRef(nr).arcRef(er).
    1.56      nodeCrossRef(ncr).arcCrossRef(ecr).
    1.57      node(fn, tn).arc(fa, ta).run();
    1.58 -  
    1.59 +
    1.60    check(countNodes(from) == countNodes(to), "Wrong copy.");
    1.61    check(countArcs(from) == countArcs(to), "Wrong copy.");
    1.62  
    1.63 @@ -86,11 +88,11 @@
    1.64      check(nr[from.target(it)] == to.target(er[it]), "Wrong copy.");
    1.65    }
    1.66  
    1.67 -  for (ListDigraph::NodeIt it(to); it != INVALID; ++it) {
    1.68 +  for (typename GR::NodeIt it(to); it != INVALID; ++it) {
    1.69      check(nr[ncr[it]] == it, "Wrong copy.");
    1.70    }
    1.71  
    1.72 -  for (ListDigraph::ArcIt it(to); it != INVALID; ++it) {
    1.73 +  for (typename GR::ArcIt it(to); it != INVALID; ++it) {
    1.74      check(er[ecr[it]] == it, "Wrong copy.");
    1.75    }
    1.76    check(tn == nr[fn], "Wrong copy.");
    1.77 @@ -98,11 +100,12 @@
    1.78  
    1.79    // Test repeated copy
    1.80    digraphCopy(from, to).run();
    1.81 -  
    1.82 +
    1.83    check(countNodes(from) == countNodes(to), "Wrong copy.");
    1.84    check(countArcs(from) == countArcs(to), "Wrong copy.");
    1.85  }
    1.86  
    1.87 +template <typename GR>
    1.88  void graph_copy_test() {
    1.89    const int nn = 10;
    1.90  
    1.91 @@ -135,21 +138,21 @@
    1.92    }
    1.93  
    1.94    // Test graph copy
    1.95 -  ListGraph to;
    1.96 -  ListGraph::NodeMap<int> tnm(to);
    1.97 -  ListGraph::ArcMap<int> tam(to);
    1.98 -  ListGraph::EdgeMap<int> tem(to);
    1.99 -  ListGraph::Node tn;
   1.100 -  ListGraph::Arc ta;
   1.101 -  ListGraph::Edge te;
   1.102 +  GR to;
   1.103 +  typename GR::template NodeMap<int> tnm(to);
   1.104 +  typename GR::template ArcMap<int> tam(to);
   1.105 +  typename GR::template EdgeMap<int> tem(to);
   1.106 +  typename GR::Node tn;
   1.107 +  typename GR::Arc ta;
   1.108 +  typename GR::Edge te;
   1.109  
   1.110 -  SmartGraph::NodeMap<ListGraph::Node> nr(from);
   1.111 -  SmartGraph::ArcMap<ListGraph::Arc> ar(from);
   1.112 -  SmartGraph::EdgeMap<ListGraph::Edge> er(from);
   1.113 +  SmartGraph::NodeMap<typename GR::Node> nr(from);
   1.114 +  SmartGraph::ArcMap<typename GR::Arc> ar(from);
   1.115 +  SmartGraph::EdgeMap<typename GR::Edge> er(from);
   1.116  
   1.117 -  ListGraph::NodeMap<SmartGraph::Node> ncr(to);
   1.118 -  ListGraph::ArcMap<SmartGraph::Arc> acr(to);
   1.119 -  ListGraph::EdgeMap<SmartGraph::Edge> ecr(to);
   1.120 +  typename GR::template NodeMap<SmartGraph::Node> ncr(to);
   1.121 +  typename GR::template ArcMap<SmartGraph::Arc> acr(to);
   1.122 +  typename GR::template EdgeMap<SmartGraph::Edge> ecr(to);
   1.123  
   1.124    graphCopy(from, to).
   1.125      nodeMap(fnm, tnm).arcMap(fam, tam).edgeMap(fem, tem).
   1.126 @@ -184,14 +187,14 @@
   1.127            "Wrong copy.");
   1.128    }
   1.129  
   1.130 -  for (ListGraph::NodeIt it(to); it != INVALID; ++it) {
   1.131 +  for (typename GR::NodeIt it(to); it != INVALID; ++it) {
   1.132      check(nr[ncr[it]] == it, "Wrong copy.");
   1.133    }
   1.134  
   1.135 -  for (ListGraph::ArcIt it(to); it != INVALID; ++it) {
   1.136 +  for (typename GR::ArcIt it(to); it != INVALID; ++it) {
   1.137      check(ar[acr[it]] == it, "Wrong copy.");
   1.138    }
   1.139 -  for (ListGraph::EdgeIt it(to); it != INVALID; ++it) {
   1.140 +  for (typename GR::EdgeIt it(to); it != INVALID; ++it) {
   1.141      check(er[ecr[it]] == it, "Wrong copy.");
   1.142    }
   1.143    check(tn == nr[fn], "Wrong copy.");
   1.144 @@ -200,16 +203,186 @@
   1.145  
   1.146    // Test repeated copy
   1.147    graphCopy(from, to).run();
   1.148 -  
   1.149 +
   1.150    check(countNodes(from) == countNodes(to), "Wrong copy.");
   1.151    check(countEdges(from) == countEdges(to), "Wrong copy.");
   1.152    check(countArcs(from) == countArcs(to), "Wrong copy.");
   1.153  }
   1.154  
   1.155 +template <typename GR>
   1.156 +void bpgraph_copy_test() {
   1.157 +  const int nn = 10;
   1.158 +
   1.159 +  // Build a graph
   1.160 +  SmartBpGraph from;
   1.161 +  SmartBpGraph::NodeMap<int> fnm(from);
   1.162 +  SmartBpGraph::RedNodeMap<int> frnm(from);
   1.163 +  SmartBpGraph::BlueNodeMap<int> fbnm(from);
   1.164 +  SmartBpGraph::ArcMap<int> fam(from);
   1.165 +  SmartBpGraph::EdgeMap<int> fem(from);
   1.166 +  SmartBpGraph::Node fn = INVALID;
   1.167 +  SmartBpGraph::RedNode frn = INVALID;
   1.168 +  SmartBpGraph::BlueNode fbn = INVALID;
   1.169 +  SmartBpGraph::Arc fa = INVALID;
   1.170 +  SmartBpGraph::Edge fe = INVALID;
   1.171 +
   1.172 +  std::vector<SmartBpGraph::RedNode> frnv;
   1.173 +  for (int i = 0; i < nn; ++i) {
   1.174 +    SmartBpGraph::RedNode node = from.addRedNode();
   1.175 +    frnv.push_back(node);
   1.176 +    fnm[node] = i * i;
   1.177 +    frnm[node] = i + i;
   1.178 +    if (i == 0) {
   1.179 +      fn = node;
   1.180 +      frn = node;
   1.181 +    }
   1.182 +  }
   1.183 +
   1.184 +  std::vector<SmartBpGraph::BlueNode> fbnv;
   1.185 +  for (int i = 0; i < nn; ++i) {
   1.186 +    SmartBpGraph::BlueNode node = from.addBlueNode();
   1.187 +    fbnv.push_back(node);
   1.188 +    fnm[node] = i * i;
   1.189 +    fbnm[node] = i + i;
   1.190 +    if (i == 0) fbn = node;
   1.191 +  }
   1.192 +
   1.193 +  for (int i = 0; i < nn; ++i) {
   1.194 +    for (int j = 0; j < nn; ++j) {
   1.195 +      SmartBpGraph::Edge edge = from.addEdge(frnv[i], fbnv[j]);
   1.196 +      fem[edge] = i * i + j * j;
   1.197 +      fam[from.direct(edge, true)] = i + j * j;
   1.198 +      fam[from.direct(edge, false)] = i * i + j;
   1.199 +      if (i == 0 && j == 0) fa = from.direct(edge, true);
   1.200 +      if (i == 0 && j == 0) fe = edge;
   1.201 +    }
   1.202 +  }
   1.203 +
   1.204 +  // Test graph copy
   1.205 +  GR to;
   1.206 +  typename GR::template NodeMap<int> tnm(to);
   1.207 +  typename GR::template RedNodeMap<int> trnm(to);
   1.208 +  typename GR::template BlueNodeMap<int> tbnm(to);
   1.209 +  typename GR::template ArcMap<int> tam(to);
   1.210 +  typename GR::template EdgeMap<int> tem(to);
   1.211 +  typename GR::Node tn;
   1.212 +  typename GR::RedNode trn;
   1.213 +  typename GR::BlueNode tbn;
   1.214 +  typename GR::Arc ta;
   1.215 +  typename GR::Edge te;
   1.216 +
   1.217 +  SmartBpGraph::NodeMap<typename GR::Node> nr(from);
   1.218 +  SmartBpGraph::RedNodeMap<typename GR::RedNode> rnr(from);
   1.219 +  SmartBpGraph::BlueNodeMap<typename GR::BlueNode> bnr(from);
   1.220 +  SmartBpGraph::ArcMap<typename GR::Arc> ar(from);
   1.221 +  SmartBpGraph::EdgeMap<typename GR::Edge> er(from);
   1.222 +
   1.223 +  typename GR::template NodeMap<SmartBpGraph::Node> ncr(to);
   1.224 +  typename GR::template RedNodeMap<SmartBpGraph::RedNode> rncr(to);
   1.225 +  typename GR::template BlueNodeMap<SmartBpGraph::BlueNode> bncr(to);
   1.226 +  typename GR::template ArcMap<SmartBpGraph::Arc> acr(to);
   1.227 +  typename GR::template EdgeMap<SmartBpGraph::Edge> ecr(to);
   1.228 +
   1.229 +  bpGraphCopy(from, to).
   1.230 +    nodeMap(fnm, tnm).
   1.231 +    redNodeMap(frnm, trnm).blueNodeMap(fbnm, tbnm).
   1.232 +    arcMap(fam, tam).edgeMap(fem, tem).
   1.233 +    nodeRef(nr).redRef(rnr).blueRef(bnr).
   1.234 +    arcRef(ar).edgeRef(er).
   1.235 +    nodeCrossRef(ncr).redCrossRef(rncr).blueCrossRef(bncr).
   1.236 +    arcCrossRef(acr).edgeCrossRef(ecr).
   1.237 +    node(fn, tn).redNode(frn, trn).blueNode(fbn, tbn).
   1.238 +    arc(fa, ta).edge(fe, te).run();
   1.239 +
   1.240 +  check(countNodes(from) == countNodes(to), "Wrong copy.");
   1.241 +  check(countRedNodes(from) == countRedNodes(to), "Wrong copy.");
   1.242 +  check(countBlueNodes(from) == countBlueNodes(to), "Wrong copy.");
   1.243 +  check(countEdges(from) == countEdges(to), "Wrong copy.");
   1.244 +  check(countArcs(from) == countArcs(to), "Wrong copy.");
   1.245 +
   1.246 +  for (SmartBpGraph::NodeIt it(from); it != INVALID; ++it) {
   1.247 +    check(ncr[nr[it]] == it, "Wrong copy.");
   1.248 +    check(fnm[it] == tnm[nr[it]], "Wrong copy.");
   1.249 +  }
   1.250 +
   1.251 +  for (SmartBpGraph::RedNodeIt it(from); it != INVALID; ++it) {
   1.252 +    check(ncr[nr[it]] == it, "Wrong copy.");
   1.253 +    check(fnm[it] == tnm[nr[it]], "Wrong copy.");
   1.254 +    check(rnr[it] == nr[it], "Wrong copy.");
   1.255 +    check(rncr[rnr[it]] == it, "Wrong copy.");
   1.256 +    check(frnm[it] == trnm[rnr[it]], "Wrong copy.");
   1.257 +    check(to.red(rnr[it]), "Wrong copy.");
   1.258 +  }
   1.259 +
   1.260 +  for (SmartBpGraph::BlueNodeIt it(from); it != INVALID; ++it) {
   1.261 +    check(ncr[nr[it]] == it, "Wrong copy.");
   1.262 +    check(fnm[it] == tnm[nr[it]], "Wrong copy.");
   1.263 +    check(bnr[it] == nr[it], "Wrong copy.");
   1.264 +    check(bncr[bnr[it]] == it, "Wrong copy.");
   1.265 +    check(fbnm[it] == tbnm[bnr[it]], "Wrong copy.");
   1.266 +    check(to.blue(bnr[it]), "Wrong copy.");
   1.267 +  }
   1.268 +
   1.269 +  for (SmartBpGraph::ArcIt it(from); it != INVALID; ++it) {
   1.270 +    check(acr[ar[it]] == it, "Wrong copy.");
   1.271 +    check(fam[it] == tam[ar[it]], "Wrong copy.");
   1.272 +    check(nr[from.source(it)] == to.source(ar[it]), "Wrong copy.");
   1.273 +    check(nr[from.target(it)] == to.target(ar[it]), "Wrong copy.");
   1.274 +  }
   1.275 +
   1.276 +  for (SmartBpGraph::EdgeIt it(from); it != INVALID; ++it) {
   1.277 +    check(ecr[er[it]] == it, "Wrong copy.");
   1.278 +    check(fem[it] == tem[er[it]], "Wrong copy.");
   1.279 +    check(nr[from.u(it)] == to.u(er[it]) || nr[from.u(it)] == to.v(er[it]),
   1.280 +          "Wrong copy.");
   1.281 +    check(nr[from.v(it)] == to.u(er[it]) || nr[from.v(it)] == to.v(er[it]),
   1.282 +          "Wrong copy.");
   1.283 +    check((from.u(it) != from.v(it)) == (to.u(er[it]) != to.v(er[it])),
   1.284 +          "Wrong copy.");
   1.285 +  }
   1.286 +
   1.287 +  for (typename GR::NodeIt it(to); it != INVALID; ++it) {
   1.288 +    check(nr[ncr[it]] == it, "Wrong copy.");
   1.289 +  }
   1.290 +  for (typename GR::RedNodeIt it(to); it != INVALID; ++it) {
   1.291 +    check(rncr[it] == ncr[it], "Wrong copy.");
   1.292 +    check(rnr[rncr[it]] == it, "Wrong copy.");
   1.293 +  }
   1.294 +  for (typename GR::BlueNodeIt it(to); it != INVALID; ++it) {
   1.295 +    check(bncr[it] == ncr[it], "Wrong copy.");
   1.296 +    check(bnr[bncr[it]] == it, "Wrong copy.");
   1.297 +  }
   1.298 +  for (typename GR::ArcIt it(to); it != INVALID; ++it) {
   1.299 +    check(ar[acr[it]] == it, "Wrong copy.");
   1.300 +  }
   1.301 +  for (typename GR::EdgeIt it(to); it != INVALID; ++it) {
   1.302 +    check(er[ecr[it]] == it, "Wrong copy.");
   1.303 +  }
   1.304 +  check(tn == nr[fn], "Wrong copy.");
   1.305 +  check(trn == rnr[frn], "Wrong copy.");
   1.306 +  check(tbn == bnr[fbn], "Wrong copy.");
   1.307 +  check(ta == ar[fa], "Wrong copy.");
   1.308 +  check(te == er[fe], "Wrong copy.");
   1.309 +
   1.310 +  // Test repeated copy
   1.311 +  bpGraphCopy(from, to).run();
   1.312 +
   1.313 +  check(countNodes(from) == countNodes(to), "Wrong copy.");
   1.314 +  check(countRedNodes(from) == countRedNodes(to), "Wrong copy.");
   1.315 +  check(countBlueNodes(from) == countBlueNodes(to), "Wrong copy.");
   1.316 +  check(countEdges(from) == countEdges(to), "Wrong copy.");
   1.317 +  check(countArcs(from) == countArcs(to), "Wrong copy.");
   1.318 +}
   1.319 +
   1.320  
   1.321  int main() {
   1.322 -  digraph_copy_test();
   1.323 -  graph_copy_test();
   1.324 +  digraph_copy_test<SmartDigraph>();
   1.325 +  digraph_copy_test<ListDigraph>();
   1.326 +  digraph_copy_test<StaticDigraph>();
   1.327 +  graph_copy_test<SmartGraph>();
   1.328 +  graph_copy_test<ListGraph>();
   1.329 +  bpgraph_copy_test<SmartBpGraph>();
   1.330 +  bpgraph_copy_test<ListBpGraph>();
   1.331  
   1.332    return 0;
   1.333  }