test/graph_copy_test.cc
author Alpar Juttner <alpar@cs.elte.hu>
Thu, 10 Jul 2008 16:13:50 +0200
changeset 202 a5ee729dc1e1
child 209 765619b7cbb2
permissions -rw-r--r--
Merge
deba@200
     1
/* -*- C++ -*-
deba@200
     2
 *
deba@200
     3
 * This file is a part of LEMON, a generic C++ optimization library
deba@200
     4
 *
deba@200
     5
 * Copyright (C) 2003-2008
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>
deba@200
    21
#include <lemon/lgf_reader.h>
deba@200
    22
#include <lemon/graph_utils.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
deba@200
    30
void digraph_copy_test() {
deba@200
    31
  const int nn = 10;
deba@200
    32
deba@200
    33
  SmartDigraph from;
deba@200
    34
  SmartDigraph::NodeMap<int> fnm(from);
deba@200
    35
  SmartDigraph::ArcMap<int> fam(from);
deba@200
    36
  SmartDigraph::Node fn = INVALID;
deba@200
    37
  SmartDigraph::Arc fa = INVALID;
deba@200
    38
deba@200
    39
  std::vector<SmartDigraph::Node> fnv;
deba@200
    40
  for (int i = 0; i < nn; ++i) {
deba@200
    41
    SmartDigraph::Node node = from.addNode();
deba@200
    42
    fnv.push_back(node);
deba@200
    43
    fnm[node] = i * i;
deba@200
    44
    if (i == 0) fn = node;
deba@200
    45
  }
deba@200
    46
deba@200
    47
  for (int i = 0; i < nn; ++i) {
deba@200
    48
    for (int j = 0; j < nn; ++j) {
deba@200
    49
      SmartDigraph::Arc arc = from.addArc(fnv[i], fnv[j]);
deba@200
    50
      fam[arc] = i + j * j;
deba@200
    51
      if (i == 0 && j == 0) fa = arc;
deba@200
    52
    }
deba@200
    53
  }
deba@200
    54
deba@200
    55
  ListDigraph to;
deba@200
    56
  ListDigraph::NodeMap<int> tnm(to);
deba@200
    57
  ListDigraph::ArcMap<int> tam(to);
deba@200
    58
  ListDigraph::Node tn;
deba@200
    59
  ListDigraph::Arc ta;
deba@200
    60
deba@200
    61
  SmartDigraph::NodeMap<ListDigraph::Node> nr(from);
deba@200
    62
  SmartDigraph::ArcMap<ListDigraph::Arc> er(from);
deba@200
    63
deba@200
    64
  ListDigraph::NodeMap<SmartDigraph::Node> ncr(to);
deba@200
    65
  ListDigraph::ArcMap<SmartDigraph::Arc> ecr(to);
deba@200
    66
deba@200
    67
  DigraphCopy<ListDigraph, SmartDigraph>(to, from).
deba@200
    68
    nodeMap(tnm, fnm).arcMap(tam, fam).
deba@200
    69
    nodeRef(nr).arcRef(er).
deba@200
    70
    nodeCrossRef(ncr).arcCrossRef(ecr).
deba@200
    71
    node(tn, fn).arc(ta, fa).run();
deba@200
    72
deba@200
    73
  for (SmartDigraph::NodeIt it(from); it != INVALID; ++it) {
deba@200
    74
    check(ncr[nr[it]] == it, "Wrong copy.");
deba@200
    75
    check(fnm[it] == tnm[nr[it]], "Wrong copy.");
deba@200
    76
  }
deba@200
    77
deba@200
    78
  for (SmartDigraph::ArcIt it(from); it != INVALID; ++it) {
deba@200
    79
    check(ecr[er[it]] == it, "Wrong copy.");
deba@200
    80
    check(fam[it] == tam[er[it]], "Wrong copy.");
deba@200
    81
    check(nr[from.source(it)] == to.source(er[it]), "Wrong copy.");
deba@200
    82
    check(nr[from.target(it)] == to.target(er[it]), "Wrong copy.");
deba@200
    83
  }
deba@200
    84
deba@200
    85
  for (ListDigraph::NodeIt it(to); it != INVALID; ++it) {
deba@200
    86
    check(nr[ncr[it]] == it, "Wrong copy.");
deba@200
    87
  }
deba@200
    88
deba@200
    89
  for (ListDigraph::ArcIt it(to); it != INVALID; ++it) {
deba@200
    90
    check(er[ecr[it]] == it, "Wrong copy.");
deba@200
    91
  }
deba@200
    92
  check(tn == nr[fn], "Wrong copy.");
deba@200
    93
  check(ta == er[fa], "Wrong copy.");
deba@200
    94
}
deba@200
    95
deba@200
    96
void graph_copy_test() {
deba@200
    97
  const int nn = 10;
deba@200
    98
deba@200
    99
  SmartGraph from;
deba@200
   100
  SmartGraph::NodeMap<int> fnm(from);
deba@200
   101
  SmartGraph::ArcMap<int> fam(from);
deba@200
   102
  SmartGraph::EdgeMap<int> fem(from);
deba@200
   103
  SmartGraph::Node fn = INVALID;
deba@200
   104
  SmartGraph::Arc fa = INVALID;
deba@200
   105
  SmartGraph::Edge fe = INVALID;
deba@200
   106
deba@200
   107
  std::vector<SmartGraph::Node> fnv;
deba@200
   108
  for (int i = 0; i < nn; ++i) {
deba@200
   109
    SmartGraph::Node node = from.addNode();
deba@200
   110
    fnv.push_back(node);
deba@200
   111
    fnm[node] = i * i;
deba@200
   112
    if (i == 0) fn = node;
deba@200
   113
  }
deba@200
   114
deba@200
   115
  for (int i = 0; i < nn; ++i) {
deba@200
   116
    for (int j = 0; j < nn; ++j) {
deba@200
   117
      SmartGraph::Edge edge = from.addEdge(fnv[i], fnv[j]);
deba@200
   118
      fem[edge] = i * i + j * j;
deba@200
   119
      fam[from.direct(edge, true)] = i + j * j;
deba@200
   120
      fam[from.direct(edge, false)] = i * i + j;
deba@200
   121
      if (i == 0 && j == 0) fa = from.direct(edge, true);
deba@200
   122
      if (i == 0 && j == 0) fe = edge;
deba@200
   123
    }
deba@200
   124
  }
deba@200
   125
  
deba@200
   126
  ListGraph to;
deba@200
   127
  ListGraph::NodeMap<int> tnm(to);
deba@200
   128
  ListGraph::ArcMap<int> tam(to);
deba@200
   129
  ListGraph::EdgeMap<int> tem(to);
deba@200
   130
  ListGraph::Node tn;
deba@200
   131
  ListGraph::Arc ta;
deba@200
   132
  ListGraph::Edge te;
deba@200
   133
deba@200
   134
  SmartGraph::NodeMap<ListGraph::Node> nr(from);
deba@200
   135
  SmartGraph::ArcMap<ListGraph::Arc> ar(from);
deba@200
   136
  SmartGraph::EdgeMap<ListGraph::Edge> er(from);
deba@200
   137
deba@200
   138
  ListGraph::NodeMap<SmartGraph::Node> ncr(to);
deba@200
   139
  ListGraph::ArcMap<SmartGraph::Arc> acr(to);
deba@200
   140
  ListGraph::EdgeMap<SmartGraph::Edge> ecr(to);
deba@200
   141
deba@200
   142
  GraphCopy<ListGraph, SmartGraph>(to, from).
deba@200
   143
    nodeMap(tnm, fnm).arcMap(tam, fam).edgeMap(tem, fem).
deba@200
   144
    nodeRef(nr).arcRef(ar).edgeRef(er).
deba@200
   145
    nodeCrossRef(ncr).arcCrossRef(acr).edgeCrossRef(ecr).
deba@200
   146
    node(tn, fn).arc(ta, fa).edge(te, fe).run();
deba@200
   147
deba@200
   148
  for (SmartGraph::NodeIt it(from); it != INVALID; ++it) {
deba@200
   149
    check(ncr[nr[it]] == it, "Wrong copy.");
deba@200
   150
    check(fnm[it] == tnm[nr[it]], "Wrong copy.");
deba@200
   151
  }
deba@200
   152
deba@200
   153
  for (SmartGraph::ArcIt it(from); it != INVALID; ++it) {
deba@200
   154
    check(acr[ar[it]] == it, "Wrong copy.");
deba@200
   155
    check(fam[it] == tam[ar[it]], "Wrong copy.");
deba@200
   156
    check(nr[from.source(it)] == to.source(ar[it]), "Wrong copy.");
deba@200
   157
    check(nr[from.target(it)] == to.target(ar[it]), "Wrong copy.");
deba@200
   158
  }
deba@200
   159
deba@200
   160
  for (SmartGraph::EdgeIt it(from); it != INVALID; ++it) {
deba@200
   161
    check(ecr[er[it]] == it, "Wrong copy.");
deba@200
   162
    check(fem[it] == tem[er[it]], "Wrong copy.");
deba@200
   163
    check(nr[from.u(it)] == to.u(er[it]) || nr[from.u(it)] == to.v(er[it]), 
deba@200
   164
	  "Wrong copy.");
deba@200
   165
    check(nr[from.v(it)] == to.u(er[it]) || nr[from.v(it)] == to.v(er[it]), 
deba@200
   166
	  "Wrong copy.");
deba@200
   167
    check((from.u(it) != from.v(it)) == (to.u(er[it]) != to.v(er[it])), 
deba@200
   168
	  "Wrong copy.");
deba@200
   169
  }
deba@200
   170
deba@200
   171
  for (ListGraph::NodeIt it(to); it != INVALID; ++it) {
deba@200
   172
    check(nr[ncr[it]] == it, "Wrong copy.");
deba@200
   173
  }
deba@200
   174
deba@200
   175
  for (ListGraph::ArcIt it(to); it != INVALID; ++it) {
deba@200
   176
    check(ar[acr[it]] == it, "Wrong copy.");
deba@200
   177
  }
deba@200
   178
  for (ListGraph::EdgeIt it(to); it != INVALID; ++it) {
deba@200
   179
    check(er[ecr[it]] == it, "Wrong copy.");
deba@200
   180
  }
deba@200
   181
  check(tn == nr[fn], "Wrong copy.");
deba@200
   182
  check(ta == ar[fa], "Wrong copy.");
deba@200
   183
  check(te == er[fe], "Wrong copy.");
deba@200
   184
}
deba@200
   185
deba@200
   186
deba@200
   187
int main() {
deba@200
   188
  digraph_copy_test();
deba@200
   189
  graph_copy_test();
deba@200
   190
deba@200
   191
  return 0; 
deba@200
   192
}