diff --git a/unit_tests/library/nearest.cpp b/unit_tests/library/nearest.cpp index 24960a7f6..9119c1042 100644 --- a/unit_tests/library/nearest.cpp +++ b/unit_tests/library/nearest.cpp @@ -4,6 +4,7 @@ #include "coordinates.hpp" #include "fixture.hpp" +#include "engine/api/flatbuffers/fbresult_generated.h" #include "osrm/nearest_parameters.hpp" #include "osrm/coordinate.hpp" @@ -117,4 +118,51 @@ BOOST_AUTO_TEST_CASE(test_nearest_response_for_location_in_small_component) } } +BOOST_AUTO_TEST_CASE(test_nearest_fb_serilization) +{ + auto osrm = getOSRM(OSRM_TEST_DATA_DIR "/ch/monaco.osrm"); + + using namespace osrm; + + NearestParameters params; + params.coordinates.push_back(get_dummy_location()); + + engine::api::ResultT result = flatbuffers::FlatBufferBuilder(); + const auto rc = osrm.Nearest(params, result); + BOOST_REQUIRE(rc == Status::Ok); + + auto &fb_result = result.get(); + auto fb = engine::api::fbresult::GetFBResult(fb_result.GetBufferPointer()); + BOOST_CHECK(!fb->error()); + + BOOST_CHECK(fb->waypoints() != nullptr); + auto waypoints = fb->waypoints(); + BOOST_CHECK(waypoints->size() > 0); // the dataset has at least one nearest coordinate + + for (const auto &waypoint : *waypoints) + { + BOOST_CHECK(waypoint->distance() >= 0); + BOOST_CHECK(waypoint->nodes()->first() != 0); + BOOST_CHECK(waypoint->nodes()->second() != 0); + } +} + +BOOST_AUTO_TEST_CASE(test_nearest_fb_error) +{ + auto osrm = getOSRM(OSRM_TEST_DATA_DIR "/ch/monaco.osrm"); + + using namespace osrm; + + NearestParameters params; + + engine::api::ResultT result = flatbuffers::FlatBufferBuilder(); + const auto rc = osrm.Nearest(params, result); + BOOST_REQUIRE(rc == Status::Error); + + auto &fb_result = result.get(); + auto fb = engine::api::fbresult::GetFBResult(fb_result.GetBufferPointer()); + BOOST_CHECK(fb->error()); + BOOST_CHECK_EQUAL(fb->code()->code()->str(), "InvalidOptions"); +} + BOOST_AUTO_TEST_SUITE_END() diff --git a/unit_tests/library/route.cpp b/unit_tests/library/route.cpp index 4ce8e6a3c..4ff7446b3 100644 --- a/unit_tests/library/route.cpp +++ b/unit_tests/library/route.cpp @@ -7,6 +7,7 @@ #include "equal_json.hpp" #include "fixture.hpp" +#include "engine/api/flatbuffers/fbresult_generated.h" #include "osrm/coordinate.hpp" #include "osrm/engine_config.hpp" #include "osrm/exception.hpp" @@ -476,4 +477,102 @@ BOOST_AUTO_TEST_CASE(test_manual_setting_of_annotations_property) BOOST_CHECK_EQUAL(annotations.size(), 6); } +BOOST_AUTO_TEST_CASE(test_route_serialize_fb) +{ + auto osrm = getOSRM(OSRM_TEST_DATA_DIR "/ch/monaco.osrm"); + + using namespace osrm; + + RouteParameters params; + params.steps = true; + params.coordinates.push_back(get_dummy_location()); + params.coordinates.push_back(get_dummy_location()); + params.coordinates.push_back(get_dummy_location()); + + engine::api::ResultT result = flatbuffers::FlatBufferBuilder(); + const auto rc = osrm.Route(params, result); + BOOST_CHECK(rc == Status::Ok); + + auto &fb_result = result.get(); + auto fb = engine::api::fbresult::GetFBResult(fb_result.GetBufferPointer()); + BOOST_CHECK(!fb->error()); + + BOOST_CHECK(fb->waypoints() != nullptr); + const auto waypoints = fb->waypoints(); + BOOST_CHECK(waypoints->size() == params.coordinates.size()); + + for (const auto &waypoint : *waypoints) + { + const auto longitude = waypoint->location()->longitute(); + const auto latitude = waypoint->location()->latitude(); + BOOST_CHECK(longitude >= -180. && longitude <= 180.); + BOOST_CHECK(latitude >= -90. && latitude <= 90.); + + BOOST_CHECK(!waypoint->hint()->str().empty()); + } + + BOOST_CHECK(fb->routes() != nullptr); + const auto routes = fb->routes(); + BOOST_REQUIRE_GT(routes->size(), 0); + + for (const auto &route : *routes) + { + BOOST_CHECK_EQUAL(route->distance(), 0); + BOOST_CHECK_EQUAL(route->duration(), 0); + + const auto &legs = route->legs(); + BOOST_CHECK(legs->size() > 0); + + for (const auto &leg : *legs) + { + BOOST_CHECK_EQUAL(leg->distance(), 0); + + BOOST_CHECK_EQUAL(leg->duration(), 0); + + BOOST_CHECK(leg->steps() != nullptr); + const auto steps = leg->steps(); + BOOST_CHECK(steps->size() > 0); + + std::size_t step_count = 0; + + for (const auto step : *steps) + { + BOOST_CHECK_EQUAL(step->distance(), 0); + + BOOST_CHECK_EQUAL(step->duration(), 0); + + BOOST_CHECK(step->maneuver() != nullptr); + + BOOST_CHECK(step->intersections() != nullptr); + const auto intersections = step->intersections(); + + for (auto intersection : *intersections) + { + const auto longitude = intersection->location()->longitute(); + const auto latitude = intersection->location()->latitude(); + BOOST_CHECK(longitude >= -180. && longitude <= 180.); + BOOST_CHECK(latitude >= -90. && latitude <= 90.); + + BOOST_CHECK(intersection->bearings() != nullptr); + const auto bearings = intersection->bearings(); + BOOST_CHECK(bearings->size() > 0); + + for (const auto bearing : *bearings) + BOOST_CHECK(0. <= bearing && bearing <= 360.); + + if (step_count > 0) + { + BOOST_CHECK(intersection->in() < bearings->size()); + } + if (step_count + 1 < steps->size()) + { + BOOST_CHECK(intersection->out() < bearings->size()); + } + } + ++step_count; + } + } + } +} + BOOST_AUTO_TEST_SUITE_END() diff --git a/unit_tests/library/table.cpp b/unit_tests/library/table.cpp index f463b32c9..111864558 100644 --- a/unit_tests/library/table.cpp +++ b/unit_tests/library/table.cpp @@ -194,4 +194,57 @@ BOOST_AUTO_TEST_CASE(test_table_no_segment_for_some_coordinates) BOOST_CHECK_EQUAL(code, "NoSegment"); } +BOOST_AUTO_TEST_CASE(test_table_serialiaze_fb) +{ + using namespace osrm; + + auto osrm = getOSRM(OSRM_TEST_DATA_DIR "/ch/monaco.osrm"); + + TableParameters params; + params.coordinates.push_back(get_dummy_location()); + params.coordinates.push_back(get_dummy_location()); + params.coordinates.push_back(get_dummy_location()); + params.sources.push_back(0); + params.destinations.push_back(2); + params.annotations = TableParameters::AnnotationsType::All; + + engine::api::ResultT result = flatbuffers::FlatBufferBuilder(); + + const auto rc = osrm.Table(params, result); + + BOOST_CHECK(rc == Status::Ok || rc == Status::Error); + + auto &fb_result = result.get(); + auto fb = engine::api::fbresult::GetFBResult(fb_result.GetBufferPointer()); + BOOST_CHECK(!fb->error()); + BOOST_CHECK(fb->table() != nullptr); + + // check that returned durations error is expected size and proportions + // this test expects a 1x1 matrix + BOOST_CHECK(fb->table()->durations() != nullptr); + auto durations_array = fb->table()->durations(); + BOOST_CHECK_EQUAL(durations_array->size(), params.sources.size() * params.destinations.size()); + + // check that returned distances error is expected size and proportions + // this test expects a 1x1 matrix + BOOST_CHECK(fb->table()->distances() != nullptr); + auto distances_array = fb->table()->distances(); + BOOST_CHECK_EQUAL(distances_array->size(), params.sources.size() * params.destinations.size()); + + // check destinations array of waypoint objects + const auto &destinations_array = fb->table()->destinations(); + BOOST_CHECK_EQUAL(destinations_array->size(), params.destinations.size()); + for (const auto &destination : *destinations_array) + { + BOOST_CHECK(waypoint_check(destination)); + } + // check sources array of waypoint objects + const auto &sources_array = fb->waypoints(); + BOOST_CHECK_EQUAL(sources_array->size(), params.sources.size()); + for (const auto &source : *sources_array) + { + BOOST_CHECK(waypoint_check(source)); + } +} + BOOST_AUTO_TEST_SUITE_END() diff --git a/unit_tests/library/waypoint_check.hpp b/unit_tests/library/waypoint_check.hpp index b14a209a3..fe80a5435 100644 --- a/unit_tests/library/waypoint_check.hpp +++ b/unit_tests/library/waypoint_check.hpp @@ -1,6 +1,7 @@ #ifndef OSRM_UNIT_TEST_WAYPOINT_CHECK #define OSRM_UNIT_TEST_WAYPOINT_CHECK +#include "engine/api/flatbuffers/fbresult_generated.h" #include "osrm/coordinate.hpp" #include "osrm/json_container.hpp" #include "util/exception.hpp" @@ -21,4 +22,12 @@ inline bool waypoint_check(json::Value waypoint) return location_coordinate.IsValid(); } +inline bool waypoint_check(const osrm::engine::api::fbresult::Waypoint *const waypoint) +{ + util::FloatLongitude lon{waypoint->location()->longitute()}; + util::FloatLatitude lat{waypoint->location()->latitude()}; + util::Coordinate location_coordinate(lon, lat); + return location_coordinate.IsValid(); +} + #endif