test/graph_copy_test.cc
changeset 200 c0e2c043c060
child 209 765619b7cbb2
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/graph_copy_test.cc	Thu Jul 10 16:05:56 2008 +0200
     1.3 @@ -0,0 +1,192 @@
     1.4 +/* -*- C++ -*-
     1.5 + *
     1.6 + * This file is a part of LEMON, a generic C++ optimization library
     1.7 + *
     1.8 + * Copyright (C) 2003-2008
     1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    1.11 + *
    1.12 + * Permission to use, modify and distribute this software is granted
    1.13 + * provided that this copyright notice appears in all copies. For
    1.14 + * precise terms see the accompanying LICENSE file.
    1.15 + *
    1.16 + * This software is provided "AS IS" with no warranty of any kind,
    1.17 + * express or implied, and with no claim as to its suitability for any
    1.18 + * purpose.
    1.19 + *
    1.20 + */
    1.21 +
    1.22 +#include <lemon/smart_graph.h>
    1.23 +#include <lemon/list_graph.h>
    1.24 +#include <lemon/lgf_reader.h>
    1.25 +#include <lemon/graph_utils.h>
    1.26 +#include <lemon/error.h>
    1.27 +
    1.28 +#include "test_tools.h"
    1.29 +
    1.30 +using namespace std;
    1.31 +using namespace lemon;
    1.32 +
    1.33 +void digraph_copy_test() {
    1.34 +  const int nn = 10;
    1.35 +
    1.36 +  SmartDigraph from;
    1.37 +  SmartDigraph::NodeMap<int> fnm(from);
    1.38 +  SmartDigraph::ArcMap<int> fam(from);
    1.39 +  SmartDigraph::Node fn = INVALID;
    1.40 +  SmartDigraph::Arc fa = INVALID;
    1.41 +
    1.42 +  std::vector<SmartDigraph::Node> fnv;
    1.43 +  for (int i = 0; i < nn; ++i) {
    1.44 +    SmartDigraph::Node node = from.addNode();
    1.45 +    fnv.push_back(node);
    1.46 +    fnm[node] = i * i;
    1.47 +    if (i == 0) fn = node;
    1.48 +  }
    1.49 +
    1.50 +  for (int i = 0; i < nn; ++i) {
    1.51 +    for (int j = 0; j < nn; ++j) {
    1.52 +      SmartDigraph::Arc arc = from.addArc(fnv[i], fnv[j]);
    1.53 +      fam[arc] = i + j * j;
    1.54 +      if (i == 0 && j == 0) fa = arc;
    1.55 +    }
    1.56 +  }
    1.57 +
    1.58 +  ListDigraph to;
    1.59 +  ListDigraph::NodeMap<int> tnm(to);
    1.60 +  ListDigraph::ArcMap<int> tam(to);
    1.61 +  ListDigraph::Node tn;
    1.62 +  ListDigraph::Arc ta;
    1.63 +
    1.64 +  SmartDigraph::NodeMap<ListDigraph::Node> nr(from);
    1.65 +  SmartDigraph::ArcMap<ListDigraph::Arc> er(from);
    1.66 +
    1.67 +  ListDigraph::NodeMap<SmartDigraph::Node> ncr(to);
    1.68 +  ListDigraph::ArcMap<SmartDigraph::Arc> ecr(to);
    1.69 +
    1.70 +  DigraphCopy<ListDigraph, SmartDigraph>(to, from).
    1.71 +    nodeMap(tnm, fnm).arcMap(tam, fam).
    1.72 +    nodeRef(nr).arcRef(er).
    1.73 +    nodeCrossRef(ncr).arcCrossRef(ecr).
    1.74 +    node(tn, fn).arc(ta, fa).run();
    1.75 +
    1.76 +  for (SmartDigraph::NodeIt it(from); it != INVALID; ++it) {
    1.77 +    check(ncr[nr[it]] == it, "Wrong copy.");
    1.78 +    check(fnm[it] == tnm[nr[it]], "Wrong copy.");
    1.79 +  }
    1.80 +
    1.81 +  for (SmartDigraph::ArcIt it(from); it != INVALID; ++it) {
    1.82 +    check(ecr[er[it]] == it, "Wrong copy.");
    1.83 +    check(fam[it] == tam[er[it]], "Wrong copy.");
    1.84 +    check(nr[from.source(it)] == to.source(er[it]), "Wrong copy.");
    1.85 +    check(nr[from.target(it)] == to.target(er[it]), "Wrong copy.");
    1.86 +  }
    1.87 +
    1.88 +  for (ListDigraph::NodeIt it(to); it != INVALID; ++it) {
    1.89 +    check(nr[ncr[it]] == it, "Wrong copy.");
    1.90 +  }
    1.91 +
    1.92 +  for (ListDigraph::ArcIt it(to); it != INVALID; ++it) {
    1.93 +    check(er[ecr[it]] == it, "Wrong copy.");
    1.94 +  }
    1.95 +  check(tn == nr[fn], "Wrong copy.");
    1.96 +  check(ta == er[fa], "Wrong copy.");
    1.97 +}
    1.98 +
    1.99 +void graph_copy_test() {
   1.100 +  const int nn = 10;
   1.101 +
   1.102 +  SmartGraph from;
   1.103 +  SmartGraph::NodeMap<int> fnm(from);
   1.104 +  SmartGraph::ArcMap<int> fam(from);
   1.105 +  SmartGraph::EdgeMap<int> fem(from);
   1.106 +  SmartGraph::Node fn = INVALID;
   1.107 +  SmartGraph::Arc fa = INVALID;
   1.108 +  SmartGraph::Edge fe = INVALID;
   1.109 +
   1.110 +  std::vector<SmartGraph::Node> fnv;
   1.111 +  for (int i = 0; i < nn; ++i) {
   1.112 +    SmartGraph::Node node = from.addNode();
   1.113 +    fnv.push_back(node);
   1.114 +    fnm[node] = i * i;
   1.115 +    if (i == 0) fn = node;
   1.116 +  }
   1.117 +
   1.118 +  for (int i = 0; i < nn; ++i) {
   1.119 +    for (int j = 0; j < nn; ++j) {
   1.120 +      SmartGraph::Edge edge = from.addEdge(fnv[i], fnv[j]);
   1.121 +      fem[edge] = i * i + j * j;
   1.122 +      fam[from.direct(edge, true)] = i + j * j;
   1.123 +      fam[from.direct(edge, false)] = i * i + j;
   1.124 +      if (i == 0 && j == 0) fa = from.direct(edge, true);
   1.125 +      if (i == 0 && j == 0) fe = edge;
   1.126 +    }
   1.127 +  }
   1.128 +  
   1.129 +  ListGraph to;
   1.130 +  ListGraph::NodeMap<int> tnm(to);
   1.131 +  ListGraph::ArcMap<int> tam(to);
   1.132 +  ListGraph::EdgeMap<int> tem(to);
   1.133 +  ListGraph::Node tn;
   1.134 +  ListGraph::Arc ta;
   1.135 +  ListGraph::Edge te;
   1.136 +
   1.137 +  SmartGraph::NodeMap<ListGraph::Node> nr(from);
   1.138 +  SmartGraph::ArcMap<ListGraph::Arc> ar(from);
   1.139 +  SmartGraph::EdgeMap<ListGraph::Edge> er(from);
   1.140 +
   1.141 +  ListGraph::NodeMap<SmartGraph::Node> ncr(to);
   1.142 +  ListGraph::ArcMap<SmartGraph::Arc> acr(to);
   1.143 +  ListGraph::EdgeMap<SmartGraph::Edge> ecr(to);
   1.144 +
   1.145 +  GraphCopy<ListGraph, SmartGraph>(to, from).
   1.146 +    nodeMap(tnm, fnm).arcMap(tam, fam).edgeMap(tem, fem).
   1.147 +    nodeRef(nr).arcRef(ar).edgeRef(er).
   1.148 +    nodeCrossRef(ncr).arcCrossRef(acr).edgeCrossRef(ecr).
   1.149 +    node(tn, fn).arc(ta, fa).edge(te, fe).run();
   1.150 +
   1.151 +  for (SmartGraph::NodeIt it(from); it != INVALID; ++it) {
   1.152 +    check(ncr[nr[it]] == it, "Wrong copy.");
   1.153 +    check(fnm[it] == tnm[nr[it]], "Wrong copy.");
   1.154 +  }
   1.155 +
   1.156 +  for (SmartGraph::ArcIt it(from); it != INVALID; ++it) {
   1.157 +    check(acr[ar[it]] == it, "Wrong copy.");
   1.158 +    check(fam[it] == tam[ar[it]], "Wrong copy.");
   1.159 +    check(nr[from.source(it)] == to.source(ar[it]), "Wrong copy.");
   1.160 +    check(nr[from.target(it)] == to.target(ar[it]), "Wrong copy.");
   1.161 +  }
   1.162 +
   1.163 +  for (SmartGraph::EdgeIt it(from); it != INVALID; ++it) {
   1.164 +    check(ecr[er[it]] == it, "Wrong copy.");
   1.165 +    check(fem[it] == tem[er[it]], "Wrong copy.");
   1.166 +    check(nr[from.u(it)] == to.u(er[it]) || nr[from.u(it)] == to.v(er[it]), 
   1.167 +	  "Wrong copy.");
   1.168 +    check(nr[from.v(it)] == to.u(er[it]) || nr[from.v(it)] == to.v(er[it]), 
   1.169 +	  "Wrong copy.");
   1.170 +    check((from.u(it) != from.v(it)) == (to.u(er[it]) != to.v(er[it])), 
   1.171 +	  "Wrong copy.");
   1.172 +  }
   1.173 +
   1.174 +  for (ListGraph::NodeIt it(to); it != INVALID; ++it) {
   1.175 +    check(nr[ncr[it]] == it, "Wrong copy.");
   1.176 +  }
   1.177 +
   1.178 +  for (ListGraph::ArcIt it(to); it != INVALID; ++it) {
   1.179 +    check(ar[acr[it]] == it, "Wrong copy.");
   1.180 +  }
   1.181 +  for (ListGraph::EdgeIt it(to); it != INVALID; ++it) {
   1.182 +    check(er[ecr[it]] == it, "Wrong copy.");
   1.183 +  }
   1.184 +  check(tn == nr[fn], "Wrong copy.");
   1.185 +  check(ta == ar[fa], "Wrong copy.");
   1.186 +  check(te == er[fe], "Wrong copy.");
   1.187 +}
   1.188 +
   1.189 +
   1.190 +int main() {
   1.191 +  digraph_copy_test();
   1.192 +  graph_copy_test();
   1.193 +
   1.194 +  return 0; 
   1.195 +}