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

opening_hours_integration_tests.cpp « opening_hours_integration_tests « opening_hours « 3party - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fb1ae4eaf5dc832701beaa80e10042aadb3d722c (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include "parse_opening_hours.hpp"
#include "rules_evaluation.hpp"

#define BOOST_TEST_MODULE OpeningHoursIntegration

#include <algorithm>
#include <ctime>
#include <map>

#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-parameter"
#endif

#include <boost/algorithm/string/case_conv.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <boost/test/included/unit_test.hpp>

#if defined(__clang__)
#pragma clang diagnostic pop
#endif

namespace
{
template <typename T>
std::string ToString(T const & t)
{
  std::stringstream sstr;
  sstr << t;
  return sstr.str();
}

template <typename T>
bool HasPeriod(std::vector<T> const & v)
{
  auto const hasPeriod = [](T const & t) { return t.HasPeriod(); };
  return std::any_of(begin(v), end(v), hasPeriod);
}

template <typename T>
bool HasPlus(std::vector<T> const & v)
{
  auto const hasPlus = [](T const & t) { return t.HasPlus(); };
  return std::any_of(begin(v), end(v), hasPlus);
}

bool HasOffset(osmoh::TMonthdayRanges const & mr)
{
  auto const hasOffset = [](osmoh::MonthdayRange const & md) {
    return
        md.GetStart().HasOffset() ||
        md.GetEnd().HasOffset();
  };
  return std::any_of(begin(mr), end(mr), hasOffset);
}

bool HasOffset(osmoh::Weekdays const & wd)
{
  auto const hasOffset = [](osmoh::WeekdayRange const & w) { return w.HasOffset(); };
  return std::any_of(begin(wd.GetWeekdayRanges()), end(wd.GetWeekdayRanges()), hasOffset);
}

template <typename ParserResult>
bool CompareNormalized(std::string const & original, ParserResult const & pretendent)
{
  auto originalCopy = original;
  auto pretendentCopy = ToString(pretendent);

  boost::to_lower(originalCopy);
  boost::to_lower(pretendentCopy);

  boost::replace_all(originalCopy, "off", "closed");

  boost::replace_all(originalCopy, " ", "");
  boost::replace_all(pretendentCopy, " ", "");

  return pretendentCopy == originalCopy;
}

enum
{
  Parsed,
  Serialised,
  Period,
  Plus,
  Offset,
  Count_
};
using TRuleFeatures = std::array<bool, Count_>;

std::ostream & operator<<(std::ostream & ost, TRuleFeatures const & f)
{
 std::copy(begin(f), end(f), std::ostream_iterator<bool>(ost, "\t"));
 return ost;
}

TRuleFeatures DescribeRule(osmoh::TRuleSequences const & rule)
{
  TRuleFeatures features{};
  for (auto const & r : rule)
  {
    features[Period] |= HasPeriod(r.GetTimes());
    features[Period] |= HasPeriod(r.GetMonths());
    features[Period] |= HasPeriod(r.GetYears());
    features[Period] |= HasPeriod(r.GetWeeks());

    features[Plus] |= HasPlus(r.GetTimes());
    features[Plus] |= HasPlus(r.GetMonths());
    features[Plus] |= HasPlus(r.GetYears());

    features[Offset] |= HasOffset(r.GetMonths());
    features[Offset] |= HasOffset(r.GetWeekdays());
  }

  return features;
}
} // namespace

/// How to run:
/// 1. copy opening-count.lst to where the binary is
/// 2. run with --log_level=message
BOOST_AUTO_TEST_CASE(OpeningHours_CountFailed)
{
  std::ifstream datalist("opening-count.lst");
  BOOST_REQUIRE_MESSAGE(datalist.is_open(),
                        "Can't open ./opening-count.lst: " << std::strerror(errno));

  std::string line;

  size_t line_num = 0;
  size_t num_failed = 0;
  size_t num_total = 0;

  std::map<size_t, size_t> hist;
  std::map<TRuleFeatures, size_t> featuresDistrib;

  while (std::getline(datalist, line))
  {
    size_t count = 1;
    std::string datastr;

    auto d = line.find('|');
    if (d == std::string::npos)
    {
      BOOST_WARN_MESSAGE((d != std::string::npos),
                         "Incorrect line " << line_num << " format: " << line);
      datastr = line;
    }
    else
    {
      count = std::stol(line.substr(0, d));
      datastr = line.substr(d + 1);
    }

    line_num++;

    osmoh::TRuleSequences rule;
    auto const isParsed = Parse(datastr, rule);
    TRuleFeatures features{};

    if (isParsed)
      features = DescribeRule(rule);
    features[Parsed] = true;
    features[Serialised] = true;

    if (!isParsed)
    {
      num_failed += count;
      ++hist[count];
      features[Parsed] = false;
      features[Serialised] = false;
      BOOST_TEST_MESSAGE("-- " << count << " :[" << datastr << "]");
    }
    else if (!CompareNormalized(datastr, rule))
    {
      num_failed += count;
      ++hist[count];
      features[Serialised] = false;
      BOOST_TEST_MESSAGE("- " << count << " :[" << datastr << "]");
      BOOST_TEST_MESSAGE("+ " << count << " :[" << ToString(rule) << "]");
    }

    featuresDistrib[features] += count;
    num_total += count;
  }

  BOOST_CHECK_MESSAGE((num_failed == 0),
                      "Failed " << num_failed <<
                      " of " << num_total <<
                      " (" << double(num_failed)/(double(num_total)/100) << "%)");

  {
    std::stringstream message;
    for (auto const & e : hist)
      message << "Weight: " << e.first << " Count: " << e.second << std::endl;

    BOOST_TEST_MESSAGE(message.str());
  }
  {
    std::stringstream message;
    message << "Parsed\tSerialised\tPeriod\tPlus\tOffset\tCount" << std::endl;
    for (auto const & e : featuresDistrib)
      message << e.first  << '\t' << e.second << std::endl;

    BOOST_TEST_MESSAGE(message.str());
  }
}