forked from organicmaps/organicmaps
Tests on Edge serialization and deserialization at transit.
This commit is contained in:
parent
64605a8d85
commit
90d2a06820
6 changed files with 63 additions and 16 deletions
|
@ -15,6 +15,22 @@ using namespace std;
|
|||
|
||||
namespace
|
||||
{
|
||||
template <typename Obj>
|
||||
void TestDeserializerFromJson(string const & jsonBuffer, string const & name, vector<Obj> const & expected)
|
||||
{
|
||||
my::Json root(jsonBuffer.c_str());
|
||||
CHECK(root.get() != nullptr, ("Cannot parse the json."));
|
||||
|
||||
DeserializerFromJson deserializer(root.get());
|
||||
|
||||
vector<Obj> 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<Stop> stops;
|
||||
deserializer(stops, "stops");
|
||||
|
||||
vector<Stop> 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<Edge> 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
|
||||
|
|
|
@ -21,7 +21,7 @@ public:
|
|||
DeserializerFromJson(json_struct_t * node) : m_node(node) {}
|
||||
|
||||
template<typename T>
|
||||
typename std::enable_if<std::is_integral<T>::value || std::is_enum<T>::value || std::is_same<T, double >::value>::type
|
||||
typename std::enable_if<std::is_integral<T>::value || std::is_enum<T>::value || std::is_same<T, double>::value>::type
|
||||
operator()(T & t, char const * name = nullptr)
|
||||
{
|
||||
GetField(t, name);
|
||||
|
|
|
@ -30,7 +30,7 @@ void TestSerialization(Obj const & obj)
|
|||
Deserializer<ReaderSource<MemReader>> 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
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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<ShapeId> m_shapeIds;
|
||||
|
|
Loading…
Add table
Reference in a new issue