test/bpgraph_test.cc
author Balazs Dezso <deba@inf.elte.hu>
Sun, 14 Nov 2010 22:48:32 +0100
changeset 1188 5ef0ab7b61cd
parent 1187 4c89e925cfe2
child 1189 a12cca3ad15a
permissions -rw-r--r--
FullBpGraph implementation (#69)
deba@1186
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
deba@1186
     2
 *
deba@1186
     3
 * This file is a part of LEMON, a generic C++ optimization library.
deba@1186
     4
 *
deba@1186
     5
 * Copyright (C) 2003-2010
deba@1186
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@1186
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@1186
     8
 *
deba@1186
     9
 * Permission to use, modify and distribute this software is granted
deba@1186
    10
 * provided that this copyright notice appears in all copies. For
deba@1186
    11
 * precise terms see the accompanying LICENSE file.
deba@1186
    12
 *
deba@1186
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@1186
    14
 * express or implied, and with no claim as to its suitability for any
deba@1186
    15
 * purpose.
deba@1186
    16
 *
deba@1186
    17
 */
deba@1186
    18
deba@1186
    19
#include <lemon/concepts/bpgraph.h>
deba@1186
    20
//#include <lemon/list_graph.h>
deba@1187
    21
#include <lemon/smart_graph.h>
deba@1188
    22
#include <lemon/full_graph.h>
deba@1186
    23
deba@1186
    24
#include "test_tools.h"
deba@1186
    25
#include "graph_test.h"
deba@1186
    26
deba@1186
    27
using namespace lemon;
deba@1186
    28
using namespace lemon::concepts;
deba@1186
    29
deba@1187
    30
template <class BpGraph>
deba@1187
    31
void checkBpGraphBuild() {
deba@1187
    32
  TEMPLATE_BPGRAPH_TYPEDEFS(BpGraph);
deba@1187
    33
deba@1187
    34
  BpGraph G;
deba@1187
    35
  checkGraphNodeList(G, 0);
deba@1187
    36
  checkGraphRedNodeList(G, 0);
deba@1187
    37
  checkGraphBlueNodeList(G, 0);
deba@1187
    38
  checkGraphEdgeList(G, 0);
deba@1187
    39
  checkGraphArcList(G, 0);
deba@1187
    40
deba@1187
    41
  G.reserveNode(3);
deba@1187
    42
  G.reserveEdge(3);
deba@1187
    43
deba@1187
    44
  Node
deba@1187
    45
    rn1 = G.addRedNode();
deba@1187
    46
  checkGraphNodeList(G, 1);
deba@1187
    47
  checkGraphRedNodeList(G, 1);
deba@1187
    48
  checkGraphBlueNodeList(G, 0);
deba@1187
    49
  checkGraphEdgeList(G, 0);
deba@1187
    50
  checkGraphArcList(G, 0);
deba@1187
    51
deba@1187
    52
  Node
deba@1187
    53
    bn1 = G.addBlueNode(),
deba@1187
    54
    bn2 = G.addBlueNode();
deba@1187
    55
  checkGraphNodeList(G, 3);
deba@1187
    56
  checkGraphRedNodeList(G, 1);
deba@1187
    57
  checkGraphBlueNodeList(G, 2);
deba@1187
    58
  checkGraphEdgeList(G, 0);
deba@1187
    59
  checkGraphArcList(G, 0);
deba@1187
    60
deba@1187
    61
  Edge e1 = G.addEdge(rn1, bn2);
deba@1187
    62
  check(G.redNode(e1) == rn1 && G.blueNode(e1) == bn2, "Wrong edge");
deba@1187
    63
  check(G.u(e1) == rn1 && G.v(e1) == bn2, "Wrong edge");
deba@1187
    64
deba@1187
    65
  checkGraphNodeList(G, 3);
deba@1187
    66
  checkGraphRedNodeList(G, 1);
deba@1187
    67
  checkGraphBlueNodeList(G, 2);
deba@1187
    68
  checkGraphEdgeList(G, 1);
deba@1187
    69
  checkGraphArcList(G, 2);
deba@1187
    70
deba@1187
    71
  checkGraphIncEdgeArcLists(G, rn1, 1);
deba@1187
    72
  checkGraphIncEdgeArcLists(G, bn1, 0);
deba@1187
    73
  checkGraphIncEdgeArcLists(G, bn2, 1);
deba@1187
    74
deba@1187
    75
  checkGraphConEdgeList(G, 1);
deba@1187
    76
  checkGraphConArcList(G, 2);
deba@1187
    77
deba@1187
    78
  Edge
deba@1187
    79
    e2 = G.addEdge(rn1, bn1),
deba@1187
    80
    e3 = G.addEdge(rn1, bn2);
deba@1187
    81
deba@1187
    82
  checkGraphNodeList(G, 3);
deba@1187
    83
  checkGraphRedNodeList(G, 1);
deba@1187
    84
  checkGraphBlueNodeList(G, 2);
deba@1187
    85
  checkGraphEdgeList(G, 3);
deba@1187
    86
  checkGraphArcList(G, 6);
deba@1187
    87
deba@1187
    88
  checkGraphIncEdgeArcLists(G, rn1, 3);
deba@1187
    89
  checkGraphIncEdgeArcLists(G, bn1, 1);
deba@1187
    90
  checkGraphIncEdgeArcLists(G, bn2, 2);
deba@1187
    91
deba@1187
    92
  checkGraphConEdgeList(G, 3);
deba@1187
    93
  checkGraphConArcList(G, 6);
deba@1187
    94
deba@1187
    95
  checkArcDirections(G);
deba@1187
    96
deba@1187
    97
  checkNodeIds(G);
deba@1187
    98
  checkRedNodeIds(G);
deba@1187
    99
  checkBlueNodeIds(G);
deba@1187
   100
  checkArcIds(G);
deba@1187
   101
  checkEdgeIds(G);
deba@1187
   102
deba@1187
   103
  checkGraphNodeMap(G);
deba@1187
   104
  checkGraphRedMap(G);
deba@1187
   105
  checkGraphBlueMap(G);
deba@1187
   106
  checkGraphArcMap(G);
deba@1187
   107
  checkGraphEdgeMap(G);
deba@1187
   108
}
deba@1187
   109
deba@1187
   110
template <class Graph>
deba@1187
   111
void checkBpGraphSnapshot() {
deba@1187
   112
  TEMPLATE_BPGRAPH_TYPEDEFS(Graph);
deba@1187
   113
deba@1187
   114
  Graph G;
deba@1187
   115
  Node 
deba@1187
   116
    n1 = G.addRedNode(),
deba@1187
   117
    n2 = G.addBlueNode(),
deba@1187
   118
    n3 = G.addBlueNode();
deba@1187
   119
  Edge 
deba@1187
   120
    e1 = G.addEdge(n1, n2),
deba@1187
   121
    e2 = G.addEdge(n1, n3);
deba@1187
   122
deba@1187
   123
  checkGraphNodeList(G, 3);
deba@1187
   124
  checkGraphRedNodeList(G, 1);
deba@1187
   125
  checkGraphBlueNodeList(G, 2);
deba@1187
   126
  checkGraphEdgeList(G, 2);
deba@1187
   127
  checkGraphArcList(G, 4);
deba@1187
   128
deba@1187
   129
  typename Graph::Snapshot snapshot(G);
deba@1187
   130
deba@1187
   131
  Node n4 = G.addRedNode();
deba@1187
   132
  G.addEdge(n4, n2);
deba@1187
   133
  G.addEdge(n4, n3);
deba@1187
   134
deba@1187
   135
  checkGraphNodeList(G, 4);
deba@1187
   136
  checkGraphRedNodeList(G, 2);
deba@1187
   137
  checkGraphBlueNodeList(G, 2);
deba@1187
   138
  checkGraphEdgeList(G, 4);
deba@1187
   139
  checkGraphArcList(G, 8);
deba@1187
   140
deba@1187
   141
  snapshot.restore();
deba@1187
   142
deba@1187
   143
  checkGraphNodeList(G, 3);
deba@1187
   144
  checkGraphRedNodeList(G, 1);
deba@1187
   145
  checkGraphBlueNodeList(G, 2);
deba@1187
   146
  checkGraphEdgeList(G, 2);
deba@1187
   147
  checkGraphArcList(G, 4);
deba@1187
   148
deba@1187
   149
  checkGraphIncEdgeArcLists(G, n1, 2);
deba@1187
   150
  checkGraphIncEdgeArcLists(G, n2, 1);
deba@1187
   151
  checkGraphIncEdgeArcLists(G, n3, 1);
deba@1187
   152
deba@1187
   153
  checkGraphConEdgeList(G, 2);
deba@1187
   154
  checkGraphConArcList(G, 4);
deba@1187
   155
deba@1187
   156
  checkNodeIds(G);
deba@1187
   157
  checkRedNodeIds(G);
deba@1187
   158
  checkBlueNodeIds(G);
deba@1187
   159
  checkArcIds(G);
deba@1187
   160
  checkEdgeIds(G);
deba@1187
   161
deba@1187
   162
  checkGraphNodeMap(G);
deba@1187
   163
  checkGraphRedMap(G);
deba@1187
   164
  checkGraphBlueMap(G);
deba@1187
   165
  checkGraphArcMap(G);
deba@1187
   166
  checkGraphEdgeMap(G);
deba@1187
   167
deba@1187
   168
  G.addRedNode();
deba@1187
   169
  snapshot.save(G);
deba@1187
   170
deba@1187
   171
  G.addEdge(G.addRedNode(), G.addBlueNode());
deba@1187
   172
deba@1187
   173
  snapshot.restore();
deba@1187
   174
  snapshot.save(G);
deba@1187
   175
deba@1187
   176
  checkGraphNodeList(G, 4);
deba@1187
   177
  checkGraphRedNodeList(G, 2);
deba@1187
   178
  checkGraphBlueNodeList(G, 2);
deba@1187
   179
  checkGraphEdgeList(G, 2);
deba@1187
   180
  checkGraphArcList(G, 4);
deba@1187
   181
deba@1187
   182
  G.addEdge(G.addRedNode(), G.addBlueNode());
deba@1187
   183
deba@1187
   184
  snapshot.restore();
deba@1187
   185
deba@1187
   186
  checkGraphNodeList(G, 4);
deba@1187
   187
  checkGraphRedNodeList(G, 2);
deba@1187
   188
  checkGraphBlueNodeList(G, 2);
deba@1187
   189
  checkGraphEdgeList(G, 2);
deba@1187
   190
  checkGraphArcList(G, 4);
deba@1187
   191
}
deba@1187
   192
deba@1187
   193
template <typename Graph>
deba@1187
   194
void checkBpGraphValidity() {
deba@1187
   195
  TEMPLATE_GRAPH_TYPEDEFS(Graph);
deba@1187
   196
  Graph g;
deba@1187
   197
deba@1187
   198
  Node
deba@1187
   199
    n1 = g.addRedNode(),
deba@1187
   200
    n2 = g.addBlueNode(),
deba@1187
   201
    n3 = g.addBlueNode();
deba@1187
   202
deba@1187
   203
  Edge
deba@1187
   204
    e1 = g.addEdge(n1, n2),
deba@1187
   205
    e2 = g.addEdge(n1, n3);
deba@1187
   206
deba@1187
   207
  check(g.valid(n1), "Wrong validity check");
deba@1187
   208
  check(g.valid(e1), "Wrong validity check");
deba@1187
   209
  check(g.valid(g.direct(e1, true)), "Wrong validity check");
deba@1187
   210
deba@1187
   211
  check(!g.valid(g.nodeFromId(-1)), "Wrong validity check");
deba@1187
   212
  check(!g.valid(g.edgeFromId(-1)), "Wrong validity check");
deba@1187
   213
  check(!g.valid(g.arcFromId(-1)), "Wrong validity check");
deba@1187
   214
}
deba@1187
   215
deba@1186
   216
void checkConcepts() {
deba@1186
   217
  { // Checking graph components
deba@1186
   218
    checkConcept<BaseBpGraphComponent, BaseBpGraphComponent >();
deba@1186
   219
deba@1186
   220
    checkConcept<IDableBpGraphComponent<>,
deba@1186
   221
      IDableBpGraphComponent<> >();
deba@1186
   222
deba@1186
   223
    checkConcept<IterableBpGraphComponent<>,
deba@1186
   224
      IterableBpGraphComponent<> >();
deba@1186
   225
deba@1186
   226
    checkConcept<AlterableBpGraphComponent<>,
deba@1186
   227
      AlterableBpGraphComponent<> >();
deba@1186
   228
deba@1186
   229
    checkConcept<MappableBpGraphComponent<>,
deba@1186
   230
      MappableBpGraphComponent<> >();
deba@1186
   231
deba@1186
   232
    checkConcept<ExtendableBpGraphComponent<>,
deba@1186
   233
      ExtendableBpGraphComponent<> >();
deba@1186
   234
deba@1186
   235
    checkConcept<ErasableBpGraphComponent<>,
deba@1186
   236
      ErasableBpGraphComponent<> >();
deba@1186
   237
deba@1187
   238
    checkConcept<ClearableBpGraphComponent<>,
deba@1187
   239
      ClearableBpGraphComponent<> >();
deba@1186
   240
deba@1186
   241
  }
deba@1186
   242
  { // Checking skeleton graph
deba@1186
   243
    checkConcept<BpGraph, BpGraph>();
deba@1186
   244
  }
deba@1187
   245
  { // Checking SmartBpGraph
deba@1187
   246
    checkConcept<BpGraph, SmartBpGraph>();
deba@1187
   247
    checkConcept<AlterableBpGraphComponent<>, SmartBpGraph>();
deba@1187
   248
    checkConcept<ExtendableBpGraphComponent<>, SmartBpGraph>();
deba@1187
   249
    checkConcept<ClearableBpGraphComponent<>, SmartBpGraph>();
deba@1187
   250
  }
deba@1186
   251
}
deba@1186
   252
deba@1188
   253
void checkFullBpGraph(int redNum, int blueNum) {
deba@1188
   254
  typedef FullBpGraph BpGraph;
deba@1188
   255
  BPGRAPH_TYPEDEFS(BpGraph);
deba@1188
   256
deba@1188
   257
  BpGraph G(redNum, blueNum);
deba@1188
   258
  checkGraphNodeList(G, redNum + blueNum);
deba@1188
   259
  checkGraphRedNodeList(G, redNum);
deba@1188
   260
  checkGraphBlueNodeList(G, blueNum);
deba@1188
   261
  checkGraphEdgeList(G, redNum * blueNum);
deba@1188
   262
  checkGraphArcList(G, 2 * redNum * blueNum);
deba@1188
   263
deba@1188
   264
  G.resize(redNum, blueNum);
deba@1188
   265
  checkGraphNodeList(G, redNum + blueNum);
deba@1188
   266
  checkGraphRedNodeList(G, redNum);
deba@1188
   267
  checkGraphBlueNodeList(G, blueNum);
deba@1188
   268
  checkGraphEdgeList(G, redNum * blueNum);
deba@1188
   269
  checkGraphArcList(G, 2 * redNum * blueNum);
deba@1188
   270
deba@1188
   271
  for (RedIt n(G); n != INVALID; ++n) {
deba@1188
   272
    checkGraphOutArcList(G, n, blueNum);
deba@1188
   273
    checkGraphInArcList(G, n, blueNum);
deba@1188
   274
    checkGraphIncEdgeList(G, n, blueNum);
deba@1188
   275
  }
deba@1188
   276
deba@1188
   277
  for (BlueIt n(G); n != INVALID; ++n) {
deba@1188
   278
    checkGraphOutArcList(G, n, redNum);
deba@1188
   279
    checkGraphInArcList(G, n, redNum);
deba@1188
   280
    checkGraphIncEdgeList(G, n, redNum);
deba@1188
   281
  }
deba@1188
   282
deba@1188
   283
  checkGraphConArcList(G, 2 * redNum * blueNum);
deba@1188
   284
  checkGraphConEdgeList(G, redNum * blueNum);
deba@1188
   285
deba@1188
   286
  checkArcDirections(G);
deba@1188
   287
deba@1188
   288
  checkNodeIds(G);
deba@1188
   289
  checkRedNodeIds(G);
deba@1188
   290
  checkBlueNodeIds(G);
deba@1188
   291
  checkArcIds(G);
deba@1188
   292
  checkEdgeIds(G);
deba@1188
   293
deba@1188
   294
  checkGraphNodeMap(G);
deba@1188
   295
  checkGraphRedMap(G);
deba@1188
   296
  checkGraphBlueMap(G);
deba@1188
   297
  checkGraphArcMap(G);
deba@1188
   298
  checkGraphEdgeMap(G);
deba@1188
   299
deba@1188
   300
  for (int i = 0; i < G.redNum(); ++i) {
deba@1188
   301
    check(G.red(G.redNode(i)), "Wrong node");
deba@1188
   302
    check(G.redIndex(G.redNode(i)) == i, "Wrong index");
deba@1188
   303
  }
deba@1188
   304
deba@1188
   305
  for (int i = 0; i < G.blueNum(); ++i) {
deba@1188
   306
    check(G.blue(G.blueNode(i)), "Wrong node");
deba@1188
   307
    check(G.blueIndex(G.blueNode(i)) == i, "Wrong index");
deba@1188
   308
  }
deba@1188
   309
deba@1188
   310
  for (NodeIt u(G); u != INVALID; ++u) {
deba@1188
   311
    for (NodeIt v(G); v != INVALID; ++v) {
deba@1188
   312
      Edge e = G.edge(u, v);
deba@1188
   313
      Arc a = G.arc(u, v);
deba@1188
   314
      if (G.red(u) == G.red(v)) {
deba@1188
   315
        check(e == INVALID, "Wrong edge lookup");
deba@1188
   316
        check(a == INVALID, "Wrong arc lookup");
deba@1188
   317
      } else {
deba@1188
   318
        check((G.u(e) == u && G.v(e) == v) ||
deba@1188
   319
              (G.u(e) == v && G.v(e) == u), "Wrong edge lookup");
deba@1188
   320
        check(G.source(a) == u && G.target(a) == v, "Wrong arc lookup");
deba@1188
   321
      }
deba@1188
   322
    }
deba@1188
   323
  }
deba@1188
   324
deba@1188
   325
}
deba@1188
   326
deba@1186
   327
void checkGraphs() {
deba@1187
   328
  { // Checking SmartGraph
deba@1187
   329
    checkBpGraphBuild<SmartBpGraph>();
deba@1187
   330
    checkBpGraphSnapshot<SmartBpGraph>();
deba@1187
   331
    checkBpGraphValidity<SmartBpGraph>();
deba@1187
   332
  }
deba@1188
   333
  { // Checking FullBpGraph
deba@1188
   334
    checkFullBpGraph(6, 8);
deba@1188
   335
    checkFullBpGraph(7, 4);
deba@1188
   336
  }
deba@1186
   337
}
deba@1186
   338
deba@1186
   339
int main() {
deba@1186
   340
  checkConcepts();
deba@1186
   341
  checkGraphs();
deba@1186
   342
  return 0;
deba@1186
   343
}