test/graph_copy_test.cc
author deba
Thu, 11 Jan 2007 21:06:47 +0000
changeset 2338 359f0b71919b
child 2344 48ecc4feb42b
permissions -rw-r--r--
Changing implementation of undirected graphs
slightly faster, 10% speed-up
deba@2290
     1
#include <lemon/smart_graph.h>
deba@2290
     2
#include <lemon/list_graph.h>
deba@2290
     3
#include <lemon/graph_reader.h>
deba@2290
     4
#include <lemon/graph_utils.h>
deba@2290
     5
#include <lemon/error.h>
deba@2290
     6
deba@2290
     7
#include "test_tools.h"
deba@2290
     8
deba@2290
     9
using namespace std;
deba@2290
    10
using namespace lemon;
deba@2290
    11
deba@2290
    12
void graph_copy_test() {
deba@2290
    13
  const int nn = 10;
deba@2290
    14
deba@2290
    15
  SmartGraph source;
deba@2290
    16
  SmartGraph::NodeMap<int> snm(source);
deba@2290
    17
  SmartGraph::EdgeMap<int> sem(source);
deba@2290
    18
  SmartGraph::Node sn = INVALID;
deba@2290
    19
  SmartGraph::Edge se = INVALID;
deba@2290
    20
deba@2290
    21
  std::vector<SmartGraph::Node> snv;
deba@2290
    22
  for (int i = 0; i < nn; ++i) {
deba@2290
    23
    SmartGraph::Node node = source.addNode();
deba@2290
    24
    snv.push_back(node);
deba@2290
    25
    snm[node] = i * i;
deba@2290
    26
    if (i == 0) sn = node;
deba@2290
    27
  }
deba@2290
    28
deba@2290
    29
  for (int i = 0; i < nn; ++i) {
deba@2290
    30
    for (int j = 0; j < nn; ++j) {
deba@2290
    31
      SmartGraph::Edge edge = source.addEdge(snv[i], snv[j]);
deba@2290
    32
      sem[edge] = i + j * j;
deba@2290
    33
      if (i == 0 && j == 0) se = edge;
deba@2290
    34
    }
deba@2290
    35
  }
deba@2290
    36
deba@2290
    37
  ListGraph target;
deba@2290
    38
  ListGraph::NodeMap<int> tnm(target);
deba@2290
    39
  ListGraph::EdgeMap<int> tem(target);
deba@2290
    40
  ListGraph::Node tn;
deba@2290
    41
  ListGraph::Edge te;
deba@2290
    42
deba@2290
    43
  SmartGraph::NodeMap<ListGraph::Node> nr(source);
deba@2290
    44
  SmartGraph::EdgeMap<ListGraph::Edge> er(source);
deba@2290
    45
deba@2290
    46
  ListGraph::NodeMap<SmartGraph::Node> ncr(target);
deba@2290
    47
  ListGraph::EdgeMap<SmartGraph::Edge> ecr(target);
deba@2290
    48
deba@2290
    49
  GraphCopy<ListGraph, SmartGraph>(target, source).
deba@2290
    50
    nodeMap(tnm, snm).edgeMap(tem, sem).
deba@2290
    51
    nodeRef(nr).edgeRef(er).
deba@2290
    52
    nodeCrossRef(ncr).edgeCrossRef(ecr).
deba@2290
    53
    node(tn, sn).edge(te, se).run();
deba@2290
    54
deba@2290
    55
  for (SmartGraph::NodeIt it(source); it != INVALID; ++it) {
deba@2290
    56
    check(ncr[nr[it]] == it, "Wrong copy.");
deba@2290
    57
    check(snm[it] == tnm[nr[it]], "Wrong copy.");
deba@2290
    58
  }
deba@2290
    59
deba@2290
    60
  for (SmartGraph::EdgeIt it(source); it != INVALID; ++it) {
deba@2290
    61
    check(ecr[er[it]] == it, "Wrong copy.");
deba@2290
    62
    check(sem[it] == tem[er[it]], "Wrong copy.");
deba@2290
    63
    check(nr[source.source(it)] == 
deba@2290
    64
                 target.source(er[it]), "Wrong copy.");
deba@2290
    65
    check(nr[source.target(it)] == 
deba@2290
    66
                 target.target(er[it]), "Wrong copy.");
deba@2290
    67
  }
deba@2290
    68
deba@2290
    69
  for (ListGraph::NodeIt it(target); it != INVALID; ++it) {
deba@2290
    70
    check(nr[ncr[it]] == it, "Wrong copy.");
deba@2290
    71
  }
deba@2290
    72
deba@2290
    73
  for (ListGraph::EdgeIt it(target); it != INVALID; ++it) {
deba@2290
    74
    check(er[ecr[it]] == it, "Wrong copy.");
deba@2290
    75
  }
deba@2290
    76
  check(tn == nr[sn], "Wrong copy.");
deba@2290
    77
  check(te == er[se], "Wrong copy.");
deba@2290
    78
}
deba@2290
    79
deba@2290
    80
void ugraph_copy_test() {
deba@2290
    81
  const int nn = 10;
deba@2290
    82
deba@2290
    83
  SmartUGraph source;
deba@2290
    84
  SmartUGraph::NodeMap<int> snm(source);
deba@2290
    85
  SmartUGraph::EdgeMap<int> sem(source);
deba@2290
    86
  SmartUGraph::UEdgeMap<int> suem(source);
deba@2290
    87
  SmartUGraph::Node sn = INVALID;
deba@2290
    88
  SmartUGraph::Edge se = INVALID;
deba@2290
    89
  SmartUGraph::UEdge sue = INVALID;
deba@2290
    90
deba@2290
    91
  std::vector<SmartGraph::Node> snv;
deba@2290
    92
  for (int i = 0; i < nn; ++i) {
deba@2290
    93
    SmartUGraph::Node node = source.addNode();
deba@2290
    94
    snv.push_back(node);
deba@2290
    95
    snm[node] = i * i;
deba@2290
    96
    if (i == 0) sn = node;
deba@2290
    97
  }
deba@2290
    98
deba@2290
    99
  for (int i = 0; i < nn; ++i) {
deba@2290
   100
    for (int j = 0; j < nn; ++j) {
deba@2290
   101
      SmartUGraph::UEdge edge = source.addEdge(snv[i], snv[j]);
deba@2290
   102
      suem[edge] = i * i + j * j;
deba@2290
   103
      sem[source.direct(edge, true)] = i + j * j;
deba@2290
   104
      sem[source.direct(edge, false)] = i * i + j;
deba@2290
   105
      if (i == 0 && j == 0) se = source.direct(edge, true);
deba@2290
   106
      if (i == 0 && j == 0) sue = edge;
deba@2290
   107
    }
deba@2290
   108
  }
deba@2290
   109
  
deba@2290
   110
  ListUGraph target;
deba@2290
   111
  ListUGraph::NodeMap<int> tnm(target);
deba@2290
   112
  ListUGraph::EdgeMap<int> tem(target);
deba@2290
   113
  ListUGraph::UEdgeMap<int> tuem(target);
deba@2290
   114
  ListUGraph::Node tn;
deba@2290
   115
  ListUGraph::Edge te;
deba@2290
   116
  ListUGraph::UEdge tue;
deba@2290
   117
deba@2290
   118
  SmartUGraph::NodeMap<ListUGraph::Node> nr(source);
deba@2290
   119
  SmartUGraph::EdgeMap<ListUGraph::Edge> er(source);
deba@2290
   120
  SmartUGraph::UEdgeMap<ListUGraph::UEdge> uer(source);
deba@2290
   121
deba@2290
   122
  ListUGraph::NodeMap<SmartUGraph::Node> ncr(target);
deba@2290
   123
  ListUGraph::EdgeMap<SmartUGraph::Edge> ecr(target);
deba@2290
   124
  ListUGraph::UEdgeMap<SmartUGraph::UEdge> uecr(target);
deba@2290
   125
deba@2290
   126
  UGraphCopy<ListUGraph, SmartUGraph>(target, source).
deba@2290
   127
    nodeMap(tnm, snm).edgeMap(tem, sem).uEdgeMap(tuem, suem).
deba@2290
   128
    nodeRef(nr).edgeRef(er).uEdgeRef(uer).
deba@2290
   129
    nodeCrossRef(ncr).edgeCrossRef(ecr).uEdgeCrossRef(uecr).
deba@2290
   130
    node(tn, sn).edge(te, se).uEdge(tue, sue).run();
deba@2290
   131
deba@2290
   132
  for (SmartUGraph::NodeIt it(source); it != INVALID; ++it) {
deba@2290
   133
    check(ncr[nr[it]] == it, "Wrong copy.");
deba@2290
   134
    check(snm[it] == tnm[nr[it]], "Wrong copy.");
deba@2290
   135
  }
deba@2290
   136
deba@2290
   137
  for (SmartUGraph::EdgeIt it(source); it != INVALID; ++it) {
deba@2290
   138
    check(ecr[er[it]] == it, "Wrong copy.");
deba@2290
   139
    check(sem[it] == tem[er[it]], "Wrong copy.");
deba@2290
   140
    check(nr[source.source(it)] == 
deba@2290
   141
                 target.source(er[it]), "Wrong copy.");
deba@2290
   142
    check(nr[source.target(it)] == 
deba@2290
   143
                 target.target(er[it]), "Wrong copy.");
deba@2290
   144
  }
deba@2290
   145
deba@2290
   146
  for (SmartUGraph::UEdgeIt it(source); it != INVALID; ++it) {
deba@2290
   147
    check(uecr[uer[it]] == it, "Wrong copy.");
deba@2290
   148
    check(suem[it] == tuem[uer[it]], "Wrong copy.");
deba@2290
   149
    check(nr[source.source(it)] == target.source(uer[it]) ||
deba@2290
   150
                 nr[source.source(it)] == target.target(uer[it]), 
deba@2290
   151
                 "Wrong copy.");
deba@2290
   152
    check(nr[source.target(it)] == target.source(uer[it]) ||
deba@2290
   153
                 nr[source.target(it)] == target.target(uer[it]), 
deba@2290
   154
                 "Wrong copy.");
deba@2290
   155
    check((source.source(it) != source.target(it)) ==
deba@2290
   156
                 (target.source(uer[it]) != target.target(uer[it])), 
deba@2290
   157
                 "Wrong copy.");
deba@2290
   158
  }
deba@2290
   159
deba@2290
   160
  for (ListUGraph::NodeIt it(target); it != INVALID; ++it) {
deba@2290
   161
    check(nr[ncr[it]] == it, "Wrong copy.");
deba@2290
   162
  }
deba@2290
   163
deba@2290
   164
  for (ListUGraph::EdgeIt it(target); it != INVALID; ++it) {
deba@2290
   165
    check(er[ecr[it]] == it, "Wrong copy.");
deba@2290
   166
  }
deba@2290
   167
  for (ListUGraph::UEdgeIt it(target); it != INVALID; ++it) {
deba@2290
   168
    check(uer[uecr[it]] == it, "Wrong copy.");
deba@2290
   169
  }
deba@2290
   170
  check(tn == nr[sn], "Wrong copy.");
deba@2290
   171
  check(te == er[se], "Wrong copy.");
deba@2290
   172
  check(tue == uer[sue], "Wrong copy.");
deba@2290
   173
}
deba@2290
   174
deba@2290
   175
void bpugraph_copy_test() {
deba@2290
   176
  const int nn = 10;
deba@2290
   177
deba@2290
   178
  SmartBpUGraph source;
deba@2290
   179
  SmartBpUGraph::ANodeMap<int> sanm(source);
deba@2290
   180
  SmartBpUGraph::BNodeMap<int> sbnm(source);
deba@2290
   181
  SmartBpUGraph::NodeMap<int> snm(source);
deba@2290
   182
  SmartBpUGraph::EdgeMap<int> sem(source);
deba@2290
   183
  SmartBpUGraph::UEdgeMap<int> suem(source);
deba@2290
   184
  SmartBpUGraph::Node sn = INVALID;
deba@2290
   185
  SmartBpUGraph::Edge se = INVALID;
deba@2290
   186
  SmartBpUGraph::UEdge sue = INVALID;
deba@2290
   187
deba@2290
   188
  std::vector<SmartBpUGraph::Node> sanv;
deba@2290
   189
  for (int i = 0; i < nn; ++i) {
deba@2290
   190
    SmartBpUGraph::Node node = source.addANode();
deba@2290
   191
    sanv.push_back(node);
deba@2290
   192
    sanm[node] = i * i;
deba@2290
   193
    snm[node] = i * i + i;
deba@2290
   194
    if (i == 0) sn = node;
deba@2290
   195
  }
deba@2290
   196
deba@2290
   197
  std::vector<SmartBpUGraph::Node> sbnv;
deba@2290
   198
  for (int i = 0; i < nn; ++i) {
deba@2290
   199
    SmartBpUGraph::Node node = source.addBNode();
deba@2290
   200
    sbnv.push_back(node);
deba@2290
   201
    sbnm[node] = i * i - i;
deba@2290
   202
    snm[node] = i * i + i + 1;
deba@2290
   203
  }
deba@2290
   204
deba@2290
   205
  for (int i = 0; i < nn; ++i) {
deba@2290
   206
    for (int j = 0; j < nn; ++j) {
deba@2290
   207
      SmartBpUGraph::UEdge edge = source.addEdge(sanv[i], sbnv[j]);
deba@2290
   208
      suem[edge] = i * i + j * j;
deba@2290
   209
      sem[source.direct(edge, true)] = i + j * j;
deba@2290
   210
      sem[source.direct(edge, false)] = i * i + j;
deba@2290
   211
      if (i == 0 && j == 0) se = source.direct(edge, true);
deba@2290
   212
      if (i == 0 && j == 0) sue = edge;
deba@2290
   213
    }
deba@2290
   214
  }
deba@2290
   215
  
deba@2290
   216
  ListBpUGraph target;
deba@2290
   217
  ListBpUGraph::ANodeMap<int> tanm(target);
deba@2290
   218
  ListBpUGraph::BNodeMap<int> tbnm(target);
deba@2290
   219
  ListBpUGraph::NodeMap<int> tnm(target);
deba@2290
   220
  ListBpUGraph::EdgeMap<int> tem(target);
deba@2290
   221
  ListBpUGraph::UEdgeMap<int> tuem(target);
deba@2290
   222
  ListBpUGraph::Node tn;
deba@2290
   223
  ListBpUGraph::Edge te;
deba@2290
   224
  ListBpUGraph::UEdge tue;
deba@2290
   225
deba@2290
   226
  SmartBpUGraph::ANodeMap<ListBpUGraph::Node> anr(source);
deba@2290
   227
  SmartBpUGraph::BNodeMap<ListBpUGraph::Node> bnr(source);
deba@2290
   228
  SmartBpUGraph::NodeMap<ListBpUGraph::Node> nr(source);
deba@2290
   229
  SmartBpUGraph::EdgeMap<ListBpUGraph::Edge> er(source);
deba@2290
   230
  SmartBpUGraph::UEdgeMap<ListBpUGraph::UEdge> uer(source);
deba@2290
   231
deba@2290
   232
  ListBpUGraph::ANodeMap<SmartBpUGraph::Node> ancr(target);
deba@2290
   233
  ListBpUGraph::BNodeMap<SmartBpUGraph::Node> bncr(target);
deba@2290
   234
  ListBpUGraph::NodeMap<SmartBpUGraph::Node> ncr(target);
deba@2290
   235
  ListBpUGraph::EdgeMap<SmartBpUGraph::Edge> ecr(target);
deba@2290
   236
  ListBpUGraph::UEdgeMap<SmartBpUGraph::UEdge> uecr(target);
deba@2290
   237
deba@2290
   238
  BpUGraphCopy<ListBpUGraph, SmartBpUGraph>(target, source).
deba@2290
   239
    aNodeMap(tanm, sanm).bNodeMap(tbnm, sbnm).nodeMap(tnm, snm).
deba@2290
   240
    edgeMap(tem, sem).uEdgeMap(tuem, suem).
deba@2290
   241
    aNodeRef(anr).bNodeRef(bnr).nodeRef(nr).edgeRef(er).uEdgeRef(uer).
deba@2290
   242
    aNodeCrossRef(ancr).bNodeCrossRef(bncr).nodeCrossRef(ncr).
deba@2290
   243
    edgeCrossRef(ecr).uEdgeCrossRef(uecr).
deba@2290
   244
    node(tn, sn).edge(te, se).uEdge(tue, sue).run();
deba@2290
   245
deba@2290
   246
  for (SmartBpUGraph::ANodeIt it(source); it != INVALID; ++it) {
deba@2290
   247
    check(nr[it] == anr[it], "Wrong copy.");
deba@2290
   248
    check(ancr[anr[it]] == it, "Wrong copy.");
deba@2290
   249
    check(sanm[it] == tanm[anr[it]], "Wrong copy.");
deba@2290
   250
  }
deba@2290
   251
deba@2290
   252
  for (SmartBpUGraph::BNodeIt it(source); it != INVALID; ++it) {
deba@2290
   253
    check(nr[it] == bnr[it], "Wrong copy.");
deba@2290
   254
    check(bncr[bnr[it]] == it, "Wrong copy.");
deba@2290
   255
    check(sbnm[it] == tbnm[bnr[it]], "Wrong copy.");
deba@2290
   256
  }
deba@2290
   257
deba@2290
   258
  for (SmartBpUGraph::NodeIt it(source); it != INVALID; ++it) {
deba@2290
   259
    check(ncr[nr[it]] == it, "Wrong copy.");
deba@2290
   260
    check(snm[it] == tnm[nr[it]], "Wrong copy.");
deba@2290
   261
  }
deba@2290
   262
deba@2290
   263
  for (SmartBpUGraph::EdgeIt it(source); it != INVALID; ++it) {
deba@2290
   264
    check(ecr[er[it]] == it, "Wrong copy.");
deba@2290
   265
    check(sem[it] == tem[er[it]], "Wrong copy.");
deba@2290
   266
    check(nr[source.source(it)] == 
deba@2290
   267
                 target.source(er[it]), "Wrong copy.");
deba@2290
   268
    check(nr[source.target(it)] == 
deba@2290
   269
                 target.target(er[it]), "Wrong copy.");
deba@2290
   270
  }
deba@2290
   271
deba@2290
   272
  for (SmartBpUGraph::UEdgeIt it(source); it != INVALID; ++it) {
deba@2290
   273
    check(uecr[uer[it]] == it, "Wrong copy.");
deba@2290
   274
    check(suem[it] == tuem[uer[it]], "Wrong copy.");
deba@2290
   275
    check(nr[source.aNode(it)] == 
deba@2290
   276
                 target.aNode(uer[it]), "Wrong copy.");
deba@2290
   277
    check(nr[source.bNode(it)] == 
deba@2290
   278
                 target.bNode(uer[it]), "Wrong copy.");
deba@2290
   279
  }
deba@2290
   280
deba@2290
   281
  for (ListBpUGraph::ANodeIt it(target); it != INVALID; ++it) {
deba@2290
   282
    check(ancr[it] == ncr[it], "Wrong copy.");
deba@2290
   283
    check(anr[ancr[it]] == it, "Wrong copy.");
deba@2290
   284
  }
deba@2290
   285
deba@2290
   286
  for (ListBpUGraph::BNodeIt it(target); it != INVALID; ++it) {
deba@2290
   287
    check(bncr[it] == ncr[it], "Wrong copy.");
deba@2290
   288
    check(bnr[bncr[it]] == it, "Wrong copy.");
deba@2290
   289
  }
deba@2290
   290
deba@2290
   291
  for (ListBpUGraph::NodeIt it(target); it != INVALID; ++it) {
deba@2290
   292
    check(nr[ncr[it]] == it, "Wrong copy.");
deba@2290
   293
  }
deba@2290
   294
deba@2290
   295
  for (ListBpUGraph::EdgeIt it(target); it != INVALID; ++it) {
deba@2290
   296
    check(er[ecr[it]] == it, "Wrong copy.");
deba@2290
   297
  }
deba@2290
   298
  for (ListBpUGraph::UEdgeIt it(target); it != INVALID; ++it) {
deba@2290
   299
    check(uer[uecr[it]] == it, "Wrong copy.");
deba@2290
   300
  }
deba@2290
   301
  check(tn == nr[sn], "Wrong copy.");
deba@2290
   302
  check(te == er[se], "Wrong copy.");
deba@2290
   303
  check(tue == uer[sue], "Wrong copy.");
deba@2290
   304
}
deba@2290
   305
deba@2290
   306
int main() {
deba@2290
   307
  graph_copy_test();
deba@2290
   308
  ugraph_copy_test();
deba@2290
   309
  bpugraph_copy_test();
deba@2290
   310
deba@2290
   311
  return 0; 
deba@2290
   312
}