COIN-OR::LEMON - Graph Library

source: lemon-main/test/graph_utils_test.cc @ 203:215bfc30b14f

Last change on this file since 203:215bfc30b14f was 171:02f4d5d9bfd7, checked in by Peter Kovacs <kpeter@…>, 16 years ago

Improve and redesign test programs + unify their output (ticket #25)

  • Move graph related utilities form test_tools.h to graph_test.h.
  • Move the contents of graph_utils_test.h to graph_utils_test.cc.
  • Rename map_test.h -> graph_maps_test.h.
  • Rename digraph_test.h -> graph_test.h.
  • Many improvements in the following files:
    • digraph_test.cc
    • graph_test.cc
    • graph_test.h
    • graph_maps_test.h
    • graph_utils_test.cc
    • bfs_test.cc
    • dfs_test.cc
    • counter_test.cc
  • Test programs print messages only if it really seems necessary.
  • Remove \file commands form .cc test files.
File size: 6.4 KB
RevLine 
[139]1/* -*- C++ -*-
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
[171]19#include <cstdlib>
20#include <ctime>
[139]21
[171]22#include <lemon/random.h>
[139]23#include <lemon/graph_utils.h>
24#include <lemon/list_graph.h>
25#include <lemon/smart_graph.h>
26
[171]27#include "graph_test.h"
[139]28#include "test_tools.h"
29
30using namespace lemon;
31
[171]32template <typename Digraph>
33void checkFindArcs() {
34  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
35
36  {
37    Digraph digraph;
38    for (int i = 0; i < 10; ++i) {
39      digraph.addNode();
40    }
41    DescriptorMap<Digraph, Node> nodes(digraph);
42    typename DescriptorMap<Digraph, Node>::InverseMap invNodes(nodes);
43    for (int i = 0; i < 100; ++i) {
44      int src = rnd[invNodes.size()];
45      int trg = rnd[invNodes.size()];
46      digraph.addArc(invNodes[src], invNodes[trg]);
47    }
48    typename Digraph::template ArcMap<bool> found(digraph, false);
49    DescriptorMap<Digraph, Arc> arcs(digraph);
50    for (NodeIt src(digraph); src != INVALID; ++src) {
51      for (NodeIt trg(digraph); trg != INVALID; ++trg) {
52        for (ConArcIt<Digraph> con(digraph, src, trg); con != INVALID; ++con) {
53          check(digraph.source(con) == src, "Wrong source.");
54          check(digraph.target(con) == trg, "Wrong target.");
55          check(found[con] == false, "The arc found already.");
56          found[con] = true;
57        }
58      }
59    }
60    for (ArcIt it(digraph); it != INVALID; ++it) {
61      check(found[it] == true, "The arc is not found.");
62    }
63  }
64
65  {
66    int num = 5;
67    Digraph fg;
68    std::vector<Node> nodes;
69    for (int i = 0; i < num; ++i) {
70      nodes.push_back(fg.addNode());
71    }
72    for (int i = 0; i < num * num; ++i) {
73      fg.addArc(nodes[i / num], nodes[i % num]);
74    }
75    check(countNodes(fg) == num, "Wrong node number.");
76    check(countArcs(fg) == num*num, "Wrong arc number.");
77    for (NodeIt src(fg); src != INVALID; ++src) {
78      for (NodeIt trg(fg); trg != INVALID; ++trg) {
79        ConArcIt<Digraph> con(fg, src, trg);
80        check(con != INVALID, "There is no connecting arc.");
81        check(fg.source(con) == src, "Wrong source.");
82        check(fg.target(con) == trg, "Wrong target.");
83        check(++con == INVALID, "There is more connecting arc.");
84      }
85    }
86    ArcLookUp<Digraph> al1(fg);
87    DynArcLookUp<Digraph> al2(fg);
88    AllArcLookUp<Digraph> al3(fg);
89    for (NodeIt src(fg); src != INVALID; ++src) {
90      for (NodeIt trg(fg); trg != INVALID; ++trg) {
91        Arc con1 = al1(src, trg);
92        Arc con2 = al2(src, trg);
93        Arc con3 = al3(src, trg);
94        Arc con4 = findArc(fg, src, trg);
95        check(con1 == con2 && con2 == con3 && con3 == con4, "Different results.")
96        check(con1 != INVALID, "There is no connecting arc.");
97        check(fg.source(con1) == src, "Wrong source.");
98        check(fg.target(con1) == trg, "Wrong target.");
99        check(al3(src, trg, con3) == INVALID, "There is more connecting arc.");
100        check(findArc(fg, src, trg, con4) == INVALID, "There is more connecting arc.");
101      }
102    }
103  }
104}
105
106template <typename Graph>
107void checkFindEdges() {
108  TEMPLATE_GRAPH_TYPEDEFS(Graph);
109  Graph graph;
110  for (int i = 0; i < 10; ++i) {
111    graph.addNode();
112  }
113  DescriptorMap<Graph, Node> nodes(graph);
114  typename DescriptorMap<Graph, Node>::InverseMap invNodes(nodes);
115  for (int i = 0; i < 100; ++i) {
116    int src = rnd[invNodes.size()];
117    int trg = rnd[invNodes.size()];
118    graph.addEdge(invNodes[src], invNodes[trg]);
119  }
120  typename Graph::template EdgeMap<int> found(graph, 0);
121  DescriptorMap<Graph, Edge> edges(graph);
122  for (NodeIt src(graph); src != INVALID; ++src) {
123    for (NodeIt trg(graph); trg != INVALID; ++trg) {
124      for (ConEdgeIt<Graph> con(graph, src, trg); con != INVALID; ++con) {
125        check( (graph.u(con) == src && graph.v(con) == trg) ||
126               (graph.v(con) == src && graph.u(con) == trg), "Wrong end nodes.");
127        ++found[con];
128        check(found[con] <= 2, "The edge found more than twice.");
129      }
130    }
131  }
132  for (EdgeIt it(graph); it != INVALID; ++it) {
133    check( (graph.u(it) != graph.v(it) && found[it] == 2) ||
134           (graph.u(it) == graph.v(it) && found[it] == 1),
135           "The edge is not found correctly.");
136  }
137}
138
139template <class Digraph>
140void checkDeg()
[139]141{
[171]142  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
143 
144  const int nodeNum = 10;
145  const int arcNum = 100;
146  Digraph digraph;
147  InDegMap<Digraph> inDeg(digraph);
148  OutDegMap<Digraph> outDeg(digraph);
149  std::vector<Node> nodes(nodeNum);
150  for (int i = 0; i < nodeNum; ++i) {
151    nodes[i] = digraph.addNode();
152  }
153  std::vector<Arc> arcs(arcNum);
154  for (int i = 0; i < arcNum; ++i) {
155    arcs[i] = digraph.addArc(nodes[rnd[nodeNum]], nodes[rnd[nodeNum]]);
156  }
157  for (int i = 0; i < nodeNum; ++i) {
158    check(inDeg[nodes[i]] == countInArcs(digraph, nodes[i]),
159          "Wrong in degree map");
160  }
161  for (int i = 0; i < nodeNum; ++i) {
162    check(outDeg[nodes[i]] == countOutArcs(digraph, nodes[i]),
163          "Wrong out degree map");
164  }
165}
166
167template <class Digraph>
168void checkSnapDeg()
169{
170  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
171
172  Digraph g;
173  Node n1=g.addNode();
174  Node n2=g.addNode();
175 
176  InDegMap<Digraph> ind(g);
177 
[139]178  g.addArc(n1,n2);
179 
[171]180  typename Digraph::Snapshot snap(g);
[139]181 
[171]182  OutDegMap<Digraph> outd(g);
[139]183 
184  check(ind[n1]==0 && ind[n2]==1, "Wrong InDegMap value.");
185  check(outd[n1]==1 && outd[n2]==0, "Wrong OutDegMap value.");
186
187  g.addArc(n1,n2);
188  g.addArc(n2,n1);
[171]189
[139]190  check(ind[n1]==1 && ind[n2]==2, "Wrong InDegMap value.");
191  check(outd[n1]==2 && outd[n2]==1, "Wrong OutDegMap value.");
192
193  snap.restore();
194
195  check(ind[n1]==0 && ind[n2]==1, "Wrong InDegMap value.");
196  check(outd[n1]==1 && outd[n2]==0, "Wrong OutDegMap value.");
197}
198
199int main() {
[171]200  // Checking ConArcIt, ConEdgeIt, ArcLookUp, AllArcLookUp, and DynArcLookUp
201  checkFindArcs<ListDigraph>();
202  checkFindArcs<SmartDigraph>();
203  checkFindEdges<ListGraph>();
204  checkFindEdges<SmartGraph>();
[139]205
[171]206  // Checking In/OutDegMap (and Snapshot feature)
207  checkDeg<ListDigraph>();
208  checkDeg<SmartDigraph>();
[139]209  checkSnapDeg<ListDigraph>();
210  checkSnapDeg<SmartDigraph>();
211
212  return 0;
213}
Note: See TracBrowser for help on using the repository browser.