Function if waypoints are in any order, but make nodejs require them to be in increasing order for sanitys sake.

This commit is contained in:
Daniel Patterson 2019-01-24 15:44:52 -08:00
parent 5f494de701
commit 8aa15fd3a2
No known key found for this signature in database
GPG Key ID: 19C12BE1725A028B
3 changed files with 35 additions and 26 deletions

View File

@ -996,6 +996,18 @@ argumentsToRouteParameter(const Nan::FunctionCallbackInfo<v8::Value> &args,
} }
params->waypoints.emplace_back(static_cast<unsigned>(waypoint_value->NumberValue())); params->waypoints.emplace_back(static_cast<unsigned>(waypoint_value->NumberValue()));
} }
if (!params->waypoints.empty())
{
for (std::size_t i = 0; i < params->waypoints.size() - 1; i++)
{
if (params->waypoints[i] >= params->waypoints[i + 1])
{
Nan::ThrowError("Waypoints must be supplied in increasing order");
return route_parameters_ptr();
}
}
}
} }
bool parsedSuccessfully = parseCommonParameters(obj, params); bool parsedSuccessfully = parseCommonParameters(obj, params);

View File

@ -83,18 +83,6 @@ Status ViaRoutePlugin::HandleRequest(const RoutingAlgorithmsInterface &algorithm
json_result); json_result);
} }
if (!route_parameters.waypoints.empty())
{
for (std::size_t i = 0; i < route_parameters.waypoints.size() - 1; i++)
{
if (route_parameters.waypoints[i] >= route_parameters.waypoints[i + 1])
{
return Error(
"InvalidValue", "Waypoints must be supplied in increasing order", json_result);
}
}
}
if (!CheckAlgorithms(route_parameters, algorithms, json_result)) if (!CheckAlgorithms(route_parameters, algorithms, json_result))
return Status::Error; return Status::Error;
@ -157,20 +145,17 @@ Status ViaRoutePlugin::HandleRequest(const RoutingAlgorithmsInterface &algorithm
auto collapse_legs = !route_parameters.waypoints.empty(); auto collapse_legs = !route_parameters.waypoints.empty();
if (collapse_legs) if (collapse_legs)
{ {
std::vector<bool> waypoint_legs; std::vector<bool> waypoint_legs(route_parameters.coordinates.size(), false);
auto waypoints_itr = route_parameters.waypoints.begin(); std::for_each(route_parameters.waypoints.begin(),
for (std::size_t i = 0; i < route_parameters.coordinates.size(); i++) route_parameters.waypoints.end(),
{ [&](const std::size_t waypoint_index) {
if (i == *waypoints_itr) BOOST_ASSERT(waypoint_index < waypoint_legs.size());
{ waypoint_legs[waypoint_index] = true;
waypoint_legs.push_back(true); });
waypoints_itr++; // First and last coordinates should always be waypoints
} // This gets validated earlier, but double-checking here, jic
else BOOST_ASSERT(waypoint_legs.front());
{ BOOST_ASSERT(waypoint_legs.back());
waypoint_legs.push_back(false);
}
}
for (std::size_t i = 0; i < routes.routes.size(); i++) for (std::size_t i = 0; i < routes.routes.size(); i++)
{ {
routes.routes[i] = CollapseInternalRouteResult(routes.routes[i], waypoint_legs); routes.routes[i] = CollapseInternalRouteResult(routes.routes[i], waypoint_legs);

View File

@ -677,4 +677,16 @@ test('route: throws on invalid waypoints values, waypoints must be an array of i
}; };
assert.throws(function () { osrm.route(options, function (err, response) { console.log(err); }); }, assert.throws(function () { osrm.route(options, function (err, response) { console.log(err); }); },
'Waypoint values must be an array of integers'); 'Waypoint values must be an array of integers');
});
test('route: throws on invalid waypoints values, waypoints must be an array of integers in increasing order', function (assert) {
assert.plan(1);
var osrm = new OSRM(monaco_path);
var options = {
steps: true,
coordinates: three_test_coordinates.concat(three_test_coordinates),
waypoints: [0,2,1,5]
};
assert.throws(function () { osrm.route(options, function (err, response) { console.error(`response: ${response}`); console.error(`error: ${err}`); }); },
/Waypoints must be supplied in increasing order/);
}); });