test/graph_utils_test.h
author deba
Tue, 14 Feb 2006 10:41:16 +0000
changeset 1968 78e6e2d1fd96
parent 1875 98698b69a902
child 2229 4dbb6dd2dd4b
permissions -rw-r--r--
Name modification
klao@946
     1
/* -*- C++ -*-
klao@946
     2
 *
alpar@1956
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@1956
     4
 *
alpar@1956
     5
 * Copyright (C) 2003-2006
alpar@1956
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1359
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
klao@946
     8
 *
klao@946
     9
 * Permission to use, modify and distribute this software is granted
klao@946
    10
 * provided that this copyright notice appears in all copies. For
klao@946
    11
 * precise terms see the accompanying LICENSE file.
klao@946
    12
 *
klao@946
    13
 * This software is provided "AS IS" with no warranty of any kind,
klao@946
    14
 * express or implied, and with no claim as to its suitability for any
klao@946
    15
 * purpose.
klao@946
    16
 *
klao@946
    17
 */
alpar@1956
    18
klao@946
    19
#ifndef LEMON_TEST_GRAPH_UTILS_TEST_H
klao@946
    20
#define LEMON_TEST_GRAPH_UTILS_TEST_H
klao@946
    21
klao@946
    22
klao@946
    23
#include "test_tools.h"
deba@1568
    24
#include <cstdlib>
deba@1568
    25
#include <ctime>
klao@946
    26
klao@946
    27
//! \ingroup misc
klao@946
    28
//! \file
klao@946
    29
//! \brief Test cases for graph utils.
klao@946
    30
namespace lemon {
klao@946
    31
  
klao@946
    32
  template <typename Graph>
klao@946
    33
  void checkGraphCounters() {
klao@946
    34
    const int num = 5;
klao@946
    35
    Graph graph;
klao@946
    36
    addPetersen(graph, num);
klao@946
    37
    bidirGraph(graph);
klao@977
    38
    check(countNodes(graph) == 2*num, "Wrong node number.");
klao@977
    39
    check(countEdges(graph) == 6*num, "Wrong edge number.");    
klao@946
    40
    for (typename Graph::NodeIt it(graph); it != INVALID; ++it) {
klao@977
    41
      check(countOutEdges(graph, it) == 3, "Wrong out degree number.");
klao@977
    42
      check(countInEdges(graph, it) == 3, "Wrong in degree number.");
klao@946
    43
    }
klao@946
    44
  }
deba@1568
    45
deba@1568
    46
  template <typename Graph>
deba@1568
    47
  void checkFindEdge() {
deba@1568
    48
    typedef typename Graph::Node Node;
deba@1568
    49
    typedef typename Graph::Edge Edge;
deba@1568
    50
    typedef typename Graph::NodeIt NodeIt;
deba@1568
    51
    typedef typename Graph::EdgeIt EdgeIt;
deba@1568
    52
    Graph graph;
deba@1568
    53
    srand(time(0));
deba@1568
    54
    for (int i = 0; i < 10; ++i) {
deba@1568
    55
      graph.addNode();
deba@1568
    56
    }
deba@1568
    57
    DescriptorMap<Graph, Node> nodes(graph);
deba@1568
    58
    typename DescriptorMap<Graph, Node>::InverseMap invNodes(nodes);
deba@1568
    59
    for (int i = 0; i < 100; ++i) {
deba@1568
    60
      int src = (int)(rand() / (RAND_MAX + 1.0) * invNodes.size());
deba@1568
    61
      int trg = (int)(rand() / (RAND_MAX + 1.0) * invNodes.size());
deba@1568
    62
      graph.addEdge(invNodes[src], invNodes[trg]);
deba@1568
    63
    }
deba@1568
    64
    typename Graph::template EdgeMap<bool> found(graph, false);
deba@1568
    65
    DescriptorMap<Graph, Edge> edges(graph);
deba@1568
    66
    for (NodeIt src(graph); src != INVALID; ++src) {
deba@1568
    67
      for (NodeIt trg(graph); trg != INVALID; ++trg) {
deba@1568
    68
	for (ConEdgeIt<Graph> con(graph, src, trg); con != INVALID; ++con) {
deba@1568
    69
	  check(graph.source(con) == src, "Wrong source.");
deba@1568
    70
	  check(graph.target(con) == trg, "Wrong target.");
deba@1568
    71
	  check(found[con] == false, "The edge found already.");
deba@1568
    72
	  found[con] = true;
deba@1568
    73
	}
deba@1568
    74
      }
deba@1568
    75
    }
deba@1568
    76
    for (EdgeIt it(graph); it != INVALID; ++it) {
deba@1568
    77
      check(found[it] == true, "The edge is not found.");
deba@1568
    78
    }
deba@1568
    79
  }
klao@946
    80
  
klao@946
    81
} //namespace lemon
klao@946
    82
klao@946
    83
klao@946
    84
#endif