COIN-OR::LEMON - Graph Library

source: lemon-main/test/bfs_test.cc @ 236:da953e387d31

Last change on this file since 236:da953e387d31 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.5 KB
Line 
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#include <lemon/concepts/digraph.h>
20#include <lemon/smart_graph.h>
21#include <lemon/list_graph.h>
22#include <lemon/lgf_reader.h>
23#include <lemon/bfs.h>
24#include <lemon/path.h>
25
26#include "graph_test.h"
27#include "test_tools.h"
28
29using namespace lemon;
30
31char test_lgf[] =
32  "@nodes\n"
33  "label\n"
34  "0\n"
35  "1\n"
36  "2\n"
37  "3\n"
38  "4\n"
39  "5\n"
40  "@arcs\n"
41  "     label\n"
42  "0 1  0\n"
43  "1 2  1\n"
44  "2 3  2\n"
45  "3 4  3\n"
46  "0 3  4\n"
47  "0 3  5\n"
48  "5 2  6\n"
49  "@attributes\n"
50  "source 0\n"
51  "target 4\n";
52
53void checkBfsCompile()
54{
55  typedef concepts::Digraph Digraph;
56  typedef Bfs<Digraph> BType;
57
58  Digraph G;
59  Digraph::Node n;
60  Digraph::Arc e;
61  int l;
62  bool b;
63  BType::DistMap d(G);
64  BType::PredMap p(G);
65  //  BType::PredNodeMap pn(G);
66
67  BType bfs_test(G);
68
69  bfs_test.run(n);
70
71  l  = bfs_test.dist(n);
72  e  = bfs_test.predArc(n);
73  n  = bfs_test.predNode(n);
74  d  = bfs_test.distMap();
75
76  p  = bfs_test.predMap();
77  //  pn = bfs_test.predNodeMap();
78  b  = bfs_test.reached(n);
79
80  Path<Digraph> pp = bfs_test.path(n);
81}
82
83void checkBfsFunctionCompile()
84{
85  typedef int VType;
86  typedef concepts::Digraph Digraph;
87  typedef Digraph::Arc Arc;
88  typedef Digraph::Node Node;
89
90  Digraph g;
91  bfs(g,Node()).run();
92  bfs(g).source(Node()).run();
93  bfs(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>())
98    .run(Node());
99}
100
101template <class Digraph>
102void checkBfs() {
103  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
104
105  Digraph G;
106  Node s, t;
107
108  std::istringstream input(test_lgf);
109  digraphReader(input, G).
110    node("source", s).
111    node("target", t).
112    run();
113
114  Bfs<Digraph> bfs_test(G);
115  bfs_test.run(s);
116
117  check(bfs_test.dist(t)==2,"Bfs found a wrong path." << bfs_test.dist(t));
118
119  Path<Digraph> p = bfs_test.path(t);
120  check(p.length()==2,"path() found a wrong path.");
121  check(checkPath(G, p),"path() found a wrong path.");
122  check(pathSource(G, p) == s,"path() found a wrong path.");
123  check(pathTarget(G, p) == t,"path() found a wrong path.");
124
125
126  for(ArcIt a(G); a!=INVALID; ++a) {
127    Node u=G.source(a);
128    Node v=G.target(a);
129    check( !bfs_test.reached(u) ||
130           (bfs_test.dist(v) <= bfs_test.dist(u)+1),
131           "Wrong output." << G.id(v) << ' ' << G.id(u));
132  }
133
134  for(NodeIt v(G); v!=INVALID; ++v) {
135    if (bfs_test.reached(v)) {
136      check(v==s || bfs_test.predArc(v)!=INVALID, "Wrong tree.");
137      if (bfs_test.predArc(v)!=INVALID ) {
138        Arc a=bfs_test.predArc(v);
139        Node u=G.source(a);
140        check(u==bfs_test.predNode(v),"Wrong tree.");
141        check(bfs_test.dist(v) - bfs_test.dist(u) == 1,
142              "Wrong distance. Difference: "
143              << std::abs(bfs_test.dist(v) - bfs_test.dist(u)
144                          - 1));
145      }
146    }
147  }
148}
149
150int main()
151{
152  checkBfs<ListDigraph>();
153  checkBfs<SmartDigraph>();
154  return 0;
155}
Note: See TracBrowser for help on using the repository browser.