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