test/graph_test.h
author Alpar Juttner <alpar@cs.elte.hu>
Fri, 26 Sep 2008 09:52:28 +0200
changeset 281 e9b4fbe163f5
parent 228 b6732e0d38c5
child 387 49d9a36b3b84
permissions -rw-r--r--
Merge
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
 *
kpeter@171
     5
 * Copyright (C) 2003-2008
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>
kpeter@171
    44
  void checkGraphArcList(const Graph &G, int cnt)
kpeter@171
    45
  {
kpeter@171
    46
    typename Graph::ArcIt e(G);
kpeter@171
    47
    for(int i=0;i<cnt;i++) {
kpeter@171
    48
      check(e!=INVALID,"Wrong Arc list linking.");
deba@228
    49
      check(G.oppositeNode(G.source(e), e) == G.target(e),
deba@228
    50
            "Wrong opposite node");
deba@228
    51
      check(G.oppositeNode(G.target(e), e) == G.source(e),
deba@228
    52
            "Wrong opposite node");
kpeter@171
    53
      ++e;
kpeter@171
    54
    }
kpeter@171
    55
    check(e==INVALID,"Wrong Arc list linking.");
kpeter@171
    56
    check(countArcs(G)==cnt,"Wrong Arc number.");
kpeter@171
    57
  }
kpeter@171
    58
kpeter@171
    59
  template<class Graph>
kpeter@171
    60
  void checkGraphOutArcList(const Graph &G, typename Graph::Node n, int cnt)
kpeter@171
    61
  {
kpeter@171
    62
    typename Graph::OutArcIt e(G,n);
kpeter@171
    63
    for(int i=0;i<cnt;i++) {
kpeter@171
    64
      check(e!=INVALID,"Wrong OutArc list linking.");
kpeter@171
    65
      check(n==G.source(e),"Wrong OutArc list linking.");
deba@228
    66
      check(n==G.baseNode(e),"Wrong OutArc list linking.");
deba@228
    67
      check(G.target(e)==G.runningNode(e),"Wrong OutArc list linking.");
kpeter@171
    68
      ++e;
kpeter@171
    69
    }
kpeter@171
    70
    check(e==INVALID,"Wrong OutArc list linking.");
kpeter@171
    71
    check(countOutArcs(G,n)==cnt,"Wrong OutArc number.");
kpeter@171
    72
  }
kpeter@171
    73
kpeter@171
    74
  template<class Graph>
kpeter@171
    75
  void checkGraphInArcList(const Graph &G, typename Graph::Node n, int cnt)
kpeter@171
    76
  {
kpeter@171
    77
    typename Graph::InArcIt e(G,n);
kpeter@171
    78
    for(int i=0;i<cnt;i++) {
kpeter@171
    79
      check(e!=INVALID,"Wrong InArc list linking.");
kpeter@171
    80
      check(n==G.target(e),"Wrong InArc list linking.");
deba@228
    81
      check(n==G.baseNode(e),"Wrong OutArc list linking.");
deba@228
    82
      check(G.source(e)==G.runningNode(e),"Wrong OutArc list linking.");
kpeter@171
    83
      ++e;
kpeter@171
    84
    }
kpeter@171
    85
    check(e==INVALID,"Wrong InArc list linking.");
kpeter@171
    86
    check(countInArcs(G,n)==cnt,"Wrong InArc number.");
kpeter@171
    87
  }
kpeter@171
    88
kpeter@171
    89
  template<class Graph>
kpeter@171
    90
  void checkGraphEdgeList(const Graph &G, int cnt)
kpeter@171
    91
  {
kpeter@171
    92
    typename Graph::EdgeIt e(G);
kpeter@171
    93
    for(int i=0;i<cnt;i++) {
kpeter@171
    94
      check(e!=INVALID,"Wrong Edge list linking.");
deba@228
    95
      check(G.oppositeNode(G.u(e), e) == G.v(e), "Wrong opposite node");
deba@228
    96
      check(G.oppositeNode(G.v(e), e) == G.u(e), "Wrong opposite node");
kpeter@171
    97
      ++e;
kpeter@171
    98
    }
kpeter@171
    99
    check(e==INVALID,"Wrong Edge list linking.");
kpeter@171
   100
    check(countEdges(G)==cnt,"Wrong Edge number.");
kpeter@171
   101
  }
kpeter@171
   102
kpeter@171
   103
  template<class Graph>
kpeter@171
   104
  void checkGraphIncEdgeList(const Graph &G, typename Graph::Node n, int cnt)
kpeter@171
   105
  {
kpeter@171
   106
    typename Graph::IncEdgeIt e(G,n);
kpeter@171
   107
    for(int i=0;i<cnt;i++) {
kpeter@171
   108
      check(e!=INVALID,"Wrong IncEdge list linking.");
kpeter@171
   109
      check(n==G.u(e) || n==G.v(e),"Wrong IncEdge list linking.");
deba@228
   110
      check(n==G.baseNode(e),"Wrong OutArc list linking.");
deba@228
   111
      check(G.u(e)==G.runningNode(e) || G.v(e)==G.runningNode(e),
deba@228
   112
            "Wrong OutArc list linking.");
kpeter@171
   113
      ++e;
kpeter@171
   114
    }
kpeter@171
   115
    check(e==INVALID,"Wrong IncEdge list linking.");
kpeter@171
   116
    check(countIncEdges(G,n)==cnt,"Wrong IncEdge number.");
kpeter@171
   117
  }
kpeter@171
   118
deba@228
   119
  template <class Graph>
deba@228
   120
  void checkGraphConArcList(const Graph &G, int cnt) {
deba@228
   121
    int i = 0;
deba@228
   122
    for (typename Graph::NodeIt u(G); u != INVALID; ++u) {
deba@228
   123
      for (typename Graph::NodeIt v(G); v != INVALID; ++v) {
deba@228
   124
        for (ConArcIt<Graph> a(G, u, v); a != INVALID; ++a) {
deba@228
   125
          check(G.source(a) == u, "Wrong iterator.");
deba@228
   126
          check(G.target(a) == v, "Wrong iterator.");
deba@228
   127
          ++i;
deba@228
   128
        }
deba@228
   129
      }
deba@228
   130
    }
deba@228
   131
    check(cnt == i, "Wrong iterator.");
kpeter@171
   132
  }
kpeter@171
   133
kpeter@171
   134
  template <class Graph>
deba@228
   135
  void checkGraphConEdgeList(const Graph &G, int cnt) {
deba@228
   136
    int i = 0;
deba@228
   137
    for (typename Graph::NodeIt u(G); u != INVALID; ++u) {
deba@228
   138
      for (typename Graph::NodeIt v(G); v != INVALID; ++v) {
deba@228
   139
        for (ConEdgeIt<Graph> e(G, u, v); e != INVALID; ++e) {
deba@228
   140
          check((G.u(e) == u && G.v(e) == v) ||
deba@228
   141
                (G.u(e) == v && G.v(e) == u), "Wrong iterator.");
deba@228
   142
          i += u == v ? 2 : 1;
deba@228
   143
        }
deba@228
   144
      }
deba@228
   145
    }
deba@228
   146
    check(2 * cnt == i, "Wrong iterator.");
kpeter@171
   147
  }
kpeter@171
   148
deba@228
   149
  template <typename Graph>
deba@228
   150
  void checkArcDirections(const Graph& G) {
deba@228
   151
    for (typename Graph::ArcIt a(G); a != INVALID; ++a) {
deba@228
   152
      check(G.source(a) == G.target(G.oppositeArc(a)), "Wrong direction");
deba@228
   153
      check(G.target(a) == G.source(G.oppositeArc(a)), "Wrong direction");
deba@228
   154
      check(G.direct(a, G.direction(a)) == a, "Wrong direction");
kpeter@171
   155
    }
kpeter@171
   156
  }
kpeter@171
   157
deba@228
   158
  template <typename Graph>
deba@228
   159
  void checkNodeIds(const Graph& G) {
deba@228
   160
    std::set<int> values;
deba@228
   161
    for (typename Graph::NodeIt n(G); n != INVALID; ++n) {
deba@228
   162
      check(G.nodeFromId(G.id(n)) == n, "Wrong id");
deba@228
   163
      check(values.find(G.id(n)) == values.end(), "Wrong id");
deba@228
   164
      check(G.id(n) <= G.maxNodeId(), "Wrong maximum id");
deba@228
   165
      values.insert(G.id(n));
kpeter@171
   166
    }
kpeter@171
   167
  }
kpeter@171
   168
deba@228
   169
  template <typename Graph>
deba@228
   170
  void checkArcIds(const Graph& G) {
deba@228
   171
    std::set<int> values;
deba@228
   172
    for (typename Graph::ArcIt a(G); a != INVALID; ++a) {
deba@228
   173
      check(G.arcFromId(G.id(a)) == a, "Wrong id");
deba@228
   174
      check(values.find(G.id(a)) == values.end(), "Wrong id");
deba@228
   175
      check(G.id(a) <= G.maxArcId(), "Wrong maximum id");
deba@228
   176
      values.insert(G.id(a));
deba@228
   177
    }
kpeter@171
   178
  }
alpar@209
   179
deba@228
   180
  template <typename Graph>
deba@228
   181
  void checkEdgeIds(const Graph& G) {
deba@228
   182
    std::set<int> values;
deba@228
   183
    for (typename Graph::EdgeIt e(G); e != INVALID; ++e) {
deba@228
   184
      check(G.edgeFromId(G.id(e)) == e, "Wrong id");
deba@228
   185
      check(values.find(G.id(e)) == values.end(), "Wrong id");
deba@228
   186
      check(G.id(e) <= G.maxEdgeId(), "Wrong maximum id");
deba@228
   187
      values.insert(G.id(e));
deba@228
   188
    }
kpeter@171
   189
  }
kpeter@171
   190
deba@228
   191
  template <typename Graph>
deba@228
   192
  void checkGraphNodeMap(const Graph& G) {
deba@228
   193
    typedef typename Graph::Node Node;
deba@228
   194
    typedef typename Graph::NodeIt NodeIt;
deba@228
   195
deba@228
   196
    typedef typename Graph::template NodeMap<int> IntNodeMap;
deba@228
   197
    IntNodeMap map(G, 42);
deba@228
   198
    for (NodeIt it(G); it != INVALID; ++it) {
deba@228
   199
      check(map[it] == 42, "Wrong map constructor.");
deba@228
   200
    }
deba@228
   201
    int s = 0;
deba@228
   202
    for (NodeIt it(G); it != INVALID; ++it) {
deba@228
   203
      map[it] = 0;
deba@228
   204
      check(map[it] == 0, "Wrong operator[].");
deba@228
   205
      map.set(it, s);
deba@228
   206
      check(map[it] == s, "Wrong set.");
deba@228
   207
      ++s;
deba@228
   208
    }
deba@228
   209
    s = s * (s - 1) / 2;
deba@228
   210
    for (NodeIt it(G); it != INVALID; ++it) {
deba@228
   211
      s -= map[it];
deba@228
   212
    }
deba@228
   213
    check(s == 0, "Wrong sum.");
deba@228
   214
kpeter@263
   215
    // map = constMap<Node>(12);
kpeter@263
   216
    // for (NodeIt it(G); it != INVALID; ++it) {
kpeter@263
   217
    //   check(map[it] == 12, "Wrong operator[].");
kpeter@263
   218
    // }
deba@228
   219
  }
deba@228
   220
deba@228
   221
  template <typename Graph>
deba@228
   222
  void checkGraphArcMap(const Graph& G) {
deba@228
   223
    typedef typename Graph::Arc Arc;
deba@228
   224
    typedef typename Graph::ArcIt ArcIt;
deba@228
   225
deba@228
   226
    typedef typename Graph::template ArcMap<int> IntArcMap;
deba@228
   227
    IntArcMap map(G, 42);
deba@228
   228
    for (ArcIt it(G); it != INVALID; ++it) {
deba@228
   229
      check(map[it] == 42, "Wrong map constructor.");
deba@228
   230
    }
deba@228
   231
    int s = 0;
deba@228
   232
    for (ArcIt it(G); it != INVALID; ++it) {
deba@228
   233
      map[it] = 0;
deba@228
   234
      check(map[it] == 0, "Wrong operator[].");
deba@228
   235
      map.set(it, s);
deba@228
   236
      check(map[it] == s, "Wrong set.");
deba@228
   237
      ++s;
deba@228
   238
    }
deba@228
   239
    s = s * (s - 1) / 2;
deba@228
   240
    for (ArcIt it(G); it != INVALID; ++it) {
deba@228
   241
      s -= map[it];
deba@228
   242
    }
deba@228
   243
    check(s == 0, "Wrong sum.");
deba@228
   244
kpeter@263
   245
    // map = constMap<Arc>(12);
kpeter@263
   246
    // for (ArcIt it(G); it != INVALID; ++it) {
kpeter@263
   247
    //   check(map[it] == 12, "Wrong operator[].");
kpeter@263
   248
    // }
deba@228
   249
  }
deba@228
   250
deba@228
   251
  template <typename Graph>
deba@228
   252
  void checkGraphEdgeMap(const Graph& G) {
deba@228
   253
    typedef typename Graph::Edge Edge;
deba@228
   254
    typedef typename Graph::EdgeIt EdgeIt;
deba@228
   255
deba@228
   256
    typedef typename Graph::template EdgeMap<int> IntEdgeMap;
deba@228
   257
    IntEdgeMap map(G, 42);
deba@228
   258
    for (EdgeIt it(G); it != INVALID; ++it) {
deba@228
   259
      check(map[it] == 42, "Wrong map constructor.");
deba@228
   260
    }
deba@228
   261
    int s = 0;
deba@228
   262
    for (EdgeIt it(G); it != INVALID; ++it) {
deba@228
   263
      map[it] = 0;
deba@228
   264
      check(map[it] == 0, "Wrong operator[].");
deba@228
   265
      map.set(it, s);
deba@228
   266
      check(map[it] == s, "Wrong set.");
deba@228
   267
      ++s;
deba@228
   268
    }
deba@228
   269
    s = s * (s - 1) / 2;
deba@228
   270
    for (EdgeIt it(G); it != INVALID; ++it) {
deba@228
   271
      s -= map[it];
deba@228
   272
    }
deba@228
   273
    check(s == 0, "Wrong sum.");
deba@228
   274
kpeter@263
   275
    // map = constMap<Edge>(12);
kpeter@263
   276
    // for (EdgeIt it(G); it != INVALID; ++it) {
kpeter@263
   277
    //   check(map[it] == 12, "Wrong operator[].");
kpeter@263
   278
    // }
deba@228
   279
  }
deba@228
   280
deba@228
   281
kpeter@171
   282
} //namespace lemon
kpeter@171
   283
kpeter@171
   284
#endif