test/graph_test.h
author Peter Kovacs <kpeter@inf.elte.hu>
Sat, 01 Nov 2008 19:22:18 +0100
changeset 354 80a4d0742e98
parent 220 a5d8c039f218
child 263 be8a861d3bb7
permissions -rw-r--r--
Improvements related to full graphs (#57)
     1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library.
     4  *
     5  * Copyright (C) 2003-2008
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8  *
     9  * Permission to use, modify and distribute this software is granted
    10  * provided that this copyright notice appears in all copies. For
    11  * precise terms see the accompanying LICENSE file.
    12  *
    13  * This software is provided "AS IS" with no warranty of any kind,
    14  * express or implied, and with no claim as to its suitability for any
    15  * purpose.
    16  *
    17  */
    18 
    19 #ifndef LEMON_TEST_GRAPH_TEST_H
    20 #define LEMON_TEST_GRAPH_TEST_H
    21 
    22 #include <set>
    23 
    24 #include <lemon/core.h>
    25 #include <lemon/maps.h>
    26 
    27 #include "test_tools.h"
    28 
    29 namespace lemon {
    30 
    31   template<class Graph>
    32   void checkGraphNodeList(const Graph &G, int cnt)
    33   {
    34     typename Graph::NodeIt n(G);
    35     for(int i=0;i<cnt;i++) {
    36       check(n!=INVALID,"Wrong Node list linking.");
    37       ++n;
    38     }
    39     check(n==INVALID,"Wrong Node list linking.");
    40     check(countNodes(G)==cnt,"Wrong Node number.");
    41   }
    42 
    43   template<class Graph>
    44   void checkGraphArcList(const Graph &G, int cnt)
    45   {
    46     typename Graph::ArcIt e(G);
    47     for(int i=0;i<cnt;i++) {
    48       check(e!=INVALID,"Wrong Arc list linking.");
    49       check(G.oppositeNode(G.source(e), e) == G.target(e),
    50             "Wrong opposite node");
    51       check(G.oppositeNode(G.target(e), e) == G.source(e),
    52             "Wrong opposite node");
    53       ++e;
    54     }
    55     check(e==INVALID,"Wrong Arc list linking.");
    56     check(countArcs(G)==cnt,"Wrong Arc number.");
    57   }
    58 
    59   template<class Graph>
    60   void checkGraphOutArcList(const Graph &G, typename Graph::Node n, int cnt)
    61   {
    62     typename Graph::OutArcIt e(G,n);
    63     for(int i=0;i<cnt;i++) {
    64       check(e!=INVALID,"Wrong OutArc list linking.");
    65       check(n==G.source(e),"Wrong OutArc list linking.");
    66       check(n==G.baseNode(e),"Wrong OutArc list linking.");
    67       check(G.target(e)==G.runningNode(e),"Wrong OutArc list linking.");
    68       ++e;
    69     }
    70     check(e==INVALID,"Wrong OutArc list linking.");
    71     check(countOutArcs(G,n)==cnt,"Wrong OutArc number.");
    72   }
    73 
    74   template<class Graph>
    75   void checkGraphInArcList(const Graph &G, typename Graph::Node n, int cnt)
    76   {
    77     typename Graph::InArcIt e(G,n);
    78     for(int i=0;i<cnt;i++) {
    79       check(e!=INVALID,"Wrong InArc list linking.");
    80       check(n==G.target(e),"Wrong InArc list linking.");
    81       check(n==G.baseNode(e),"Wrong OutArc list linking.");
    82       check(G.source(e)==G.runningNode(e),"Wrong OutArc list linking.");
    83       ++e;
    84     }
    85     check(e==INVALID,"Wrong InArc list linking.");
    86     check(countInArcs(G,n)==cnt,"Wrong InArc number.");
    87   }
    88 
    89   template<class Graph>
    90   void checkGraphEdgeList(const Graph &G, int cnt)
    91   {
    92     typename Graph::EdgeIt e(G);
    93     for(int i=0;i<cnt;i++) {
    94       check(e!=INVALID,"Wrong Edge list linking.");
    95       check(G.oppositeNode(G.u(e), e) == G.v(e), "Wrong opposite node");
    96       check(G.oppositeNode(G.v(e), e) == G.u(e), "Wrong opposite node");
    97       ++e;
    98     }
    99     check(e==INVALID,"Wrong Edge list linking.");
   100     check(countEdges(G)==cnt,"Wrong Edge number.");
   101   }
   102 
   103   template<class Graph>
   104   void checkGraphIncEdgeList(const Graph &G, typename Graph::Node n, int cnt)
   105   {
   106     typename Graph::IncEdgeIt e(G,n);
   107     for(int i=0;i<cnt;i++) {
   108       check(e!=INVALID,"Wrong IncEdge list linking.");
   109       check(n==G.u(e) || n==G.v(e),"Wrong IncEdge list linking.");
   110       check(n==G.baseNode(e),"Wrong OutArc list linking.");
   111       check(G.u(e)==G.runningNode(e) || G.v(e)==G.runningNode(e),
   112             "Wrong OutArc list linking.");
   113       ++e;
   114     }
   115     check(e==INVALID,"Wrong IncEdge list linking.");
   116     check(countIncEdges(G,n)==cnt,"Wrong IncEdge number.");
   117   }
   118 
   119   template <class Graph>
   120   void checkGraphConArcList(const Graph &G, int cnt) {
   121     int i = 0;
   122     for (typename Graph::NodeIt u(G); u != INVALID; ++u) {
   123       for (typename Graph::NodeIt v(G); v != INVALID; ++v) {
   124         for (ConArcIt<Graph> a(G, u, v); a != INVALID; ++a) {
   125           check(G.source(a) == u, "Wrong iterator.");
   126           check(G.target(a) == v, "Wrong iterator.");
   127           ++i;
   128         }
   129       }
   130     }
   131     check(cnt == i, "Wrong iterator.");
   132   }
   133 
   134   template <class Graph>
   135   void checkGraphConEdgeList(const Graph &G, int cnt) {
   136     int i = 0;
   137     for (typename Graph::NodeIt u(G); u != INVALID; ++u) {
   138       for (typename Graph::NodeIt v(G); v != INVALID; ++v) {
   139         for (ConEdgeIt<Graph> e(G, u, v); e != INVALID; ++e) {
   140           check((G.u(e) == u && G.v(e) == v) ||
   141                 (G.u(e) == v && G.v(e) == u), "Wrong iterator.");
   142           i += u == v ? 2 : 1;
   143         }
   144       }
   145     }
   146     check(2 * cnt == i, "Wrong iterator.");
   147   }
   148 
   149   template <typename Graph>
   150   void checkArcDirections(const Graph& G) {
   151     for (typename Graph::ArcIt a(G); a != INVALID; ++a) {
   152       check(G.source(a) == G.target(G.oppositeArc(a)), "Wrong direction");
   153       check(G.target(a) == G.source(G.oppositeArc(a)), "Wrong direction");
   154       check(G.direct(a, G.direction(a)) == a, "Wrong direction");
   155     }
   156   }
   157 
   158   template <typename Graph>
   159   void checkNodeIds(const Graph& G) {
   160     std::set<int> values;
   161     for (typename Graph::NodeIt n(G); n != INVALID; ++n) {
   162       check(G.nodeFromId(G.id(n)) == n, "Wrong id");
   163       check(values.find(G.id(n)) == values.end(), "Wrong id");
   164       check(G.id(n) <= G.maxNodeId(), "Wrong maximum id");
   165       values.insert(G.id(n));
   166     }
   167   }
   168 
   169   template <typename Graph>
   170   void checkArcIds(const Graph& G) {
   171     std::set<int> values;
   172     for (typename Graph::ArcIt a(G); a != INVALID; ++a) {
   173       check(G.arcFromId(G.id(a)) == a, "Wrong id");
   174       check(values.find(G.id(a)) == values.end(), "Wrong id");
   175       check(G.id(a) <= G.maxArcId(), "Wrong maximum id");
   176       values.insert(G.id(a));
   177     }
   178   }
   179 
   180   template <typename Graph>
   181   void checkEdgeIds(const Graph& G) {
   182     std::set<int> values;
   183     for (typename Graph::EdgeIt e(G); e != INVALID; ++e) {
   184       check(G.edgeFromId(G.id(e)) == e, "Wrong id");
   185       check(values.find(G.id(e)) == values.end(), "Wrong id");
   186       check(G.id(e) <= G.maxEdgeId(), "Wrong maximum id");
   187       values.insert(G.id(e));
   188     }
   189   }
   190 
   191   template <typename Graph>
   192   void checkGraphNodeMap(const Graph& G) {
   193     typedef typename Graph::Node Node;
   194     typedef typename Graph::NodeIt NodeIt;
   195 
   196     typedef typename Graph::template NodeMap<int> IntNodeMap;
   197     IntNodeMap map(G, 42);
   198     for (NodeIt it(G); it != INVALID; ++it) {
   199       check(map[it] == 42, "Wrong map constructor.");
   200     }
   201     int s = 0;
   202     for (NodeIt it(G); it != INVALID; ++it) {
   203       map[it] = 0;
   204       check(map[it] == 0, "Wrong operator[].");
   205       map.set(it, s);
   206       check(map[it] == s, "Wrong set.");
   207       ++s;
   208     }
   209     s = s * (s - 1) / 2;
   210     for (NodeIt it(G); it != INVALID; ++it) {
   211       s -= map[it];
   212     }
   213     check(s == 0, "Wrong sum.");
   214 
   215     map = constMap<Node>(12);
   216     for (NodeIt it(G); it != INVALID; ++it) {
   217       check(map[it] == 12, "Wrong operator[].");
   218     }
   219   }
   220 
   221   template <typename Graph>
   222   void checkGraphArcMap(const Graph& G) {
   223     typedef typename Graph::Arc Arc;
   224     typedef typename Graph::ArcIt ArcIt;
   225 
   226     typedef typename Graph::template ArcMap<int> IntArcMap;
   227     IntArcMap map(G, 42);
   228     for (ArcIt it(G); it != INVALID; ++it) {
   229       check(map[it] == 42, "Wrong map constructor.");
   230     }
   231     int s = 0;
   232     for (ArcIt it(G); it != INVALID; ++it) {
   233       map[it] = 0;
   234       check(map[it] == 0, "Wrong operator[].");
   235       map.set(it, s);
   236       check(map[it] == s, "Wrong set.");
   237       ++s;
   238     }
   239     s = s * (s - 1) / 2;
   240     for (ArcIt it(G); it != INVALID; ++it) {
   241       s -= map[it];
   242     }
   243     check(s == 0, "Wrong sum.");
   244 
   245     map = constMap<Arc>(12);
   246     for (ArcIt it(G); it != INVALID; ++it) {
   247       check(map[it] == 12, "Wrong operator[].");
   248     }
   249   }
   250 
   251   template <typename Graph>
   252   void checkGraphEdgeMap(const Graph& G) {
   253     typedef typename Graph::Edge Edge;
   254     typedef typename Graph::EdgeIt EdgeIt;
   255 
   256     typedef typename Graph::template EdgeMap<int> IntEdgeMap;
   257     IntEdgeMap map(G, 42);
   258     for (EdgeIt it(G); it != INVALID; ++it) {
   259       check(map[it] == 42, "Wrong map constructor.");
   260     }
   261     int s = 0;
   262     for (EdgeIt it(G); it != INVALID; ++it) {
   263       map[it] = 0;
   264       check(map[it] == 0, "Wrong operator[].");
   265       map.set(it, s);
   266       check(map[it] == s, "Wrong set.");
   267       ++s;
   268     }
   269     s = s * (s - 1) / 2;
   270     for (EdgeIt it(G); it != INVALID; ++it) {
   271       s -= map[it];
   272     }
   273     check(s == 0, "Wrong sum.");
   274 
   275     map = constMap<Edge>(12);
   276     for (EdgeIt it(G); it != INVALID; ++it) {
   277       check(map[it] == 12, "Wrong operator[].");
   278     }
   279   }
   280 
   281 
   282 } //namespace lemon
   283 
   284 #endif