forked from valhalla/valhalla
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_truck_restrictions.cc
168 lines (135 loc) · 6.73 KB
/
test_truck_restrictions.cc
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "gurka.h"
#include "test.h"
#include <gtest/gtest.h>
using namespace valhalla;
inline float getDuration(const valhalla::Api& route) {
return route.directions().routes(0).legs(0).summary().time();
}
class TruckRestrictionTest : public ::testing::TestWithParam<std::pair<std::string, std::string>> {
protected:
static gurka::map map;
static void SetUpTestSuite() {
constexpr double gridsize = 10;
const std::string ascii_map = R"(
A---B---C---D
)";
const gurka::ways ways = {
{"AB", {{"highway", "residential"}}},
// all restrictions should be higher than our defaults, so we can actually see the impact of
// any single one
{"BC",
{{"highway", "residential"},
{"maxheight", "5"},
{"maxlength", "25"},
{"maxwidth", "3"},
{"hazmat", "destination"},
{"maxaxles", "8"},
{"maxaxleload", "10"}}},
{"CD", {{"highway", "residential"}}},
};
const auto layout = gurka::detail::map_to_coordinates(ascii_map, gridsize);
map = gurka::buildtiles(layout, ways, {}, {}, "test/data/truck_restrictions");
}
};
gurka::map TruckRestrictionTest::map = {};
TEST_P(TruckRestrictionTest, NotAllowed) {
std::string option, v;
std::tie(option, v) = GetParam();
// "no path could be found for input" should be raised if we exceed this costing option
try {
gurka::do_action(Options::route, map, {"A", "D"}, "truck",
{{"/costing_options/truck/" + option, v}});
FAIL() << "Expected no path to be found";
} catch (const valhalla_exception_t& err) { EXPECT_EQ(err.code, 442); } catch (...) {
FAIL() << "Expected valhalla_exception_t.";
};
}
TEST_F(TruckRestrictionTest, Allowed) {
// without setting a costing option, we should get a path
auto res = gurka::do_action(Options::route, map, {"A", "D"}, "truck");
gurka::assert::raw::expect_path(res, {"AB", "BC", "CD"});
}
INSTANTIATE_TEST_SUITE_P(TruckRestrictions,
TruckRestrictionTest,
::testing::Values(std::pair<std::string, std::string>{"height", "6"},
std::pair<std::string, std::string>{"width", "4"},
std::pair<std::string, std::string>{"length", "30"},
std::pair<std::string, std::string>{"hazmat", "1"},
std::pair<std::string, std::string>{"axle_load", "11"},
std::pair<std::string, std::string>{"axle_count", "10"}));
TEST(TruckSpeed, MaxTruckSpeed) {
constexpr double gridsize = 500;
const std::string ascii_map = R"(
A----------B
)";
const gurka::ways ways = {{"AB", {{"highway", "motorway"}}}};
const auto layout = gurka::detail::map_to_coordinates(ascii_map, gridsize);
gurka::map map = gurka::buildtiles(layout, ways, {}, {}, "test/data/truckspeed");
map.config.put("mjolnir.traffic_extract", "test/data/truckspeed/traffic.tar");
test::build_live_traffic_data(map.config);
valhalla::Api default_route =
gurka::do_action(valhalla::Options::route, map, {"A", "B"}, "truck", {});
// should be clamped to kMaxAssumedTruckSpeed
valhalla::Api clamped_top_speed_route =
gurka::do_action(valhalla::Options::route, map, {"A", "B"}, "truck",
{{"/costing_options/truck/top_speed", "100"},
{"/date_time/type", "0"},
{"/date_time/value", "current"}});
// just below kMaxAssumedTruckSpeed
valhalla::Api low_top_speed_route =
gurka::do_action(valhalla::Options::route, map, {"A", "B"}, "truck",
{{"/costing_options/truck/top_speed", "70"},
{"/date_time/type", "0"},
{"/date_time/value", "current"}});
test::customize_live_traffic_data(map.config, [&](baldr::GraphReader& reader,
baldr::TrafficTile& tile, u_int32_t index,
valhalla::baldr::TrafficSpeed* traffic_speed) {
baldr::GraphId tile_id(tile.header->tile_id);
auto AB = gurka::findEdge(reader, map.nodes, "AB", "B", tile_id);
if (std::get<1>(AB) != nullptr && std::get<0>(AB).id() == index) {
traffic_speed->overall_encoded_speed = 140 >> 1;
traffic_speed->breakpoint1 = 255;
traffic_speed->encoded_speed1 = 140 >> 1;
} else {
traffic_speed->overall_encoded_speed = UNKNOWN_TRAFFIC_SPEED_RAW - 1;
}
});
valhalla::Api modified_traffic_route =
gurka::do_action(valhalla::Options::route, map, {"A", "B"}, "truck",
{{"/date_time/type", "0"},
{"/date_time/value", "current"},
{"/costing_options/truck/speed_types/0", "current"}});
test::customize_live_traffic_data(map.config, [&](baldr::GraphReader& reader,
baldr::TrafficTile& tile, u_int32_t index,
valhalla::baldr::TrafficSpeed* traffic_speed) {
baldr::GraphId tile_id(tile.header->tile_id);
auto AB = gurka::findEdge(reader, map.nodes, "AB", "B", tile_id);
if (std::get<1>(AB) != nullptr && std::get<0>(AB).id() == index) {
traffic_speed->overall_encoded_speed = 50 >> 1;
traffic_speed->breakpoint1 = 255;
traffic_speed->encoded_speed1 = 50 >> 1;
} else {
traffic_speed->overall_encoded_speed = UNKNOWN_TRAFFIC_SPEED_RAW - 1;
}
});
valhalla::Api modified_traffic_low_speed_route =
gurka::do_action(valhalla::Options::route, map, {"A", "B"}, "truck",
{{"/date_time/type", "0"},
{"/date_time/value", "current"},
{"/costing_options/truck/speed_types/0", "current"}});
gurka::assert::raw::expect_path(default_route, {"AB"});
auto default_time = getDuration(default_route);
auto clamped_top_speed_time = getDuration(clamped_top_speed_route);
auto low_top_speed_time = getDuration(low_top_speed_route);
auto traffic_time = getDuration(modified_traffic_route);
auto traffic_low_speed_time = getDuration(modified_traffic_low_speed_route);
// default and clamped durations should be the same in this case
ASSERT_EQ(default_time, clamped_top_speed_time);
// expect a trip to take longer when a low top speed is set
ASSERT_LT(default_time, low_top_speed_time);
// expect duration to be equal to default if traffic speed is higher than kMaxAssumedTruckCost
// and no truck specific speed tag is set on the way
ASSERT_EQ(default_time, traffic_time);
// expect lower traffic speeds (< kMaxAssumedTruckSpeed ) to lead to a lower duration
ASSERT_LT(traffic_time, traffic_low_speed_time);
}