From 90d2a06820974f7ca08f1459724974a8a958734a Mon Sep 17 00:00:00 2001 From: Vladimir Byko-Ianko Date: Tue, 3 Oct 2017 19:18:50 +0300 Subject: [PATCH] Tests on Edge serialization and deserialization at transit. --- generator/generator_tests/transit_test.cpp | 60 +++++++++++++++---- generator/transit_generator.hpp | 2 +- .../routing_common_tests/transit_test.cpp | 9 ++- routing_common/transit_serdes.hpp | 2 +- routing_common/transit_types.cpp | 4 +- routing_common/transit_types.hpp | 2 +- 6 files changed, 63 insertions(+), 16 deletions(-) diff --git a/generator/generator_tests/transit_test.cpp b/generator/generator_tests/transit_test.cpp index 0be48689ca..38dc5ca5d7 100644 --- a/generator/generator_tests/transit_test.cpp +++ b/generator/generator_tests/transit_test.cpp @@ -15,6 +15,22 @@ using namespace std; namespace { +template +void TestDeserializerFromJson(string const & jsonBuffer, string const & name, vector const & expected) +{ + my::Json root(jsonBuffer.c_str()); + CHECK(root.get() != nullptr, ("Cannot parse the json.")); + + DeserializerFromJson deserializer(root.get()); + + vector objects; + deserializer(objects, name.c_str()); + + TEST_EQUAL(objects.size(), expected.size(), ()); + for (size_t i = 0; i < objects.size(); ++i) + TEST(objects[i].IsEqualForTesting(expected[i]), (objects[i], "is not equal to", expected[i])); +} + UNIT_TEST(DeserializerFromJson_Stops) { string const jsonBuffer = R"( @@ -48,22 +64,44 @@ UNIT_TEST(DeserializerFromJson_Stops) } ]})"; - my::Json root(jsonBuffer.c_str()); - CHECK(root.get() != nullptr, ("Cannot parse the json.")); - - DeserializerFromJson deserializer(root.get()); - - vector stops; - deserializer(stops, "stops"); - vector const expected = { Stop(343259523 /* id */, 1234 /* featureId */, kInvalidTransferId /* transfer id */, {19207936, 19207937} /* lineIds */, {27.4970954, 64.20146835878187} /* point */), Stop(266680843 /* id */, 2345 /* featureId */, 5 /* transfer id */, {19213568, 19213569} /* lineIds */, {27.5227942, 64.25206634443111} /* point */)}; - TEST_EQUAL(stops.size(), expected.size(), ()); - for (size_t i = 0; i < stops.size(); ++i) - TEST(stops[i].IsEqualForTesting(expected[i]), (stops[i], "is not equal to", expected[i])); + TestDeserializerFromJson(jsonBuffer, "stops", expected); +} + +UNIT_TEST(DeserializerFromJson_Edges) +{ + string const jsonBuffer = R"( + { + "edges": [ + { + "finish_stop_id": 442018445, + "line_id": 72551680, + "shape_ids": [5, 7], + "weight" : 234.5, + "start_stop_id": 442018444, + "transfer": false + }, + { + "finish_stop_id": 442018446, + "line_id": 72551680, + "shape_ids": [], + "weight" : 345.6, + "start_stop_id": 442018445, + "transfer": false + } + ]})"; + + vector const expected = { + Edge(442018444 /* start stop id */, 442018445 /* finish stop id */, 234.5 /* weight */, + 72551680 /* line id */, false /* transfer */, {5, 7} /* shape ids */), + Edge(442018445 /* start stop id */, 442018446 /* finish stop id */, 345.6 /* weight */, + 72551680 /* line id */, false /* transfer */, {} /* shape ids */)}; + + TestDeserializerFromJson(jsonBuffer, "edges", expected); } } // namespace diff --git a/generator/transit_generator.hpp b/generator/transit_generator.hpp index 88aa825aff..8a6fd99c7e 100644 --- a/generator/transit_generator.hpp +++ b/generator/transit_generator.hpp @@ -21,7 +21,7 @@ public: DeserializerFromJson(json_struct_t * node) : m_node(node) {} template - typename std::enable_if::value || std::is_enum::value || std::is_same::value>::type + typename std::enable_if::value || std::is_enum::value || std::is_same::value>::type operator()(T & t, char const * name = nullptr) { GetField(t, name); diff --git a/routing_common/routing_common_tests/transit_test.cpp b/routing_common/routing_common_tests/transit_test.cpp index 5ba9038543..179631211a 100644 --- a/routing_common/routing_common_tests/transit_test.cpp +++ b/routing_common/routing_common_tests/transit_test.cpp @@ -30,7 +30,7 @@ void TestSerialization(Obj const & obj) Deserializer> deserializer(src); deserializedObj.Visit(deserializer); - TEST(obj.IsEqualForTesting(deserializedObj), ()); + TEST(obj.IsEqualForTesting(deserializedObj), (obj, "is not equal to", deserializedObj)); } UNIT_TEST(Transit_HeaderSerialization) @@ -58,4 +58,11 @@ UNIT_TEST(Transit_StopSerialization) TestSerialization(stop); } } + +UNIT_TEST(Transit_EdgeSerialization) +{ + Edge edge(1 /* start stop id */, 2 /* finish stop id */, 123.4 /* weight */, 11 /* line id */, + false /* transfer */, {1, 2, 3} /* shape ids */); + TestSerialization(edge); +} } // namespace diff --git a/routing_common/transit_serdes.hpp b/routing_common/transit_serdes.hpp index 8032e5c38a..244638cc1c 100644 --- a/routing_common/transit_serdes.hpp +++ b/routing_common/transit_serdes.hpp @@ -93,7 +93,7 @@ public: ReadPrimitiveFromSource(m_source, t); } - double operator()(double & d, char const * name = nullptr) + void operator()(double & d, char const * name = nullptr) { uint32_t ui; (*this)(ui, name); diff --git a/routing_common/transit_types.cpp b/routing_common/transit_types.cpp index 77db8c4f19..92393cf207 100644 --- a/routing_common/transit_types.cpp +++ b/routing_common/transit_types.cpp @@ -76,8 +76,10 @@ Edge::Edge(StopId startStopId, StopId finishStopId, double weight, LineId lineId bool Edge::IsEqualForTesting(Edge const & edge) const { + double constexpr kWeightEqualEpsilon = 1e-2; return m_startStopId == edge.m_startStopId && m_finishStopId == edge.m_finishStopId && - m_weight == edge.m_weight && m_lineId == edge.m_lineId && m_transfer == edge.m_transfer && + my::AlmostEqualAbs(m_weight, edge.m_weight, kWeightEqualEpsilon) && + m_lineId == edge.m_lineId && m_transfer == edge.m_transfer && m_shapeIds == edge.m_shapeIds; } } // namespace transit diff --git a/routing_common/transit_types.hpp b/routing_common/transit_types.hpp index 1ecf287654..7ad7e0e610 100644 --- a/routing_common/transit_types.hpp +++ b/routing_common/transit_types.hpp @@ -96,7 +96,7 @@ public: private: StopId m_startStopId = kInvalidStopId; StopId m_finishStopId = kInvalidStopId; - double m_weight = kInvalidWeight; + double m_weight = kInvalidWeight; // in seconds LineId m_lineId = kInvalidLineId; bool m_transfer = false; std::vector m_shapeIds;