diff options
| author | Carlo Zancanaro <carlo@pc-4w14-0.cs.usyd.edu.au> | 2012-07-10 13:44:21 +1000 | 
|---|---|---|
| committer | Carlo Zancanaro <carlo@pc-4w14-0.cs.usyd.edu.au> | 2012-07-10 13:44:21 +1000 | 
| commit | ef4a319984d22b88a9024ff523700d657621124d (patch) | |
| tree | 2821db78ccd6ce61ca64ad6fc98ba825b060ca6a /lemon/test/euler_test.cc | |
| parent | d3f13fdd23e17a8f4bb4083c24fee331a28351eb (diff) | |
Add the LEMON graph library source to the repo
I'll likely be using it, so this just makes it easier to get to from
elsewhere. If I end up not using it then I can just delete it.
Diffstat (limited to 'lemon/test/euler_test.cc')
| -rw-r--r-- | lemon/test/euler_test.cc | 223 | 
1 files changed, 223 insertions, 0 deletions
diff --git a/lemon/test/euler_test.cc b/lemon/test/euler_test.cc new file mode 100644 index 0000000..3f134b6 --- /dev/null +++ b/lemon/test/euler_test.cc @@ -0,0 +1,223 @@ +/* -*- mode: C++; indent-tabs-mode: nil; -*- + * + * This file is a part of LEMON, a generic C++ optimization library. + * + * Copyright (C) 2003-2010 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport + * (Egervary Research Group on Combinatorial Optimization, EGRES). + * + * Permission to use, modify and distribute this software is granted + * provided that this copyright notice appears in all copies. For + * precise terms see the accompanying LICENSE file. + * + * This software is provided "AS IS" with no warranty of any kind, + * express or implied, and with no claim as to its suitability for any + * purpose. + * + */ + +#include <lemon/euler.h> +#include <lemon/list_graph.h> +#include <lemon/adaptors.h> +#include "test_tools.h" + +using namespace lemon; + +template <typename Digraph> +void checkDiEulerIt(const Digraph& g, +                    const typename Digraph::Node& start = INVALID) +{ +  typename Digraph::template ArcMap<int> visitationNumber(g, 0); + +  DiEulerIt<Digraph> e(g, start); +  if (e == INVALID) return; +  typename Digraph::Node firstNode = g.source(e); +  typename Digraph::Node lastNode = g.target(e); +  if (start != INVALID) { +    check(firstNode == start, "checkDiEulerIt: Wrong first node"); +  } + +  for (; e != INVALID; ++e) { +    if (e != INVALID) lastNode = g.target(e); +    ++visitationNumber[e]; +  } + +  check(firstNode == lastNode, +      "checkDiEulerIt: First and last nodes are not the same"); + +  for (typename Digraph::ArcIt a(g); a != INVALID; ++a) +  { +    check(visitationNumber[a] == 1, +        "checkDiEulerIt: Not visited or multiple times visited arc found"); +  } +} + +template <typename Graph> +void checkEulerIt(const Graph& g, +                  const typename Graph::Node& start = INVALID) +{ +  typename Graph::template EdgeMap<int> visitationNumber(g, 0); + +  EulerIt<Graph> e(g, start); +  if (e == INVALID) return; +  typename Graph::Node firstNode = g.source(typename Graph::Arc(e)); +  typename Graph::Node lastNode = g.target(typename Graph::Arc(e)); +  if (start != INVALID) { +    check(firstNode == start, "checkEulerIt: Wrong first node"); +  } + +  for (; e != INVALID; ++e) { +    if (e != INVALID) lastNode = g.target(typename Graph::Arc(e)); +    ++visitationNumber[e]; +  } + +  check(firstNode == lastNode, +      "checkEulerIt: First and last nodes are not the same"); + +  for (typename Graph::EdgeIt e(g); e != INVALID; ++e) +  { +    check(visitationNumber[e] == 1, +        "checkEulerIt: Not visited or multiple times visited edge found"); +  } +} + +int main() +{ +  typedef ListDigraph Digraph; +  typedef Undirector<Digraph> Graph; + +  { +    Digraph d; +    Graph g(d); + +    checkDiEulerIt(d); +    checkDiEulerIt(g); +    checkEulerIt(g); + +    check(eulerian(d), "This graph is Eulerian"); +    check(eulerian(g), "This graph is Eulerian"); +  } +  { +    Digraph d; +    Graph g(d); +    Digraph::Node n = d.addNode(); + +    checkDiEulerIt(d); +    checkDiEulerIt(g); +    checkEulerIt(g); + +    check(eulerian(d), "This graph is Eulerian"); +    check(eulerian(g), "This graph is Eulerian"); +  } +  { +    Digraph d; +    Graph g(d); +    Digraph::Node n = d.addNode(); +    d.addArc(n, n); + +    checkDiEulerIt(d); +    checkDiEulerIt(g); +    checkEulerIt(g); + +    check(eulerian(d), "This graph is Eulerian"); +    check(eulerian(g), "This graph is Eulerian"); +  } +  { +    Digraph d; +    Graph g(d); +    Digraph::Node n1 = d.addNode(); +    Digraph::Node n2 = d.addNode(); +    Digraph::Node n3 = d.addNode(); + +    d.addArc(n1, n2); +    d.addArc(n2, n1); +    d.addArc(n2, n3); +    d.addArc(n3, n2); + +    checkDiEulerIt(d); +    checkDiEulerIt(d, n2); +    checkDiEulerIt(g); +    checkDiEulerIt(g, n2); +    checkEulerIt(g); +    checkEulerIt(g, n2); + +    check(eulerian(d), "This graph is Eulerian"); +    check(eulerian(g), "This graph is Eulerian"); +  } +  { +    Digraph d; +    Graph g(d); +    Digraph::Node n1 = d.addNode(); +    Digraph::Node n2 = d.addNode(); +    Digraph::Node n3 = d.addNode(); +    Digraph::Node n4 = d.addNode(); +    Digraph::Node n5 = d.addNode(); +    Digraph::Node n6 = d.addNode(); + +    d.addArc(n1, n2); +    d.addArc(n2, n4); +    d.addArc(n1, n3); +    d.addArc(n3, n4); +    d.addArc(n4, n1); +    d.addArc(n3, n5); +    d.addArc(n5, n2); +    d.addArc(n4, n6); +    d.addArc(n2, n6); +    d.addArc(n6, n1); +    d.addArc(n6, n3); + +    checkDiEulerIt(d); +    checkDiEulerIt(d, n1); +    checkDiEulerIt(d, n5); + +    checkDiEulerIt(g); +    checkDiEulerIt(g, n1); +    checkDiEulerIt(g, n5); +    checkEulerIt(g); +    checkEulerIt(g, n1); +    checkEulerIt(g, n5); + +    check(eulerian(d), "This graph is Eulerian"); +    check(eulerian(g), "This graph is Eulerian"); +  } +  { +    Digraph d; +    Graph g(d); +    Digraph::Node n0 = d.addNode(); +    Digraph::Node n1 = d.addNode(); +    Digraph::Node n2 = d.addNode(); +    Digraph::Node n3 = d.addNode(); +    Digraph::Node n4 = d.addNode(); +    Digraph::Node n5 = d.addNode(); + +    d.addArc(n1, n2); +    d.addArc(n2, n3); +    d.addArc(n3, n1); + +    checkDiEulerIt(d); +    checkDiEulerIt(d, n2); + +    checkDiEulerIt(g); +    checkDiEulerIt(g, n2); +    checkEulerIt(g); +    checkEulerIt(g, n2); + +    check(!eulerian(d), "This graph is not Eulerian"); +    check(!eulerian(g), "This graph is not Eulerian"); +  } +  { +    Digraph d; +    Graph g(d); +    Digraph::Node n1 = d.addNode(); +    Digraph::Node n2 = d.addNode(); +    Digraph::Node n3 = d.addNode(); + +    d.addArc(n1, n2); +    d.addArc(n2, n3); + +    check(!eulerian(d), "This graph is not Eulerian"); +    check(!eulerian(g), "This graph is not Eulerian"); +  } + +  return 0; +}  | 
