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

features_offsets_table_test.cpp « indexer_tests « indexer - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d08cca7486d2401d1f0bc95e581850051bc2b351 (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
#include "testing/testing.hpp"

#include "indexer/features_offsets_table.hpp"
#include "indexer/data_header.hpp"
#include "indexer/features_vector.hpp"

#include "platform/local_country_file_utils.hpp"
#include "platform/platform.hpp"

#include "coding/file_container.hpp"

#include "base/scope_guard.hpp"

#include "defines.hpp"

#include "std/bind.hpp"
#include "std/string.hpp"


using namespace platform;

namespace feature
{
  UNIT_TEST(FeaturesOffsetsTable_Empty)
  {
    FeaturesOffsetsTable::Builder builder;
    unique_ptr<FeaturesOffsetsTable> table(FeaturesOffsetsTable::Build(builder));
    TEST(table.get(), ());
    TEST_EQUAL(static_cast<uint64_t>(0), table->size(), ());
  }

  UNIT_TEST(FeaturesOffsetsTable_Basic)
  {
    FeaturesOffsetsTable::Builder builder;
    builder.PushOffset(1);
    builder.PushOffset(4);
    builder.PushOffset(17);
    builder.PushOffset(128);
    builder.PushOffset(129);
    builder.PushOffset(510);
    builder.PushOffset(513);
    builder.PushOffset(1024);

    unique_ptr<FeaturesOffsetsTable> table(FeaturesOffsetsTable::Build(builder));
    TEST(table.get(), ());
    TEST_EQUAL(static_cast<uint64_t>(8), table->size(), ());

    TEST_EQUAL(static_cast<uint64_t>(1), table->GetFeatureOffset(0), ());
    TEST_EQUAL(static_cast<uint64_t>(4), table->GetFeatureOffset(1), ());
    TEST_EQUAL(static_cast<uint64_t>(17), table->GetFeatureOffset(2), ());
    TEST_EQUAL(static_cast<uint64_t>(128), table->GetFeatureOffset(3), ());
    TEST_EQUAL(static_cast<uint64_t>(129), table->GetFeatureOffset(4), ());
    TEST_EQUAL(static_cast<uint64_t>(510), table->GetFeatureOffset(5), ());
    TEST_EQUAL(static_cast<uint64_t>(513), table->GetFeatureOffset(6), ());
    TEST_EQUAL(static_cast<uint64_t>(1024), table->GetFeatureOffset(7), ());

    TEST_EQUAL(static_cast<size_t>(0), table->GetFeatureIndexbyOffset(1), ());
    TEST_EQUAL(static_cast<size_t>(1), table->GetFeatureIndexbyOffset(4), ());
    TEST_EQUAL(static_cast<size_t>(2), table->GetFeatureIndexbyOffset(17), ());
    TEST_EQUAL(static_cast<size_t>(3), table->GetFeatureIndexbyOffset(128), ());
    TEST_EQUAL(static_cast<size_t>(4), table->GetFeatureIndexbyOffset(129), ());
    TEST_EQUAL(static_cast<size_t>(5), table->GetFeatureIndexbyOffset(510), ());
    TEST_EQUAL(static_cast<size_t>(6), table->GetFeatureIndexbyOffset(513), ());
    TEST_EQUAL(static_cast<size_t>(7), table->GetFeatureIndexbyOffset(1024), ());
  }

  UNIT_TEST(FeaturesOffsetsTable_CreateIfNotExistsAndLoad)
  {
    string const testFileName = "minsk-pass";

    LocalCountryFile localFile = LocalCountryFile::MakeForTesting(testFileName);
    string const indexFile = CountryIndexes::GetPath(localFile, CountryIndexes::Index::Offsets);
    FileWriter::DeleteFileX(indexFile);

    unique_ptr<FeaturesOffsetsTable> table = FeaturesOffsetsTable::CreateIfNotExistsAndLoad(localFile);
    MY_SCOPE_GUARD(deleteTestFileIndexGuard, bind(&FileWriter::DeleteFileX, cref(indexFile)));
    TEST(table.get(), ());

    uint64_t builderSize = 0;
    FilesContainerR cont(GetPlatform().GetReader(testFileName + DATA_FILE_EXTENSION));
    FeaturesVectorTest(cont).GetVector().ForEach([&builderSize](FeatureType const &, uint32_t)
    {
      ++builderSize;
    });
    TEST_EQUAL(builderSize, table->size(), ());

    table = unique_ptr<FeaturesOffsetsTable>();
    table = unique_ptr<FeaturesOffsetsTable>(FeaturesOffsetsTable::Load(indexFile));
    TEST_EQUAL(builderSize, table->size(), ());
  }

  UNIT_TEST(FeaturesOffsetsTable_ReadWrite)
  {
    string const testFileName = "test_file";
    Platform & pl = GetPlatform();

    FilesContainerR baseContainer(pl.GetReader("minsk-pass" DATA_FILE_EXTENSION));

    LocalCountryFile localFile = LocalCountryFile::MakeForTesting(testFileName);
    TEST(CountryIndexes::PreparePlaceOnDisk(localFile), ());

    string const indexFile = CountryIndexes::GetPath(localFile, CountryIndexes::Index::Offsets);
    FileWriter::DeleteFileX(indexFile);

    FeaturesOffsetsTable::Builder builder;
    FeaturesVector::ForEachOffset(baseContainer.GetReader(DATA_FILE_TAG), [&builder](uint32_t offset)
    {
      builder.PushOffset(offset);
    });

    unique_ptr<FeaturesOffsetsTable> table(FeaturesOffsetsTable::Build(builder));
    TEST(table.get(), ());
    TEST_EQUAL(builder.size(), table->size(), ());

    string const testFile = pl.WritablePathForFile(testFileName + DATA_FILE_EXTENSION);
    MY_SCOPE_GUARD(deleteTestFileGuard, bind(&FileWriter::DeleteFileX, cref(testFile)));
    MY_SCOPE_GUARD(deleteTestFileIndexGuard, bind(&FileWriter::DeleteFileX, cref(indexFile)));

    // Store table in a temporary data file.
    {
      FilesContainerW testContainer(testFile);

      // Just copy all sections except a possibly existing offsets
      // table section.
      baseContainer.ForEachTag([&baseContainer, &testContainer](string const & tag)
      {
        testContainer.Write(baseContainer.GetReader(tag), tag);
      });
      table->Save(indexFile);
      testContainer.Finish();
    }

    // Load table from the temporary data file.
    {
      unique_ptr<FeaturesOffsetsTable> loadedTable(FeaturesOffsetsTable::Load(indexFile));
      TEST(loadedTable.get(), ());

      TEST_EQUAL(table->size(), loadedTable->size(), ());
      for (uint64_t i = 0; i < table->size(); ++i)
        TEST_EQUAL(table->GetFeatureOffset(i), loadedTable->GetFeatureOffset(i), ());
    }
  }
}  // namespace feature