test/graph_utils_test.cc
author hegyi
Thu, 20 Oct 2005 15:50:23 +0000
changeset 1731 616bc933c2bc
parent 1728 eb8bb91ba9e2
child 1772 dd1e0c442fe0
permissions -rw-r--r--
Mapselector widget reached its first release, but there are still work to do on it, I know...
klao@946
     1
// -*- c++ -*-
klao@946
     2
klao@946
     3
#include <iostream>
klao@946
     4
#include <vector>
klao@946
     5
klao@977
     6
#include <lemon/graph_utils.h>
klao@977
     7
klao@946
     8
#include <lemon/list_graph.h>
klao@946
     9
#include <lemon/smart_graph.h>
klao@946
    10
#include <lemon/full_graph.h>
klao@946
    11
klao@946
    12
#include "test_tools.h"
klao@946
    13
#include "graph_utils_test.h"
klao@946
    14
klao@946
    15
klao@946
    16
using namespace lemon;
klao@946
    17
alpar@1459
    18
template<class Graph>
alpar@1459
    19
void checkSnapDeg() 
alpar@1459
    20
{
alpar@1459
    21
  Graph g;
alpar@1459
    22
  typename Graph::Node n1=g.addNode();
alpar@1459
    23
  typename Graph::Node n2=g.addNode();
alpar@1459
    24
   
alpar@1459
    25
  InDegMap<Graph> ind(g);
alpar@1459
    26
 
alpar@1459
    27
  g.addEdge(n1,n2);
alpar@1459
    28
  
alpar@1459
    29
  typename Graph::SnapShot snap(g);
alpar@1459
    30
  
alpar@1459
    31
  OutDegMap<Graph> outd(g);
alpar@1459
    32
  
alpar@1459
    33
  check(ind[n1]==0 && ind[n2]==1, "Wrong InDegMap value.");
alpar@1459
    34
  check(outd[n1]==1 && outd[n2]==0, "Wrong OutDegMap value.");
alpar@1459
    35
alpar@1459
    36
  g.addEdge(n1,n2);
alpar@1459
    37
  g.addEdge(n2,n1);
alpar@1459
    38
 
alpar@1459
    39
  check(ind[n1]==1 && ind[n2]==2, "Wrong InDegMap value.");
alpar@1459
    40
  check(outd[n1]==2 && outd[n2]==1, "Wrong OutDegMap value.");
alpar@1459
    41
alpar@1459
    42
  snap.restore();
alpar@1459
    43
alpar@1459
    44
  check(ind[n1]==0 && ind[n2]==1, "Wrong InDegMap value.");
alpar@1459
    45
  check(outd[n1]==1 && outd[n2]==0, "Wrong OutDegMap value.");
alpar@1459
    46
  
alpar@1459
    47
}
klao@946
    48
klao@946
    49
int main() {
klao@946
    50
  ///\file
klao@946
    51
  { // checking list graph
klao@946
    52
    checkGraphCounters<ListGraph>();
deba@1568
    53
    checkFindEdge<ListGraph>();
klao@946
    54
  }
klao@946
    55
  { // checking smart graph
klao@946
    56
    checkGraphCounters<SmartGraph>();
deba@1568
    57
    checkFindEdge<SmartGraph>();
klao@946
    58
  }
klao@977
    59
  {
klao@977
    60
    int num = 5;
klao@977
    61
    FullGraph fg(num);
klao@977
    62
    check(countNodes(fg) == num, "FullGraph: wrong node number.");
deba@1568
    63
    check(countEdges(fg) == num*num, "FullGraph: wrong edge number.");
deba@1568
    64
    for (FullGraph::NodeIt src(fg); src != INVALID; ++src) {
deba@1568
    65
      for (FullGraph::NodeIt trg(fg); trg != INVALID; ++trg) {
deba@1568
    66
	ConEdgeIt<FullGraph> con(fg, src, trg);
deba@1568
    67
	check(con != INVALID, "There is no connecting edge.");
deba@1568
    68
	check(fg.source(con) == src, "Wrong source.");
deba@1568
    69
	check(fg.target(con) == trg, "Wrong target.");
deba@1568
    70
	check(++con == INVALID, "There is more connecting edge.");
deba@1568
    71
      }
deba@1568
    72
    }
klao@977
    73
  }
klao@946
    74
alpar@1459
    75
  //check In/OutDegMap (and SnapShot feature)
alpar@1459
    76
alpar@1459
    77
  checkSnapDeg<ListGraph>();
alpar@1459
    78
  checkSnapDeg<SmartGraph>();
alpar@1459
    79
  
deba@1728
    80
  {
deba@1728
    81
    const int nodeNum = 10;
deba@1728
    82
    const int edgeNum = 100;
deba@1728
    83
    ListGraph graph;
deba@1728
    84
    InDegMap<ListGraph> inDeg(graph);
deba@1729
    85
    OutDegMap<ListGraph> outDeg(graph);
deba@1728
    86
    std::vector<ListGraph::Node> nodes(nodeNum);
deba@1728
    87
    for (int i = 0; i < nodeNum; ++i) {
deba@1728
    88
      nodes[i] = graph.addNode();
deba@1728
    89
    }
deba@1728
    90
    std::vector<ListGraph::Edge> edges(edgeNum);
deba@1728
    91
    for (int i = 0; i < edgeNum; ++i) {
deba@1728
    92
      edges[i] = 
deba@1728
    93
	graph.addEdge(nodes[urandom(nodeNum)], nodes[urandom(nodeNum)]);
deba@1728
    94
    }
deba@1728
    95
    for (int i = 0; i < nodeNum; ++i) {
deba@1728
    96
      check(inDeg[nodes[i]] == countInEdges(graph, nodes[i]), 
deba@1728
    97
	    "Wrong in degree map");
deba@1728
    98
    }
deba@1728
    99
    for (int i = 0; i < nodeNum; ++i) {
deba@1729
   100
      check(outDeg[nodes[i]] == countOutEdges(graph, nodes[i]), 
deba@1728
   101
	    "Wrong in degree map");
deba@1728
   102
    }
deba@1728
   103
  }
alpar@1459
   104
alpar@1459
   105
  ///Everything is OK
klao@946
   106
  std::cout << __FILE__ ": All tests passed.\n";
klao@946
   107
klao@946
   108
  return 0;
klao@946
   109
}