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

processor_test.cpp « search_integration_tests « search - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8af3dfbd3a01cd1f5839c1c8e6fe977d8e91d51d (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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
#include "testing/testing.hpp"

#include "search/retrieval.hpp"
#include "search/search_integration_tests/helpers.hpp"
#include "search/search_tests_support/test_results_matching.hpp"
#include "search/search_tests_support/test_search_request.hpp"
#include "search/token_slice.hpp"

#include "generator/feature_builder.hpp"
#include "generator/generator_tests_support/test_feature.hpp"
#include "generator/generator_tests_support/test_mwm_builder.hpp"

#include "indexer/feature.hpp"
#include "indexer/index.hpp"

#include "geometry/mercator.hpp"
#include "geometry/point2d.hpp"
#include "geometry/rect2d.hpp"

#include "base/assert.hpp"
#include "base/math.hpp"

#include "std/shared_ptr.hpp"
#include "std/vector.hpp"

using namespace generator::tests_support;
using namespace search::tests_support;

namespace search
{
namespace
{
class TestHotel : public TestPOI
{
public:
  TestHotel(m2::PointD const & center, string const & name, string const & lang, float rating,
            int priceRate)
    : TestPOI(center, name, lang), m_rating(rating), m_priceRate(priceRate)
  {
    CHECK_GREATER_OR_EQUAL(m_rating, 0.0, ());
    CHECK_LESS_OR_EQUAL(m_rating, 10.0, ());

    CHECK_GREATER_OR_EQUAL(m_priceRate, 0, ());
    CHECK_LESS_OR_EQUAL(m_priceRate, 5, ());

    SetTypes({{"tourism", "hotel"}});
  }

  // TestPOI overrides:
  void Serialize(FeatureBuilder1 & fb) const override
  {
    TestPOI::Serialize(fb);

    auto & metadata = fb.GetMetadataForTesting();
    metadata.Set(feature::Metadata::FMD_RATING, strings::to_string(m_rating));
    metadata.Set(feature::Metadata::FMD_PRICE_RATE, strings::to_string(m_priceRate));
  }

private:
  float const m_rating;
  int const m_priceRate;
};

class ProcessorTest : public SearchTest
{
public:
  unique_ptr<TestSearchRequest> MakeRequest(string const & query)
  {
    SearchParams params;
    params.m_query = query;
    params.m_inputLocale = "en";
    params.m_mode = Mode::Everywhere;
    params.m_suggestsEnabled = false;

    auto request = make_unique<TestSearchRequest>(m_engine, params, m_viewport);
    request->Run();
    return request;
  }

  bool MatchResults(vector<shared_ptr<MatchingRule>> rules,
                    vector<search::Result> const & actual) const
  {
    return ::MatchResults(m_engine, rules, actual);
  }
};

UNIT_CLASS_TEST(ProcessorTest, Smoke)
{
  string const countryName = "Wonderland";
  TestCountry wonderlandCountry(m2::PointD(10, 10), countryName, "en");

  TestCity losAlamosCity(m2::PointD(10, 10), "Los Alamos", "en", 100 /* rank */);
  TestCity mskCity(m2::PointD(0, 0), "Moscow", "en", 100 /* rank */);
  TestCity torontoCity(m2::PointD(-10, -10), "Toronto", "en", 100 /* rank */);

  TestVillage longPondVillage(m2::PointD(15, 15), "Long Pond Village", "en", 10 /* rank */);
  TestStreet feynmanStreet(
      vector<m2::PointD>{m2::PointD(9.999, 9.999), m2::PointD(10, 10), m2::PointD(10.001, 10.001)},
      "Feynman street", "en");
  TestStreet bohrStreet1(
      vector<m2::PointD>{m2::PointD(9.999, 10.001), m2::PointD(10, 10), m2::PointD(10.001, 9.999)},
      "Bohr street", "en");
  TestStreet bohrStreet2(vector<m2::PointD>{m2::PointD(10.001, 9.999), m2::PointD(10.002, 9.998)},
                         "Bohr street", "en");
  TestStreet bohrStreet3(vector<m2::PointD>{m2::PointD(10.002, 9.998), m2::PointD(10.003, 9.997)},
                         "Bohr street", "en");
  TestStreet firstAprilStreet(vector<m2::PointD>{m2::PointD(14.998, 15), m2::PointD(15.002, 15)},
                              "1st April street", "en");

  TestBuilding feynmanHouse(m2::PointD(10, 10), "Feynman house", "1 unit 1", feynmanStreet, "en");
  TestBuilding bohrHouse(m2::PointD(10, 10), "Bohr house", "1 unit 1", bohrStreet1, "en");
  TestBuilding hilbertHouse(
      vector<m2::PointD>{
          {10.0005, 10.0005}, {10.0006, 10.0005}, {10.0006, 10.0006}, {10.0005, 10.0006}},
      "Hilbert house", "1 unit 2", bohrStreet1, "en");
  TestBuilding descartesHouse(m2::PointD(10, 10), "Descartes house", "2", "en");
  TestBuilding bornHouse(m2::PointD(14.999, 15), "Born house", "8", firstAprilStreet, "en");

  TestPOI busStop(m2::PointD(0, 0), "Bus stop", "en");
  TestPOI tramStop(m2::PointD(0.0001, 0.0001), "Tram stop", "en");
  TestPOI quantumTeleport1(m2::PointD(0.0002, 0.0002), "Quantum teleport 1", "en");

  TestPOI quantumTeleport2(m2::PointD(10, 10), "Quantum teleport 2", "en");
  quantumTeleport2.SetHouseNumber("3");
  quantumTeleport2.SetStreet(feynmanStreet);

  TestPOI quantumCafe(m2::PointD(-0.0002, -0.0002), "Quantum cafe", "en");
  TestPOI lantern1(m2::PointD(10.0005, 10.0005), "lantern 1", "en");
  TestPOI lantern2(m2::PointD(10.0006, 10.0005), "lantern 2", "en");

  TestStreet stradaDrive(vector<m2::PointD>{m2::PointD(-10.001, -10.001), m2::PointD(-10, -10),
                                            m2::PointD(-9.999, -9.999)},
                         "Strada drive", "en");
  TestBuilding terranceHouse(m2::PointD(-10, -10), "", "155", stradaDrive, "en");

  BuildWorld([&](TestMwmBuilder & builder)
             {
               builder.Add(wonderlandCountry);
               builder.Add(losAlamosCity);
               builder.Add(mskCity);
               builder.Add(torontoCity);
             });
  auto wonderlandId = BuildCountry(countryName, [&](TestMwmBuilder & builder)
                                   {
                                     builder.Add(losAlamosCity);
                                     builder.Add(mskCity);
                                     builder.Add(torontoCity);
                                     builder.Add(longPondVillage);

                                     builder.Add(feynmanStreet);
                                     builder.Add(bohrStreet1);
                                     builder.Add(bohrStreet2);
                                     builder.Add(bohrStreet3);
                                     builder.Add(firstAprilStreet);

                                     builder.Add(feynmanHouse);
                                     builder.Add(bohrHouse);
                                     builder.Add(hilbertHouse);
                                     builder.Add(descartesHouse);
                                     builder.Add(bornHouse);

                                     builder.Add(busStop);
                                     builder.Add(tramStop);
                                     builder.Add(quantumTeleport1);
                                     builder.Add(quantumTeleport2);
                                     builder.Add(quantumCafe);
                                     builder.Add(lantern1);
                                     builder.Add(lantern2);

                                     builder.Add(stradaDrive);
                                     builder.Add(terranceHouse);
                                   });

  SetViewport(m2::RectD(m2::PointD(-1.0, -1.0), m2::PointD(1.0, 1.0)));
  {
    TRules rules = {ExactMatch(wonderlandId, busStop)};
    TEST(ResultsMatch("Bus stop", rules), ());
  }
  {
    TRules rules = {ExactMatch(wonderlandId, quantumCafe),
                    ExactMatch(wonderlandId, quantumTeleport1),
                    ExactMatch(wonderlandId, quantumTeleport2)};
    TEST(ResultsMatch("quantum", rules), ());
  }
  {
    TRules rules = {ExactMatch(wonderlandId, quantumCafe),
                    ExactMatch(wonderlandId, quantumTeleport1)};
    TEST(ResultsMatch("quantum Moscow ", rules), ());
  }
  {
    TEST(ResultsMatch("     ", TRules()), ());
  }
  {
    TRules rules = {ExactMatch(wonderlandId, quantumTeleport2)};
    TEST(ResultsMatch("teleport feynman street", rules), ());
  }
  {
    TRules rules = {ExactMatch(wonderlandId, quantumTeleport2)};
    TEST(ResultsMatch("feynman street 3", rules), ());
  }
  {
    TRules rules = {ExactMatch(wonderlandId, feynmanHouse), ExactMatch(wonderlandId, lantern1)};
    TEST(ResultsMatch("feynman street 1", rules), ());
  }
  {
    TRules rules = {ExactMatch(wonderlandId, bohrHouse), ExactMatch(wonderlandId, hilbertHouse),
                    ExactMatch(wonderlandId, lantern1)};
    TEST(ResultsMatch("bohr street 1", rules), ());
  }
  {
    TEST(ResultsMatch("bohr street 1 unit 3", TRules()), ());
  }
  {
    TRules rules = {ExactMatch(wonderlandId, lantern1), ExactMatch(wonderlandId, lantern2)};
    TEST(ResultsMatch("bohr street 1 lantern ", rules), ());
  }
  {
    TRules rules = {ExactMatch(wonderlandId, feynmanHouse)};
    TEST(ResultsMatch("wonderland los alamos feynman 1 unit 1 ", rules), ());
  }
  {
    // It's possible to find Descartes house by name.
    TRules rules = {ExactMatch(wonderlandId, descartesHouse)};
    TEST(ResultsMatch("Los Alamos Descartes", rules), ());
  }
  {
    // It's not possible to find Descartes house by house number,
    // because it doesn't belong to Los Alamos streets. But it still
    // exists.
    TRules rules = {ExactMatch(wonderlandId, lantern2), ExactMatch(wonderlandId, quantumTeleport2)};
    TEST(ResultsMatch("Los Alamos 2", rules), ());
  }
  {
    TRules rules = {ExactMatch(wonderlandId, bornHouse)};
    TEST(ResultsMatch("long pond 1st april street 8", rules), ());
  }

  {
    TRules rules = {ExactMatch(wonderlandId, terranceHouse)};
    TEST(ResultsMatch("Toronto strada drive 155", rules), ());
  }
}

UNIT_CLASS_TEST(ProcessorTest, SearchInWorld)
{
  string const countryName = "Wonderland";
  TestCountry wonderland(m2::PointD(0, 0), countryName, "en");
  TestCity losAlamos(m2::PointD(0, 0), "Los Alamos", "en", 100 /* rank */);

  auto testWorldId = BuildWorld([&](TestMwmBuilder & builder)
                                {
                                  builder.Add(wonderland);
                                  builder.Add(losAlamos);
                                });
  RegisterCountry(countryName, m2::RectD(m2::PointD(-1.0, -1.0), m2::PointD(1.0, 1.0)));

  SetViewport(m2::RectD(m2::PointD(-1.0, -1.0), m2::PointD(-0.5, -0.5)));
  {
    TRules rules = {ExactMatch(testWorldId, losAlamos)};
    TEST(ResultsMatch("Los Alamos", rules), ());
  }
  {
    TRules rules = {ExactMatch(testWorldId, wonderland)};
    TEST(ResultsMatch("Wonderland", rules), ());
  }
  {
    TRules rules = {ExactMatch(testWorldId, losAlamos)};
    TEST(ResultsMatch("Wonderland Los Alamos", rules), ());
  }
}

UNIT_CLASS_TEST(ProcessorTest, SearchByName)
{
  string const countryName = "Wonderland";
  TestCity london(m2::PointD(1, 1), "London", "en", 100 /* rank */);
  TestPark hydePark(vector<m2::PointD>{m2::PointD(0.5, 0.5), m2::PointD(1.5, 0.5),
                                       m2::PointD(1.5, 1.5), m2::PointD(0.5, 1.5)},
                    "Hyde Park", "en");
  TestPOI cafe(m2::PointD(1.0, 1.0), "London Cafe", "en");

  auto worldId = BuildWorld([&](TestMwmBuilder & builder)
                            {
                              builder.Add(london);
                            });
  auto wonderlandId = BuildCountry(countryName, [&](TestMwmBuilder & builder)
                                   {
                                     builder.Add(hydePark);
                                     builder.Add(cafe);
                                   });

  SetViewport(m2::RectD(m2::PointD(-1.0, -1.0), m2::PointD(-0.9, -0.9)));
  {
    TRules rules = {ExactMatch(wonderlandId, hydePark)};
    TEST(ResultsMatch("hyde park", rules), ());
    TEST(ResultsMatch("london hyde park", rules), ());
    TEST(ResultsMatch("hyde london park", TRules()), ());
  }

  SetViewport(m2::RectD(m2::PointD(0.5, 0.5), m2::PointD(1.5, 1.5)));
  {
    TRules rules = {ExactMatch(worldId, london)};
    TEST(ResultsMatch("london", Mode::Downloader, rules), ());
  }
  {
    TRules rules = {ExactMatch(worldId, london), ExactMatch(wonderlandId, cafe)};
    TEST(ResultsMatch("london", Mode::Everywhere, rules), ());
  }
}

UNIT_CLASS_TEST(ProcessorTest, DisableSuggests)
{
  TestCity london1(m2::PointD(1, 1), "London", "en", 100 /* rank */);
  TestCity london2(m2::PointD(-1, -1), "London", "en", 100 /* rank */);

  auto worldId = BuildWorld([&](TestMwmBuilder & builder)
                            {
                              builder.Add(london1);
                              builder.Add(london2);
                            });

  SetViewport(m2::RectD(m2::PointD(0.5, 0.5), m2::PointD(1.5, 1.5)));
  {
    SearchParams params;
    params.m_query = "londo";
    params.m_inputLocale = "en";
    params.m_mode = Mode::Downloader;
    params.m_suggestsEnabled = false;

    TestSearchRequest request(m_engine, params, m_viewport);
    request.Run();
    TRules rules = {ExactMatch(worldId, london1), ExactMatch(worldId, london2)};

    TEST(MatchResults(rules, request.Results()), ());
  }
}

UNIT_CLASS_TEST(ProcessorTest, TestRankingInfo)
{
  string const countryName = "Wonderland";

  TestCity sanFrancisco(m2::PointD(1, 1), "San Francisco", "en", 100 /* rank */);
  // Golden Gate Bridge-bridge is located in this test on the Golden
  // Gate Bridge-street. Therefore, there are several valid parses of
  // the query "Golden Gate Bridge", and search engine must return
  // both features (street and bridge) as they were matched by full
  // name.
  TestStreet goldenGateStreet(
      vector<m2::PointD>{m2::PointD(-0.5, -0.5), m2::PointD(0, 0), m2::PointD(0.5, 0.5)},
      "Golden Gate Bridge", "en");

  TestPOI goldenGateBridge(m2::PointD(0, 0), "Golden Gate Bridge", "en");

  TestPOI waterfall(m2::PointD(0.5, 0.5), "", "en");
  waterfall.SetTypes({{"waterway", "waterfall"}});

  TestPOI lermontov(m2::PointD(1, 1), "Лермонтовъ", "en");
  lermontov.SetTypes({{"amenity", "cafe"}});

  // A low-rank city with two noname cafes.
  TestCity lermontovo(m2::PointD(-1, -1), "Лермонтово", "en", 0 /* rank */);
  TestPOI cafe1(m2::PointD(-1.01, -1.01), "", "en");
  cafe1.SetTypes({{"amenity", "cafe"}});
  TestPOI cafe2(m2::PointD(-0.99, -0.99), "", "en");
  cafe2.SetTypes({{"amenity", "cafe"}});

  // A low-rank village with a single noname cafe.
  TestVillage pushkino(m2::PointD(-10, -10), "Pushkino", "en", 0 /* rank */);
  TestPOI cafe3(m2::PointD(-10.01, -10.01), "", "en");
  cafe3.SetTypes({{"amenity", "cafe"}});

  auto worldId = BuildWorld([&](TestMwmBuilder & builder)
                            {
                              builder.Add(sanFrancisco);
                              builder.Add(lermontovo);
                            });
  auto wonderlandId = BuildCountry(countryName, [&](TestMwmBuilder & builder)
                                   {
                                     builder.Add(cafe1);
                                     builder.Add(cafe2);
                                     builder.Add(cafe3);
                                     builder.Add(goldenGateBridge);
                                     builder.Add(goldenGateStreet);
                                     builder.Add(lermontov);
                                     builder.Add(pushkino);
                                     builder.Add(waterfall);
                                   });

  SetViewport(m2::RectD(m2::PointD(-0.5, -0.5), m2::PointD(0.5, 0.5)));
  {
    auto request = MakeRequest("golden gate bridge ");

    TRules rules = {ExactMatch(wonderlandId, goldenGateBridge),
                    ExactMatch(wonderlandId, goldenGateStreet)};

    TEST(MatchResults(rules, request->Results()), ());
    for (auto const & result : request->Results())
    {
      auto const & info = result.GetRankingInfo();
      TEST_EQUAL(NAME_SCORE_FULL_MATCH, info.m_nameScore, (result));
      TEST(!info.m_pureCats, (result));
      TEST(!info.m_falseCats, (result));
    }
  }

  // This test is quite important and must always pass.
  {
    auto request = MakeRequest("cafe лермонтов");
    auto const & results = request->Results();

    TRules rules{ExactMatch(wonderlandId, cafe1), ExactMatch(wonderlandId, cafe2),
                 ExactMatch(wonderlandId, lermontov)};
    TEST(MatchResults(rules, results), ());

    TEST_EQUAL(3, results.size(), ("Unexpected number of retrieved cafes."));
    auto const & top = results.front();
    TEST(MatchResults({ExactMatch(wonderlandId, lermontov)}, {top}), ());
  }

  {
    TRules rules{ExactMatch(wonderlandId, waterfall)};
    TEST(ResultsMatch("waterfall", rules), ());
  }

  SetViewport(m2::RectD(m2::PointD(-10.5, -10.5), m2::PointD(-9.5, -9.5)));
  {
    TRules rules{ExactMatch(wonderlandId, cafe3)};
    TEST(ResultsMatch("cafe pushkino ", rules), ());
  }
}

UNIT_CLASS_TEST(ProcessorTest, TestHouseNumbers)
{
  string const countryName = "HouseNumberLand";

  TestCity greenCity(m2::PointD(0, 0), "Зеленоград", "ru", 100 /* rank */);

  TestStreet street(
      vector<m2::PointD>{m2::PointD(0.0, -10.0), m2::PointD(0, 0), m2::PointD(5.0, 5.0)},
      "Генерала Генералова", "ru");

  TestBuilding building0(m2::PointD(2.0, 2.0), "", "100", "en");
  TestBuilding building1(m2::PointD(3.0, 3.0), "", "к200", "ru");
  TestBuilding building2(m2::PointD(4.0, 4.0), "", "300 строение 400", "ru");

  BuildWorld([&](TestMwmBuilder & builder) { builder.Add(greenCity); });
  auto countryId = BuildCountry(countryName, [&](TestMwmBuilder & builder) {
    builder.Add(street);
    builder.Add(building0);
    builder.Add(building1);
    builder.Add(building2);
  });

  {
    TRules rules{ExactMatch(countryId, building0)};
    TEST(ResultsMatch("Зеленоград генералова к100", "ru", rules), ());
  }
  {
    TRules rules{ExactMatch(countryId, building1)};
    TEST(ResultsMatch("Зеленоград генералова к200", "ru", rules), ());
  }
  {
    TRules rules{ExactMatch(countryId, building1)};
    TEST(ResultsMatch("Зеленоград к200 генералова", "ru", rules), ());
  }
  {
    TRules rules{ExactMatch(countryId, building2)};
    TEST(ResultsMatch("Зеленоград 300 строение 400 генералова", "ru", rules), ());
  }
  {
    TRules rules{};
    TEST(ResultsMatch("Зеленоград генералова строе 300", "ru", rules), ());
  }
  {
    TRules rules{ExactMatch(countryId, building2)};
    TEST(ResultsMatch("Зеленоград генералова 300 строе", "ru", rules), ());
  }
}

UNIT_CLASS_TEST(ProcessorTest, TestPostcodes)
{
  string const countryName = "Russia";

  TestCity dolgoprudny(m2::PointD(0, 0), "Долгопрудный", "ru", 100 /* rank */);
  TestCity london(m2::PointD(10, 10), "London", "en", 100 /* rank */);

  TestStreet street(
      vector<m2::PointD>{m2::PointD(-0.5, 0.0), m2::PointD(0, 0), m2::PointD(0.5, 0.0)},
      "Первомайская", "ru");
  street.SetPostcode("141701");

  TestBuilding building28(m2::PointD(0.0, 0.00001), "", "28а", street, "ru");
  building28.SetPostcode("141701");

  TestBuilding building29(m2::PointD(0.0, -0.00001), "", "29", street, "ru");
  building29.SetPostcode("141701");

  TestPOI building30(m2::PointD(0.00002, 0.00002), "", "en");
  building30.SetHouseNumber("30");
  building30.SetPostcode("141701");
  building30.SetTypes({{"building", "address"}});

  TestBuilding building31(m2::PointD(0.00001, 0.00001), "", "31", street, "ru");
  building31.SetPostcode("141702");

  TestBuilding building1(m2::PointD(10, 10), "", "1", "en");
  building1.SetPostcode("WC2H 7BX");

  BuildWorld([&](TestMwmBuilder & builder)
             {
               builder.Add(dolgoprudny);
               builder.Add(london);
             });
  auto countryId = BuildCountry(countryName, [&](TestMwmBuilder & builder)
                                {
                                  builder.Add(street);
                                  builder.Add(building28);
                                  builder.Add(building29);
                                  builder.Add(building30);
                                  builder.Add(building31);

                                  builder.Add(building1);
                                });

  // Tests that postcode is added to the search index.
  {
    MwmContext context(m_engine.GetMwmHandleById(countryId));
    my::Cancellable cancellable;

    QueryParams params;
    params.m_tokens.emplace_back();
    params.m_tokens.back().push_back(strings::MakeUniString("141702"));
    auto features = RetrievePostcodeFeatures(context, cancellable,
                                             TokenSlice(params, 0, params.m_tokens.size()));
    TEST_EQUAL(1, features->PopCount(), ());

    uint64_t index = 0;
    while (!features->GetBit(index))
      ++index;

    Index::FeaturesLoaderGuard loader(m_engine, countryId);
    FeatureType ft;
    TEST(loader.GetFeatureByIndex(index, ft), ());

    auto rule = ExactMatch(countryId, building31);
    TEST(rule->Matches(ft), ());
  }

  {
    TRules rules{ExactMatch(countryId, building28)};
    TEST(ResultsMatch("Долгопрудный первомайская 28а", "ru", rules), ());
  }
  {
    TRules rules{ExactMatch(countryId, building28)};
    TEST(ResultsMatch("Долгопрудный первомайская 28а, 141701", "ru", rules), ());
  }
  {
    TRules rules{ExactMatch(countryId, building28), ExactMatch(countryId, building29),
                 ExactMatch(countryId, building30), ExactMatch(countryId, street)};
    TEST(ResultsMatch("Долгопрудный первомайская 141701", "ru", rules), ());
  }
  {
    TRules rules{ExactMatch(countryId, building31)};
    TEST(ResultsMatch("Долгопрудный первомайская 141702", "ru", rules), ());
  }
  {
    TRules rules{ExactMatch(countryId, building28), ExactMatch(countryId, building29),
                 ExactMatch(countryId, building30), ExactMatch(countryId, street)};
    TEST(ResultsMatch("Долгопрудный 141701", "ru", rules), ());
  }
  {
    TRules rules{ExactMatch(countryId, building31)};
    TEST(ResultsMatch("Долгопрудный 141702", "ru", rules), ());
  }

  {
    string const kQueries[] = {"london WC2H 7BX", "london WC2H 7", "london WC2H ", "london WC"};
    for (auto const & query : kQueries)
    {
      TRules rules{ExactMatch(countryId, building1)};
      TEST(ResultsMatch(query, rules), (query));
    }
  }
}

UNIT_CLASS_TEST(ProcessorTest, TestCategories)
{
  string const countryName = "Wonderland";

  TestCity sanFrancisco(m2::PointD(0, 0), "San Francisco", "en", 100 /* rank */);

  TestPOI nonameBeach(m2::PointD(0, 0), "", "ru");
  nonameBeach.SetTypes({{"natural", "beach"}});

  TestPOI namedBeach(m2::PointD(0.2, 0.2), "San Francisco beach", "en");
  namedBeach.SetTypes({{"natural", "beach"}});

  TestPOI nonameAtm(m2::PointD(0, 0), "", "en");
  nonameAtm.SetTypes({{"amenity", "atm"}});

  TestPOI namedAtm(m2::PointD(0.3, 0.3), "ATM", "en");
  namedAtm.SetTypes({{"amenity", "atm"}});

  TestPOI busStop(m2::PointD(0.00005, 0.0005), "ATM Bus Stop", "en");
  busStop.SetTypes({{"highway", "bus_stop"}});

  TestPOI cafe(m2::PointD(0.0001, 0.0001), "Cafe", "en");
  cafe.SetTypes({{"amenity", "cafe"}, {"internet_access", "wlan"}});

  TestPOI toi(m2::PointD(0.0001, 0.0001), "", "en");
  toi.SetTypes({{"amenity", "toilets"}});

  BuildWorld([&](TestMwmBuilder & builder)
             {
               builder.Add(sanFrancisco);
             });
  auto wonderlandId = BuildCountry(countryName, [&](TestMwmBuilder & builder)
                                   {
                                     builder.Add(busStop);
                                     builder.Add(cafe);
                                     builder.Add(namedAtm);
                                     builder.Add(namedBeach);
                                     builder.Add(nonameAtm);
                                     builder.Add(nonameBeach);
                                     builder.Add(toi);
                                   });

  SetViewport(m2::RectD(m2::PointD(-0.5, -0.5), m2::PointD(0.5, 0.5)));

  {
    TRules const rules = {ExactMatch(wonderlandId, nonameAtm), ExactMatch(wonderlandId, namedAtm),
                          ExactMatch(wonderlandId, busStop)};

    auto request = MakeRequest("atm");
    TEST(MatchResults(rules, request->Results()), ());
    for (auto const & result : request->Results())
    {
      Index::FeaturesLoaderGuard loader(m_engine, wonderlandId);
      FeatureType ft;
      TEST(loader.GetFeatureByIndex(result.GetFeatureID().m_index, ft), ());

      auto const & info = result.GetRankingInfo();

      if (busStop.Matches(ft))
      {
        TEST(!info.m_pureCats, (result));
        TEST(info.m_falseCats, (result));
      }
      else
      {
        TEST(info.m_pureCats, (result));
        TEST(!info.m_falseCats, (result));
      }
    }
  }

  {
    TRules const rules = {ExactMatch(wonderlandId, nonameAtm), ExactMatch(wonderlandId, namedAtm)};

    auto request = MakeRequest("#atm");

    TEST(MatchResults(rules, request->Results()), ());
    for (auto const & result : request->Results())
    {
      auto const & info = result.GetRankingInfo();

      // Token with a hashtag should not participate in name-score
      // calculations.
      TEST_EQUAL(NAME_SCORE_ZERO, info.m_nameScore, (result));
    }
  }

  // Tests that inexistent hashtagged categories do not crash.
  TEST(ResultsMatch("#void-", TRules{}), ());

  TEST(ResultsMatch("wifi", {ExactMatch(wonderlandId, cafe)}), ());
  TEST(ResultsMatch("toilet", {ExactMatch(wonderlandId, toi)}), ());
  TEST(ResultsMatch("beach ",
                    {ExactMatch(wonderlandId, nonameBeach), ExactMatch(wonderlandId, namedBeach)}),
       ());
}

UNIT_CLASS_TEST(ProcessorTest, TestCoords)
{
  auto request = MakeRequest("51.681644 39.183481");
  auto const & results = request->Results();
  TEST_EQUAL(results.size(), 1, ());

  auto const & result = results[0];
  TEST_EQUAL(result.GetResultType(), Result::RESULT_LATLON, ());
  TEST(result.HasPoint(), ());

  m2::PointD const expected = MercatorBounds::FromLatLon(51.681644, 39.183481);
  auto const actual = result.GetFeatureCenter();
  TEST(MercatorBounds::DistanceOnEarth(expected, actual) <= 1.0, ());
}

UNIT_CLASS_TEST(ProcessorTest, HotelsFiltering)
{
  char const countryName[] = "Wonderland";

  TestHotel h1(m2::PointD(0, 0), "h1", "en", 8.0 /* rating */, 2 /* priceRate */);
  TestHotel h2(m2::PointD(0, 1), "h2", "en", 7.0 /* rating */, 5 /* priceRate */);
  TestHotel h3(m2::PointD(1, 0), "h3", "en", 9.0 /* rating */, 0 /* priceRate */);
  TestHotel h4(m2::PointD(1, 1), "h4", "en", 2.0 /* rating */, 4 /* priceRate */);

  auto id = BuildCountry(countryName, [&](TestMwmBuilder & builder) {
    builder.Add(h1);
    builder.Add(h2);
    builder.Add(h3);
    builder.Add(h4);
  });

  SearchParams params;
  params.m_query = "hotel";
  params.m_inputLocale = "en";
  params.m_mode = Mode::Everywhere;
  params.m_suggestsEnabled = false;

  SetViewport(m2::RectD(m2::PointD(-1, -1), m2::PointD(2, 2)));
  {
    TestSearchRequest request(m_engine, params, m_viewport);
    request.Run();
    TRules rules = {ExactMatch(id, h1), ExactMatch(id, h2), ExactMatch(id, h3), ExactMatch(id, h4)};
    TEST(MatchResults(rules, request.Results()), ());
  }

  using namespace hotels_filter;

  params.m_hotelsFilter = And(Gt<Rating>(7.0), Le<PriceRate>(2));
  {
    TestSearchRequest request(m_engine, params, m_viewport);
    request.Run();
    TRules rules = {ExactMatch(id, h1), ExactMatch(id, h3)};
    TEST(MatchResults(rules, request.Results()), ());
  }

  params.m_hotelsFilter = Or(Eq<Rating>(9.0), Le<PriceRate>(4));
  {
    TestSearchRequest request(m_engine, params, m_viewport);
    request.Run();
    TRules rules = {ExactMatch(id, h1), ExactMatch(id, h3), ExactMatch(id, h4)};
    TEST(MatchResults(rules, request.Results()), ());
  }

  params.m_hotelsFilter = Or(And(Eq<Rating>(7.0), Eq<PriceRate>(5)), Eq<PriceRate>(4));
  {
    TestSearchRequest request(m_engine, params, m_viewport);
    request.Run();
    TRules rules = {ExactMatch(id, h2), ExactMatch(id, h4)};
    TEST(MatchResults(rules, request.Results()), ());
  }
}
}  // namespace
}  // namespace search