Review fixes.

This commit is contained in:
Maxim Pimenov 2016-03-02 21:00:27 +03:00 committed by Sergey Yershov
parent 72be2d2632
commit 618362c66a
4 changed files with 146 additions and 31 deletions

View file

@ -3,6 +3,7 @@
#include "base/logging.hpp"
#include "base/string_utils.hpp"
#include "std/algorithm.hpp"
#include "std/sstream.hpp"
#include "std/string.hpp"
@ -10,8 +11,6 @@
namespace
{
void FromJSONObject(json_t * root, string & result) { result = string(json_string_value(root)); }
void FromJSONObject(json_t * root, string const & field, string & result)
{
if (!json_is_object(root))
@ -100,7 +99,7 @@ void FromJSONObject(json_t * root, string const & field, search::Sample::Result:
r = search::Sample::Result::Relevance::RELEVANCE_IRRELEVANT;
}
void FromJSONObject(json_t * root, search::Sample::Result & result)
void FromJSON(json_t * root, search::Sample::Result & result)
{
FromJSONObject(root, "position", result.m_pos);
FromJSONObject(root, "name", result.m_name);
@ -120,12 +119,61 @@ void FromJSONObject(json_t * root, string const & field, vector<T> & result)
size_t sz = json_array_size(arr);
result.resize(sz);
for (size_t i = 0; i < sz; ++i)
FromJSONObject(json_array_get(arr, i), result[i]);
FromJSON(json_array_get(arr, i), result[i]);
}
bool LessRect(m2::RectD const & lhs, m2::RectD const & rhs)
{
if (lhs.minX() != rhs.minX())
return lhs.minX() < rhs.minX();
if (lhs.minY() != rhs.minY())
return lhs.minY() < rhs.minY();
if (lhs.maxX() != rhs.maxX())
return lhs.maxX() < rhs.maxX();
if (lhs.maxY() != rhs.maxY())
return lhs.maxY() < rhs.maxY();
return false;
}
template <typename T>
bool Less(vector<T> lhs, vector<T> rhs)
{
sort(lhs.begin(), lhs.end());
sort(rhs.begin(), rhs.end());
return lhs < rhs;
}
template <typename T>
bool Equal(vector<T> lhs, vector<T> rhs)
{
sort(lhs.begin(), lhs.end());
sort(rhs.begin(), rhs.end());
return lhs == rhs;
}
} // namespace
namespace search
{
bool Sample::Result::operator<(Sample::Result const & rhs) const
{
if (m_pos != rhs.m_pos)
return m_pos < rhs.m_pos;
if (m_name != rhs.m_name)
return m_name < rhs.m_name;
if (m_houseNumber != rhs.m_houseNumber)
return m_houseNumber < rhs.m_houseNumber;
if (m_relevance != rhs.m_relevance)
return m_relevance < rhs.m_relevance;
return Less(m_types, rhs.m_types);
}
bool Sample::Result::operator==(Sample::Result const & rhs) const
{
// Note: Strict equality for points and viewports.
return m_pos == rhs.m_pos && m_name == rhs.m_name && m_houseNumber == rhs.m_houseNumber &&
Equal(m_types, rhs.m_types) && m_relevance == rhs.m_relevance;
}
bool Sample::DeserializeFromJSON(string const & jsonStr)
{
try
@ -141,6 +189,25 @@ bool Sample::DeserializeFromJSON(string const & jsonStr)
return false;
}
bool Sample::operator<(Sample const & rhs) const
{
if (m_query != rhs.m_query)
return m_query < rhs.m_query;
if (m_locale != rhs.m_locale)
return m_locale < rhs.m_locale;
if (m_pos != rhs.m_pos)
return m_pos < rhs.m_pos;
if (m_viewport != rhs.m_viewport)
return LessRect(m_viewport, rhs.m_viewport);
return Less(m_results, rhs.m_results);
}
bool Sample::operator==(Sample const & rhs) const
{
return m_query == rhs.m_query && m_locale == rhs.m_locale && m_pos == rhs.m_pos &&
m_viewport == rhs.m_viewport && Equal(m_results, rhs.m_results);
}
// static
bool Sample::DeserializeFromJSON(string const & jsonStr, vector<Sample> & samples)
{
@ -162,25 +229,6 @@ bool Sample::DeserializeFromJSON(string const & jsonStr, vector<Sample> & sample
return false;
}
string Sample::ToStringDebug() const
{
ostringstream oss;
oss << "[";
oss << "query: " << DebugPrint(m_query) << " ";
oss << "locale: " << m_locale << " ";
oss << "pos: " << DebugPrint(m_pos) << " ";
oss << "viewport: " << DebugPrint(m_viewport) << " ";
oss << "results: [";
for (size_t i = 0; i < m_results.size(); ++i)
{
if (i > 0)
oss << " ";
oss << DebugPrint(m_results[i]);
}
oss << "]";
return oss.str();
}
void Sample::DeserializeFromJSONImpl(json_t * root)
{
FromJSONObject(root, "query", m_query);
@ -219,5 +267,22 @@ string DebugPrint(Sample::Result const & r)
return oss.str();
}
string DebugPrint(Sample const & s) { return s.ToStringDebug(); }
string DebugPrint(Sample const & s)
{
ostringstream oss;
oss << "[";
oss << "query: " << DebugPrint(s.m_query) << " ";
oss << "locale: " << s.m_locale << " ";
oss << "pos: " << DebugPrint(s.m_pos) << " ";
oss << "viewport: " << DebugPrint(s.m_viewport) << " ";
oss << "results: [";
for (size_t i = 0; i < s.m_results.size(); ++i)
{
if (i > 0)
oss << " ";
oss << DebugPrint(s.m_results[i]);
}
oss << "]";
return oss.str();
}
} // namespace search

View file

@ -11,9 +11,8 @@
namespace search
{
class Sample
struct Sample
{
public:
struct Result
{
enum Relevance
@ -23,6 +22,10 @@ public:
RELEVANCE_VITAL
};
bool operator<(Result const & rhs) const;
bool operator==(Result const & rhs) const;
m2::PointD m_pos = m2::PointD(0, 0);
strings::UniString m_name;
string m_houseNumber;
@ -34,9 +37,10 @@ public:
static bool DeserializeFromJSON(string const & jsonStr, vector<Sample> & samples);
string ToStringDebug() const;
bool operator<(Sample const & rhs) const;
bool operator==(Sample const & rhs) const;
private:
void DeserializeFromJSONImpl(json_t * root);
strings::UniString m_query;

View file

@ -219,13 +219,16 @@ int FindResult(TestSearchEngine & engine, string const & mwmName, uint64_t const
// Another attempt. If the queries are stale, feature id is useless.
// However, some information may be recovered from (lat, lon).
double const kEps = 1e-6;
double const kEps = 1e-2;
for (size_t i = 0; i < results.size(); ++i)
{
auto const & r = results[i];
if (r.HasPoint() &&
my::AlmostEqualAbs(r.GetFeatureCenter(), MercatorBounds::FromLatLon(lat, lon), kEps))
{
double const dist = MercatorBounds::DistanceOnEarth(r.GetFeatureCenter(),
MercatorBounds::FromLatLon(lat, lon));
LOG(LDEBUG, ("dist =", dist));
return i;
}
}

View file

@ -4,8 +4,43 @@
#include "base/logging.hpp"
#include "std/algorithm.hpp"
using namespace search;
namespace
{
search::Sample g_cuba;
search::Sample g_riga;
void FillGlobals()
{
g_cuba.m_query = strings::MakeUniString("cuba");
g_cuba.m_locale = "en";
g_cuba.m_pos = {37.618706, 99.53730574302003};
g_cuba.m_viewport = {37.1336, 67.1349, 38.0314, 67.7348};
search::Sample::Result cubaRes;
cubaRes.m_name = strings::MakeUniString("Cuba");
cubaRes.m_relevance = search::Sample::Result::RELEVANCE_RELEVANT;
cubaRes.m_types.push_back("place-country");
cubaRes.m_pos = {-80.832886, 15.521132748163712};
cubaRes.m_houseNumber = "";
g_cuba.m_results = {cubaRes};
g_riga.m_query = strings::MakeUniString("riga");
g_riga.m_locale = "en";
g_riga.m_pos = {37.65376, 98.51110651930014};
g_riga.m_viewport = {37.5064, 67.0476, 37.7799, 67.304};
search::Sample::Result rigaRes;
rigaRes.m_name = strings::MakeUniString("Rīga");
rigaRes.m_relevance = search::Sample::Result::RELEVANCE_VITAL;
rigaRes.m_types.push_back("place-city-capital-2");
rigaRes.m_pos = {24.105186, 107.7819569220319};
rigaRes.m_houseNumber = "";
g_riga.m_results = {rigaRes, rigaRes};
}
} // namespace
UNIT_TEST(Sample_Smoke)
{
auto const jsonStr = R"EOF(
@ -41,8 +76,8 @@ UNIT_TEST(Sample_Smoke)
Sample s;
TEST(s.DeserializeFromJSON(jsonStr), ());
LOG(LINFO, (DebugPrint(s)));
FillGlobals();
TEST_EQUAL(s, g_cuba, ());
}
UNIT_TEST(Sample_BadViewport)
@ -157,4 +192,12 @@ UNIT_TEST(Sample_Arrays)
vector<Sample> samples;
TEST(Sample::DeserializeFromJSON(jsonStr, samples), ());
FillGlobals();
vector<Sample> expected = {g_cuba, g_riga};
sort(samples.begin(), samples.end());
sort(expected.begin(), expected.end());
TEST_EQUAL(samples, expected, ());
}