Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Piotr Wygocki
committed
Oct 22, 2018
1 parent
87e3ef8
commit da83bc8
Showing
1 changed file
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
//======================================================================= | ||
// Copyright 2015 University of Warsaw. | ||
// Authors: Piotr Wygocki | ||
// | ||
// Distributed under the Boost Software License, Version 1.0. (See | ||
// accompanying file LICENSE_1_0.txt or copy at | ||
// http://www.boost.org/LICENSE_1_0.txt) | ||
//======================================================================= | ||
#define BOOST_TEST_DYN_LINK | ||
#define BOOST_TEST_MODULE non_trivial_distance_map_test | ||
|
||
#include <boost/test/unit_test.hpp> | ||
|
||
#include <boost/graph/adjacency_list.hpp> | ||
#include <boost/graph/dijkstra_shortest_paths.hpp> | ||
|
||
|
||
typedef boost::property<boost::edge_weight_t, int> EdgeProp; | ||
typedef boost::adjacency_list< | ||
boost::vecS, boost::vecS, boost::undirectedS, | ||
boost::property<boost::vertex_color_t, int>, EdgeProp> Graph; | ||
typedef std::pair<int, int> Edge; | ||
typedef boost::property_map<Graph, boost::vertex_index_t>::const_type idx_type; | ||
|
||
template <typename ExaminedMap, typename Tag> | ||
class examined_recorder : boost::base_visitor< examined_recorder<ExaminedMap, Tag> > { | ||
|
||
//! Stores distances to the closest vertex in a particular layer | ||
ExaminedMap m_examined_map; | ||
|
||
public: | ||
typedef Tag event_filter; | ||
|
||
//! Constructor | ||
explicit examined_recorder(ExaminedMap const examined_map) : m_examined_map(examined_map) {} | ||
|
||
//! Copies examined value from precdessor | ||
template <typename Vertex> | ||
void operator()(Vertex const v, Graph const &g) const { | ||
put(m_examined_map, v, 1); | ||
} | ||
|
||
}; | ||
|
||
|
||
template <typename ExaminedMap, typename Tag> | ||
examined_recorder<ExaminedMap, Tag> | ||
make_examined_recorder(ExaminedMap examined_map, Tag) { | ||
return examined_recorder<ExaminedMap, Tag>(examined_map); | ||
} | ||
|
||
|
||
BOOST_AUTO_TEST_CASE(non_trivial_distance_map_test) { | ||
enum nodes { A, B, C, D, E, num_nodes}; | ||
Edge edge_array[] = { Edge(A, B), Edge(B, C), Edge(C, D), Edge(D, E)}; | ||
int const weights[] = {1, 1, 1, 1}; | ||
std::vector<int> distance(num_nodes, 1); | ||
distance[0] = 0; distance[1] = 100; | ||
int const num_arcs = sizeof(edge_array) / sizeof(Edge); | ||
|
||
Graph g(edge_array, edge_array + num_arcs, weights, num_nodes); | ||
idx_type idx = get(boost::vertex_index, g); | ||
std::vector<int> examined(num_vertices(g), -1); | ||
|
||
boost::dijkstra_shortest_paths_no_init( | ||
g, | ||
A, | ||
boost::dummy_property_map(), | ||
make_iterator_property_map(distance.begin(), idx, distance[0]), | ||
get(boost::edge_weight, g), | ||
idx, | ||
std::less<int>(), | ||
boost::closed_plus<int>(), | ||
0, | ||
boost::make_dijkstra_visitor(make_examined_recorder( | ||
make_iterator_property_map(examined.begin(), idx, examined[0]), | ||
boost::on_examine_vertex()) | ||
) | ||
); | ||
BOOST_CHECK_EQUAL(examined[A], 1); | ||
BOOST_CHECK_EQUAL(examined[B], 1); | ||
BOOST_CHECK_EQUAL(examined[C], -1); | ||
BOOST_CHECK_EQUAL(examined[D], -1); | ||
BOOST_CHECK_EQUAL(examined[E], -1); | ||
} | ||
|