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

test_results_matching.cpp « search_tests_support « search - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 381f18f8760dd96cf8db601acb579294083863bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "search/search_tests_support/test_results_matching.hpp"

#include "generator/generator_tests_support/test_feature.hpp"

#include "indexer/data_source.hpp"
#include "indexer/feature_decl.hpp"

#include <sstream>

using namespace std;
using namespace generator::tests_support;

namespace search
{
namespace tests_support
{
ExactMatchingRule::ExactMatchingRule(MwmSet::MwmId const & mwmId, TestFeature const & feature)
  : m_mwmId(mwmId), m_feature(feature)
{
}

bool ExactMatchingRule::Matches(FeatureType const & feature) const
{
  if (m_mwmId != feature.GetID().m_mwmId)
    return false;
  return m_feature.Matches(feature);
}

string ExactMatchingRule::ToString() const
{
  ostringstream os;
  os << "ExactMatchingRule [ " << DebugPrint(m_mwmId) << ", " << DebugPrint(m_feature) << " ]";
  return os.str();
}

AlternativesMatchingRule::AlternativesMatchingRule(initializer_list<shared_ptr<MatchingRule>> rules)
  : m_rules(move(rules))
{
}

bool AlternativesMatchingRule::Matches(FeatureType const & feature) const
{
  for (auto const & rule : m_rules)
  {
    if (rule->Matches(feature))
      return true;
  }
  return false;
}

string AlternativesMatchingRule::ToString() const
{
  ostringstream os;
  os << "OrRule [ ";
  for (auto it = m_rules.cbegin(); it != m_rules.cend(); ++it)
  {
    os << (*it)->ToString();
    if (it + 1 != m_rules.cend())
      os << " | ";
  }
  os << " ]";
  return os.str();
}

bool MatchResults(DataSourceBase const & dataSource, vector<shared_ptr<MatchingRule>> rules,
                  vector<search::Result> const & actual)
{
  vector<FeatureID> resultIds;
  for (auto const & a : actual)
    resultIds.push_back(a.GetFeatureID());
  sort(resultIds.begin(), resultIds.end());

  vector<string> unexpected;
  auto removeMatched = [&rules, &unexpected](FeatureType const & feature)
  {
    for (auto it = rules.begin(); it != rules.end(); ++it)
    {
      if ((*it)->Matches(feature))
      {
        rules.erase(it);
        return;
      }
    }
    unexpected.push_back(DebugPrint(feature) + " from " + DebugPrint(feature.GetID().m_mwmId));
  };
  dataSource.ReadFeatures(removeMatched, resultIds);

  if (rules.empty() && unexpected.empty())
    return true;

  ostringstream os;
  os << "Unsatisfied rules:" << endl;
  for (auto const & e : rules)
    os << "  " << DebugPrint(*e) << endl;
  os << "Unexpected retrieved features:" << endl;
  for (auto const & u : unexpected)
    os << "  " << u << endl;

  LOG(LWARNING, (os.str()));
  return false;
}

bool MatchResults(DataSourceBase const & dataSource, vector<shared_ptr<MatchingRule>> rules,
                  search::Results const & actual)
{
  vector<search::Result> const results(actual.begin(), actual.end());
  return MatchResults(dataSource, rules, results);
}

bool ResultMatches(DataSourceBase const & dataSource, shared_ptr<MatchingRule> rule,
                   search::Result const & result)
{
  bool matches = false;
  dataSource.ReadFeature([&](FeatureType const & ft) { matches = rule->Matches(ft); }, result.GetFeatureID());
  return matches;
}

string DebugPrint(MatchingRule const & rule) { return rule.ToString(); }
}  // namespace tests_support
}  // namespace search