COIN-OR::LEMON - Graph Library

source: lemon-1.1/test/dfs_test.cc @ 245:da1d220b176b

Last change on this file since 245:da1d220b176b was 228:b6732e0d38c5, checked in by Balazs Dezso <deba@…>, 16 years ago

Reworking graph testing

  • The graph tests check more graph functionality.
  • The petersen graph is too regular, therefore special graphs are used.
  • The graph_test.h contains just general tools to test graphs.
File size: 3.1 KB
RevLine 
[209]1/* -*- mode: C++; indent-tabs-mode: nil; -*-
[100]2 *
[209]3 * This file is a part of LEMON, a generic C++ optimization library.
[100]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
[171]19#include <lemon/concepts/digraph.h>
20#include <lemon/smart_graph.h>
[100]21#include <lemon/list_graph.h>
[228]22#include <lemon/lgf_reader.h>
23
[100]24#include <lemon/dfs.h>
25#include <lemon/path.h>
[171]26
27#include "graph_test.h"
28#include "test_tools.h"
[100]29
30using namespace lemon;
31
[228]32char test_lgf[] =
33  "@nodes\n"
34  "label\n"
35  "0\n"
36  "1\n"
37  "2\n"
38  "3\n"
39  "4\n"
40  "5\n"
41  "6\n"
42  "@arcs\n"
43  "     label\n"
44  "0 1  0\n"
45  "1 2  1\n"
46  "2 3  2\n"
47  "1 4  3\n"
48  "4 2  4\n"
49  "4 5  5\n"
50  "5 0  6\n"
51  "6 3  7\n"
52  "@attributes\n"
53  "source 0\n"
54  "target 5\n";
55
[209]56void checkDfsCompile()
[100]57{
58  typedef concepts::Digraph Digraph;
59  typedef Dfs<Digraph> DType;
[209]60
[100]61  Digraph G;
[171]62  Digraph::Node n;
63  Digraph::Arc e;
[100]64  int l;
65  bool b;
66  DType::DistMap d(G);
67  DType::PredMap p(G);
[209]68
[100]69  DType dfs_test(G);
[209]70
[100]71  dfs_test.run(n);
[209]72
[100]73  l  = dfs_test.dist(n);
74  e  = dfs_test.predArc(n);
75  n  = dfs_test.predNode(n);
76  d  = dfs_test.distMap();
77  p  = dfs_test.predMap();
78  b  = dfs_test.reached(n);
79
80  Path<Digraph> pp = dfs_test.path(n);
81}
82
[209]83void checkDfsFunctionCompile()
[100]84{
85  typedef int VType;
86  typedef concepts::Digraph Digraph;
87  typedef Digraph::Arc Arc;
88  typedef Digraph::Node Node;
[209]89
[100]90  Digraph g;
91  dfs(g,Node()).run();
92  dfs(g).source(Node()).run();
93  dfs(g)
94    .predMap(concepts::WriteMap<Node,Arc>())
95    .distMap(concepts::WriteMap<Node,VType>())
96    .reachedMap(concepts::ReadWriteMap<Node,bool>())
97    .processedMap(concepts::WriteMap<Node,bool>())
[209]98    .run(Node());
[100]99}
100
[171]101template <class Digraph>
102void checkDfs() {
103  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
[100]104
105  Digraph G;
106  Node s, t;
[209]107
[228]108  std::istringstream input(test_lgf);
109  digraphReader(input, G).
110    node("source", s).
111    node("target", t).
112    run();
[209]113
[100]114  Dfs<Digraph> dfs_test(G);
[209]115  dfs_test.run(s);
116
[100]117  Path<Digraph> p = dfs_test.path(t);
[171]118  check(p.length() == dfs_test.dist(t),"path() found a wrong path.");
[100]119  check(checkPath(G, p),"path() found a wrong path.");
120  check(pathSource(G, p) == s,"path() found a wrong path.");
121  check(pathTarget(G, p) == t,"path() found a wrong path.");
[209]122
[100]123  for(NodeIt v(G); v!=INVALID; ++v) {
[228]124    if (dfs_test.reached(v)) {
125      check(v==s || dfs_test.predArc(v)!=INVALID, "Wrong tree.");
126      if (dfs_test.predArc(v)!=INVALID ) {
127        Arc e=dfs_test.predArc(v);
128        Node u=G.source(e);
129        check(u==dfs_test.predNode(v),"Wrong tree.");
130        check(dfs_test.dist(v) - dfs_test.dist(u) == 1,
131              "Wrong distance. (" << dfs_test.dist(u) << "->"
132              <<dfs_test.dist(v) << ')');
133      }
[100]134    }
135  }
136}
137
[171]138int main()
139{
140  checkDfs<ListDigraph>();
141  checkDfs<SmartDigraph>();
142  return 0;
143}
Note: See TracBrowser for help on using the repository browser.