test/digraph_test.cc
author Alpar Juttner <alpar@cs.elte.hu>
Wed, 22 Sep 2010 09:24:07 +0200
branch1.0
changeset 1008 b7ed95cd94a3
parent 228 b6732e0d38c5
child 388 2d87dbd7f8c8
permissions -rw-r--r--
Merge bugfix #392 to branch 1.0
alpar@209
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
deba@57
     2
 *
alpar@209
     3
 * This file is a part of LEMON, a generic C++ optimization library.
deba@57
     4
 *
alpar@107
     5
 * Copyright (C) 2003-2008
deba@57
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@57
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@57
     8
 *
deba@57
     9
 * Permission to use, modify and distribute this software is granted
deba@57
    10
 * provided that this copyright notice appears in all copies. For
deba@57
    11
 * precise terms see the accompanying LICENSE file.
deba@57
    12
 *
deba@57
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@57
    14
 * express or implied, and with no claim as to its suitability for any
deba@57
    15
 * purpose.
deba@57
    16
 *
deba@57
    17
 */
deba@57
    18
deba@57
    19
#include <lemon/concepts/digraph.h>
deba@57
    20
#include <lemon/list_graph.h>
kpeter@171
    21
#include <lemon/smart_graph.h>
deba@57
    22
//#include <lemon/full_graph.h>
deba@57
    23
//#include <lemon/hypercube_graph.h>
deba@57
    24
deba@57
    25
#include "test_tools.h"
kpeter@171
    26
#include "graph_test.h"
deba@57
    27
deba@57
    28
using namespace lemon;
deba@57
    29
using namespace lemon::concepts;
deba@57
    30
deba@228
    31
template <class Digraph>
kpeter@387
    32
void checkDigraphBuild() {
deba@228
    33
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
deba@228
    34
  Digraph G;
deba@228
    35
deba@228
    36
  checkGraphNodeList(G, 0);
deba@228
    37
  checkGraphArcList(G, 0);
deba@228
    38
deba@228
    39
  Node
deba@228
    40
    n1 = G.addNode(),
deba@228
    41
    n2 = G.addNode(),
deba@228
    42
    n3 = G.addNode();
deba@228
    43
  checkGraphNodeList(G, 3);
deba@228
    44
  checkGraphArcList(G, 0);
deba@228
    45
deba@228
    46
  Arc a1 = G.addArc(n1, n2);
deba@228
    47
  check(G.source(a1) == n1 && G.target(a1) == n2, "Wrong arc");
deba@228
    48
  checkGraphNodeList(G, 3);
deba@228
    49
  checkGraphArcList(G, 1);
deba@228
    50
deba@228
    51
  checkGraphOutArcList(G, n1, 1);
deba@228
    52
  checkGraphOutArcList(G, n2, 0);
deba@228
    53
  checkGraphOutArcList(G, n3, 0);
deba@228
    54
deba@228
    55
  checkGraphInArcList(G, n1, 0);
deba@228
    56
  checkGraphInArcList(G, n2, 1);
deba@228
    57
  checkGraphInArcList(G, n3, 0);
deba@228
    58
deba@228
    59
  checkGraphConArcList(G, 1);
deba@228
    60
kpeter@387
    61
  Arc a2 = G.addArc(n2, n1),
kpeter@387
    62
      a3 = G.addArc(n2, n3),
kpeter@387
    63
      a4 = G.addArc(n2, n3);
kpeter@387
    64
kpeter@387
    65
  checkGraphNodeList(G, 3);
kpeter@387
    66
  checkGraphArcList(G, 4);
kpeter@387
    67
kpeter@387
    68
  checkGraphOutArcList(G, n1, 1);
kpeter@387
    69
  checkGraphOutArcList(G, n2, 3);
kpeter@387
    70
  checkGraphOutArcList(G, n3, 0);
kpeter@387
    71
kpeter@387
    72
  checkGraphInArcList(G, n1, 1);
kpeter@387
    73
  checkGraphInArcList(G, n2, 1);
kpeter@387
    74
  checkGraphInArcList(G, n3, 2);
kpeter@387
    75
kpeter@387
    76
  checkGraphConArcList(G, 4);
kpeter@387
    77
kpeter@387
    78
  checkNodeIds(G);
kpeter@387
    79
  checkArcIds(G);
kpeter@387
    80
  checkGraphNodeMap(G);
kpeter@387
    81
  checkGraphArcMap(G);
kpeter@387
    82
}
kpeter@387
    83
kpeter@387
    84
template <class Digraph>
kpeter@387
    85
void checkDigraphSplit() {
kpeter@387
    86
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
kpeter@387
    87
kpeter@387
    88
  Digraph G;
kpeter@387
    89
  Node n1 = G.addNode(), n2 = G.addNode(), n3 = G.addNode();
kpeter@387
    90
  Arc a1 = G.addArc(n1, n2), a2 = G.addArc(n2, n1),
kpeter@387
    91
      a3 = G.addArc(n2, n3), a4 = G.addArc(n2, n3);
kpeter@387
    92
kpeter@387
    93
  Node n4 = G.split(n2);
kpeter@387
    94
kpeter@387
    95
  check(G.target(OutArcIt(G, n2)) == n4 &&
kpeter@387
    96
        G.source(InArcIt(G, n4)) == n2,
kpeter@387
    97
        "Wrong split.");
kpeter@387
    98
kpeter@387
    99
  checkGraphNodeList(G, 4);
kpeter@387
   100
  checkGraphArcList(G, 5);
kpeter@387
   101
kpeter@387
   102
  checkGraphOutArcList(G, n1, 1);
kpeter@387
   103
  checkGraphOutArcList(G, n2, 1);
kpeter@387
   104
  checkGraphOutArcList(G, n3, 0);
kpeter@387
   105
  checkGraphOutArcList(G, n4, 3);
kpeter@387
   106
kpeter@387
   107
  checkGraphInArcList(G, n1, 1);
kpeter@387
   108
  checkGraphInArcList(G, n2, 1);
kpeter@387
   109
  checkGraphInArcList(G, n3, 2);
kpeter@387
   110
  checkGraphInArcList(G, n4, 1);
kpeter@387
   111
kpeter@387
   112
  checkGraphConArcList(G, 5);
kpeter@387
   113
}
kpeter@387
   114
kpeter@387
   115
template <class Digraph>
kpeter@387
   116
void checkDigraphAlter() {
kpeter@387
   117
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
kpeter@387
   118
kpeter@387
   119
  Digraph G;
kpeter@387
   120
  Node n1 = G.addNode(), n2 = G.addNode(),
kpeter@387
   121
       n3 = G.addNode(), n4 = G.addNode();
kpeter@387
   122
  Arc a1 = G.addArc(n1, n2), a2 = G.addArc(n4, n1),
kpeter@387
   123
      a3 = G.addArc(n4, n3), a4 = G.addArc(n4, n3),
kpeter@387
   124
      a5 = G.addArc(n2, n4);
kpeter@387
   125
kpeter@387
   126
  checkGraphNodeList(G, 4);
kpeter@387
   127
  checkGraphArcList(G, 5);
kpeter@387
   128
kpeter@387
   129
  // Check changeSource() and changeTarget()
kpeter@387
   130
  G.changeTarget(a4, n1);
kpeter@387
   131
kpeter@387
   132
  checkGraphNodeList(G, 4);
kpeter@387
   133
  checkGraphArcList(G, 5);
kpeter@387
   134
kpeter@387
   135
  checkGraphOutArcList(G, n1, 1);
kpeter@387
   136
  checkGraphOutArcList(G, n2, 1);
kpeter@387
   137
  checkGraphOutArcList(G, n3, 0);
kpeter@387
   138
  checkGraphOutArcList(G, n4, 3);
kpeter@387
   139
kpeter@387
   140
  checkGraphInArcList(G, n1, 2);
kpeter@387
   141
  checkGraphInArcList(G, n2, 1);
kpeter@387
   142
  checkGraphInArcList(G, n3, 1);
kpeter@387
   143
  checkGraphInArcList(G, n4, 1);
kpeter@387
   144
kpeter@387
   145
  checkGraphConArcList(G, 5);
kpeter@387
   146
kpeter@387
   147
  G.changeSource(a4, n3);
kpeter@387
   148
kpeter@387
   149
  checkGraphNodeList(G, 4);
kpeter@387
   150
  checkGraphArcList(G, 5);
kpeter@387
   151
kpeter@387
   152
  checkGraphOutArcList(G, n1, 1);
kpeter@387
   153
  checkGraphOutArcList(G, n2, 1);
kpeter@387
   154
  checkGraphOutArcList(G, n3, 1);
kpeter@387
   155
  checkGraphOutArcList(G, n4, 2);
kpeter@387
   156
kpeter@387
   157
  checkGraphInArcList(G, n1, 2);
kpeter@387
   158
  checkGraphInArcList(G, n2, 1);
kpeter@387
   159
  checkGraphInArcList(G, n3, 1);
kpeter@387
   160
  checkGraphInArcList(G, n4, 1);
kpeter@387
   161
kpeter@387
   162
  checkGraphConArcList(G, 5);
kpeter@387
   163
kpeter@387
   164
  // Check contract()
kpeter@387
   165
  G.contract(n2, n4, false);
kpeter@387
   166
kpeter@387
   167
  checkGraphNodeList(G, 3);
kpeter@387
   168
  checkGraphArcList(G, 5);
kpeter@387
   169
kpeter@387
   170
  checkGraphOutArcList(G, n1, 1);
kpeter@387
   171
  checkGraphOutArcList(G, n2, 3);
kpeter@387
   172
  checkGraphOutArcList(G, n3, 1);
kpeter@387
   173
kpeter@387
   174
  checkGraphInArcList(G, n1, 2);
kpeter@387
   175
  checkGraphInArcList(G, n2, 2);
kpeter@387
   176
  checkGraphInArcList(G, n3, 1);
kpeter@387
   177
kpeter@387
   178
  checkGraphConArcList(G, 5);
kpeter@387
   179
kpeter@387
   180
  G.contract(n2, n1);
kpeter@387
   181
kpeter@387
   182
  checkGraphNodeList(G, 2);
kpeter@387
   183
  checkGraphArcList(G, 3);
kpeter@387
   184
kpeter@387
   185
  checkGraphOutArcList(G, n2, 2);
kpeter@387
   186
  checkGraphOutArcList(G, n3, 1);
kpeter@387
   187
kpeter@387
   188
  checkGraphInArcList(G, n2, 2);
kpeter@387
   189
  checkGraphInArcList(G, n3, 1);
kpeter@387
   190
kpeter@387
   191
  checkGraphConArcList(G, 3);
kpeter@387
   192
}
kpeter@387
   193
kpeter@387
   194
template <class Digraph>
kpeter@387
   195
void checkDigraphErase() {
kpeter@387
   196
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
kpeter@387
   197
kpeter@387
   198
  Digraph G;
kpeter@387
   199
  Node n1 = G.addNode(), n2 = G.addNode(),
kpeter@387
   200
       n3 = G.addNode(), n4 = G.addNode();
kpeter@387
   201
  Arc a1 = G.addArc(n1, n2), a2 = G.addArc(n4, n1),
kpeter@387
   202
      a3 = G.addArc(n4, n3), a4 = G.addArc(n3, n1),
kpeter@387
   203
      a5 = G.addArc(n2, n4);
kpeter@387
   204
kpeter@387
   205
  // Check arc deletion
kpeter@387
   206
  G.erase(a1);
kpeter@387
   207
kpeter@387
   208
  checkGraphNodeList(G, 4);
kpeter@387
   209
  checkGraphArcList(G, 4);
kpeter@387
   210
kpeter@387
   211
  checkGraphOutArcList(G, n1, 0);
kpeter@387
   212
  checkGraphOutArcList(G, n2, 1);
kpeter@387
   213
  checkGraphOutArcList(G, n3, 1);
kpeter@387
   214
  checkGraphOutArcList(G, n4, 2);
kpeter@387
   215
kpeter@387
   216
  checkGraphInArcList(G, n1, 2);
kpeter@387
   217
  checkGraphInArcList(G, n2, 0);
kpeter@387
   218
  checkGraphInArcList(G, n3, 1);
kpeter@387
   219
  checkGraphInArcList(G, n4, 1);
kpeter@387
   220
kpeter@387
   221
  checkGraphConArcList(G, 4);
kpeter@387
   222
kpeter@387
   223
  // Check node deletion
kpeter@387
   224
  G.erase(n4);
kpeter@387
   225
kpeter@387
   226
  checkGraphNodeList(G, 3);
kpeter@387
   227
  checkGraphArcList(G, 1);
kpeter@387
   228
kpeter@387
   229
  checkGraphOutArcList(G, n1, 0);
kpeter@387
   230
  checkGraphOutArcList(G, n2, 0);
kpeter@387
   231
  checkGraphOutArcList(G, n3, 1);
kpeter@387
   232
  checkGraphOutArcList(G, n4, 0);
kpeter@387
   233
kpeter@387
   234
  checkGraphInArcList(G, n1, 1);
kpeter@387
   235
  checkGraphInArcList(G, n2, 0);
kpeter@387
   236
  checkGraphInArcList(G, n3, 0);
kpeter@387
   237
  checkGraphInArcList(G, n4, 0);
kpeter@387
   238
kpeter@387
   239
  checkGraphConArcList(G, 1);
kpeter@387
   240
}
kpeter@387
   241
kpeter@387
   242
kpeter@387
   243
template <class Digraph>
kpeter@387
   244
void checkDigraphSnapshot() {
kpeter@387
   245
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
kpeter@387
   246
kpeter@387
   247
  Digraph G;
kpeter@387
   248
  Node n1 = G.addNode(), n2 = G.addNode(), n3 = G.addNode();
kpeter@387
   249
  Arc a1 = G.addArc(n1, n2), a2 = G.addArc(n2, n1),
kpeter@387
   250
      a3 = G.addArc(n2, n3), a4 = G.addArc(n2, n3);
kpeter@387
   251
kpeter@387
   252
  typename Digraph::Snapshot snapshot(G);
kpeter@387
   253
kpeter@387
   254
  Node n = G.addNode();
kpeter@387
   255
  G.addArc(n3, n);
kpeter@387
   256
  G.addArc(n, n3);
kpeter@387
   257
kpeter@387
   258
  checkGraphNodeList(G, 4);
kpeter@387
   259
  checkGraphArcList(G, 6);
kpeter@387
   260
kpeter@387
   261
  snapshot.restore();
kpeter@387
   262
deba@228
   263
  checkGraphNodeList(G, 3);
deba@228
   264
  checkGraphArcList(G, 4);
deba@228
   265
deba@228
   266
  checkGraphOutArcList(G, n1, 1);
deba@228
   267
  checkGraphOutArcList(G, n2, 3);
deba@228
   268
  checkGraphOutArcList(G, n3, 0);
deba@228
   269
deba@228
   270
  checkGraphInArcList(G, n1, 1);
deba@228
   271
  checkGraphInArcList(G, n2, 1);
deba@228
   272
  checkGraphInArcList(G, n3, 2);
deba@228
   273
deba@228
   274
  checkGraphConArcList(G, 4);
deba@228
   275
deba@228
   276
  checkNodeIds(G);
deba@228
   277
  checkArcIds(G);
deba@228
   278
  checkGraphNodeMap(G);
deba@228
   279
  checkGraphArcMap(G);
deba@228
   280
kpeter@387
   281
  G.addNode();
kpeter@387
   282
  snapshot.save(G);
kpeter@387
   283
kpeter@387
   284
  G.addArc(G.addNode(), G.addNode());
kpeter@387
   285
kpeter@387
   286
  snapshot.restore();
kpeter@387
   287
kpeter@387
   288
  checkGraphNodeList(G, 4);
kpeter@387
   289
  checkGraphArcList(G, 4);
deba@228
   290
}
deba@228
   291
deba@228
   292
void checkConcepts() {
kpeter@171
   293
  { // Checking digraph components
deba@57
   294
    checkConcept<BaseDigraphComponent, BaseDigraphComponent >();
deba@57
   295
alpar@209
   296
    checkConcept<IDableDigraphComponent<>,
deba@57
   297
      IDableDigraphComponent<> >();
deba@57
   298
alpar@209
   299
    checkConcept<IterableDigraphComponent<>,
deba@57
   300
      IterableDigraphComponent<> >();
deba@57
   301
alpar@209
   302
    checkConcept<MappableDigraphComponent<>,
deba@57
   303
      MappableDigraphComponent<> >();
deba@57
   304
  }
kpeter@171
   305
  { // Checking skeleton digraph
deba@57
   306
    checkConcept<Digraph, Digraph>();
deba@57
   307
  }
kpeter@171
   308
  { // Checking ListDigraph
kpeter@171
   309
    checkConcept<Digraph, ListDigraph>();
deba@57
   310
    checkConcept<AlterableDigraphComponent<>, ListDigraph>();
deba@57
   311
    checkConcept<ExtendableDigraphComponent<>, ListDigraph>();
deba@57
   312
    checkConcept<ClearableDigraphComponent<>, ListDigraph>();
deba@57
   313
    checkConcept<ErasableDigraphComponent<>, ListDigraph>();
kpeter@171
   314
  }
kpeter@171
   315
  { // Checking SmartDigraph
kpeter@171
   316
    checkConcept<Digraph, SmartDigraph>();
kpeter@171
   317
    checkConcept<AlterableDigraphComponent<>, SmartDigraph>();
kpeter@171
   318
    checkConcept<ExtendableDigraphComponent<>, SmartDigraph>();
kpeter@171
   319
    checkConcept<ClearableDigraphComponent<>, SmartDigraph>();
kpeter@171
   320
  }
kpeter@171
   321
//  { // Checking FullDigraph
kpeter@171
   322
//    checkConcept<Digraph, FullDigraph>();
kpeter@171
   323
//  }
kpeter@171
   324
//  { // Checking HyperCubeDigraph
kpeter@171
   325
//    checkConcept<Digraph, HyperCubeDigraph>();
kpeter@171
   326
//  }
kpeter@171
   327
}
deba@57
   328
kpeter@171
   329
template <typename Digraph>
deba@228
   330
void checkDigraphValidity() {
kpeter@171
   331
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
kpeter@171
   332
  Digraph g;
kpeter@171
   333
kpeter@171
   334
  Node
kpeter@171
   335
    n1 = g.addNode(),
kpeter@171
   336
    n2 = g.addNode(),
kpeter@171
   337
    n3 = g.addNode();
kpeter@171
   338
kpeter@171
   339
  Arc
kpeter@171
   340
    e1 = g.addArc(n1, n2),
kpeter@171
   341
    e2 = g.addArc(n2, n3);
kpeter@171
   342
kpeter@171
   343
  check(g.valid(n1), "Wrong validity check");
kpeter@171
   344
  check(g.valid(e1), "Wrong validity check");
kpeter@171
   345
kpeter@171
   346
  check(!g.valid(g.nodeFromId(-1)), "Wrong validity check");
kpeter@171
   347
  check(!g.valid(g.arcFromId(-1)), "Wrong validity check");
kpeter@171
   348
}
kpeter@171
   349
kpeter@171
   350
template <typename Digraph>
deba@228
   351
void checkDigraphValidityErase() {
kpeter@171
   352
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
kpeter@171
   353
  Digraph g;
kpeter@171
   354
kpeter@171
   355
  Node
kpeter@171
   356
    n1 = g.addNode(),
kpeter@171
   357
    n2 = g.addNode(),
kpeter@171
   358
    n3 = g.addNode();
kpeter@171
   359
kpeter@171
   360
  Arc
kpeter@171
   361
    e1 = g.addArc(n1, n2),
kpeter@171
   362
    e2 = g.addArc(n2, n3);
kpeter@171
   363
kpeter@171
   364
  check(g.valid(n1), "Wrong validity check");
kpeter@171
   365
  check(g.valid(e1), "Wrong validity check");
kpeter@171
   366
kpeter@171
   367
  g.erase(n1);
kpeter@171
   368
kpeter@171
   369
  check(!g.valid(n1), "Wrong validity check");
kpeter@171
   370
  check(g.valid(n2), "Wrong validity check");
kpeter@171
   371
  check(g.valid(n3), "Wrong validity check");
kpeter@171
   372
  check(!g.valid(e1), "Wrong validity check");
kpeter@171
   373
  check(g.valid(e2), "Wrong validity check");
kpeter@171
   374
kpeter@171
   375
  check(!g.valid(g.nodeFromId(-1)), "Wrong validity check");
kpeter@171
   376
  check(!g.valid(g.arcFromId(-1)), "Wrong validity check");
kpeter@171
   377
}
kpeter@171
   378
deba@228
   379
void checkDigraphs() {
kpeter@171
   380
  { // Checking ListDigraph
kpeter@387
   381
    checkDigraphBuild<ListDigraph>();
kpeter@387
   382
    checkDigraphSplit<ListDigraph>();
kpeter@387
   383
    checkDigraphAlter<ListDigraph>();
kpeter@387
   384
    checkDigraphErase<ListDigraph>();
kpeter@387
   385
    checkDigraphSnapshot<ListDigraph>();
deba@228
   386
    checkDigraphValidityErase<ListDigraph>();
deba@57
   387
  }
kpeter@171
   388
  { // Checking SmartDigraph
kpeter@387
   389
    checkDigraphBuild<SmartDigraph>();
kpeter@387
   390
    checkDigraphSplit<SmartDigraph>();
kpeter@387
   391
    checkDigraphSnapshot<SmartDigraph>();
deba@228
   392
    checkDigraphValidity<SmartDigraph>();
kpeter@171
   393
  }
kpeter@171
   394
}
deba@57
   395
kpeter@171
   396
int main() {
deba@228
   397
  checkDigraphs();
deba@228
   398
  checkConcepts();
deba@57
   399
  return 0;
deba@57
   400
}