Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuri Gorshenin <y@maps.me>2017-06-22 16:44:54 +0300
committerYuri Gorshenin <mipt.vi002@gmail.com>2017-07-05 16:41:38 +0300
commit4f0146194b6e4246ab8976a8545a419c587ba6df (patch)
treecbc807513a6c0693bf988d3b0b559bc733191f9d
parent4302e1a9b4b4bc85b35644409764406e0edb89b5 (diff)
[ugc] Fixes to tests.
-rw-r--r--ugc/api.cpp18
-rw-r--r--ugc/api.hpp4
-rw-r--r--ugc/serdes.hpp41
-rw-r--r--ugc/types.hpp3
-rw-r--r--ugc/ugc_tests/serdes_tests.cpp20
5 files changed, 53 insertions, 33 deletions
diff --git a/ugc/api.cpp b/ugc/api.cpp
index 7750aceee9..98d85ea2dd 100644
--- a/ugc/api.cpp
+++ b/ugc/api.cpp
@@ -7,16 +7,16 @@
using namespace std;
using namespace ugc;
+namespace ugc
+{
namespace
{
-Time FromDaysAgo(uint32_t days)
+Time FromDaysAgo(Time time, uint32_t days)
{
- return std::chrono::system_clock::now() - std::chrono::hours(days * 24);
+ return time - std::chrono::hours(days * 24);
}
} // namespace
-namespace ugc
-{
Api::Api(Index const & index, std::string const & filename) : m_index(index), m_storage(filename) {}
void Api::GetUGC(FeatureID const & id, UGCCallback callback)
@@ -35,7 +35,7 @@ void Api::SetUGCUpdate(FeatureID const & id, UGCUpdate const & ugc)
}
// static
-UGC Api::MakeTestUGC1()
+UGC Api::MakeTestUGC1(Time now)
{
Rating rating;
rating.m_ratings.emplace_back("food" /* key */, 4.0 /* value */);
@@ -46,11 +46,11 @@ UGC Api::MakeTestUGC1()
vector<Review> reviews;
reviews.emplace_back(20 /* id */, Text("Damn good coffee", StringUtf8Multilang::kEnglishCode),
Author(UID(987654321 /* hi */, 123456789 /* lo */), "Cole"),
- 5.0 /* rating */, Sentiment::Positive, FromDaysAgo(10));
+ 5.0 /* rating */, Sentiment::Positive, FromDaysAgo(now, 10));
reviews.emplace_back(67812 /* id */,
Text("Clean place, reasonably priced", StringUtf8Multilang::kDefaultCode),
Author(UID(0 /* hi */, 315 /* lo */), "Cooper"), 5.0 /* rating */,
- Sentiment::Positive, FromDaysAgo(1));
+ Sentiment::Positive, FromDaysAgo(now, 1));
vector<Attribute> attributes;
attributes.emplace_back("best-drink", "Coffee");
@@ -59,7 +59,7 @@ UGC Api::MakeTestUGC1()
}
// static
-UGC Api::MakeTestUGC2()
+UGC Api::MakeTestUGC2(Time now)
{
Rating rating;
rating.m_ratings.emplace_back("food" /* key */, 5.0 /* value */);
@@ -71,7 +71,7 @@ UGC Api::MakeTestUGC2()
reviews.emplace_back(119 /* id */,
Text("This pie's so good it is a crime", StringUtf8Multilang::kDefaultCode),
Author(UID(0 /* hi */, 315 /* lo */), "Cooper"), 5.0 /* rating */,
- Sentiment::Positive, FromDaysAgo(1));
+ Sentiment::Positive, FromDaysAgo(now, 1));
vector<Attribute> attributes;
attributes.emplace_back("best-drink", "Coffee");
diff --git a/ugc/api.hpp b/ugc/api.hpp
index 56b53c365b..67961893c2 100644
--- a/ugc/api.hpp
+++ b/ugc/api.hpp
@@ -25,8 +25,8 @@ public:
void SetUGCUpdate(FeatureID const & id, UGCUpdate const & ugc);
- static UGC MakeTestUGC1();
- static UGC MakeTestUGC2();
+ static UGC MakeTestUGC1(Time now = Clock::now());
+ static UGC MakeTestUGC2(Time now = Clock::now());
private:
void GetUGCImpl(FeatureID const & id, UGCCallback callback);
diff --git a/ugc/serdes.hpp b/ugc/serdes.hpp
index a6be07e468..6ec99878e1 100644
--- a/ugc/serdes.hpp
+++ b/ugc/serdes.hpp
@@ -8,25 +8,27 @@
#include "coding/varint.hpp"
#include "coding/write_to_sink.hpp"
+#include "base/exception.hpp"
+
#include <cmath>
#include <cstdint>
namespace ugc
{
-enum class Version : uint8_t
-{
- V0
-};
+DECLARE_EXCEPTION(SerDesException, RootException);
+DECLARE_EXCEPTION(BadBlob, SerDesException);
-struct HeaderV0
+enum class Version : uint8_t
{
+ V0,
+ Latest = V0
};
template <typename Sink>
class Serializer
{
public:
- Serializer(Sink & sink, HeaderV0 const & header) : m_sink(sink), m_header(header) {}
+ Serializer(Sink & sink) : m_sink(sink) {}
void operator()(uint8_t const d, char const * /* name */ = nullptr) { WriteToSink(m_sink, d); }
void operator()(uint32_t const d, char const * /* name */ = nullptr) { WriteToSink(m_sink, d); }
@@ -79,14 +81,13 @@ public:
private:
Sink & m_sink;
- HeaderV0 const m_header;
};
template <typename Source>
class DeserializerV0
{
public:
- DeserializerV0(Source & source, HeaderV0 const & header) : m_source(source), m_header(header) {}
+ DeserializerV0(Source & source) : m_source(source) {}
void operator()(uint8_t & d, char const * /* name */ = nullptr)
{
@@ -157,6 +158,28 @@ public:
private:
Source & m_source;
- HeaderV0 const m_header;
};
+
+template <typename Sink>
+void Serialize(Sink & sink, UGC const & ugc)
+{
+ WriteToSink(sink, static_cast<uint8_t>(Version::Latest));
+ Serializer<Sink> ser(sink);
+ ser(ugc);
+}
+
+template <typename Source>
+void Deserialize(Source & source, UGC & ugc)
+{
+ uint8_t version = 0;
+ ReadPrimitiveFromSource(source, version);
+ if (version == static_cast<uint8_t>(Version::V0))
+ {
+ DeserializerV0<Source> des(source);
+ des(ugc);
+ return;
+ }
+
+ MYTHROW(BadBlob, ("Unknown data version:", static_cast<int>(version)));
+}
} // namespace ugc
diff --git a/ugc/types.hpp b/ugc/types.hpp
index fd746d029d..7a6de3e533 100644
--- a/ugc/types.hpp
+++ b/ugc/types.hpp
@@ -27,7 +27,8 @@
namespace ugc
{
using TranslationKey = std::string;
-using Time = std::chrono::time_point<std::chrono::system_clock>;
+using Clock = std::chrono::system_clock;
+using Time = std::chrono::time_point<Clock>;
enum class Sentiment
{
diff --git a/ugc/ugc_tests/serdes_tests.cpp b/ugc/ugc_tests/serdes_tests.cpp
index 5687570f32..79465ad522 100644
--- a/ugc/ugc_tests/serdes_tests.cpp
+++ b/ugc/ugc_tests/serdes_tests.cpp
@@ -38,16 +38,14 @@ ReaderSource<MemReader> MakeSource(Buffer const & buffer)
UNIT_TEST(SerDes_Rating)
{
- auto expectedRating = GetTestRating();
+ auto const expectedRating = GetTestRating();
TEST_EQUAL(expectedRating, expectedRating, ());
- HeaderV0 header;
-
Buffer buffer;
{
auto sink = MakeSink(buffer);
- Ser ser(sink, header);
+ Ser ser(sink);
ser(expectedRating);
}
@@ -55,7 +53,7 @@ UNIT_TEST(SerDes_Rating)
{
auto source = MakeSource(buffer);
- Des des(source, header);
+ Des des(source);
des(actualRating);
}
@@ -64,24 +62,22 @@ UNIT_TEST(SerDes_Rating)
UNIT_TEST(SerDes_UGC)
{
- auto expectedUGC = Api::MakeTestUGC1();
+ // Time must be in whole days to prevent lose of precision during
+ // serialization/deserialization.
+ auto const expectedUGC = Api::MakeTestUGC1(Time(chrono::hours(24 * 100)));
TEST_EQUAL(expectedUGC, expectedUGC, ());
- HeaderV0 header;
-
Buffer buffer;
{
auto sink = MakeSink(buffer);
- Ser ser(sink, header);
- ser(expectedUGC);
+ Serialize(sink, expectedUGC);
}
UGC actualUGC({} /* rating */, {} /* reviews */, {} /* attributes */);
{
auto source = MakeSource(buffer);
- Des des(source, header);
- des(actualUGC);
+ Deserialize(source, actualUGC);
}
TEST_EQUAL(expectedUGC, actualUGC, ());