test/graph_test.h
author Balazs Dezso <deba@inf.elte.hu>
Sun, 14 Nov 2010 20:06:23 +0100
changeset 1187 4c89e925cfe2
parent 463 88ed40ad0d4f
child 1193 c8fa41fcc4a7
permissions -rw-r--r--
SmartBpGraph implementation (#69)
alpar@209
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
kpeter@171
     2
 *
alpar@209
     3
 * This file is a part of LEMON, a generic C++ optimization library.
kpeter@171
     4
 *
alpar@463
     5
 * Copyright (C) 2003-2009
kpeter@171
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
kpeter@171
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
kpeter@171
     8
 *
kpeter@171
     9
 * Permission to use, modify and distribute this software is granted
kpeter@171
    10
 * provided that this copyright notice appears in all copies. For
kpeter@171
    11
 * precise terms see the accompanying LICENSE file.
kpeter@171
    12
 *
kpeter@171
    13
 * This software is provided "AS IS" with no warranty of any kind,
kpeter@171
    14
 * express or implied, and with no claim as to its suitability for any
kpeter@171
    15
 * purpose.
kpeter@171
    16
 *
kpeter@171
    17
 */
kpeter@171
    18
kpeter@171
    19
#ifndef LEMON_TEST_GRAPH_TEST_H
kpeter@171
    20
#define LEMON_TEST_GRAPH_TEST_H
kpeter@171
    21
deba@228
    22
#include <set>
deba@228
    23
deba@220
    24
#include <lemon/core.h>
deba@228
    25
#include <lemon/maps.h>
deba@228
    26
kpeter@171
    27
#include "test_tools.h"
kpeter@171
    28
kpeter@171
    29
namespace lemon {
kpeter@171
    30
kpeter@171
    31
  template<class Graph>
kpeter@171
    32
  void checkGraphNodeList(const Graph &G, int cnt)
kpeter@171
    33
  {
kpeter@171
    34
    typename Graph::NodeIt n(G);
kpeter@171
    35
    for(int i=0;i<cnt;i++) {
kpeter@171
    36
      check(n!=INVALID,"Wrong Node list linking.");
kpeter@171
    37
      ++n;
kpeter@171
    38
    }
kpeter@171
    39
    check(n==INVALID,"Wrong Node list linking.");
kpeter@171
    40
    check(countNodes(G)==cnt,"Wrong Node number.");
kpeter@171
    41
  }
kpeter@171
    42
kpeter@171
    43
  template<class Graph>
deba@1187
    44
  void checkGraphRedNodeList(const Graph &G, int cnt)
deba@1187
    45
  {
deba@1187
    46
    typename Graph::RedIt n(G);
deba@1187
    47
    for(int i=0;i<cnt;i++) {
deba@1187
    48
      check(n!=INVALID,"Wrong red Node list linking.");
deba@1187
    49
      ++n;
deba@1187
    50
    }
deba@1187
    51
    check(n==INVALID,"Wrong red Node list linking.");
deba@1187
    52
    check(countRedNodes(G)==cnt,"Wrong red Node number.");
deba@1187
    53
  }
deba@1187
    54
deba@1187
    55
  template<class Graph>
deba@1187
    56
  void checkGraphBlueNodeList(const Graph &G, int cnt)
deba@1187
    57
  {
deba@1187
    58
    typename Graph::BlueIt n(G);
deba@1187
    59
    for(int i=0;i<cnt;i++) {
deba@1187
    60
      check(n!=INVALID,"Wrong blue Node list linking.");
deba@1187
    61
      ++n;
deba@1187
    62
    }
deba@1187
    63
    check(n==INVALID,"Wrong blue Node list linking.");
deba@1187
    64
    check(countBlueNodes(G)==cnt,"Wrong blue Node number.");
deba@1187
    65
  }
deba@1187
    66
deba@1187
    67
  template<class Graph>
kpeter@171
    68
  void checkGraphArcList(const Graph &G, int cnt)
kpeter@171
    69
  {
kpeter@171
    70
    typename Graph::ArcIt e(G);
kpeter@171
    71
    for(int i=0;i<cnt;i++) {
kpeter@171
    72
      check(e!=INVALID,"Wrong Arc list linking.");
deba@228
    73
      check(G.oppositeNode(G.source(e), e) == G.target(e),
deba@228
    74
            "Wrong opposite node");
deba@228
    75
      check(G.oppositeNode(G.target(e), e) == G.source(e),
deba@228
    76
            "Wrong opposite node");
kpeter@171
    77
      ++e;
kpeter@171
    78
    }
kpeter@171
    79
    check(e==INVALID,"Wrong Arc list linking.");
kpeter@171
    80
    check(countArcs(G)==cnt,"Wrong Arc number.");
kpeter@171
    81
  }
kpeter@171
    82
kpeter@171
    83
  template<class Graph>
kpeter@171
    84
  void checkGraphOutArcList(const Graph &G, typename Graph::Node n, int cnt)
kpeter@171
    85
  {
kpeter@171
    86
    typename Graph::OutArcIt e(G,n);
kpeter@171
    87
    for(int i=0;i<cnt;i++) {
kpeter@171
    88
      check(e!=INVALID,"Wrong OutArc list linking.");
kpeter@171
    89
      check(n==G.source(e),"Wrong OutArc list linking.");
deba@228
    90
      check(n==G.baseNode(e),"Wrong OutArc list linking.");
deba@228
    91
      check(G.target(e)==G.runningNode(e),"Wrong OutArc list linking.");
kpeter@171
    92
      ++e;
kpeter@171
    93
    }
kpeter@171
    94
    check(e==INVALID,"Wrong OutArc list linking.");
kpeter@171
    95
    check(countOutArcs(G,n)==cnt,"Wrong OutArc number.");
kpeter@171
    96
  }
kpeter@171
    97
kpeter@171
    98
  template<class Graph>
kpeter@171
    99
  void checkGraphInArcList(const Graph &G, typename Graph::Node n, int cnt)
kpeter@171
   100
  {
kpeter@171
   101
    typename Graph::InArcIt e(G,n);
kpeter@171
   102
    for(int i=0;i<cnt;i++) {
kpeter@171
   103
      check(e!=INVALID,"Wrong InArc list linking.");
kpeter@171
   104
      check(n==G.target(e),"Wrong InArc list linking.");
deba@228
   105
      check(n==G.baseNode(e),"Wrong OutArc list linking.");
deba@228
   106
      check(G.source(e)==G.runningNode(e),"Wrong OutArc list linking.");
kpeter@171
   107
      ++e;
kpeter@171
   108
    }
kpeter@171
   109
    check(e==INVALID,"Wrong InArc list linking.");
kpeter@171
   110
    check(countInArcs(G,n)==cnt,"Wrong InArc number.");
kpeter@171
   111
  }
kpeter@171
   112
kpeter@171
   113
  template<class Graph>
kpeter@171
   114
  void checkGraphEdgeList(const Graph &G, int cnt)
kpeter@171
   115
  {
kpeter@171
   116
    typename Graph::EdgeIt e(G);
kpeter@171
   117
    for(int i=0;i<cnt;i++) {
kpeter@171
   118
      check(e!=INVALID,"Wrong Edge list linking.");
deba@228
   119
      check(G.oppositeNode(G.u(e), e) == G.v(e), "Wrong opposite node");
deba@228
   120
      check(G.oppositeNode(G.v(e), e) == G.u(e), "Wrong opposite node");
kpeter@171
   121
      ++e;
kpeter@171
   122
    }
kpeter@171
   123
    check(e==INVALID,"Wrong Edge list linking.");
kpeter@171
   124
    check(countEdges(G)==cnt,"Wrong Edge number.");
kpeter@171
   125
  }
kpeter@171
   126
kpeter@171
   127
  template<class Graph>
kpeter@171
   128
  void checkGraphIncEdgeList(const Graph &G, typename Graph::Node n, int cnt)
kpeter@171
   129
  {
kpeter@171
   130
    typename Graph::IncEdgeIt e(G,n);
kpeter@171
   131
    for(int i=0;i<cnt;i++) {
kpeter@171
   132
      check(e!=INVALID,"Wrong IncEdge list linking.");
kpeter@171
   133
      check(n==G.u(e) || n==G.v(e),"Wrong IncEdge list linking.");
deba@228
   134
      check(n==G.baseNode(e),"Wrong OutArc list linking.");
deba@228
   135
      check(G.u(e)==G.runningNode(e) || G.v(e)==G.runningNode(e),
deba@228
   136
            "Wrong OutArc list linking.");
kpeter@171
   137
      ++e;
kpeter@171
   138
    }
kpeter@171
   139
    check(e==INVALID,"Wrong IncEdge list linking.");
kpeter@171
   140
    check(countIncEdges(G,n)==cnt,"Wrong IncEdge number.");
kpeter@171
   141
  }
kpeter@171
   142
deba@228
   143
  template <class Graph>
kpeter@387
   144
  void checkGraphIncEdgeArcLists(const Graph &G, typename Graph::Node n,
kpeter@387
   145
                                 int cnt)
kpeter@387
   146
  {
kpeter@387
   147
    checkGraphIncEdgeList(G, n, cnt);
kpeter@387
   148
    checkGraphOutArcList(G, n, cnt);
kpeter@387
   149
    checkGraphInArcList(G, n, cnt);
kpeter@387
   150
  }
kpeter@387
   151
kpeter@387
   152
  template <class Graph>
deba@228
   153
  void checkGraphConArcList(const Graph &G, int cnt) {
deba@228
   154
    int i = 0;
deba@228
   155
    for (typename Graph::NodeIt u(G); u != INVALID; ++u) {
deba@228
   156
      for (typename Graph::NodeIt v(G); v != INVALID; ++v) {
deba@228
   157
        for (ConArcIt<Graph> a(G, u, v); a != INVALID; ++a) {
deba@228
   158
          check(G.source(a) == u, "Wrong iterator.");
deba@228
   159
          check(G.target(a) == v, "Wrong iterator.");
deba@228
   160
          ++i;
deba@228
   161
        }
deba@228
   162
      }
deba@228
   163
    }
deba@228
   164
    check(cnt == i, "Wrong iterator.");
kpeter@171
   165
  }
kpeter@171
   166
kpeter@171
   167
  template <class Graph>
deba@228
   168
  void checkGraphConEdgeList(const Graph &G, int cnt) {
deba@228
   169
    int i = 0;
deba@228
   170
    for (typename Graph::NodeIt u(G); u != INVALID; ++u) {
deba@228
   171
      for (typename Graph::NodeIt v(G); v != INVALID; ++v) {
deba@228
   172
        for (ConEdgeIt<Graph> e(G, u, v); e != INVALID; ++e) {
deba@228
   173
          check((G.u(e) == u && G.v(e) == v) ||
deba@228
   174
                (G.u(e) == v && G.v(e) == u), "Wrong iterator.");
deba@228
   175
          i += u == v ? 2 : 1;
deba@228
   176
        }
deba@228
   177
      }
deba@228
   178
    }
deba@228
   179
    check(2 * cnt == i, "Wrong iterator.");
kpeter@171
   180
  }
kpeter@171
   181
deba@228
   182
  template <typename Graph>
deba@228
   183
  void checkArcDirections(const Graph& G) {
deba@228
   184
    for (typename Graph::ArcIt a(G); a != INVALID; ++a) {
deba@228
   185
      check(G.source(a) == G.target(G.oppositeArc(a)), "Wrong direction");
deba@228
   186
      check(G.target(a) == G.source(G.oppositeArc(a)), "Wrong direction");
deba@228
   187
      check(G.direct(a, G.direction(a)) == a, "Wrong direction");
kpeter@171
   188
    }
kpeter@171
   189
  }
kpeter@171
   190
deba@228
   191
  template <typename Graph>
deba@228
   192
  void checkNodeIds(const Graph& G) {
deba@1187
   193
    typedef typename Graph::Node Node;
deba@228
   194
    std::set<int> values;
deba@228
   195
    for (typename Graph::NodeIt n(G); n != INVALID; ++n) {
deba@228
   196
      check(G.nodeFromId(G.id(n)) == n, "Wrong id");
deba@228
   197
      check(values.find(G.id(n)) == values.end(), "Wrong id");
deba@228
   198
      check(G.id(n) <= G.maxNodeId(), "Wrong maximum id");
deba@228
   199
      values.insert(G.id(n));
kpeter@171
   200
    }
deba@1187
   201
    check(G.maxId(Node()) <= G.maxNodeId(), "Wrong maximum id");
deba@1187
   202
  }
deba@1187
   203
deba@1187
   204
  template <typename Graph>
deba@1187
   205
  void checkRedNodeIds(const Graph& G) {
deba@1187
   206
    typedef typename Graph::RedNode RedNode;
deba@1187
   207
    std::set<int> values;
deba@1187
   208
    for (typename Graph::RedIt n(G); n != INVALID; ++n) {
deba@1187
   209
      check(G.red(n), "Wrong partition");
deba@1187
   210
      check(G.redId(n) == G.id(RedNode(n)), "Wrong id");
deba@1187
   211
      check(values.find(G.redId(n)) == values.end(), "Wrong id");
deba@1187
   212
      check(G.redId(n) <= G.maxRedId(), "Wrong maximum id");
deba@1187
   213
      values.insert(G.id(n));
deba@1187
   214
    }
deba@1187
   215
    check(G.maxId(RedNode()) == G.maxRedId(), "Wrong maximum id");
deba@1187
   216
  }
deba@1187
   217
deba@1187
   218
  template <typename Graph>
deba@1187
   219
  void checkBlueNodeIds(const Graph& G) {
deba@1187
   220
    typedef typename Graph::BlueNode BlueNode;
deba@1187
   221
    std::set<int> values;
deba@1187
   222
    for (typename Graph::BlueIt n(G); n != INVALID; ++n) {
deba@1187
   223
      check(G.blue(n), "Wrong partition");
deba@1187
   224
      check(G.blueId(n) == G.id(BlueNode(n)), "Wrong id");
deba@1187
   225
      check(values.find(G.blueId(n)) == values.end(), "Wrong id");
deba@1187
   226
      check(G.blueId(n) <= G.maxBlueId(), "Wrong maximum id");
deba@1187
   227
      values.insert(G.id(n));
deba@1187
   228
    }
deba@1187
   229
    check(G.maxId(BlueNode()) == G.maxBlueId(), "Wrong maximum id");
kpeter@171
   230
  }
kpeter@171
   231
deba@228
   232
  template <typename Graph>
deba@228
   233
  void checkArcIds(const Graph& G) {
deba@1187
   234
    typedef typename Graph::Arc Arc;
deba@228
   235
    std::set<int> values;
deba@228
   236
    for (typename Graph::ArcIt a(G); a != INVALID; ++a) {
deba@228
   237
      check(G.arcFromId(G.id(a)) == a, "Wrong id");
deba@228
   238
      check(values.find(G.id(a)) == values.end(), "Wrong id");
deba@228
   239
      check(G.id(a) <= G.maxArcId(), "Wrong maximum id");
deba@228
   240
      values.insert(G.id(a));
deba@228
   241
    }
deba@1187
   242
    check(G.maxId(Arc()) <= G.maxArcId(), "Wrong maximum id");
kpeter@171
   243
  }
alpar@209
   244
deba@228
   245
  template <typename Graph>
deba@228
   246
  void checkEdgeIds(const Graph& G) {
deba@1187
   247
    typedef typename Graph::Edge Edge;
deba@228
   248
    std::set<int> values;
deba@228
   249
    for (typename Graph::EdgeIt e(G); e != INVALID; ++e) {
deba@228
   250
      check(G.edgeFromId(G.id(e)) == e, "Wrong id");
deba@228
   251
      check(values.find(G.id(e)) == values.end(), "Wrong id");
deba@228
   252
      check(G.id(e) <= G.maxEdgeId(), "Wrong maximum id");
deba@228
   253
      values.insert(G.id(e));
deba@228
   254
    }
deba@1187
   255
    check(G.maxId(Edge()) <= G.maxEdgeId(), "Wrong maximum id");
kpeter@171
   256
  }
kpeter@171
   257
deba@228
   258
  template <typename Graph>
deba@228
   259
  void checkGraphNodeMap(const Graph& G) {
deba@228
   260
    typedef typename Graph::Node Node;
deba@228
   261
    typedef typename Graph::NodeIt NodeIt;
deba@228
   262
deba@228
   263
    typedef typename Graph::template NodeMap<int> IntNodeMap;
deba@228
   264
    IntNodeMap map(G, 42);
deba@228
   265
    for (NodeIt it(G); it != INVALID; ++it) {
deba@228
   266
      check(map[it] == 42, "Wrong map constructor.");
deba@228
   267
    }
deba@228
   268
    int s = 0;
deba@228
   269
    for (NodeIt it(G); it != INVALID; ++it) {
deba@228
   270
      map[it] = 0;
deba@228
   271
      check(map[it] == 0, "Wrong operator[].");
deba@228
   272
      map.set(it, s);
deba@228
   273
      check(map[it] == s, "Wrong set.");
deba@228
   274
      ++s;
deba@228
   275
    }
deba@228
   276
    s = s * (s - 1) / 2;
deba@228
   277
    for (NodeIt it(G); it != INVALID; ++it) {
deba@228
   278
      s -= map[it];
deba@228
   279
    }
deba@228
   280
    check(s == 0, "Wrong sum.");
deba@228
   281
kpeter@263
   282
    // map = constMap<Node>(12);
kpeter@263
   283
    // for (NodeIt it(G); it != INVALID; ++it) {
kpeter@263
   284
    //   check(map[it] == 12, "Wrong operator[].");
kpeter@263
   285
    // }
deba@228
   286
  }
deba@228
   287
deba@228
   288
  template <typename Graph>
deba@1187
   289
  void checkGraphRedMap(const Graph& G) {
deba@1187
   290
    typedef typename Graph::Node Node;
deba@1187
   291
    typedef typename Graph::RedIt RedIt;
deba@1187
   292
deba@1187
   293
    typedef typename Graph::template RedMap<int> IntRedMap;
deba@1187
   294
    IntRedMap map(G, 42);
deba@1187
   295
    for (RedIt it(G); it != INVALID; ++it) {
deba@1187
   296
      check(map[it] == 42, "Wrong map constructor.");
deba@1187
   297
    }
deba@1187
   298
    int s = 0;
deba@1187
   299
    for (RedIt it(G); it != INVALID; ++it) {
deba@1187
   300
      map[it] = 0;
deba@1187
   301
      check(map[it] == 0, "Wrong operator[].");
deba@1187
   302
      map.set(it, s);
deba@1187
   303
      check(map[it] == s, "Wrong set.");
deba@1187
   304
      ++s;
deba@1187
   305
    }
deba@1187
   306
    s = s * (s - 1) / 2;
deba@1187
   307
    for (RedIt it(G); it != INVALID; ++it) {
deba@1187
   308
      s -= map[it];
deba@1187
   309
    }
deba@1187
   310
    check(s == 0, "Wrong sum.");
deba@1187
   311
deba@1187
   312
    // map = constMap<Node>(12);
deba@1187
   313
    // for (NodeIt it(G); it != INVALID; ++it) {
deba@1187
   314
    //   check(map[it] == 12, "Wrong operator[].");
deba@1187
   315
    // }
deba@1187
   316
  }
deba@1187
   317
deba@1187
   318
  template <typename Graph>
deba@1187
   319
  void checkGraphBlueMap(const Graph& G) {
deba@1187
   320
    typedef typename Graph::Node Node;
deba@1187
   321
    typedef typename Graph::BlueIt BlueIt;
deba@1187
   322
deba@1187
   323
    typedef typename Graph::template BlueMap<int> IntBlueMap;
deba@1187
   324
    IntBlueMap map(G, 42);
deba@1187
   325
    for (BlueIt it(G); it != INVALID; ++it) {
deba@1187
   326
      check(map[it] == 42, "Wrong map constructor.");
deba@1187
   327
    }
deba@1187
   328
    int s = 0;
deba@1187
   329
    for (BlueIt it(G); it != INVALID; ++it) {
deba@1187
   330
      map[it] = 0;
deba@1187
   331
      check(map[it] == 0, "Wrong operator[].");
deba@1187
   332
      map.set(it, s);
deba@1187
   333
      check(map[it] == s, "Wrong set.");
deba@1187
   334
      ++s;
deba@1187
   335
    }
deba@1187
   336
    s = s * (s - 1) / 2;
deba@1187
   337
    for (BlueIt it(G); it != INVALID; ++it) {
deba@1187
   338
      s -= map[it];
deba@1187
   339
    }
deba@1187
   340
    check(s == 0, "Wrong sum.");
deba@1187
   341
deba@1187
   342
    // map = constMap<Node>(12);
deba@1187
   343
    // for (NodeIt it(G); it != INVALID; ++it) {
deba@1187
   344
    //   check(map[it] == 12, "Wrong operator[].");
deba@1187
   345
    // }
deba@1187
   346
  }
deba@1187
   347
deba@1187
   348
  template <typename Graph>
deba@228
   349
  void checkGraphArcMap(const Graph& G) {
deba@228
   350
    typedef typename Graph::Arc Arc;
deba@228
   351
    typedef typename Graph::ArcIt ArcIt;
deba@228
   352
deba@228
   353
    typedef typename Graph::template ArcMap<int> IntArcMap;
deba@228
   354
    IntArcMap map(G, 42);
deba@228
   355
    for (ArcIt it(G); it != INVALID; ++it) {
deba@228
   356
      check(map[it] == 42, "Wrong map constructor.");
deba@228
   357
    }
deba@228
   358
    int s = 0;
deba@228
   359
    for (ArcIt it(G); it != INVALID; ++it) {
deba@228
   360
      map[it] = 0;
deba@228
   361
      check(map[it] == 0, "Wrong operator[].");
deba@228
   362
      map.set(it, s);
deba@228
   363
      check(map[it] == s, "Wrong set.");
deba@228
   364
      ++s;
deba@228
   365
    }
deba@228
   366
    s = s * (s - 1) / 2;
deba@228
   367
    for (ArcIt it(G); it != INVALID; ++it) {
deba@228
   368
      s -= map[it];
deba@228
   369
    }
deba@228
   370
    check(s == 0, "Wrong sum.");
deba@228
   371
kpeter@263
   372
    // map = constMap<Arc>(12);
kpeter@263
   373
    // for (ArcIt it(G); it != INVALID; ++it) {
kpeter@263
   374
    //   check(map[it] == 12, "Wrong operator[].");
kpeter@263
   375
    // }
deba@228
   376
  }
deba@228
   377
deba@228
   378
  template <typename Graph>
deba@228
   379
  void checkGraphEdgeMap(const Graph& G) {
deba@228
   380
    typedef typename Graph::Edge Edge;
deba@228
   381
    typedef typename Graph::EdgeIt EdgeIt;
deba@228
   382
deba@228
   383
    typedef typename Graph::template EdgeMap<int> IntEdgeMap;
deba@228
   384
    IntEdgeMap map(G, 42);
deba@228
   385
    for (EdgeIt it(G); it != INVALID; ++it) {
deba@228
   386
      check(map[it] == 42, "Wrong map constructor.");
deba@228
   387
    }
deba@228
   388
    int s = 0;
deba@228
   389
    for (EdgeIt it(G); it != INVALID; ++it) {
deba@228
   390
      map[it] = 0;
deba@228
   391
      check(map[it] == 0, "Wrong operator[].");
deba@228
   392
      map.set(it, s);
deba@228
   393
      check(map[it] == s, "Wrong set.");
deba@228
   394
      ++s;
deba@228
   395
    }
deba@228
   396
    s = s * (s - 1) / 2;
deba@228
   397
    for (EdgeIt it(G); it != INVALID; ++it) {
deba@228
   398
      s -= map[it];
deba@228
   399
    }
deba@228
   400
    check(s == 0, "Wrong sum.");
deba@228
   401
kpeter@263
   402
    // map = constMap<Edge>(12);
kpeter@263
   403
    // for (EdgeIt it(G); it != INVALID; ++it) {
kpeter@263
   404
    //   check(map[it] == 12, "Wrong operator[].");
kpeter@263
   405
    // }
deba@228
   406
  }
deba@228
   407
deba@228
   408
kpeter@171
   409
} //namespace lemon
kpeter@171
   410
kpeter@171
   411
#endif