COIN-OR::LEMON - Graph Library

Ticket #51: 51-3-test-new-03f1dc010de8.patch

File 51-3-test-new-03f1dc010de8.patch, 7.2 KB (added by Peter Kovacs, 15 years ago)

Add a test file for Bellman-Ford (on the top of [9496ed797f20])

  • test/CMakeLists.txt

    # HG changeset patch
    # User Peter Kovacs <kpeter@inf.elte.hu>
    # Date 1249212404 -7200
    # Node ID 03f1dc010de8bab185ab48bfb31d5b455551ba20
    # Parent  9496ed797f20537523edb8a14c85979c629e35ad
    Add a detailed test file for BellmanFord (#51)
    
    diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
    a b  
    99
    1010SET(TESTS
    1111  adaptors_test
     12  bellman_ford_test
    1213  bfs_test
    1314  circulation_test
    1415  connectivity_test
  • test/Makefile.am

    diff --git a/test/Makefile.am b/test/Makefile.am
    a b  
    77
    88check_PROGRAMS += \
    99        test/adaptors_test \
     10        test/bellman_ford_test \
    1011        test/bfs_test \
    1112        test/circulation_test \
    1213        test/connectivity_test \
     
    5253XFAIL_TESTS += test/test_tools_fail$(EXEEXT)
    5354
    5455test_adaptors_test_SOURCES = test/adaptors_test.cc
     56test_bellman_ford_test_SOURCES = test/bellman_ford_test.cc
    5557test_bfs_test_SOURCES = test/bfs_test.cc
    5658test_circulation_test_SOURCES = test/circulation_test.cc
    5759test_counter_test_SOURCES = test/counter_test.cc
  • new file test/bellman_ford_test.cc

    diff --git a/test/bellman_ford_test.cc b/test/bellman_ford_test.cc
    new file mode 100644
    - +  
     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-2009
     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/bellman_ford.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  "@arcs\n"
     40  "    length\n"
     41  "0 1 3\n"
     42  "1 2 -3\n"
     43  "1 2 -5\n"
     44  "1 3 -2\n"
     45  "0 2 -1\n"
     46  "1 2 -4\n"
     47  "0 3 2\n"
     48  "4 2 -5\n"
     49  "2 3 1\n"
     50  "@attributes\n"
     51  "source 0\n"
     52  "target 3\n";
     53
     54
     55void checkBellmanFordCompile()
     56{
     57  typedef int Value;
     58  typedef concepts::Digraph Digraph;
     59  typedef concepts::ReadMap<Digraph::Arc,Value> LengthMap;
     60  typedef BellmanFord<Digraph, LengthMap> BF;
     61  typedef Digraph::Node Node;
     62  typedef Digraph::Arc Arc;
     63
     64  Digraph gr;
     65  Node s, t, n;
     66  Arc e;
     67  Value l;
     68  int k;
     69  bool b;
     70  BF::DistMap d(gr);
     71  BF::PredMap p(gr);
     72  LengthMap length;
     73  concepts::Path<Digraph> pp;
     74
     75  {
     76    BF bf_test(gr,length);
     77    const BF& const_bf_test = bf_test;
     78
     79    bf_test.run(s);
     80    bf_test.run(s,k);
     81
     82    bf_test.init();
     83    bf_test.addSource(s);
     84    bf_test.addSource(s, 1);
     85    b = bf_test.processNextRound();
     86    b = bf_test.processNextWeakRound();
     87
     88    bf_test.start();
     89    bf_test.checkedStart();
     90    bf_test.limitedStart(k);
     91
     92    l  = const_bf_test.dist(t);
     93    e  = const_bf_test.predArc(t);
     94    s  = const_bf_test.predNode(t);
     95    b  = const_bf_test.reached(t);
     96    d  = const_bf_test.distMap();
     97    p  = const_bf_test.predMap();
     98    pp = const_bf_test.path(t);
     99   
     100    for (BF::ActiveIt it(const_bf_test); it != INVALID; ++it) {}
     101  }
     102  {
     103    BF::SetPredMap<concepts::ReadWriteMap<Node,Arc> >
     104      ::SetDistMap<concepts::ReadWriteMap<Node,Value> >
     105      ::SetOperationTraits<BellmanFordDefaultOperationTraits<Value> >
     106      ::Create bf_test(gr,length);
     107
     108    LengthMap length_map;
     109    concepts::ReadWriteMap<Node,Arc> pred_map;
     110    concepts::ReadWriteMap<Node,Value> dist_map;
     111   
     112    bf_test
     113      .lengthMap(length_map)
     114      .predMap(pred_map)
     115      .distMap(dist_map);
     116
     117    bf_test.run(s);
     118    bf_test.run(s,k);
     119
     120    bf_test.init();
     121    bf_test.addSource(s);
     122    bf_test.addSource(s, 1);
     123    b = bf_test.processNextRound();
     124    b = bf_test.processNextWeakRound();
     125
     126    bf_test.start();
     127    bf_test.checkedStart();
     128    bf_test.limitedStart(k);
     129
     130    l  = bf_test.dist(t);
     131    e  = bf_test.predArc(t);
     132    s  = bf_test.predNode(t);
     133    b  = bf_test.reached(t);
     134    pp = bf_test.path(t);
     135  }
     136}
     137
     138void checkBellmanFordFunctionCompile()
     139{
     140  typedef int Value;
     141  typedef concepts::Digraph Digraph;
     142  typedef Digraph::Arc Arc;
     143  typedef Digraph::Node Node;
     144  typedef concepts::ReadMap<Digraph::Arc,Value> LengthMap;
     145
     146  Digraph g;
     147  bool b;
     148  bellmanFord(g,LengthMap()).run(Node());
     149  b = bellmanFord(g,LengthMap()).run(Node(),Node());
     150  bellmanFord(g,LengthMap())
     151    .predMap(concepts::ReadWriteMap<Node,Arc>())
     152    .distMap(concepts::ReadWriteMap<Node,Value>())
     153    .run(Node());
     154  b=bellmanFord(g,LengthMap())
     155    .predMap(concepts::ReadWriteMap<Node,Arc>())
     156    .distMap(concepts::ReadWriteMap<Node,Value>())
     157    .path(concepts::Path<Digraph>())
     158    .dist(Value())
     159    .run(Node(),Node());
     160}
     161
     162
     163template <typename Digraph, typename Value>
     164void checkBellmanFord() {
     165  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
     166  typedef typename Digraph::template ArcMap<Value> LengthMap;
     167
     168  Digraph gr;
     169  Node s, t;
     170  LengthMap length(gr);
     171
     172  std::istringstream input(test_lgf);
     173  digraphReader(gr, input).
     174    arcMap("length", length).
     175    node("source", s).
     176    node("target", t).
     177    run();
     178
     179  BellmanFord<Digraph, LengthMap>
     180    bf(gr, length);
     181  bf.run(s);
     182  Path<Digraph> p = bf.path(t);
     183
     184  check(bf.reached(t) && bf.dist(t) == -1, "Bellman-Ford found a wrong path.");
     185  check(p.length() == 3, "path() found a wrong path.");
     186  check(checkPath(gr, p), "path() found a wrong path.");
     187  check(pathSource(gr, p) == s, "path() found a wrong path.");
     188  check(pathTarget(gr, p) == t, "path() found a wrong path.");
     189 
     190  ListPath<Digraph> path;
     191  Value dist;
     192  bool reached = bellmanFord(gr,length).path(path).dist(dist).run(s,t);
     193
     194  std::cout << "**" << gr.id(s) <<"->"<<gr.id(t)<<":"<<reached<<","<<dist<<","<<path.length()<<"\n";
     195
     196  check(reached && dist == -1, "Bellman-Ford found a wrong path.");
     197  check(path.length() == 3, "path() found a wrong path.");
     198  check(checkPath(gr, path), "path() found a wrong path.");
     199  check(pathSource(gr, path) == s, "path() found a wrong path.");
     200  check(pathTarget(gr, path) == t, "path() found a wrong path.");
     201
     202  for(ArcIt e(gr); e!=INVALID; ++e) {
     203    Node u=gr.source(e);
     204    Node v=gr.target(e);
     205    check(!bf.reached(u) || (bf.dist(v) - bf.dist(u) <= length[e]),
     206          "Wrong output. dist(target)-dist(source)-arc_length=" <<
     207          bf.dist(v) - bf.dist(u) - length[e]);
     208  }
     209
     210  for(NodeIt v(gr); v!=INVALID; ++v) {
     211    if (bf.reached(v)) {
     212      check(v==s || bf.predArc(v)!=INVALID, "Wrong tree.");
     213      if (bf.predArc(v)!=INVALID ) {
     214        Arc e=bf.predArc(v);
     215        Node u=gr.source(e);
     216        check(u==bf.predNode(v),"Wrong tree.");
     217        check(bf.dist(v) - bf.dist(u) == length[e],
     218              "Wrong distance! Difference: " <<
     219              bf.dist(v) - bf.dist(u) - length[e]);
     220      }
     221    }
     222  }
     223}
     224
     225int main() {
     226  checkBellmanFord<ListDigraph, int>();
     227  checkBellmanFord<SmartDigraph, double>();
     228  return 0;
     229}