alpar@209: /* -*- mode: C++; indent-tabs-mode: nil; -*-
kpeter@171:  *
alpar@209:  * This file is a part of LEMON, a generic C++ optimization library.
kpeter@171:  *
alpar@440:  * Copyright (C) 2003-2009
kpeter@171:  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
kpeter@171:  * (Egervary Research Group on Combinatorial Optimization, EGRES).
kpeter@171:  *
kpeter@171:  * Permission to use, modify and distribute this software is granted
kpeter@171:  * provided that this copyright notice appears in all copies. For
kpeter@171:  * precise terms see the accompanying LICENSE file.
kpeter@171:  *
kpeter@171:  * This software is provided "AS IS" with no warranty of any kind,
kpeter@171:  * express or implied, and with no claim as to its suitability for any
kpeter@171:  * purpose.
kpeter@171:  *
kpeter@171:  */
kpeter@171: 
kpeter@171: #ifndef LEMON_TEST_GRAPH_TEST_H
kpeter@171: #define LEMON_TEST_GRAPH_TEST_H
kpeter@171: 
deba@228: #include <set>
deba@228: 
deba@220: #include <lemon/core.h>
deba@228: #include <lemon/maps.h>
deba@228: 
kpeter@171: #include "test_tools.h"
kpeter@171: 
kpeter@171: namespace lemon {
kpeter@171: 
kpeter@171:   template<class Graph>
kpeter@171:   void checkGraphNodeList(const Graph &G, int cnt)
kpeter@171:   {
kpeter@171:     typename Graph::NodeIt n(G);
kpeter@171:     for(int i=0;i<cnt;i++) {
kpeter@171:       check(n!=INVALID,"Wrong Node list linking.");
kpeter@171:       ++n;
kpeter@171:     }
kpeter@171:     check(n==INVALID,"Wrong Node list linking.");
kpeter@171:     check(countNodes(G)==cnt,"Wrong Node number.");
kpeter@171:   }
kpeter@171: 
kpeter@171:   template<class Graph>
kpeter@171:   void checkGraphArcList(const Graph &G, int cnt)
kpeter@171:   {
kpeter@171:     typename Graph::ArcIt e(G);
kpeter@171:     for(int i=0;i<cnt;i++) {
kpeter@171:       check(e!=INVALID,"Wrong Arc list linking.");
deba@228:       check(G.oppositeNode(G.source(e), e) == G.target(e),
deba@228:             "Wrong opposite node");
deba@228:       check(G.oppositeNode(G.target(e), e) == G.source(e),
deba@228:             "Wrong opposite node");
kpeter@171:       ++e;
kpeter@171:     }
kpeter@171:     check(e==INVALID,"Wrong Arc list linking.");
kpeter@171:     check(countArcs(G)==cnt,"Wrong Arc number.");
kpeter@171:   }
kpeter@171: 
kpeter@171:   template<class Graph>
kpeter@171:   void checkGraphOutArcList(const Graph &G, typename Graph::Node n, int cnt)
kpeter@171:   {
kpeter@171:     typename Graph::OutArcIt e(G,n);
kpeter@171:     for(int i=0;i<cnt;i++) {
kpeter@171:       check(e!=INVALID,"Wrong OutArc list linking.");
kpeter@171:       check(n==G.source(e),"Wrong OutArc list linking.");
deba@228:       check(n==G.baseNode(e),"Wrong OutArc list linking.");
deba@228:       check(G.target(e)==G.runningNode(e),"Wrong OutArc list linking.");
kpeter@171:       ++e;
kpeter@171:     }
kpeter@171:     check(e==INVALID,"Wrong OutArc list linking.");
kpeter@171:     check(countOutArcs(G,n)==cnt,"Wrong OutArc number.");
kpeter@171:   }
kpeter@171: 
kpeter@171:   template<class Graph>
kpeter@171:   void checkGraphInArcList(const Graph &G, typename Graph::Node n, int cnt)
kpeter@171:   {
kpeter@171:     typename Graph::InArcIt e(G,n);
kpeter@171:     for(int i=0;i<cnt;i++) {
kpeter@171:       check(e!=INVALID,"Wrong InArc list linking.");
kpeter@171:       check(n==G.target(e),"Wrong InArc list linking.");
deba@228:       check(n==G.baseNode(e),"Wrong OutArc list linking.");
deba@228:       check(G.source(e)==G.runningNode(e),"Wrong OutArc list linking.");
kpeter@171:       ++e;
kpeter@171:     }
kpeter@171:     check(e==INVALID,"Wrong InArc list linking.");
kpeter@171:     check(countInArcs(G,n)==cnt,"Wrong InArc number.");
kpeter@171:   }
kpeter@171: 
kpeter@171:   template<class Graph>
kpeter@171:   void checkGraphEdgeList(const Graph &G, int cnt)
kpeter@171:   {
kpeter@171:     typename Graph::EdgeIt e(G);
kpeter@171:     for(int i=0;i<cnt;i++) {
kpeter@171:       check(e!=INVALID,"Wrong Edge list linking.");
deba@228:       check(G.oppositeNode(G.u(e), e) == G.v(e), "Wrong opposite node");
deba@228:       check(G.oppositeNode(G.v(e), e) == G.u(e), "Wrong opposite node");
kpeter@171:       ++e;
kpeter@171:     }
kpeter@171:     check(e==INVALID,"Wrong Edge list linking.");
kpeter@171:     check(countEdges(G)==cnt,"Wrong Edge number.");
kpeter@171:   }
kpeter@171: 
kpeter@171:   template<class Graph>
kpeter@171:   void checkGraphIncEdgeList(const Graph &G, typename Graph::Node n, int cnt)
kpeter@171:   {
kpeter@171:     typename Graph::IncEdgeIt e(G,n);
kpeter@171:     for(int i=0;i<cnt;i++) {
kpeter@171:       check(e!=INVALID,"Wrong IncEdge list linking.");
kpeter@171:       check(n==G.u(e) || n==G.v(e),"Wrong IncEdge list linking.");
deba@228:       check(n==G.baseNode(e),"Wrong OutArc list linking.");
deba@228:       check(G.u(e)==G.runningNode(e) || G.v(e)==G.runningNode(e),
deba@228:             "Wrong OutArc list linking.");
kpeter@171:       ++e;
kpeter@171:     }
kpeter@171:     check(e==INVALID,"Wrong IncEdge list linking.");
kpeter@171:     check(countIncEdges(G,n)==cnt,"Wrong IncEdge number.");
kpeter@171:   }
kpeter@171: 
deba@228:   template <class Graph>
kpeter@374:   void checkGraphIncEdgeArcLists(const Graph &G, typename Graph::Node n,
kpeter@374:                                  int cnt)
kpeter@374:   {
kpeter@374:     checkGraphIncEdgeList(G, n, cnt);
kpeter@374:     checkGraphOutArcList(G, n, cnt);
kpeter@374:     checkGraphInArcList(G, n, cnt);
kpeter@374:   }
kpeter@374: 
kpeter@374:   template <class Graph>
deba@228:   void checkGraphConArcList(const Graph &G, int cnt) {
deba@228:     int i = 0;
deba@228:     for (typename Graph::NodeIt u(G); u != INVALID; ++u) {
deba@228:       for (typename Graph::NodeIt v(G); v != INVALID; ++v) {
deba@228:         for (ConArcIt<Graph> a(G, u, v); a != INVALID; ++a) {
deba@228:           check(G.source(a) == u, "Wrong iterator.");
deba@228:           check(G.target(a) == v, "Wrong iterator.");
deba@228:           ++i;
deba@228:         }
deba@228:       }
deba@228:     }
deba@228:     check(cnt == i, "Wrong iterator.");
kpeter@171:   }
kpeter@171: 
kpeter@171:   template <class Graph>
deba@228:   void checkGraphConEdgeList(const Graph &G, int cnt) {
deba@228:     int i = 0;
deba@228:     for (typename Graph::NodeIt u(G); u != INVALID; ++u) {
deba@228:       for (typename Graph::NodeIt v(G); v != INVALID; ++v) {
deba@228:         for (ConEdgeIt<Graph> e(G, u, v); e != INVALID; ++e) {
deba@228:           check((G.u(e) == u && G.v(e) == v) ||
deba@228:                 (G.u(e) == v && G.v(e) == u), "Wrong iterator.");
deba@228:           i += u == v ? 2 : 1;
deba@228:         }
deba@228:       }
deba@228:     }
deba@228:     check(2 * cnt == i, "Wrong iterator.");
kpeter@171:   }
kpeter@171: 
deba@228:   template <typename Graph>
deba@228:   void checkArcDirections(const Graph& G) {
deba@228:     for (typename Graph::ArcIt a(G); a != INVALID; ++a) {
deba@228:       check(G.source(a) == G.target(G.oppositeArc(a)), "Wrong direction");
deba@228:       check(G.target(a) == G.source(G.oppositeArc(a)), "Wrong direction");
deba@228:       check(G.direct(a, G.direction(a)) == a, "Wrong direction");
kpeter@171:     }
kpeter@171:   }
kpeter@171: 
deba@228:   template <typename Graph>
deba@228:   void checkNodeIds(const Graph& G) {
deba@228:     std::set<int> values;
deba@228:     for (typename Graph::NodeIt n(G); n != INVALID; ++n) {
deba@228:       check(G.nodeFromId(G.id(n)) == n, "Wrong id");
deba@228:       check(values.find(G.id(n)) == values.end(), "Wrong id");
deba@228:       check(G.id(n) <= G.maxNodeId(), "Wrong maximum id");
deba@228:       values.insert(G.id(n));
kpeter@171:     }
kpeter@171:   }
kpeter@171: 
deba@228:   template <typename Graph>
deba@228:   void checkArcIds(const Graph& G) {
deba@228:     std::set<int> values;
deba@228:     for (typename Graph::ArcIt a(G); a != INVALID; ++a) {
deba@228:       check(G.arcFromId(G.id(a)) == a, "Wrong id");
deba@228:       check(values.find(G.id(a)) == values.end(), "Wrong id");
deba@228:       check(G.id(a) <= G.maxArcId(), "Wrong maximum id");
deba@228:       values.insert(G.id(a));
deba@228:     }
kpeter@171:   }
alpar@209: 
deba@228:   template <typename Graph>
deba@228:   void checkEdgeIds(const Graph& G) {
deba@228:     std::set<int> values;
deba@228:     for (typename Graph::EdgeIt e(G); e != INVALID; ++e) {
deba@228:       check(G.edgeFromId(G.id(e)) == e, "Wrong id");
deba@228:       check(values.find(G.id(e)) == values.end(), "Wrong id");
deba@228:       check(G.id(e) <= G.maxEdgeId(), "Wrong maximum id");
deba@228:       values.insert(G.id(e));
deba@228:     }
kpeter@171:   }
kpeter@171: 
deba@228:   template <typename Graph>
deba@228:   void checkGraphNodeMap(const Graph& G) {
deba@228:     typedef typename Graph::Node Node;
deba@228:     typedef typename Graph::NodeIt NodeIt;
deba@228: 
deba@228:     typedef typename Graph::template NodeMap<int> IntNodeMap;
deba@228:     IntNodeMap map(G, 42);
deba@228:     for (NodeIt it(G); it != INVALID; ++it) {
deba@228:       check(map[it] == 42, "Wrong map constructor.");
deba@228:     }
deba@228:     int s = 0;
deba@228:     for (NodeIt it(G); it != INVALID; ++it) {
deba@228:       map[it] = 0;
deba@228:       check(map[it] == 0, "Wrong operator[].");
deba@228:       map.set(it, s);
deba@228:       check(map[it] == s, "Wrong set.");
deba@228:       ++s;
deba@228:     }
deba@228:     s = s * (s - 1) / 2;
deba@228:     for (NodeIt it(G); it != INVALID; ++it) {
deba@228:       s -= map[it];
deba@228:     }
deba@228:     check(s == 0, "Wrong sum.");
deba@228: 
kpeter@263:     // map = constMap<Node>(12);
kpeter@263:     // for (NodeIt it(G); it != INVALID; ++it) {
kpeter@263:     //   check(map[it] == 12, "Wrong operator[].");
kpeter@263:     // }
deba@228:   }
deba@228: 
deba@228:   template <typename Graph>
deba@228:   void checkGraphArcMap(const Graph& G) {
deba@228:     typedef typename Graph::Arc Arc;
deba@228:     typedef typename Graph::ArcIt ArcIt;
deba@228: 
deba@228:     typedef typename Graph::template ArcMap<int> IntArcMap;
deba@228:     IntArcMap map(G, 42);
deba@228:     for (ArcIt it(G); it != INVALID; ++it) {
deba@228:       check(map[it] == 42, "Wrong map constructor.");
deba@228:     }
deba@228:     int s = 0;
deba@228:     for (ArcIt it(G); it != INVALID; ++it) {
deba@228:       map[it] = 0;
deba@228:       check(map[it] == 0, "Wrong operator[].");
deba@228:       map.set(it, s);
deba@228:       check(map[it] == s, "Wrong set.");
deba@228:       ++s;
deba@228:     }
deba@228:     s = s * (s - 1) / 2;
deba@228:     for (ArcIt it(G); it != INVALID; ++it) {
deba@228:       s -= map[it];
deba@228:     }
deba@228:     check(s == 0, "Wrong sum.");
deba@228: 
kpeter@263:     // map = constMap<Arc>(12);
kpeter@263:     // for (ArcIt it(G); it != INVALID; ++it) {
kpeter@263:     //   check(map[it] == 12, "Wrong operator[].");
kpeter@263:     // }
deba@228:   }
deba@228: 
deba@228:   template <typename Graph>
deba@228:   void checkGraphEdgeMap(const Graph& G) {
deba@228:     typedef typename Graph::Edge Edge;
deba@228:     typedef typename Graph::EdgeIt EdgeIt;
deba@228: 
deba@228:     typedef typename Graph::template EdgeMap<int> IntEdgeMap;
deba@228:     IntEdgeMap map(G, 42);
deba@228:     for (EdgeIt it(G); it != INVALID; ++it) {
deba@228:       check(map[it] == 42, "Wrong map constructor.");
deba@228:     }
deba@228:     int s = 0;
deba@228:     for (EdgeIt it(G); it != INVALID; ++it) {
deba@228:       map[it] = 0;
deba@228:       check(map[it] == 0, "Wrong operator[].");
deba@228:       map.set(it, s);
deba@228:       check(map[it] == s, "Wrong set.");
deba@228:       ++s;
deba@228:     }
deba@228:     s = s * (s - 1) / 2;
deba@228:     for (EdgeIt it(G); it != INVALID; ++it) {
deba@228:       s -= map[it];
deba@228:     }
deba@228:     check(s == 0, "Wrong sum.");
deba@228: 
kpeter@263:     // map = constMap<Edge>(12);
kpeter@263:     // for (EdgeIt it(G); it != INVALID; ++it) {
kpeter@263:     //   check(map[it] == 12, "Wrong operator[].");
kpeter@263:     // }
deba@228:   }
deba@228: 
deba@228: 
kpeter@171: } //namespace lemon
kpeter@171: 
kpeter@171: #endif