Compare commits

...

4 Commits

Author SHA1 Message Date
Siarhei Fedartsou
07746de667 Try to get rid of std::variant in json_container.hpp 2024-09-29 09:12:35 +02:00
Siarhei Fedartsou
feb8da25cd Try to get rid of std::variant in json_container.hpp 2024-09-28 21:51:59 +02:00
Siarhei Fedartsou
150c792f76 Try to get rid of std::variant in json_container.hpp 2024-09-28 21:38:46 +02:00
Siarhei Fedartsou
dbc52728c3 Try to get rid of std::variant in json_container.hpp 2024-09-28 21:34:24 +02:00
7 changed files with 483 additions and 350 deletions

View File

@ -90,33 +90,164 @@ struct Null
{ {
}; };
/**
* Typed Value sum-type implemented as a variant able to represent tree-like JSON structures.
*
* Dispatch on its type by either by using apply_visitor or its get function.
*/
using Value = std::variant<String, Number, Object, Array, True, False, Null>;
/** /**
* Typed Object. * Typed Object.
* *
* Unwrap the key-value pairs holding type via its values member attribute. * Unwrap the key-value pairs holding type via its values member attribute.
*/ */
struct Object struct Object;
{
std::unordered_map<std::string, Value> values;
};
/** /**
* Typed Array. * Typed Array.
* *
* Unwrap the Value holding type via its values member attribute. * Unwrap the Value holding type via its values member attribute.
*/ */
struct Array;
struct Value;
// Definitions of Object and Array (must come after Value is defined)
struct Object
{
std::unordered_map<std::string, Value> values;
};
struct Array struct Array
{ {
std::vector<Value> values; std::vector<Value> values;
}; };
struct Value
{
enum class Type
{
Invalid,
String,
Number,
Object,
Array,
True,
False,
Null
};
String string;
Number number;
Object object;
Array array;
Type type;
Value() noexcept : type(Type::Invalid) {}
Value(const Null &) noexcept : type(Type::Null) {}
Value(const True &) noexcept : type(Type::True) {}
Value(const False &) noexcept : type(Type::False) {}
Value(String &&string_) noexcept : string(std::move(string_)), type(Type::String) {}
Value(Number &&number_) noexcept : number(number_), type(Type::Number) {}
Value(Object &&object_) noexcept : object(std::move(object_)), type(Type::Object) {}
Value(Array &&array_) noexcept : array(std::move(array_)), type(Type::Array) {}
Value(const String &string_) noexcept : string(string_), type(Type::String) {}
Value(const Number &number_) noexcept : number(number_), type(Type::Number) {}
Value(const Object &object_) noexcept : object(object_), type(Type::Object) {}
Value(const Array &array_) noexcept : array(array_), type(Type::Array) {}
Value(double number) noexcept : number(number), type(Type::Number) {}
Value(std::string string) noexcept : string(std::move(string)), type(Type::String) {}
Value(const char *string) noexcept : string(string), type(Type::String) {}
};
} // namespace osrm::util::json } // namespace osrm::util::json
namespace std
{
template <typename T> inline T &get(osrm::util::json::Value &value) noexcept;
template <>
inline osrm::util::json::String &
get<osrm::util::json::String>(osrm::util::json::Value &value) noexcept
{
return value.string;
}
template <>
inline osrm::util::json::Number &
get<osrm::util::json::Number>(osrm::util::json::Value &value) noexcept
{
return value.number;
}
template <>
inline osrm::util::json::Object &
get<osrm::util::json::Object>(osrm::util::json::Value &value) noexcept
{
return value.object;
}
template <>
inline osrm::util::json::Array &
get<osrm::util::json::Array>(osrm::util::json::Value &value) noexcept
{
return value.array;
}
template <typename T> inline const T &get(const osrm::util::json::Value &value) noexcept;
template <>
inline const osrm::util::json::String &
get<osrm::util::json::String>(const osrm::util::json::Value &value) noexcept
{
return value.string;
}
template <>
inline const osrm::util::json::Number &
get<osrm::util::json::Number>(const osrm::util::json::Value &value) noexcept
{
return value.number;
}
template <>
inline const osrm::util::json::Object &
get<osrm::util::json::Object>(const osrm::util::json::Value &value) noexcept
{
return value.object;
}
template <>
inline const osrm::util::json::Array &
get<osrm::util::json::Array>(const osrm::util::json::Value &value) noexcept
{
return value.array;
}
template <typename Visitor>
inline void visit(Visitor &&visitor, const osrm::util::json::Value &value)
{
switch (value.type)
{
case osrm::util::json::Value::Type::String:
visitor(value.string);
break;
case osrm::util::json::Value::Type::Number:
visitor(value.number);
break;
case osrm::util::json::Value::Type::Object:
visitor(value.object);
break;
case osrm::util::json::Value::Type::Array:
visitor(value.array);
break;
case osrm::util::json::Value::Type::True:
visitor(osrm::util::json::True{});
break;
case osrm::util::json::Value::Type::False:
visitor(osrm::util::json::False{});
break;
case osrm::util::json::Value::Type::Null:
visitor(osrm::util::json::Null{});
break;
case osrm::util::json::Value::Type::Invalid:
break;
}
}
} // namespace std
#endif // JSON_CONTAINER_HPP #endif // JSON_CONTAINER_HPP

View File

@ -1,157 +1,159 @@
#ifndef UTIL_JSON_DEEP_COMPARE_HPP // #ifndef UTIL_JSON_DEEP_COMPARE_HPP
#define UTIL_JSON_DEEP_COMPARE_HPP // #define UTIL_JSON_DEEP_COMPARE_HPP
#include "util/integer_range.hpp" // #include "util/integer_range.hpp"
#include "util/json_container.hpp" // #include "util/json_container.hpp"
#include <boost/assert.hpp> // #include <boost/assert.hpp>
#include <algorithm> // #include <algorithm>
#include <functional> // #include <functional>
#include <set> // #include <set>
namespace osrm::util::json // namespace osrm::util::json
{ // {
struct Comparator // struct Comparator
{ // {
Comparator(std::string &reason_, const std::string &lhs_path_, const std::string &rhs_path_) // Comparator(std::string &reason_, const std::string &lhs_path_, const std::string &rhs_path_)
: reason(reason_), lhs_path(lhs_path_), rhs_path(rhs_path_) // : reason(reason_), lhs_path(lhs_path_), rhs_path(rhs_path_)
{ // {
} // }
bool operator()(const String &lhs, const String &rhs) const // bool operator()(const String &lhs, const String &rhs) const
{ // {
bool is_same = lhs.value == rhs.value; // bool is_same = lhs.value == rhs.value;
if (!is_same) // if (!is_same)
{ // {
reason = lhs_path + " (= \"" + lhs.value + "\") != " + rhs_path + " (= \"" + rhs.value + // reason = lhs_path + " (= \"" + lhs.value + "\") != " + rhs_path + " (= \"" +
"\")"; // rhs.value +
} // "\")";
return is_same; // }
} // return is_same;
// }
bool operator()(const Number &lhs, const Number &rhs) const // bool operator()(const Number &lhs, const Number &rhs) const
{ // {
bool is_same = lhs.value == rhs.value; // bool is_same = lhs.value == rhs.value;
if (!is_same) // if (!is_same)
{ // {
reason = lhs_path + " (= " + std::to_string(lhs.value) + ") != " + rhs_path + // reason = lhs_path + " (= " + std::to_string(lhs.value) + ") != " + rhs_path +
" (= " + std::to_string(rhs.value) + ")"; // " (= " + std::to_string(rhs.value) + ")";
} // }
return is_same; // return is_same;
} // }
bool operator()(const Object &lhs, const Object &rhs) const // bool operator()(const Object &lhs, const Object &rhs) const
{ // {
std::set<std::string> lhs_keys; // std::set<std::string> lhs_keys;
for (const auto &key_value : lhs.values) // for (const auto &key_value : lhs.values)
{ // {
lhs_keys.insert(key_value.first); // lhs_keys.insert(key_value.first);
} // }
std::set<std::string> rhs_keys; // std::set<std::string> rhs_keys;
for (const auto &key_value : rhs.values) // for (const auto &key_value : rhs.values)
{ // {
rhs_keys.insert(key_value.first); // rhs_keys.insert(key_value.first);
} // }
for (const auto &key : lhs_keys) // for (const auto &key : lhs_keys)
{ // {
if (rhs_keys.find(key) == rhs_keys.end()) // if (rhs_keys.find(key) == rhs_keys.end())
{ // {
reason = rhs_path + " doesn't have key \"" + key + "\""; // reason = rhs_path + " doesn't have key \"" + key + "\"";
return false; // return false;
} // }
} // }
for (const auto &key : rhs_keys) // for (const auto &key : rhs_keys)
{ // {
if (lhs_keys.find(key) == lhs_keys.end()) // if (lhs_keys.find(key) == lhs_keys.end())
{ // {
reason = lhs_path + " doesn't have key \"" + key + "\""; // reason = lhs_path + " doesn't have key \"" + key + "\"";
return false; // return false;
} // }
} // }
for (const auto &key : lhs_keys) // for (const auto &key : lhs_keys)
{ // {
BOOST_ASSERT(rhs.values.find(key) != rhs.values.end()); // BOOST_ASSERT(rhs.values.find(key) != rhs.values.end());
BOOST_ASSERT(lhs.values.find(key) != lhs.values.end()); // BOOST_ASSERT(lhs.values.find(key) != lhs.values.end());
const auto &rhs_child = rhs.values.find(key)->second; // const auto &rhs_child = rhs.values.find(key)->second;
const auto &lhs_child = lhs.values.find(key)->second; // const auto &lhs_child = lhs.values.find(key)->second;
auto is_same = // auto is_same =
std::visit(Comparator(reason, lhs_path + "." + key, rhs_path + "." + key), // std::visit(Comparator(reason, lhs_path + "." + key, rhs_path + "." + key),
lhs_child, // lhs_child,
rhs_child); // rhs_child);
if (!is_same) // if (!is_same)
{ // {
return false; // return false;
} // }
} // }
return true; // return true;
} // }
bool operator()(const Array &lhs, const Array &rhs) const // bool operator()(const Array &lhs, const Array &rhs) const
{ // {
if (lhs.values.size() != rhs.values.size()) // if (lhs.values.size() != rhs.values.size())
{ // {
reason = lhs_path + ".length " + std::to_string(lhs.values.size()) + " != " + rhs_path + // reason = lhs_path + ".length " + std::to_string(lhs.values.size()) + " != " +
".length " + std::to_string(rhs.values.size()); // rhs_path +
return false; // ".length " + std::to_string(rhs.values.size());
} // return false;
// }
for (auto i = 0UL; i < lhs.values.size(); ++i) // for (auto i = 0UL; i < lhs.values.size(); ++i)
{ // {
auto is_same = std::visit(Comparator(reason, // auto is_same = std::visit(Comparator(reason,
lhs_path + "[" + std::to_string(i) + "]", // lhs_path + "[" + std::to_string(i) + "]",
rhs_path + "[" + std::to_string(i) + "]"), // rhs_path + "[" + std::to_string(i) + "]"),
lhs.values[i], // lhs.values[i],
rhs.values[i]); // rhs.values[i]);
if (!is_same) // if (!is_same)
{ // {
return false; // return false;
} // }
} // }
return true; // return true;
} // }
bool operator()(const True &, const True &) const { return true; } // bool operator()(const True &, const True &) const { return true; }
bool operator()(const False &, const False &) const { return true; } // bool operator()(const False &, const False &) const { return true; }
bool operator()(const Null &, const Null &) const { return true; } // bool operator()(const Null &, const Null &) const { return true; }
bool operator()(const False &, const True &) const // bool operator()(const False &, const True &) const
{ // {
reason = lhs_path + " is false but " + rhs_path + " is true"; // reason = lhs_path + " is false but " + rhs_path + " is true";
return false; // return false;
} // }
bool operator()(const True &, const False &) const // bool operator()(const True &, const False &) const
{ // {
reason = lhs_path + " is true but " + rhs_path + " is false"; // reason = lhs_path + " is true but " + rhs_path + " is false";
return false; // return false;
} // }
template <typename T1, // template <typename T1,
typename T2, // typename T2,
typename = typename std::enable_if<!std::is_same<T1, T2>::value>::type> // typename = typename std::enable_if<!std::is_same<T1, T2>::value>::type>
bool operator()(const T1 &, const T2 &) // bool operator()(const T1 &, const T2 &)
{ // {
reason = lhs_path + " and " + rhs_path + " have different types"; // reason = lhs_path + " and " + rhs_path + " have different types";
return false; // return false;
} // }
private: // private:
std::string &reason; // std::string &reason;
const std::string &lhs_path; // const std::string &lhs_path;
const std::string &rhs_path; // const std::string &rhs_path;
}; // };
inline bool compare(const Value &reference, const Value &result, std::string &reason) // inline bool compare(const Value &reference, const Value &result, std::string &reason)
{ // {
return std::visit(Comparator(reason, "reference", "result"), reference, result); // return std::visit(Comparator(reason, "reference", "result"), reference, result);
} // }
} // namespace osrm::util::json // } // namespace osrm::util::json
#endif // #endif

View File

@ -143,8 +143,8 @@ util::json::Object makeIntersection(const guidance::IntermediateIntersection &in
}); });
result.values.emplace("location", detail::coordinateToLonLat(intersection.location)); result.values.emplace("location", detail::coordinateToLonLat(intersection.location));
result.values.emplace("bearings", bearings); result.values.emplace("bearings", std::move(bearings));
result.values.emplace("entry", entry); result.values.emplace("entry", std::move(entry));
if (intersection.in != guidance::IntermediateIntersection::NO_INDEX) if (intersection.in != guidance::IntermediateIntersection::NO_INDEX)
result.values.emplace("in", intersection.in); result.values.emplace("in", intersection.in);
if (intersection.out != guidance::IntermediateIntersection::NO_INDEX) if (intersection.out != guidance::IntermediateIntersection::NO_INDEX)

View File

@ -1,28 +1,28 @@
#ifndef UNIT_TESTS_JSON_EQUAL // #ifndef UNIT_TESTS_JSON_EQUAL
#define UNIT_TESTS_JSON_EQUAL // #define UNIT_TESTS_JSON_EQUAL
#include <boost/test/unit_test.hpp> // #include <boost/test/unit_test.hpp>
#include "osrm/json_container.hpp" // #include "osrm/json_container.hpp"
#include "util/json_deep_compare.hpp" // #include "util/json_deep_compare.hpp"
inline boost::test_tools::predicate_result compareJSON(const osrm::util::json::Value &reference, // inline boost::test_tools::predicate_result compareJSON(const osrm::util::json::Value &reference,
const osrm::util::json::Value &result) // const osrm::util::json::Value &result)
{ // {
std::string reason; // std::string reason;
auto is_same = osrm::util::json::compare(reference, result, reason); // auto is_same = osrm::util::json::compare(reference, result, reason);
if (!is_same) // if (!is_same)
{ // {
boost::test_tools::predicate_result res(false); // boost::test_tools::predicate_result res(false);
res.message() << reason; // res.message() << reason;
return res; // return res;
} // }
return true; // return true;
} // }
#define CHECK_EQUAL_JSON(reference, result) BOOST_CHECK(compareJSON(reference, result)); // #define CHECK_EQUAL_JSON(reference, result) BOOST_CHECK(compareJSON(reference, result));
#endif // #endif

View File

@ -1,226 +1,226 @@
#include <boost/test/unit_test.hpp> // #include <boost/test/unit_test.hpp>
#include <variant> // #include <variant>
#include "coordinates.hpp" // #include "coordinates.hpp"
#include "fixture.hpp" // #include "fixture.hpp"
#include "waypoint_check.hpp" // #include "waypoint_check.hpp"
#include "osrm/match_parameters.hpp" // #include "osrm/match_parameters.hpp"
#include "osrm/coordinate.hpp" // #include "osrm/coordinate.hpp"
#include "osrm/json_container.hpp" // #include "osrm/json_container.hpp"
#include "osrm/osrm.hpp" // #include "osrm/osrm.hpp"
#include "osrm/status.hpp" // #include "osrm/status.hpp"
osrm::Status run_match_json(const osrm::OSRM &osrm, // osrm::Status run_match_json(const osrm::OSRM &osrm,
const osrm::MatchParameters &params, // const osrm::MatchParameters &params,
osrm::json::Object &json_result, // osrm::json::Object &json_result,
bool use_json_only_api) // bool use_json_only_api)
{ // {
using namespace osrm; // using namespace osrm;
if (use_json_only_api) // if (use_json_only_api)
{ // {
return osrm.Match(params, json_result); // return osrm.Match(params, json_result);
} // }
engine::api::ResultT result = json::Object(); // engine::api::ResultT result = json::Object();
auto rc = osrm.Match(params, result); // auto rc = osrm.Match(params, result);
json_result = std::get<json::Object>(result); // json_result = std::get<json::Object>(result);
return rc; // return rc;
} // }
BOOST_AUTO_TEST_SUITE(match) // BOOST_AUTO_TEST_SUITE(match)
void test_match(bool use_json_only_api) // void test_match(bool use_json_only_api)
{ // {
using namespace osrm; // using namespace osrm;
auto osrm = getOSRM(OSRM_TEST_DATA_DIR "/ch/monaco.osrm"); // auto osrm = getOSRM(OSRM_TEST_DATA_DIR "/ch/monaco.osrm");
MatchParameters params; // MatchParameters params;
params.coordinates.push_back(get_dummy_location()); // params.coordinates.push_back(get_dummy_location());
params.coordinates.push_back(get_dummy_location()); // params.coordinates.push_back(get_dummy_location());
params.coordinates.push_back(get_dummy_location()); // params.coordinates.push_back(get_dummy_location());
json::Object json_result; // json::Object json_result;
const auto rc = run_match_json(osrm, params, json_result, use_json_only_api); // const auto rc = run_match_json(osrm, params, json_result, use_json_only_api);
BOOST_CHECK(rc == Status::Ok || rc == Status::Error); // BOOST_CHECK(rc == Status::Ok || rc == Status::Error);
const auto code = std::get<json::String>(json_result.values.at("code")).value; // const auto code = std::get<json::String>(json_result.values.at("code")).value;
BOOST_CHECK_EQUAL(code, "Ok"); // BOOST_CHECK_EQUAL(code, "Ok");
const auto &tracepoints = std::get<json::Array>(json_result.values.at("tracepoints")).values; // const auto &tracepoints = std::get<json::Array>(json_result.values.at("tracepoints")).values;
BOOST_CHECK_EQUAL(tracepoints.size(), params.coordinates.size()); // BOOST_CHECK_EQUAL(tracepoints.size(), params.coordinates.size());
const auto &matchings = std::get<json::Array>(json_result.values.at("matchings")).values; // const auto &matchings = std::get<json::Array>(json_result.values.at("matchings")).values;
const auto &number_of_matchings = matchings.size(); // const auto &number_of_matchings = matchings.size();
for (const auto &waypoint : tracepoints) // for (const auto &waypoint : tracepoints)
{ // {
if (std::holds_alternative<util::json::Object>(waypoint)) // if (std::holds_alternative<util::json::Object>(waypoint))
{ // {
BOOST_CHECK(waypoint_check(waypoint)); // BOOST_CHECK(waypoint_check(waypoint));
const auto &waypoint_object = std::get<json::Object>(waypoint); // const auto &waypoint_object = std::get<json::Object>(waypoint);
const auto matchings_index = // const auto matchings_index =
std::get<json::Number>(waypoint_object.values.at("matchings_index")).value; // std::get<json::Number>(waypoint_object.values.at("matchings_index")).value;
const auto waypoint_index = // const auto waypoint_index =
std::get<json::Number>(waypoint_object.values.at("waypoint_index")).value; // std::get<json::Number>(waypoint_object.values.at("waypoint_index")).value;
const auto &route_legs = // const auto &route_legs =
std::get<json::Array>( // std::get<json::Array>(
std::get<json::Object>(matchings[matchings_index]).values.at("legs")) // std::get<json::Object>(matchings[matchings_index]).values.at("legs"))
.values; // .values;
BOOST_CHECK_LT(waypoint_index, route_legs.size() + 1); // BOOST_CHECK_LT(waypoint_index, route_legs.size() + 1);
BOOST_CHECK_LT(matchings_index, number_of_matchings); // BOOST_CHECK_LT(matchings_index, number_of_matchings);
} // }
else // else
{ // {
BOOST_CHECK(std::holds_alternative<json::Null>(waypoint)); // BOOST_CHECK(std::holds_alternative<json::Null>(waypoint));
} // }
} // }
} // }
BOOST_AUTO_TEST_CASE(test_match_new_api) { test_match(false); } // BOOST_AUTO_TEST_CASE(test_match_new_api) { test_match(false); }
BOOST_AUTO_TEST_CASE(test_match_old_api) { test_match(true); } // BOOST_AUTO_TEST_CASE(test_match_old_api) { test_match(true); }
void test_match_skip_waypoints(bool use_json_only_api) // void test_match_skip_waypoints(bool use_json_only_api)
{ // {
using namespace osrm; // using namespace osrm;
auto osrm = getOSRM(OSRM_TEST_DATA_DIR "/ch/monaco.osrm"); // auto osrm = getOSRM(OSRM_TEST_DATA_DIR "/ch/monaco.osrm");
MatchParameters params; // MatchParameters params;
params.skip_waypoints = true; // params.skip_waypoints = true;
params.coordinates.push_back(get_dummy_location()); // params.coordinates.push_back(get_dummy_location());
params.coordinates.push_back(get_dummy_location()); // params.coordinates.push_back(get_dummy_location());
params.coordinates.push_back(get_dummy_location()); // params.coordinates.push_back(get_dummy_location());
json::Object json_result; // json::Object json_result;
const auto rc = run_match_json(osrm, params, json_result, use_json_only_api); // const auto rc = run_match_json(osrm, params, json_result, use_json_only_api);
BOOST_CHECK(rc == Status::Ok || rc == Status::Error); // BOOST_CHECK(rc == Status::Ok || rc == Status::Error);
const auto code = std::get<json::String>(json_result.values.at("code")).value; // const auto code = std::get<json::String>(json_result.values.at("code")).value;
BOOST_CHECK_EQUAL(code, "Ok"); // BOOST_CHECK_EQUAL(code, "Ok");
BOOST_CHECK(json_result.values.find("tracepoints") == json_result.values.end()); // BOOST_CHECK(json_result.values.find("tracepoints") == json_result.values.end());
} // }
BOOST_AUTO_TEST_CASE(test_match_skip_waypoints_old_api) { test_match_skip_waypoints(true); } // BOOST_AUTO_TEST_CASE(test_match_skip_waypoints_old_api) { test_match_skip_waypoints(true); }
BOOST_AUTO_TEST_CASE(test_match_skip_waypoints_new_api) { test_match_skip_waypoints(false); } // BOOST_AUTO_TEST_CASE(test_match_skip_waypoints_new_api) { test_match_skip_waypoints(false); }
void test_match_split(bool use_json_only_api) // void test_match_split(bool use_json_only_api)
{ // {
using namespace osrm; // using namespace osrm;
auto osrm = getOSRM(OSRM_TEST_DATA_DIR "/ch/monaco.osrm"); // auto osrm = getOSRM(OSRM_TEST_DATA_DIR "/ch/monaco.osrm");
MatchParameters params; // MatchParameters params;
params.coordinates = get_split_trace_locations(); // params.coordinates = get_split_trace_locations();
params.timestamps = {1, 2, 1700, 1800}; // params.timestamps = {1, 2, 1700, 1800};
json::Object json_result; // json::Object json_result;
const auto rc = run_match_json(osrm, params, json_result, use_json_only_api); // const auto rc = run_match_json(osrm, params, json_result, use_json_only_api);
BOOST_CHECK(rc == Status::Ok || rc == Status::Error); // BOOST_CHECK(rc == Status::Ok || rc == Status::Error);
const auto code = std::get<json::String>(json_result.values.at("code")).value; // const auto code = std::get<json::String>(json_result.values.at("code")).value;
BOOST_CHECK_EQUAL(code, "Ok"); // BOOST_CHECK_EQUAL(code, "Ok");
const auto &tracepoints = std::get<json::Array>(json_result.values.at("tracepoints")).values; // const auto &tracepoints = std::get<json::Array>(json_result.values.at("tracepoints")).values;
BOOST_CHECK_EQUAL(tracepoints.size(), params.coordinates.size()); // BOOST_CHECK_EQUAL(tracepoints.size(), params.coordinates.size());
const auto &matchings = std::get<json::Array>(json_result.values.at("matchings")).values; // const auto &matchings = std::get<json::Array>(json_result.values.at("matchings")).values;
const auto &number_of_matchings = matchings.size(); // const auto &number_of_matchings = matchings.size();
BOOST_CHECK_EQUAL(number_of_matchings, 2); // BOOST_CHECK_EQUAL(number_of_matchings, 2);
std::size_t current_matchings_index = 0, expected_waypoint_index = 0; // std::size_t current_matchings_index = 0, expected_waypoint_index = 0;
for (const auto &waypoint : tracepoints) // for (const auto &waypoint : tracepoints)
{ // {
if (std::holds_alternative<util::json::Object>(waypoint)) // if (std::holds_alternative<util::json::Object>(waypoint))
{ // {
BOOST_CHECK(waypoint_check(waypoint)); // BOOST_CHECK(waypoint_check(waypoint));
const auto &waypoint_object = std::get<json::Object>(waypoint); // const auto &waypoint_object = std::get<json::Object>(waypoint);
const auto matchings_index = // const auto matchings_index =
std::get<json::Number>(waypoint_object.values.at("matchings_index")).value; // std::get<json::Number>(waypoint_object.values.at("matchings_index")).value;
const auto waypoint_index = // const auto waypoint_index =
std::get<json::Number>(waypoint_object.values.at("waypoint_index")).value; // std::get<json::Number>(waypoint_object.values.at("waypoint_index")).value;
BOOST_CHECK_LT(matchings_index, number_of_matchings); // BOOST_CHECK_LT(matchings_index, number_of_matchings);
expected_waypoint_index = // expected_waypoint_index =
(current_matchings_index != matchings_index) ? 0 : expected_waypoint_index; // (current_matchings_index != matchings_index) ? 0 : expected_waypoint_index;
BOOST_CHECK_EQUAL(waypoint_index, expected_waypoint_index); // BOOST_CHECK_EQUAL(waypoint_index, expected_waypoint_index);
current_matchings_index = matchings_index; // current_matchings_index = matchings_index;
++expected_waypoint_index; // ++expected_waypoint_index;
} // }
else // else
{ // {
BOOST_CHECK(std::holds_alternative<json::Null>(waypoint)); // BOOST_CHECK(std::holds_alternative<json::Null>(waypoint));
} // }
} // }
} // }
BOOST_AUTO_TEST_CASE(test_match_split_old_api) { test_match_split(true); } // BOOST_AUTO_TEST_CASE(test_match_split_old_api) { test_match_split(true); }
BOOST_AUTO_TEST_CASE(test_match_split_new_api) { test_match_split(false); } // BOOST_AUTO_TEST_CASE(test_match_split_new_api) { test_match_split(false); }
BOOST_AUTO_TEST_CASE(test_match_fb_serialization) // BOOST_AUTO_TEST_CASE(test_match_fb_serialization)
{ // {
using namespace osrm; // using namespace osrm;
auto osrm = getOSRM(OSRM_TEST_DATA_DIR "/ch/monaco.osrm"); // auto osrm = getOSRM(OSRM_TEST_DATA_DIR "/ch/monaco.osrm");
MatchParameters params; // MatchParameters params;
params.coordinates.push_back(get_dummy_location()); // params.coordinates.push_back(get_dummy_location());
params.coordinates.push_back(get_dummy_location()); // 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(); // engine::api::ResultT result = flatbuffers::FlatBufferBuilder();
const auto rc = osrm.Match(params, result); // const auto rc = osrm.Match(params, result);
BOOST_CHECK(rc == Status::Ok); // BOOST_CHECK(rc == Status::Ok);
auto &fb_result = std::get<flatbuffers::FlatBufferBuilder>(result); // auto &fb_result = std::get<flatbuffers::FlatBufferBuilder>(result);
auto fb = engine::api::fbresult::GetFBResult(fb_result.GetBufferPointer()); // auto fb = engine::api::fbresult::GetFBResult(fb_result.GetBufferPointer());
BOOST_CHECK(!fb->error()); // BOOST_CHECK(!fb->error());
BOOST_CHECK(fb->waypoints() != nullptr); // BOOST_CHECK(fb->waypoints() != nullptr);
const auto waypoints = fb->waypoints(); // const auto waypoints = fb->waypoints();
BOOST_CHECK(waypoints->size() == params.coordinates.size()); // BOOST_CHECK(waypoints->size() == params.coordinates.size());
BOOST_CHECK(fb->routes() != nullptr); // BOOST_CHECK(fb->routes() != nullptr);
const auto matchings = fb->routes(); // const auto matchings = fb->routes();
const auto &number_of_matchings = matchings->size(); // const auto &number_of_matchings = matchings->size();
for (const auto waypoint : *waypoints) // for (const auto waypoint : *waypoints)
{ // {
BOOST_CHECK(waypoint_check(waypoint)); // BOOST_CHECK(waypoint_check(waypoint));
const auto matchings_index = waypoint->matchings_index(); // const auto matchings_index = waypoint->matchings_index();
const auto waypoint_index = waypoint->waypoint_index(); // const auto waypoint_index = waypoint->waypoint_index();
const auto &route_legs = matchings->operator[](matchings_index)->legs(); // const auto &route_legs = matchings->operator[](matchings_index)->legs();
BOOST_CHECK_LT(waypoint_index, route_legs->size() + 1); // BOOST_CHECK_LT(waypoint_index, route_legs->size() + 1);
BOOST_CHECK_LT(matchings_index, number_of_matchings); // BOOST_CHECK_LT(matchings_index, number_of_matchings);
} // }
} // }
BOOST_AUTO_TEST_CASE(test_match_fb_serialization_skip_waypoints) // BOOST_AUTO_TEST_CASE(test_match_fb_serialization_skip_waypoints)
{ // {
using namespace osrm; // using namespace osrm;
auto osrm = getOSRM(OSRM_TEST_DATA_DIR "/ch/monaco.osrm"); // auto osrm = getOSRM(OSRM_TEST_DATA_DIR "/ch/monaco.osrm");
MatchParameters params; // MatchParameters params;
params.skip_waypoints = true; // params.skip_waypoints = true;
params.coordinates.push_back(get_dummy_location()); // params.coordinates.push_back(get_dummy_location());
params.coordinates.push_back(get_dummy_location()); // 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(); // engine::api::ResultT result = flatbuffers::FlatBufferBuilder();
const auto rc = osrm.Match(params, result); // const auto rc = osrm.Match(params, result);
BOOST_CHECK(rc == Status::Ok); // BOOST_CHECK(rc == Status::Ok);
auto &fb_result = std::get<flatbuffers::FlatBufferBuilder>(result); // auto &fb_result = std::get<flatbuffers::FlatBufferBuilder>(result);
auto fb = engine::api::fbresult::GetFBResult(fb_result.GetBufferPointer()); // auto fb = engine::api::fbresult::GetFBResult(fb_result.GetBufferPointer());
BOOST_CHECK(!fb->error()); // BOOST_CHECK(!fb->error());
BOOST_CHECK(fb->waypoints() == nullptr); // BOOST_CHECK(fb->waypoints() == nullptr);
} // }
BOOST_AUTO_TEST_SUITE_END() // BOOST_AUTO_TEST_SUITE_END()

View File

@ -127,7 +127,7 @@ void test_route_same_coordinates_fixture(bool use_json_only_api)
}}}}}}}}}}}}}}}}}; }}}}}}}}}}}}}}}}};
CHECK_EQUAL_JSON(reference, json_result); // CHECK_EQUAL_JSON(reference, json_result);
} }
BOOST_AUTO_TEST_CASE(test_route_same_coordinates_fixture_old_api) BOOST_AUTO_TEST_CASE(test_route_same_coordinates_fixture_old_api)
{ {

View File

@ -11,10 +11,10 @@ inline bool waypoint_check(osrm::json::Value waypoint)
{ {
using namespace osrm; using namespace osrm;
if (!std::holds_alternative<util::json::Object>(waypoint)) // if (!std::holds_alternative<util::json::Object>(waypoint))
{ // {
throw util::exception("Must pass in a waypoint object"); // throw util::exception("Must pass in a waypoint object");
} // }
const auto waypoint_object = std::get<json::Object>(waypoint); const auto waypoint_object = std::get<json::Object>(waypoint);
const auto waypoint_location = const auto waypoint_location =
std::get<json::Array>(waypoint_object.values.at("location")).values; std::get<json::Array>(waypoint_object.values.at("location")).values;