osrm-backend/unit_tests/extractor/graph_compressor.cpp

265 lines
9.4 KiB
C++

#include "extractor/graph_compressor.hpp"
#include "extractor/compressed_edge_container.hpp"
#include "extractor/restriction.hpp"
#include "util/node_based_graph.hpp"
#include "util/typedefs.hpp"
#include "../unit_tests/mocks/mock_scripting_environment.hpp"
#include <boost/test/test_case_template.hpp>
#include <boost/test/unit_test.hpp>
#include <iostream>
#include <unordered_set>
#include <vector>
BOOST_AUTO_TEST_SUITE(graph_compressor)
using namespace osrm;
using namespace osrm::extractor;
using InputEdge = util::NodeBasedDynamicGraph::InputEdge;
using Graph = util::NodeBasedDynamicGraph;
namespace
{
// creates a default edge of unit weight
inline InputEdge MakeUnitEdge(const NodeID from, const NodeID to)
{
// src, tgt, dist, edge_id, name_id, fwd, bkwd, roundabout, circular, startpoint, local access,
// split edge, travel_mode
return {
from, // source
to, // target
1, // weight
1, // duration
SPECIAL_EDGEID, // edge_id
0, // name_id
false, // reversed
false, // roundabout
false, // circular
false, // startpoint
false, // is_left_hand_driving
true, // split edge
TRAVEL_MODE_INACCESSIBLE, // travel_mode
0, // classes
INVALID_LANE_DESCRIPTIONID // lane_description_id
};
}
} // namespace
BOOST_AUTO_TEST_CASE(long_road_test)
{
//
// 0---1---2---3---4
//
GraphCompressor compressor;
std::unordered_set<NodeID> barrier_nodes;
std::unordered_set<NodeID> traffic_lights;
std::vector<TurnRestriction> restrictions;
std::vector<ConditionalTurnRestriction> conditional_restrictions;
CompressedEdgeContainer container;
test::MockScriptingEnvironment scripting_environment;
std::vector<InputEdge> edges = {MakeUnitEdge(0, 1),
MakeUnitEdge(1, 0),
MakeUnitEdge(1, 2),
MakeUnitEdge(2, 1),
MakeUnitEdge(2, 3),
MakeUnitEdge(3, 2),
MakeUnitEdge(3, 4),
MakeUnitEdge(4, 3)};
BOOST_ASSERT(edges[0].data.IsCompatibleTo(edges[2].data));
BOOST_ASSERT(edges[2].data.IsCompatibleTo(edges[4].data));
BOOST_ASSERT(edges[4].data.IsCompatibleTo(edges[6].data));
Graph graph(5, edges);
compressor.Compress(barrier_nodes,
traffic_lights,
scripting_environment,
restrictions,
conditional_restrictions,
graph,
container);
BOOST_CHECK_EQUAL(graph.FindEdge(0, 1), SPECIAL_EDGEID);
BOOST_CHECK_EQUAL(graph.FindEdge(1, 2), SPECIAL_EDGEID);
BOOST_CHECK_EQUAL(graph.FindEdge(2, 3), SPECIAL_EDGEID);
BOOST_CHECK_EQUAL(graph.FindEdge(3, 4), SPECIAL_EDGEID);
BOOST_CHECK(graph.FindEdge(0, 4) != SPECIAL_EDGEID);
}
BOOST_AUTO_TEST_CASE(loop_test)
{
//
// 0---1---2
// | |
// 5---4---3
//
GraphCompressor compressor;
std::unordered_set<NodeID> barrier_nodes;
std::unordered_set<NodeID> traffic_lights;
std::vector<TurnRestriction> restrictions;
std::vector<ConditionalTurnRestriction> conditional_restrictions;
CompressedEdgeContainer container;
test::MockScriptingEnvironment scripting_environment;
std::vector<InputEdge> edges = {MakeUnitEdge(0, 1),
MakeUnitEdge(0, 5),
MakeUnitEdge(1, 0),
MakeUnitEdge(1, 2),
MakeUnitEdge(2, 1),
MakeUnitEdge(2, 3),
MakeUnitEdge(3, 2),
MakeUnitEdge(3, 4),
MakeUnitEdge(4, 3),
MakeUnitEdge(4, 5),
MakeUnitEdge(5, 0),
MakeUnitEdge(5, 4)};
BOOST_ASSERT(edges.size() == 12);
BOOST_ASSERT(edges[0].data.IsCompatibleTo(edges[1].data));
BOOST_ASSERT(edges[1].data.IsCompatibleTo(edges[2].data));
BOOST_ASSERT(edges[2].data.IsCompatibleTo(edges[3].data));
BOOST_ASSERT(edges[3].data.IsCompatibleTo(edges[4].data));
BOOST_ASSERT(edges[4].data.IsCompatibleTo(edges[5].data));
BOOST_ASSERT(edges[5].data.IsCompatibleTo(edges[6].data));
BOOST_ASSERT(edges[6].data.IsCompatibleTo(edges[7].data));
BOOST_ASSERT(edges[7].data.IsCompatibleTo(edges[8].data));
BOOST_ASSERT(edges[8].data.IsCompatibleTo(edges[9].data));
BOOST_ASSERT(edges[9].data.IsCompatibleTo(edges[10].data));
BOOST_ASSERT(edges[10].data.IsCompatibleTo(edges[11].data));
Graph graph(6, edges);
compressor.Compress(barrier_nodes,
traffic_lights,
scripting_environment,
restrictions,
conditional_restrictions,
graph,
container);
BOOST_CHECK_EQUAL(graph.FindEdge(5, 0), SPECIAL_EDGEID);
BOOST_CHECK_EQUAL(graph.FindEdge(0, 1), SPECIAL_EDGEID);
BOOST_CHECK_EQUAL(graph.FindEdge(1, 2), SPECIAL_EDGEID);
BOOST_CHECK_EQUAL(graph.FindEdge(2, 3), SPECIAL_EDGEID);
BOOST_CHECK(graph.FindEdge(5, 3) != SPECIAL_EDGEID);
BOOST_CHECK(graph.FindEdge(3, 4) != SPECIAL_EDGEID);
BOOST_CHECK(graph.FindEdge(4, 5) != SPECIAL_EDGEID);
}
BOOST_AUTO_TEST_CASE(t_intersection)
{
//
// 0---1---2
// |
// 3
//
GraphCompressor compressor;
std::unordered_set<NodeID> barrier_nodes;
std::unordered_set<NodeID> traffic_lights;
std::vector<TurnRestriction> restrictions;
std::vector<ConditionalTurnRestriction> conditional_restrictions;
CompressedEdgeContainer container;
test::MockScriptingEnvironment scripting_environment;
std::vector<InputEdge> edges = {MakeUnitEdge(0, 1),
MakeUnitEdge(1, 0),
MakeUnitEdge(1, 2),
MakeUnitEdge(1, 3),
MakeUnitEdge(2, 1),
MakeUnitEdge(3, 1)};
BOOST_ASSERT(edges[0].data.IsCompatibleTo(edges[1].data));
BOOST_ASSERT(edges[1].data.IsCompatibleTo(edges[2].data));
BOOST_ASSERT(edges[2].data.IsCompatibleTo(edges[3].data));
BOOST_ASSERT(edges[3].data.IsCompatibleTo(edges[4].data));
BOOST_ASSERT(edges[4].data.IsCompatibleTo(edges[5].data));
Graph graph(4, edges);
compressor.Compress(barrier_nodes,
traffic_lights,
scripting_environment,
restrictions,
conditional_restrictions,
graph,
container);
BOOST_CHECK(graph.FindEdge(0, 1) != SPECIAL_EDGEID);
BOOST_CHECK(graph.FindEdge(1, 2) != SPECIAL_EDGEID);
BOOST_CHECK(graph.FindEdge(1, 3) != SPECIAL_EDGEID);
}
BOOST_AUTO_TEST_CASE(street_name_changes)
{
//
// 0---1---2
//
GraphCompressor compressor;
std::unordered_set<NodeID> barrier_nodes;
std::unordered_set<NodeID> traffic_lights;
std::vector<TurnRestriction> restrictions;
std::vector<ConditionalTurnRestriction> conditional_restrictions;
CompressedEdgeContainer container;
test::MockScriptingEnvironment scripting_environment;
std::vector<InputEdge> edges = {
MakeUnitEdge(0, 1), MakeUnitEdge(1, 0), MakeUnitEdge(1, 2), MakeUnitEdge(2, 1)};
edges[2].data.name_id = edges[3].data.name_id = 1;
BOOST_ASSERT(edges[0].data.IsCompatibleTo(edges[1].data));
BOOST_ASSERT(edges[2].data.IsCompatibleTo(edges[3].data));
Graph graph(5, edges);
compressor.Compress(barrier_nodes,
traffic_lights,
scripting_environment,
restrictions,
conditional_restrictions,
graph,
container);
BOOST_CHECK(graph.FindEdge(0, 1) != SPECIAL_EDGEID);
BOOST_CHECK(graph.FindEdge(1, 2) != SPECIAL_EDGEID);
}
BOOST_AUTO_TEST_CASE(direction_changes)
{
//
// 0-->1---2
//
GraphCompressor compressor;
std::unordered_set<NodeID> barrier_nodes;
std::unordered_set<NodeID> traffic_lights;
std::vector<TurnRestriction> restrictions;
std::vector<ConditionalTurnRestriction> conditional_restrictions;
CompressedEdgeContainer container;
test::MockScriptingEnvironment scripting_environment;
std::vector<InputEdge> edges = {
MakeUnitEdge(0, 1), MakeUnitEdge(1, 0), MakeUnitEdge(1, 2), MakeUnitEdge(2, 1)};
// make first edge point forward
edges[1].data.reversed = true;
Graph graph(5, edges);
compressor.Compress(barrier_nodes,
traffic_lights,
scripting_environment,
restrictions,
conditional_restrictions,
graph,
container);
BOOST_CHECK(graph.FindEdge(0, 1) != SPECIAL_EDGEID);
BOOST_CHECK(graph.FindEdge(1, 2) != SPECIAL_EDGEID);
}
BOOST_AUTO_TEST_SUITE_END()