test/graph_copy_test.cc
author Peter Kovacs <kpeter@inf.elte.hu>
Sun, 09 Jan 2011 00:56:52 +0100
changeset 1034 ef200e268af2
parent 893 d395358592df
child 1022 523e45e37e52
permissions -rw-r--r--
Unifications and improvements in TSP algorithms (#386)
alpar@209
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
deba@200
     2
 *
alpar@209
     3
 * This file is a part of LEMON, a generic C++ optimization library.
deba@200
     4
 *
alpar@440
     5
 * Copyright (C) 2003-2009
deba@200
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@200
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@200
     8
 *
deba@200
     9
 * Permission to use, modify and distribute this software is granted
deba@200
    10
 * provided that this copyright notice appears in all copies. For
deba@200
    11
 * precise terms see the accompanying LICENSE file.
deba@200
    12
 *
deba@200
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@200
    14
 * express or implied, and with no claim as to its suitability for any
deba@200
    15
 * purpose.
deba@200
    16
 *
deba@200
    17
 */
deba@200
    18
deba@200
    19
#include <lemon/smart_graph.h>
deba@200
    20
#include <lemon/list_graph.h>
kpeter@894
    21
#include <lemon/static_graph.h>
deba@200
    22
#include <lemon/lgf_reader.h>
deba@200
    23
#include <lemon/error.h>
deba@200
    24
deba@200
    25
#include "test_tools.h"
deba@200
    26
deba@200
    27
using namespace std;
deba@200
    28
using namespace lemon;
deba@200
    29
kpeter@894
    30
template <typename GR>
deba@200
    31
void digraph_copy_test() {
deba@200
    32
  const int nn = 10;
deba@200
    33
kpeter@890
    34
  // Build a digraph
deba@200
    35
  SmartDigraph from;
deba@200
    36
  SmartDigraph::NodeMap<int> fnm(from);
deba@200
    37
  SmartDigraph::ArcMap<int> fam(from);
deba@200
    38
  SmartDigraph::Node fn = INVALID;
deba@200
    39
  SmartDigraph::Arc fa = INVALID;
deba@200
    40
deba@200
    41
  std::vector<SmartDigraph::Node> fnv;
deba@200
    42
  for (int i = 0; i < nn; ++i) {
deba@200
    43
    SmartDigraph::Node node = from.addNode();
deba@200
    44
    fnv.push_back(node);
deba@200
    45
    fnm[node] = i * i;
deba@200
    46
    if (i == 0) fn = node;
deba@200
    47
  }
deba@200
    48
deba@200
    49
  for (int i = 0; i < nn; ++i) {
deba@200
    50
    for (int j = 0; j < nn; ++j) {
deba@200
    51
      SmartDigraph::Arc arc = from.addArc(fnv[i], fnv[j]);
deba@200
    52
      fam[arc] = i + j * j;
deba@200
    53
      if (i == 0 && j == 0) fa = arc;
deba@200
    54
    }
deba@200
    55
  }
kpeter@894
    56
  
kpeter@894
    57
  // Test digraph copy
kpeter@894
    58
  GR to;
kpeter@894
    59
  typename GR::template NodeMap<int> tnm(to);
kpeter@894
    60
  typename GR::template ArcMap<int> tam(to);
kpeter@894
    61
  typename GR::Node tn;
kpeter@894
    62
  typename GR::Arc ta;
deba@200
    63
kpeter@894
    64
  SmartDigraph::NodeMap<typename GR::Node> nr(from);
kpeter@894
    65
  SmartDigraph::ArcMap<typename GR::Arc> er(from);
deba@200
    66
kpeter@894
    67
  typename GR::template NodeMap<SmartDigraph::Node> ncr(to);
kpeter@894
    68
  typename GR::template ArcMap<SmartDigraph::Arc> ecr(to);
deba@200
    69
kpeter@282
    70
  digraphCopy(from, to).
kpeter@282
    71
    nodeMap(fnm, tnm).arcMap(fam, tam).
deba@200
    72
    nodeRef(nr).arcRef(er).
deba@200
    73
    nodeCrossRef(ncr).arcCrossRef(ecr).
kpeter@282
    74
    node(fn, tn).arc(fa, ta).run();
kpeter@890
    75
  
kpeter@890
    76
  check(countNodes(from) == countNodes(to), "Wrong copy.");
kpeter@890
    77
  check(countArcs(from) == countArcs(to), "Wrong copy.");
deba@200
    78
deba@200
    79
  for (SmartDigraph::NodeIt it(from); it != INVALID; ++it) {
deba@200
    80
    check(ncr[nr[it]] == it, "Wrong copy.");
deba@200
    81
    check(fnm[it] == tnm[nr[it]], "Wrong copy.");
deba@200
    82
  }
deba@200
    83
deba@200
    84
  for (SmartDigraph::ArcIt it(from); it != INVALID; ++it) {
deba@200
    85
    check(ecr[er[it]] == it, "Wrong copy.");
deba@200
    86
    check(fam[it] == tam[er[it]], "Wrong copy.");
deba@200
    87
    check(nr[from.source(it)] == to.source(er[it]), "Wrong copy.");
deba@200
    88
    check(nr[from.target(it)] == to.target(er[it]), "Wrong copy.");
deba@200
    89
  }
deba@200
    90
kpeter@894
    91
  for (typename GR::NodeIt it(to); it != INVALID; ++it) {
deba@200
    92
    check(nr[ncr[it]] == it, "Wrong copy.");
deba@200
    93
  }
deba@200
    94
kpeter@894
    95
  for (typename GR::ArcIt it(to); it != INVALID; ++it) {
deba@200
    96
    check(er[ecr[it]] == it, "Wrong copy.");
deba@200
    97
  }
deba@200
    98
  check(tn == nr[fn], "Wrong copy.");
deba@200
    99
  check(ta == er[fa], "Wrong copy.");
kpeter@890
   100
kpeter@890
   101
  // Test repeated copy
kpeter@890
   102
  digraphCopy(from, to).run();
kpeter@890
   103
  
kpeter@890
   104
  check(countNodes(from) == countNodes(to), "Wrong copy.");
kpeter@890
   105
  check(countArcs(from) == countArcs(to), "Wrong copy.");
deba@200
   106
}
deba@200
   107
kpeter@894
   108
template <typename GR>
deba@200
   109
void graph_copy_test() {
deba@200
   110
  const int nn = 10;
deba@200
   111
kpeter@890
   112
  // Build a graph
deba@200
   113
  SmartGraph from;
deba@200
   114
  SmartGraph::NodeMap<int> fnm(from);
deba@200
   115
  SmartGraph::ArcMap<int> fam(from);
deba@200
   116
  SmartGraph::EdgeMap<int> fem(from);
deba@200
   117
  SmartGraph::Node fn = INVALID;
deba@200
   118
  SmartGraph::Arc fa = INVALID;
deba@200
   119
  SmartGraph::Edge fe = INVALID;
deba@200
   120
deba@200
   121
  std::vector<SmartGraph::Node> fnv;
deba@200
   122
  for (int i = 0; i < nn; ++i) {
deba@200
   123
    SmartGraph::Node node = from.addNode();
deba@200
   124
    fnv.push_back(node);
deba@200
   125
    fnm[node] = i * i;
deba@200
   126
    if (i == 0) fn = node;
deba@200
   127
  }
deba@200
   128
deba@200
   129
  for (int i = 0; i < nn; ++i) {
deba@200
   130
    for (int j = 0; j < nn; ++j) {
deba@200
   131
      SmartGraph::Edge edge = from.addEdge(fnv[i], fnv[j]);
deba@200
   132
      fem[edge] = i * i + j * j;
deba@200
   133
      fam[from.direct(edge, true)] = i + j * j;
deba@200
   134
      fam[from.direct(edge, false)] = i * i + j;
deba@200
   135
      if (i == 0 && j == 0) fa = from.direct(edge, true);
deba@200
   136
      if (i == 0 && j == 0) fe = edge;
deba@200
   137
    }
deba@200
   138
  }
alpar@209
   139
kpeter@890
   140
  // Test graph copy
kpeter@894
   141
  GR to;
kpeter@894
   142
  typename GR::template NodeMap<int> tnm(to);
kpeter@894
   143
  typename GR::template ArcMap<int> tam(to);
kpeter@894
   144
  typename GR::template EdgeMap<int> tem(to);
kpeter@894
   145
  typename GR::Node tn;
kpeter@894
   146
  typename GR::Arc ta;
kpeter@894
   147
  typename GR::Edge te;
deba@200
   148
kpeter@894
   149
  SmartGraph::NodeMap<typename GR::Node> nr(from);
kpeter@894
   150
  SmartGraph::ArcMap<typename GR::Arc> ar(from);
kpeter@894
   151
  SmartGraph::EdgeMap<typename GR::Edge> er(from);
deba@200
   152
kpeter@894
   153
  typename GR::template NodeMap<SmartGraph::Node> ncr(to);
kpeter@894
   154
  typename GR::template ArcMap<SmartGraph::Arc> acr(to);
kpeter@894
   155
  typename GR::template EdgeMap<SmartGraph::Edge> ecr(to);
deba@200
   156
kpeter@282
   157
  graphCopy(from, to).
kpeter@282
   158
    nodeMap(fnm, tnm).arcMap(fam, tam).edgeMap(fem, tem).
deba@200
   159
    nodeRef(nr).arcRef(ar).edgeRef(er).
deba@200
   160
    nodeCrossRef(ncr).arcCrossRef(acr).edgeCrossRef(ecr).
kpeter@282
   161
    node(fn, tn).arc(fa, ta).edge(fe, te).run();
deba@200
   162
kpeter@890
   163
  check(countNodes(from) == countNodes(to), "Wrong copy.");
kpeter@890
   164
  check(countEdges(from) == countEdges(to), "Wrong copy.");
kpeter@890
   165
  check(countArcs(from) == countArcs(to), "Wrong copy.");
kpeter@890
   166
deba@200
   167
  for (SmartGraph::NodeIt it(from); it != INVALID; ++it) {
deba@200
   168
    check(ncr[nr[it]] == it, "Wrong copy.");
deba@200
   169
    check(fnm[it] == tnm[nr[it]], "Wrong copy.");
deba@200
   170
  }
deba@200
   171
deba@200
   172
  for (SmartGraph::ArcIt it(from); it != INVALID; ++it) {
deba@200
   173
    check(acr[ar[it]] == it, "Wrong copy.");
deba@200
   174
    check(fam[it] == tam[ar[it]], "Wrong copy.");
deba@200
   175
    check(nr[from.source(it)] == to.source(ar[it]), "Wrong copy.");
deba@200
   176
    check(nr[from.target(it)] == to.target(ar[it]), "Wrong copy.");
deba@200
   177
  }
deba@200
   178
deba@200
   179
  for (SmartGraph::EdgeIt it(from); it != INVALID; ++it) {
deba@200
   180
    check(ecr[er[it]] == it, "Wrong copy.");
deba@200
   181
    check(fem[it] == tem[er[it]], "Wrong copy.");
alpar@209
   182
    check(nr[from.u(it)] == to.u(er[it]) || nr[from.u(it)] == to.v(er[it]),
alpar@209
   183
          "Wrong copy.");
alpar@209
   184
    check(nr[from.v(it)] == to.u(er[it]) || nr[from.v(it)] == to.v(er[it]),
alpar@209
   185
          "Wrong copy.");
alpar@209
   186
    check((from.u(it) != from.v(it)) == (to.u(er[it]) != to.v(er[it])),
alpar@209
   187
          "Wrong copy.");
deba@200
   188
  }
deba@200
   189
kpeter@894
   190
  for (typename GR::NodeIt it(to); it != INVALID; ++it) {
deba@200
   191
    check(nr[ncr[it]] == it, "Wrong copy.");
deba@200
   192
  }
deba@200
   193
kpeter@894
   194
  for (typename GR::ArcIt it(to); it != INVALID; ++it) {
deba@200
   195
    check(ar[acr[it]] == it, "Wrong copy.");
deba@200
   196
  }
kpeter@894
   197
  for (typename GR::EdgeIt it(to); it != INVALID; ++it) {
deba@200
   198
    check(er[ecr[it]] == it, "Wrong copy.");
deba@200
   199
  }
deba@200
   200
  check(tn == nr[fn], "Wrong copy.");
deba@200
   201
  check(ta == ar[fa], "Wrong copy.");
deba@200
   202
  check(te == er[fe], "Wrong copy.");
kpeter@890
   203
kpeter@890
   204
  // Test repeated copy
kpeter@890
   205
  graphCopy(from, to).run();
kpeter@890
   206
  
kpeter@890
   207
  check(countNodes(from) == countNodes(to), "Wrong copy.");
kpeter@890
   208
  check(countEdges(from) == countEdges(to), "Wrong copy.");
kpeter@890
   209
  check(countArcs(from) == countArcs(to), "Wrong copy.");
deba@200
   210
}
deba@200
   211
deba@200
   212
deba@200
   213
int main() {
kpeter@894
   214
  digraph_copy_test<SmartDigraph>();
kpeter@894
   215
  digraph_copy_test<ListDigraph>();
kpeter@894
   216
  digraph_copy_test<StaticDigraph>();
kpeter@894
   217
  graph_copy_test<SmartGraph>();
kpeter@894
   218
  graph_copy_test<ListGraph>();
deba@200
   219
alpar@209
   220
  return 0;
deba@200
   221
}