test/digraph_test.cc
author Alpar Juttner <alpar@cs.elte.hu>
Fri, 07 Nov 2008 07:18:37 +0000
changeset 371 164fe3abc024
parent 364 b4a01426c0d9
child 375 2d87dbd7f8c8
permissions -rw-r--r--
Merge
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@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>
deba@228
    31
void checkDigraph() {
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
deba@228
    60
  Arc a2 = G.addArc(n2, n1), a3 = G.addArc(n2, n3), a4 = G.addArc(n2, n3);
deba@228
    61
  checkGraphNodeList(G, 3);
deba@228
    62
  checkGraphArcList(G, 4);
deba@228
    63
deba@228
    64
  checkGraphOutArcList(G, n1, 1);
deba@228
    65
  checkGraphOutArcList(G, n2, 3);
deba@228
    66
  checkGraphOutArcList(G, n3, 0);
deba@228
    67
deba@228
    68
  checkGraphInArcList(G, n1, 1);
deba@228
    69
  checkGraphInArcList(G, n2, 1);
deba@228
    70
  checkGraphInArcList(G, n3, 2);
deba@228
    71
deba@228
    72
  checkGraphConArcList(G, 4);
deba@228
    73
deba@228
    74
  checkNodeIds(G);
deba@228
    75
  checkArcIds(G);
deba@228
    76
  checkGraphNodeMap(G);
deba@228
    77
  checkGraphArcMap(G);
deba@228
    78
deba@228
    79
}
deba@228
    80
deba@353
    81
void checkFullDigraph(int num) {
deba@353
    82
  typedef FullDigraph Digraph;
deba@353
    83
  DIGRAPH_TYPEDEFS(Digraph);
deba@353
    84
  Digraph G(num);
deba@353
    85
deba@353
    86
  checkGraphNodeList(G, num);
deba@353
    87
  checkGraphArcList(G, num * num);
deba@353
    88
deba@353
    89
  for (NodeIt n(G); n != INVALID; ++n) {
deba@353
    90
    checkGraphOutArcList(G, n, num);
deba@353
    91
    checkGraphInArcList(G, n, num);
deba@353
    92
  }
deba@353
    93
deba@353
    94
  checkGraphConArcList(G, num * num);
deba@353
    95
deba@353
    96
  checkNodeIds(G);
deba@353
    97
  checkArcIds(G);
deba@353
    98
  checkGraphNodeMap(G);
deba@353
    99
  checkGraphArcMap(G);
deba@353
   100
deba@353
   101
  for (int i = 0; i < G.nodeNum(); ++i) {
deba@353
   102
    check(G.index(G(i)) == i, "Wrong index");
deba@353
   103
  }
deba@353
   104
deba@353
   105
  for (NodeIt s(G); s != INVALID; ++s) {
deba@353
   106
    for (NodeIt t(G); t != INVALID; ++t) {
deba@353
   107
      Arc a = G.arc(s, t);
deba@353
   108
      check(G.source(a) == s && G.target(a) == t, "Wrong arc lookup");
deba@353
   109
    }
deba@353
   110
  }
deba@353
   111
deba@353
   112
}
deba@353
   113
deba@228
   114
void checkConcepts() {
kpeter@171
   115
  { // Checking digraph components
deba@57
   116
    checkConcept<BaseDigraphComponent, BaseDigraphComponent >();
deba@57
   117
alpar@209
   118
    checkConcept<IDableDigraphComponent<>,
deba@57
   119
      IDableDigraphComponent<> >();
deba@57
   120
alpar@209
   121
    checkConcept<IterableDigraphComponent<>,
deba@57
   122
      IterableDigraphComponent<> >();
deba@57
   123
alpar@209
   124
    checkConcept<MappableDigraphComponent<>,
deba@57
   125
      MappableDigraphComponent<> >();
deba@57
   126
  }
kpeter@171
   127
  { // Checking skeleton digraph
deba@57
   128
    checkConcept<Digraph, Digraph>();
deba@57
   129
  }
kpeter@171
   130
  { // Checking ListDigraph
kpeter@171
   131
    checkConcept<Digraph, ListDigraph>();
deba@57
   132
    checkConcept<AlterableDigraphComponent<>, ListDigraph>();
deba@57
   133
    checkConcept<ExtendableDigraphComponent<>, ListDigraph>();
deba@57
   134
    checkConcept<ClearableDigraphComponent<>, ListDigraph>();
deba@57
   135
    checkConcept<ErasableDigraphComponent<>, ListDigraph>();
kpeter@171
   136
  }
kpeter@171
   137
  { // Checking SmartDigraph
kpeter@171
   138
    checkConcept<Digraph, SmartDigraph>();
kpeter@171
   139
    checkConcept<AlterableDigraphComponent<>, SmartDigraph>();
kpeter@171
   140
    checkConcept<ExtendableDigraphComponent<>, SmartDigraph>();
kpeter@171
   141
    checkConcept<ClearableDigraphComponent<>, SmartDigraph>();
kpeter@171
   142
  }
kpeter@354
   143
  { // Checking FullDigraph
kpeter@354
   144
    checkConcept<Digraph, FullDigraph>();
kpeter@354
   145
  }
kpeter@171
   146
}
deba@57
   147
kpeter@171
   148
template <typename Digraph>
deba@228
   149
void checkDigraphValidity() {
kpeter@171
   150
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
kpeter@171
   151
  Digraph g;
kpeter@171
   152
kpeter@171
   153
  Node
kpeter@171
   154
    n1 = g.addNode(),
kpeter@171
   155
    n2 = g.addNode(),
kpeter@171
   156
    n3 = g.addNode();
kpeter@171
   157
kpeter@171
   158
  Arc
kpeter@171
   159
    e1 = g.addArc(n1, n2),
kpeter@171
   160
    e2 = g.addArc(n2, n3);
kpeter@171
   161
kpeter@171
   162
  check(g.valid(n1), "Wrong validity check");
kpeter@171
   163
  check(g.valid(e1), "Wrong validity check");
kpeter@171
   164
kpeter@171
   165
  check(!g.valid(g.nodeFromId(-1)), "Wrong validity check");
kpeter@171
   166
  check(!g.valid(g.arcFromId(-1)), "Wrong validity check");
kpeter@171
   167
}
kpeter@171
   168
kpeter@171
   169
template <typename Digraph>
deba@228
   170
void checkDigraphValidityErase() {
kpeter@171
   171
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
kpeter@171
   172
  Digraph g;
kpeter@171
   173
kpeter@171
   174
  Node
kpeter@171
   175
    n1 = g.addNode(),
kpeter@171
   176
    n2 = g.addNode(),
kpeter@171
   177
    n3 = g.addNode();
kpeter@171
   178
kpeter@171
   179
  Arc
kpeter@171
   180
    e1 = g.addArc(n1, n2),
kpeter@171
   181
    e2 = g.addArc(n2, n3);
kpeter@171
   182
kpeter@171
   183
  check(g.valid(n1), "Wrong validity check");
kpeter@171
   184
  check(g.valid(e1), "Wrong validity check");
kpeter@171
   185
kpeter@171
   186
  g.erase(n1);
kpeter@171
   187
kpeter@171
   188
  check(!g.valid(n1), "Wrong validity check");
kpeter@171
   189
  check(g.valid(n2), "Wrong validity check");
kpeter@171
   190
  check(g.valid(n3), "Wrong validity check");
kpeter@171
   191
  check(!g.valid(e1), "Wrong validity check");
kpeter@171
   192
  check(g.valid(e2), "Wrong validity check");
kpeter@171
   193
kpeter@171
   194
  check(!g.valid(g.nodeFromId(-1)), "Wrong validity check");
kpeter@171
   195
  check(!g.valid(g.arcFromId(-1)), "Wrong validity check");
kpeter@171
   196
}
kpeter@171
   197
deba@228
   198
void checkDigraphs() {
kpeter@171
   199
  { // Checking ListDigraph
deba@57
   200
    checkDigraph<ListDigraph>();
deba@228
   201
    checkDigraphValidityErase<ListDigraph>();
deba@57
   202
  }
kpeter@171
   203
  { // Checking SmartDigraph
kpeter@171
   204
    checkDigraph<SmartDigraph>();
deba@228
   205
    checkDigraphValidity<SmartDigraph>();
kpeter@171
   206
  }
deba@353
   207
  { // Checking FullDigraph
deba@353
   208
    checkFullDigraph(8);
deba@353
   209
  }
kpeter@171
   210
}
deba@57
   211
kpeter@171
   212
int main() {
deba@228
   213
  checkDigraphs();
deba@228
   214
  checkConcepts();
deba@57
   215
  return 0;
deba@57
   216
}