From 306b0610a292f34fd9328b9c9113d152e695ad53 Mon Sep 17 00:00:00 2001 From: Patrick Niklaus Date: Thu, 24 Mar 2016 21:02:55 +0100 Subject: [PATCH] Add route fixture test --- CMakeLists.txt | 2 +- include/util/json_deep_compare.hpp | 158 +++++++++++++++++++++++++++++ unit_tests/library/equal_json.hpp | 27 +++++ unit_tests/library/route.cpp | 47 +++++++++ unit_tests/library/table.cpp | 6 +- unit_tests/library/trip.cpp | 6 +- 6 files changed, 237 insertions(+), 9 deletions(-) create mode 100644 include/util/json_deep_compare.hpp create mode 100644 unit_tests/library/equal_json.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 77f018bed..dd3be2449 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -87,7 +87,7 @@ add_library(osrm_contract $ $) add_library(osrm_store $ $) # Unit tests -add_executable(engine-tests unit_tests/engine_tests.cpp ${EngineTestsGlob} $ $) +add_executable(engine-tests unit_tests/engine_tests.cpp ${EngineTestsGlob} $ $ $) add_executable(extractor-tests unit_tests/extractor_tests.cpp ${ExtractorTestsGlob} $ $) add_executable(util-tests unit_tests/util_tests.cpp ${UtilTestsGlob} $) add_executable(server-tests unit_tests/server_tests.cpp ${ServerTestsGlob} $ $) diff --git a/include/util/json_deep_compare.hpp b/include/util/json_deep_compare.hpp new file mode 100644 index 000000000..32b0bde4b --- /dev/null +++ b/include/util/json_deep_compare.hpp @@ -0,0 +1,158 @@ +#ifndef UTIL_JSON_DEEP_COMPARE_HPP +#define UTIL_JSON_DEEP_COMPARE_HPP + +#include "util/json_container.hpp" +#include "util/integer_range.hpp" + +#include + +#include +#include +#include + +namespace osrm +{ +namespace util +{ +namespace json +{ + +struct Comparator : mapbox::util::static_visitor +{ + Comparator(std::string &reason_, const std::string &lhs_path_, const std::string &rhs_path_) + : reason(reason_), lhs_path(lhs_path_), rhs_path(rhs_path_) + { + } + + bool operator()(const String &lhs, const String &rhs) const { + bool is_same = lhs.value == rhs.value; + if (!is_same) + { + reason = lhs_path + " (= \"" + lhs.value + "\") != " + rhs_path + " (= \"" + rhs.value + "\")"; + } + return is_same; + } + + bool operator()(const Number &lhs, const Number &rhs) const { + bool is_same = lhs.value == rhs.value; + if (!is_same) + { + reason = lhs_path + " (= " + std::to_string(lhs.value) + ") != " + rhs_path + " (= " + std::to_string(rhs.value) + ")"; + } + return is_same; + } + + bool operator()(const Object &lhs, const Object &rhs) const + { + std::set lhs_keys; + for (const auto &key_value : lhs.values) + { + lhs_keys.insert(key_value.first); + } + + std::set rhs_keys; + for (const auto &key_value : rhs.values) + { + rhs_keys.insert(key_value.first); + } + + for (const auto &key : lhs_keys) + { + if (rhs_keys.find(key) == rhs_keys.end()) + { + reason = rhs_path + " doesn't have key \"" + key + "\""; + return false; + } + } + + for (const auto &key : rhs_keys) + { + if (lhs_keys.find(key) == lhs_keys.end()) + { + reason = lhs_path + " doesn't have key \"" + key + "\""; + return false; + } + } + + for (const auto &key : lhs_keys) + { + BOOST_ASSERT(rhs.values.find(key) != rhs.values.end()); + BOOST_ASSERT(lhs.values.find(key) != lhs.values.end()); + + const auto &rhs_child = rhs.values.find(key)->second; + const auto &lhs_child = lhs.values.find(key)->second; + auto is_same = mapbox::util::apply_visitor( + Comparator(reason, lhs_path + "." + key, rhs_path + "." + key), lhs_child, + rhs_child); + if (!is_same) + { + return false; + } + } + return true; + } + + bool operator()(const Array &lhs, const Array &rhs) const + { + if (lhs.values.size() != rhs.values.size()) + { + reason = lhs_path + ".length " + std::to_string(lhs.values.size()) + " != " + rhs_path + + ".length " + std::to_string(rhs.values.size()); + return false; + } + + for (auto i = 0UL; i < lhs.values.size(); ++i) + { + auto is_same = mapbox::util::apply_visitor( + Comparator(reason, lhs_path + "[" + std::to_string(i) + "]", + rhs_path + "[" + std::to_string(i) + "]"), + lhs.values[i], rhs.values[i]); + if (!is_same) + { + return false; + } + } + + return true; + } + + bool operator()(const True &, const True &) const { return true; } + bool operator()(const False &, const False &) const { return true; } + bool operator()(const Null &, const Null &) const { return true; } + + bool operator()(const False &, const True &) const + { + reason = lhs_path + " is false but " + rhs_path + " is true"; + return false; + } + bool operator()(const True &, const False &) const + { + reason = lhs_path + " is true but " + rhs_path + " is false"; + return false; + } + + template ::value>::type> + bool operator()(const T1 &, const T2 &) + { + reason = lhs_path + " and " + rhs_path + " have different types"; + return false; + } + + private: + std::string &reason; + const std::string &lhs_path; + const std::string &rhs_path; +}; + +inline bool compare(const Value &reference, const Value &result, std::string &reason) +{ + return mapbox::util::apply_visitor(Comparator(reason, "reference", "result"), reference, + result); +} +} +} +} + +#endif diff --git a/unit_tests/library/equal_json.hpp b/unit_tests/library/equal_json.hpp new file mode 100644 index 000000000..8e2f9ac6a --- /dev/null +++ b/unit_tests/library/equal_json.hpp @@ -0,0 +1,27 @@ +#ifndef UNIT_TESTS_JSON_EQUAL +#define UNIT_TESTS_JSON_EQUAL + +#include + +#include "util/json_deep_compare.hpp" + +boost::test_tools::predicate_result compareJSON(const osrm::util::json::Value &reference, + const osrm::util::json::Value &result) +{ + std::string reason; + auto is_same = osrm::util::json::compare(reference, result, reason); + if (!is_same) + { + boost::test_tools::predicate_result res(false); + + res.message() << reason; + + return res; + } + + return true; +} + +#define CHECK_EQUAL_JSON(reference, result) BOOST_CHECK(compareJSON(reference, result)); + +#endif diff --git a/unit_tests/library/route.cpp b/unit_tests/library/route.cpp index 49fc3c820..e93cebaf0 100644 --- a/unit_tests/library/route.cpp +++ b/unit_tests/library/route.cpp @@ -3,6 +3,7 @@ #include "args.hpp" #include "fixture.hpp" +#include "equal_json.hpp" #include "coordinates.hpp" #include "osrm/route_parameters.hpp" @@ -15,6 +16,52 @@ BOOST_AUTO_TEST_SUITE(route) +BOOST_AUTO_TEST_CASE(test_route_same_coordinates_fixture) +{ + const auto args = get_args(); + auto osrm = getOSRM(args.at(0)); + + using namespace osrm; + + RouteParameters params; + params.coordinates.push_back(get_dummy_location()); + params.coordinates.push_back(get_dummy_location()); + + json::Object result; + const auto rc = osrm.Route(params, result); + BOOST_CHECK(rc == Status::Ok); + + json::Object reference{ + {{"code", "ok"}, + {"waypoints", + json::Array{{json::Object{{{"name", ""}, {"location", json::Array{}}, {"hint", ""}}}, + json::Object{{{"name", ""}, {"location", json::Array{}}, {"hint", ""}}}}}}, + {"routes", json::Array{{json::Object{ + {{"distance", 0.}, + {"duration", 0.}, + {"geometry", ""}, + {"legs", json::Array{{json::Object{ + {{"distance", 0.}, + {"duration", 0.}, + {"summary", ""}, + {"steps", json::Array{{json::Object{ + {{"duration", 0.}, + {"distance", 0.}, + {"geometry", ""}, + {"name", ""}, + {"mode", "driving"}, + {"maneuver", json::Object{{ + {"type", "depart"}, + {"location", json::Array{}}, + {"modifier", ""}, + {"bearing_before", 0.}, + {"bearing_after", 0.}, + {"exit", 0}, + }}}}}}}}}}}}}}}}}}}}; + + CHECK_EQUAL_JSON(reference, result); +} + BOOST_AUTO_TEST_CASE(test_route_same_coordinates) { const auto args = get_args(); diff --git a/unit_tests/library/table.cpp b/unit_tests/library/table.cpp index 47620b9df..bab5312ed 100644 --- a/unit_tests/library/table.cpp +++ b/unit_tests/library/table.cpp @@ -2,6 +2,7 @@ #include #include "args.hpp" +#include "fixture.hpp" #include "osrm/table_parameters.hpp" @@ -20,10 +21,7 @@ BOOST_AUTO_TEST_CASE(test_table) using namespace osrm; - EngineConfig config{args[0]}; - config.use_shared_memory = false; - - OSRM osrm{config}; + auto osrm = getOSRM(args[0]); /* TableParameters params; diff --git a/unit_tests/library/trip.cpp b/unit_tests/library/trip.cpp index 88785c4a0..7bfd1c222 100644 --- a/unit_tests/library/trip.cpp +++ b/unit_tests/library/trip.cpp @@ -2,6 +2,7 @@ #include #include "args.hpp" +#include "fixture.hpp" #include "osrm/trip_parameters.hpp" @@ -20,10 +21,7 @@ BOOST_AUTO_TEST_CASE(test_trip) using namespace osrm; - EngineConfig config{args[0]}; - config.use_shared_memory = false; - - OSRM osrm{config}; + auto osrm = getOSRM(args[0]); /* TripParameters params;