File: cycle_test.hpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (92 lines) | stat: -rw-r--r-- 2,808 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// (C) Copyright 2013 Louis Dionne
//
// Modified from `tiernan_all_cycles.cpp`.
//
// Use, modification and distribution are subject to the
// Boost Software License, Version 1.0 (See accompanying file
// LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)

#ifndef BOOST_GRAPH_TEST_CYCLE_TEST_HPP
#define BOOST_GRAPH_TEST_CYCLE_TEST_HPP

#include <boost/assert.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/graph/directed_graph.hpp>
#include <boost/graph/erdos_renyi_generator.hpp>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/graph_utility.hpp>
#include <boost/graph/undirected_graph.hpp>
#include <boost/next_prior.hpp>
#include <boost/random/linear_congruential.hpp>
#include <cstddef>
#include <iostream>

namespace cycle_test_detail
{
using namespace boost;

struct cycle_validator
{
    explicit cycle_validator(std::size_t& number_of_cycles)
    : cycles(number_of_cycles)
    {
    }

    template < typename Path, typename Graph >
    void cycle(Path const& p, Graph const& g)
    {
        ++cycles;
        // Check to make sure that each of the vertices in the path
        // is truly connected and that the back is connected to the
        // front - it's not validating that we find all paths, just
        // that the paths are valid.
        typename Path::const_iterator i, j, last = prior(p.end());
        for (i = p.begin(); i != last; ++i)
        {
            j = boost::next(i);
            BOOST_ASSERT(edge(*i, *j, g).second);
        }
        BOOST_ASSERT(edge(p.back(), p.front(), g).second);
    }

    std::size_t& cycles;
};

template < typename Graph, typename Algorithm >
void test_one(Algorithm algorithm, std::size_t num_cycles_expected)
{
    typedef erdos_renyi_iterator< minstd_rand, Graph > er;

    // Generate random graph with N vertices and probability P
    // of edge connection.
    static std::size_t const N = 20;
    static double const P = 0.1;
    minstd_rand rng;

    Graph g(er(rng, N, P), er(), N);
    renumber_indices(g);
    print_edges(g, get(vertex_index, g));

    std::size_t cycles = 0;
    cycle_validator vis(cycles);
    algorithm(g, vis);
    std::cout << "# cycles: " << vis.cycles << "\n";

    BOOST_TEST(vis.cycles == num_cycles_expected);
}
} // end namespace cycle_test_detail

template < typename Algorithm > void cycle_test(Algorithm const& algorithm,
  std::size_t num_cycles_expected_undirected,
  std::size_t num_cycles_expected_directed)
{
    std::cout << "*** undirected ***\n";
    cycle_test_detail::test_one< boost::undirected_graph<> >(algorithm,
      num_cycles_expected_undirected);

    std::cout << "*** directed ***\n";
    cycle_test_detail::test_one< boost::directed_graph<> >(algorithm,
      num_cycles_expected_directed);
}

#endif // !BOOST_GRAPH_TEST_CYCLE_TEST_HPP