Add route fixture test
This commit is contained in:
		
							parent
							
								
									7e5274e6e6
								
							
						
					
					
						commit
						306b0610a2
					
				| @ -87,7 +87,7 @@ add_library(osrm_contract $<TARGET_OBJECTS:CONTRACTOR> $<TARGET_OBJECTS:UTIL>) | ||||
| add_library(osrm_store $<TARGET_OBJECTS:STORAGE> $<TARGET_OBJECTS:UTIL>) | ||||
| 
 | ||||
| # Unit tests | ||||
| add_executable(engine-tests unit_tests/engine_tests.cpp ${EngineTestsGlob} $<TARGET_OBJECTS:ENGINE> $<TARGET_OBJECTS:UTIL>) | ||||
| add_executable(engine-tests unit_tests/engine_tests.cpp ${EngineTestsGlob} $<TARGET_OBJECTS:ENGINE> $<TARGET_OBJECTS:STORAGE> $<TARGET_OBJECTS:UTIL>) | ||||
| add_executable(extractor-tests unit_tests/extractor_tests.cpp ${ExtractorTestsGlob} $<TARGET_OBJECTS:EXTRACTOR> $<TARGET_OBJECTS:UTIL>) | ||||
| add_executable(util-tests unit_tests/util_tests.cpp ${UtilTestsGlob} $<TARGET_OBJECTS:UTIL>) | ||||
| add_executable(server-tests unit_tests/server_tests.cpp ${ServerTestsGlob} $<TARGET_OBJECTS:UTIL> $<TARGET_OBJECTS:SERVER>) | ||||
|  | ||||
							
								
								
									
										158
									
								
								include/util/json_deep_compare.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										158
									
								
								include/util/json_deep_compare.hpp
									
									
									
									
									
										Normal file
									
								
							| @ -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 <boost/assert.hpp> | ||||
| 
 | ||||
| #include <algorithm> | ||||
| #include <functional> | ||||
| #include <set> | ||||
| 
 | ||||
| namespace osrm | ||||
| { | ||||
| namespace util | ||||
| { | ||||
| namespace json | ||||
| { | ||||
| 
 | ||||
| struct Comparator : mapbox::util::static_visitor<bool> | ||||
| { | ||||
|     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<std::string> lhs_keys; | ||||
|         for (const auto &key_value : lhs.values) | ||||
|         { | ||||
|             lhs_keys.insert(key_value.first); | ||||
|         } | ||||
| 
 | ||||
|         std::set<std::string> 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 <typename T1, | ||||
|               typename T2, | ||||
|               typename = typename std::enable_if<!std::is_same<T1, T2>::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 | ||||
							
								
								
									
										27
									
								
								unit_tests/library/equal_json.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								unit_tests/library/equal_json.hpp
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,27 @@ | ||||
| #ifndef UNIT_TESTS_JSON_EQUAL | ||||
| #define UNIT_TESTS_JSON_EQUAL | ||||
| 
 | ||||
| #include <boost/test/included/unit_test.hpp> | ||||
| 
 | ||||
| #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 | ||||
| @ -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(); | ||||
|  | ||||
| @ -2,6 +2,7 @@ | ||||
| #include <boost/test/test_case_template.hpp> | ||||
| 
 | ||||
| #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; | ||||
|  | ||||
| @ -2,6 +2,7 @@ | ||||
| #include <boost/test/test_case_template.hpp> | ||||
| 
 | ||||
| #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; | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user