test/digraph_test.cc
author Peter Kovacs <kpeter@inf.elte.hu>
Sat, 26 Sep 2009 10:15:49 +0200
changeset 743 94ef0a5c0005
parent 737 9d6c3e8b2421
child 780 580af8cf2f6a
permissions -rw-r--r--
Add bib->dox converter and initial references.bib (#184)
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@440
     5
 * Copyright (C) 2003-2009
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@353
    22
#include <lemon/full_graph.h>
deba@57
    23
deba@57
    24
#include "test_tools.h"
kpeter@171
    25
#include "graph_test.h"
deba@57
    26
deba@57
    27
using namespace lemon;
deba@57
    28
using namespace lemon::concepts;
deba@57
    29
deba@228
    30
template <class Digraph>
kpeter@374
    31
void checkDigraphBuild() {
deba@228
    32
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
deba@228
    33
  Digraph G;
deba@228
    34
deba@228
    35
  checkGraphNodeList(G, 0);
deba@228
    36
  checkGraphArcList(G, 0);
deba@228
    37
kpeter@736
    38
  G.reserveNode(3);
kpeter@736
    39
  G.reserveArc(4);
kpeter@736
    40
deba@228
    41
  Node
deba@228
    42
    n1 = G.addNode(),
deba@228
    43
    n2 = G.addNode(),
deba@228
    44
    n3 = G.addNode();
deba@228
    45
  checkGraphNodeList(G, 3);
deba@228
    46
  checkGraphArcList(G, 0);
deba@228
    47
deba@228
    48
  Arc a1 = G.addArc(n1, n2);
deba@228
    49
  check(G.source(a1) == n1 && G.target(a1) == n2, "Wrong arc");
deba@228
    50
  checkGraphNodeList(G, 3);
deba@228
    51
  checkGraphArcList(G, 1);
deba@228
    52
deba@228
    53
  checkGraphOutArcList(G, n1, 1);
deba@228
    54
  checkGraphOutArcList(G, n2, 0);
deba@228
    55
  checkGraphOutArcList(G, n3, 0);
deba@228
    56
deba@228
    57
  checkGraphInArcList(G, n1, 0);
deba@228
    58
  checkGraphInArcList(G, n2, 1);
deba@228
    59
  checkGraphInArcList(G, n3, 0);
deba@228
    60
deba@228
    61
  checkGraphConArcList(G, 1);
deba@228
    62
kpeter@374
    63
  Arc a2 = G.addArc(n2, n1),
kpeter@374
    64
      a3 = G.addArc(n2, n3),
kpeter@374
    65
      a4 = G.addArc(n2, n3);
kpeter@374
    66
kpeter@374
    67
  checkGraphNodeList(G, 3);
kpeter@374
    68
  checkGraphArcList(G, 4);
kpeter@374
    69
kpeter@374
    70
  checkGraphOutArcList(G, n1, 1);
kpeter@374
    71
  checkGraphOutArcList(G, n2, 3);
kpeter@374
    72
  checkGraphOutArcList(G, n3, 0);
kpeter@374
    73
kpeter@374
    74
  checkGraphInArcList(G, n1, 1);
kpeter@374
    75
  checkGraphInArcList(G, n2, 1);
kpeter@374
    76
  checkGraphInArcList(G, n3, 2);
kpeter@374
    77
kpeter@374
    78
  checkGraphConArcList(G, 4);
kpeter@374
    79
kpeter@374
    80
  checkNodeIds(G);
kpeter@374
    81
  checkArcIds(G);
kpeter@374
    82
  checkGraphNodeMap(G);
kpeter@374
    83
  checkGraphArcMap(G);
kpeter@374
    84
}
kpeter@374
    85
kpeter@374
    86
template <class Digraph>
kpeter@374
    87
void checkDigraphSplit() {
kpeter@374
    88
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
kpeter@374
    89
kpeter@374
    90
  Digraph G;
kpeter@374
    91
  Node n1 = G.addNode(), n2 = G.addNode(), n3 = G.addNode();
kpeter@374
    92
  Arc a1 = G.addArc(n1, n2), a2 = G.addArc(n2, n1),
kpeter@374
    93
      a3 = G.addArc(n2, n3), a4 = G.addArc(n2, n3);
kpeter@374
    94
kpeter@374
    95
  Node n4 = G.split(n2);
kpeter@374
    96
kpeter@374
    97
  check(G.target(OutArcIt(G, n2)) == n4 &&
kpeter@374
    98
        G.source(InArcIt(G, n4)) == n2,
kpeter@374
    99
        "Wrong split.");
kpeter@374
   100
kpeter@374
   101
  checkGraphNodeList(G, 4);
kpeter@374
   102
  checkGraphArcList(G, 5);
kpeter@374
   103
kpeter@374
   104
  checkGraphOutArcList(G, n1, 1);
kpeter@374
   105
  checkGraphOutArcList(G, n2, 1);
kpeter@374
   106
  checkGraphOutArcList(G, n3, 0);
kpeter@374
   107
  checkGraphOutArcList(G, n4, 3);
kpeter@374
   108
kpeter@374
   109
  checkGraphInArcList(G, n1, 1);
kpeter@374
   110
  checkGraphInArcList(G, n2, 1);
kpeter@374
   111
  checkGraphInArcList(G, n3, 2);
kpeter@374
   112
  checkGraphInArcList(G, n4, 1);
kpeter@374
   113
kpeter@374
   114
  checkGraphConArcList(G, 5);
kpeter@374
   115
}
kpeter@374
   116
kpeter@374
   117
template <class Digraph>
kpeter@374
   118
void checkDigraphAlter() {
kpeter@374
   119
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
kpeter@374
   120
kpeter@374
   121
  Digraph G;
kpeter@374
   122
  Node n1 = G.addNode(), n2 = G.addNode(),
kpeter@374
   123
       n3 = G.addNode(), n4 = G.addNode();
kpeter@374
   124
  Arc a1 = G.addArc(n1, n2), a2 = G.addArc(n4, n1),
kpeter@374
   125
      a3 = G.addArc(n4, n3), a4 = G.addArc(n4, n3),
kpeter@374
   126
      a5 = G.addArc(n2, n4);
kpeter@374
   127
kpeter@374
   128
  checkGraphNodeList(G, 4);
kpeter@374
   129
  checkGraphArcList(G, 5);
kpeter@374
   130
kpeter@374
   131
  // Check changeSource() and changeTarget()
kpeter@374
   132
  G.changeTarget(a4, n1);
kpeter@374
   133
kpeter@374
   134
  checkGraphNodeList(G, 4);
kpeter@374
   135
  checkGraphArcList(G, 5);
kpeter@374
   136
kpeter@374
   137
  checkGraphOutArcList(G, n1, 1);
kpeter@374
   138
  checkGraphOutArcList(G, n2, 1);
kpeter@374
   139
  checkGraphOutArcList(G, n3, 0);
kpeter@374
   140
  checkGraphOutArcList(G, n4, 3);
kpeter@374
   141
kpeter@374
   142
  checkGraphInArcList(G, n1, 2);
kpeter@374
   143
  checkGraphInArcList(G, n2, 1);
kpeter@374
   144
  checkGraphInArcList(G, n3, 1);
kpeter@374
   145
  checkGraphInArcList(G, n4, 1);
kpeter@374
   146
kpeter@374
   147
  checkGraphConArcList(G, 5);
kpeter@374
   148
kpeter@374
   149
  G.changeSource(a4, n3);
kpeter@374
   150
kpeter@374
   151
  checkGraphNodeList(G, 4);
kpeter@374
   152
  checkGraphArcList(G, 5);
kpeter@374
   153
kpeter@374
   154
  checkGraphOutArcList(G, n1, 1);
kpeter@374
   155
  checkGraphOutArcList(G, n2, 1);
kpeter@374
   156
  checkGraphOutArcList(G, n3, 1);
kpeter@374
   157
  checkGraphOutArcList(G, n4, 2);
kpeter@374
   158
kpeter@374
   159
  checkGraphInArcList(G, n1, 2);
kpeter@374
   160
  checkGraphInArcList(G, n2, 1);
kpeter@374
   161
  checkGraphInArcList(G, n3, 1);
kpeter@374
   162
  checkGraphInArcList(G, n4, 1);
kpeter@374
   163
kpeter@374
   164
  checkGraphConArcList(G, 5);
kpeter@374
   165
kpeter@374
   166
  // Check contract()
kpeter@374
   167
  G.contract(n2, n4, false);
kpeter@374
   168
kpeter@374
   169
  checkGraphNodeList(G, 3);
kpeter@374
   170
  checkGraphArcList(G, 5);
kpeter@374
   171
kpeter@374
   172
  checkGraphOutArcList(G, n1, 1);
kpeter@374
   173
  checkGraphOutArcList(G, n2, 3);
kpeter@374
   174
  checkGraphOutArcList(G, n3, 1);
kpeter@374
   175
kpeter@374
   176
  checkGraphInArcList(G, n1, 2);
kpeter@374
   177
  checkGraphInArcList(G, n2, 2);
kpeter@374
   178
  checkGraphInArcList(G, n3, 1);
kpeter@374
   179
kpeter@374
   180
  checkGraphConArcList(G, 5);
kpeter@374
   181
kpeter@374
   182
  G.contract(n2, n1);
kpeter@374
   183
kpeter@374
   184
  checkGraphNodeList(G, 2);
kpeter@374
   185
  checkGraphArcList(G, 3);
kpeter@374
   186
kpeter@374
   187
  checkGraphOutArcList(G, n2, 2);
kpeter@374
   188
  checkGraphOutArcList(G, n3, 1);
kpeter@374
   189
kpeter@374
   190
  checkGraphInArcList(G, n2, 2);
kpeter@374
   191
  checkGraphInArcList(G, n3, 1);
kpeter@374
   192
kpeter@374
   193
  checkGraphConArcList(G, 3);
kpeter@374
   194
}
kpeter@374
   195
kpeter@374
   196
template <class Digraph>
kpeter@374
   197
void checkDigraphErase() {
kpeter@374
   198
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
kpeter@374
   199
kpeter@374
   200
  Digraph G;
kpeter@374
   201
  Node n1 = G.addNode(), n2 = G.addNode(),
kpeter@374
   202
       n3 = G.addNode(), n4 = G.addNode();
kpeter@374
   203
  Arc a1 = G.addArc(n1, n2), a2 = G.addArc(n4, n1),
kpeter@374
   204
      a3 = G.addArc(n4, n3), a4 = G.addArc(n3, n1),
kpeter@374
   205
      a5 = G.addArc(n2, n4);
kpeter@374
   206
kpeter@374
   207
  // Check arc deletion
kpeter@374
   208
  G.erase(a1);
kpeter@374
   209
kpeter@374
   210
  checkGraphNodeList(G, 4);
kpeter@374
   211
  checkGraphArcList(G, 4);
kpeter@374
   212
kpeter@374
   213
  checkGraphOutArcList(G, n1, 0);
kpeter@374
   214
  checkGraphOutArcList(G, n2, 1);
kpeter@374
   215
  checkGraphOutArcList(G, n3, 1);
kpeter@374
   216
  checkGraphOutArcList(G, n4, 2);
kpeter@374
   217
kpeter@374
   218
  checkGraphInArcList(G, n1, 2);
kpeter@374
   219
  checkGraphInArcList(G, n2, 0);
kpeter@374
   220
  checkGraphInArcList(G, n3, 1);
kpeter@374
   221
  checkGraphInArcList(G, n4, 1);
kpeter@374
   222
kpeter@374
   223
  checkGraphConArcList(G, 4);
kpeter@374
   224
kpeter@374
   225
  // Check node deletion
kpeter@374
   226
  G.erase(n4);
kpeter@374
   227
kpeter@374
   228
  checkGraphNodeList(G, 3);
kpeter@374
   229
  checkGraphArcList(G, 1);
kpeter@374
   230
kpeter@374
   231
  checkGraphOutArcList(G, n1, 0);
kpeter@374
   232
  checkGraphOutArcList(G, n2, 0);
kpeter@374
   233
  checkGraphOutArcList(G, n3, 1);
kpeter@374
   234
  checkGraphOutArcList(G, n4, 0);
kpeter@374
   235
kpeter@374
   236
  checkGraphInArcList(G, n1, 1);
kpeter@374
   237
  checkGraphInArcList(G, n2, 0);
kpeter@374
   238
  checkGraphInArcList(G, n3, 0);
kpeter@374
   239
  checkGraphInArcList(G, n4, 0);
kpeter@374
   240
kpeter@374
   241
  checkGraphConArcList(G, 1);
kpeter@374
   242
}
kpeter@374
   243
kpeter@374
   244
kpeter@374
   245
template <class Digraph>
kpeter@374
   246
void checkDigraphSnapshot() {
kpeter@374
   247
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
kpeter@374
   248
kpeter@374
   249
  Digraph G;
kpeter@374
   250
  Node n1 = G.addNode(), n2 = G.addNode(), n3 = G.addNode();
kpeter@374
   251
  Arc a1 = G.addArc(n1, n2), a2 = G.addArc(n2, n1),
kpeter@374
   252
      a3 = G.addArc(n2, n3), a4 = G.addArc(n2, n3);
kpeter@374
   253
kpeter@374
   254
  typename Digraph::Snapshot snapshot(G);
kpeter@374
   255
kpeter@374
   256
  Node n = G.addNode();
kpeter@374
   257
  G.addArc(n3, n);
kpeter@374
   258
  G.addArc(n, n3);
kpeter@374
   259
kpeter@374
   260
  checkGraphNodeList(G, 4);
kpeter@374
   261
  checkGraphArcList(G, 6);
kpeter@374
   262
kpeter@374
   263
  snapshot.restore();
kpeter@374
   264
deba@228
   265
  checkGraphNodeList(G, 3);
deba@228
   266
  checkGraphArcList(G, 4);
deba@228
   267
deba@228
   268
  checkGraphOutArcList(G, n1, 1);
deba@228
   269
  checkGraphOutArcList(G, n2, 3);
deba@228
   270
  checkGraphOutArcList(G, n3, 0);
deba@228
   271
deba@228
   272
  checkGraphInArcList(G, n1, 1);
deba@228
   273
  checkGraphInArcList(G, n2, 1);
deba@228
   274
  checkGraphInArcList(G, n3, 2);
deba@228
   275
deba@228
   276
  checkGraphConArcList(G, 4);
deba@228
   277
deba@228
   278
  checkNodeIds(G);
deba@228
   279
  checkArcIds(G);
deba@228
   280
  checkGraphNodeMap(G);
deba@228
   281
  checkGraphArcMap(G);
deba@228
   282
kpeter@374
   283
  G.addNode();
kpeter@374
   284
  snapshot.save(G);
kpeter@374
   285
kpeter@374
   286
  G.addArc(G.addNode(), G.addNode());
kpeter@374
   287
kpeter@374
   288
  snapshot.restore();
kpeter@740
   289
  snapshot.save(G);
kpeter@740
   290
kpeter@740
   291
  checkGraphNodeList(G, 4);
kpeter@740
   292
  checkGraphArcList(G, 4);
kpeter@740
   293
kpeter@740
   294
  G.addArc(G.addNode(), G.addNode());
kpeter@740
   295
kpeter@740
   296
  snapshot.restore();
kpeter@374
   297
kpeter@374
   298
  checkGraphNodeList(G, 4);
kpeter@374
   299
  checkGraphArcList(G, 4);
deba@228
   300
}
deba@228
   301
deba@228
   302
void checkConcepts() {
kpeter@171
   303
  { // Checking digraph components
deba@57
   304
    checkConcept<BaseDigraphComponent, BaseDigraphComponent >();
deba@57
   305
alpar@209
   306
    checkConcept<IDableDigraphComponent<>,
deba@57
   307
      IDableDigraphComponent<> >();
deba@57
   308
alpar@209
   309
    checkConcept<IterableDigraphComponent<>,
deba@57
   310
      IterableDigraphComponent<> >();
deba@57
   311
alpar@209
   312
    checkConcept<MappableDigraphComponent<>,
deba@57
   313
      MappableDigraphComponent<> >();
deba@57
   314
  }
kpeter@171
   315
  { // Checking skeleton digraph
deba@57
   316
    checkConcept<Digraph, Digraph>();
deba@57
   317
  }
kpeter@171
   318
  { // Checking ListDigraph
kpeter@171
   319
    checkConcept<Digraph, ListDigraph>();
deba@57
   320
    checkConcept<AlterableDigraphComponent<>, ListDigraph>();
deba@57
   321
    checkConcept<ExtendableDigraphComponent<>, ListDigraph>();
deba@57
   322
    checkConcept<ClearableDigraphComponent<>, ListDigraph>();
deba@57
   323
    checkConcept<ErasableDigraphComponent<>, ListDigraph>();
kpeter@171
   324
  }
kpeter@171
   325
  { // Checking SmartDigraph
kpeter@171
   326
    checkConcept<Digraph, SmartDigraph>();
kpeter@171
   327
    checkConcept<AlterableDigraphComponent<>, SmartDigraph>();
kpeter@171
   328
    checkConcept<ExtendableDigraphComponent<>, SmartDigraph>();
kpeter@171
   329
    checkConcept<ClearableDigraphComponent<>, SmartDigraph>();
kpeter@171
   330
  }
kpeter@354
   331
  { // Checking FullDigraph
kpeter@354
   332
    checkConcept<Digraph, FullDigraph>();
kpeter@354
   333
  }
kpeter@171
   334
}
deba@57
   335
kpeter@171
   336
template <typename Digraph>
deba@228
   337
void checkDigraphValidity() {
kpeter@171
   338
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
kpeter@171
   339
  Digraph g;
kpeter@171
   340
kpeter@171
   341
  Node
kpeter@171
   342
    n1 = g.addNode(),
kpeter@171
   343
    n2 = g.addNode(),
kpeter@171
   344
    n3 = g.addNode();
kpeter@171
   345
kpeter@171
   346
  Arc
kpeter@171
   347
    e1 = g.addArc(n1, n2),
kpeter@171
   348
    e2 = g.addArc(n2, n3);
kpeter@171
   349
kpeter@171
   350
  check(g.valid(n1), "Wrong validity check");
kpeter@171
   351
  check(g.valid(e1), "Wrong validity check");
kpeter@171
   352
kpeter@171
   353
  check(!g.valid(g.nodeFromId(-1)), "Wrong validity check");
kpeter@171
   354
  check(!g.valid(g.arcFromId(-1)), "Wrong validity check");
kpeter@171
   355
}
kpeter@171
   356
kpeter@171
   357
template <typename Digraph>
deba@228
   358
void checkDigraphValidityErase() {
kpeter@171
   359
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
kpeter@171
   360
  Digraph g;
kpeter@171
   361
kpeter@171
   362
  Node
kpeter@171
   363
    n1 = g.addNode(),
kpeter@171
   364
    n2 = g.addNode(),
kpeter@171
   365
    n3 = g.addNode();
kpeter@171
   366
kpeter@171
   367
  Arc
kpeter@171
   368
    e1 = g.addArc(n1, n2),
kpeter@171
   369
    e2 = g.addArc(n2, n3);
kpeter@171
   370
kpeter@171
   371
  check(g.valid(n1), "Wrong validity check");
kpeter@171
   372
  check(g.valid(e1), "Wrong validity check");
kpeter@171
   373
kpeter@171
   374
  g.erase(n1);
kpeter@171
   375
kpeter@171
   376
  check(!g.valid(n1), "Wrong validity check");
kpeter@171
   377
  check(g.valid(n2), "Wrong validity check");
kpeter@171
   378
  check(g.valid(n3), "Wrong validity check");
kpeter@171
   379
  check(!g.valid(e1), "Wrong validity check");
kpeter@171
   380
  check(g.valid(e2), "Wrong validity check");
kpeter@171
   381
kpeter@171
   382
  check(!g.valid(g.nodeFromId(-1)), "Wrong validity check");
kpeter@171
   383
  check(!g.valid(g.arcFromId(-1)), "Wrong validity check");
kpeter@171
   384
}
kpeter@171
   385
alpar@375
   386
void checkFullDigraph(int num) {
alpar@375
   387
  typedef FullDigraph Digraph;
alpar@375
   388
  DIGRAPH_TYPEDEFS(Digraph);
kpeter@737
   389
alpar@375
   390
  Digraph G(num);
kpeter@737
   391
  check(G.nodeNum() == num && G.arcNum() == num * num, "Wrong size");
kpeter@737
   392
kpeter@737
   393
  G.resize(num);
kpeter@737
   394
  check(G.nodeNum() == num && G.arcNum() == num * num, "Wrong size");
alpar@375
   395
alpar@375
   396
  checkGraphNodeList(G, num);
alpar@375
   397
  checkGraphArcList(G, num * num);
alpar@375
   398
alpar@375
   399
  for (NodeIt n(G); n != INVALID; ++n) {
alpar@375
   400
    checkGraphOutArcList(G, n, num);
alpar@375
   401
    checkGraphInArcList(G, n, num);
alpar@375
   402
  }
alpar@375
   403
alpar@375
   404
  checkGraphConArcList(G, num * num);
alpar@375
   405
alpar@375
   406
  checkNodeIds(G);
alpar@375
   407
  checkArcIds(G);
alpar@375
   408
  checkGraphNodeMap(G);
alpar@375
   409
  checkGraphArcMap(G);
alpar@375
   410
alpar@375
   411
  for (int i = 0; i < G.nodeNum(); ++i) {
alpar@375
   412
    check(G.index(G(i)) == i, "Wrong index");
alpar@375
   413
  }
alpar@375
   414
alpar@375
   415
  for (NodeIt s(G); s != INVALID; ++s) {
alpar@375
   416
    for (NodeIt t(G); t != INVALID; ++t) {
alpar@375
   417
      Arc a = G.arc(s, t);
alpar@375
   418
      check(G.source(a) == s && G.target(a) == t, "Wrong arc lookup");
alpar@375
   419
    }
alpar@375
   420
  }
alpar@375
   421
}
alpar@375
   422
deba@228
   423
void checkDigraphs() {
kpeter@171
   424
  { // Checking ListDigraph
kpeter@374
   425
    checkDigraphBuild<ListDigraph>();
kpeter@374
   426
    checkDigraphSplit<ListDigraph>();
kpeter@374
   427
    checkDigraphAlter<ListDigraph>();
kpeter@374
   428
    checkDigraphErase<ListDigraph>();
kpeter@374
   429
    checkDigraphSnapshot<ListDigraph>();
deba@228
   430
    checkDigraphValidityErase<ListDigraph>();
deba@57
   431
  }
kpeter@171
   432
  { // Checking SmartDigraph
kpeter@374
   433
    checkDigraphBuild<SmartDigraph>();
kpeter@374
   434
    checkDigraphSplit<SmartDigraph>();
kpeter@374
   435
    checkDigraphSnapshot<SmartDigraph>();
deba@228
   436
    checkDigraphValidity<SmartDigraph>();
kpeter@171
   437
  }
deba@353
   438
  { // Checking FullDigraph
deba@353
   439
    checkFullDigraph(8);
deba@353
   440
  }
kpeter@171
   441
}
deba@57
   442
kpeter@171
   443
int main() {
deba@228
   444
  checkDigraphs();
deba@228
   445
  checkConcepts();
deba@57
   446
  return 0;
deba@57
   447
}