File: find.cpp

package info (click to toggle)
zimlib 8.1.1-0.2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,592 kB
  • sloc: cpp: 14,011; ansic: 548; python: 241; makefile: 8; sh: 5
file content (298 lines) | stat: -rw-r--r-- 8,981 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (C) 2009 Miguel Rocha
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
 * NON-INFRINGEMENT.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 *
 */

#include <zim/zim.h>
#include <zim/archive.h>
#include <zim/error.h>

#include "tools.h"

#include "gtest/gtest.h"

namespace
{
// Not found cases


using zim::unittests::getDataFilePath;
using zim::unittests::TempZimArchive;
using zim::unittests::TestItem;

// ByTitle
#if WITH_TEST_DATA
TEST(FindTests, NotFoundByTitle)
{
    for(auto& testfile:getDataFilePath("wikibooks_be_all_nopic_2017-02.zim")) {
        zim::Archive archive (testfile.path);

        auto range0 = archive.findByTitle("unkownTitle");
        auto range1 = archive.findByTitle("j/body.js");
        ASSERT_EQ(range0.begin(), range0.end());
        ASSERT_EQ(range1.begin(), range1.end());
    }
}

// By Path
TEST(FindTests, NotFoundByPath)
{
    for(auto& testfile:getDataFilePath("wikibooks_be_all_nopic_2017-02.zim")) {
        zim::Archive archive (testfile.path);

        auto range0 = archive.findByPath("unkwonUrl");
        auto range1 = archive.findByPath("U/unkwonUrl");
        auto range2 = archive.findByPath("A/unkwonUrl");
        auto range3 = archive.findByPath("X");
        auto range4 = archive.findByPath("X/");
        ASSERT_EQ(range0.begin(), range0.end());
        ASSERT_EQ(range1.begin(), range1.end());
        ASSERT_EQ(range2.begin(), range2.end());
        ASSERT_EQ(range3.begin(), range3.end());
        ASSERT_EQ(range4.begin(), range4.end());
    }
}

// Found cases

// ByTitle
TEST(FindTests, ByTitle)
{
    for(auto& testfile:getDataFilePath("wikibooks_be_all_nopic_2017-02.zim")) {
        zim::Archive archive (testfile.path);

        auto range0 = archive.findByTitle("Першая старонка");

        auto count = 0;
        for(auto& entry: range0) {
          count++;
          ASSERT_EQ(entry.getTitle().find("Першая старонка"), 0);
        }
        if (testfile.category == "withns") {
          // On the withns test file, there are two entry with this title:
          //  the entry itself and the index.html (a redirection)
          ASSERT_EQ(count, 2);
        } else {
          // On new test file, the main page redirection is store in `W` namespace,
          // so the findByTitle found only 1 entry in `C` namespace.
          ASSERT_EQ(count, 1);
        }

        auto range1 = archive.findByTitle("Украінская");

        count = 0;
        for(auto& entry: range1) {
          count++;
          ASSERT_EQ(entry.getTitle().find("Украінская"), 0);
        }
        ASSERT_EQ(count, 5);

        // Offset from end
        auto range2 = archive.findByTitle("Украінская");
        range2 = range2.offset(0, 2);
        count = 0;
        for(auto& entry: range2) {
          count++;
          ASSERT_EQ(entry.getTitle().find("Украінская"), 0);
        }
        ASSERT_EQ(count, 2);

        // Offset from start
        auto range3 = archive.findByTitle("Украінская");
        range3 = range3.offset(1, 4);
        count = 0;
        for(auto& entry: range3) {
          count++;
          ASSERT_EQ(entry.getTitle().find("Украінская"), 0);
        }
        ASSERT_EQ(count, 4);

        // Offset with more max results greater than the number of results
        auto range4 = archive.findByTitle("Украінская");
        range4 = range4.offset(0, 10);
        count = 0;
        for(auto& entry: range4) {
          count++;
          ASSERT_EQ(entry.getTitle().find("Украінская"), 0);
        }
        ASSERT_EQ(count, 5);

        // Offset with start greater than the number of results
        auto range5 = archive.findByTitle("Украінская");
        range5 = range5.offset(10, 5);
        count = 0;
        for(auto& entry: range5) {
          count++;
          ASSERT_EQ(entry.getTitle().find("Украінская"), 0);
        }
        ASSERT_EQ(count, 0);
    }
}

#define CHECK_FIND_TITLE_COUNT(prefix, expected_count) \
{ \
  auto count = 0; \
  auto range = archive.findByTitle(prefix); \
  for(auto& entry: range) { \
    count++; \
    ASSERT_EQ(entry.getTitle().find(prefix), 0); \
  } \
  ASSERT_EQ(count, expected_count); \
}

TEST(FindTests, ByTitleWithDuplicate)
{
  TempZimArchive tza("testZim");
  zim::writer::Creator creator;
  creator.startZimCreation(tza.getPath());
  creator.addItem(std::make_shared<TestItem>("article0", "text/html", "AAA", ""));
  creator.addItem(std::make_shared<TestItem>("article1", "text/html", "BB", ""));
  creator.addItem(std::make_shared<TestItem>("article2", "text/html", "BBB", ""));
  creator.addItem(std::make_shared<TestItem>("article3", "text/html", "BBB", ""));
  creator.addItem(std::make_shared<TestItem>("article4", "text/html", "BBBB", ""));
  creator.addItem(std::make_shared<TestItem>("article5", "text/html", "CCC", ""));
  creator.addItem(std::make_shared<TestItem>("article6", "text/html", "CCC", ""));
  creator.finishZimCreation();

  zim::Archive archive(tza.getPath());
  // First binary seach step will look for index 3 (0+6/2) which is a BBB,
  // but we want to be sure it returns article2 which is the start of the range "BBB*"
  CHECK_FIND_TITLE_COUNT("BBB", 3)
  CHECK_FIND_TITLE_COUNT("BB", 4)
  CHECK_FIND_TITLE_COUNT("BBBB", 1)
  CHECK_FIND_TITLE_COUNT("CCC", 2)
  CHECK_FIND_TITLE_COUNT("C", 2)
}


// By Path
TEST(FindTests, ByPath)
{
  for(auto& testfile:getDataFilePath("wikibooks_be_all_nopic_2017-02.zim", "withns")) {
    zim::Archive archive (testfile.path);

    auto range0 = archive.findByPath("A/Main_Page.html");
    auto range1 = archive.findByPath("I/s/");
    auto range2 = archive.findByPath("-/j/head.js");
    auto range3 = archive.findByPath("I");
    auto range4 = archive.findByPath("I/");
    auto range5 = archive.findByPath("");
    auto range6 = archive.findByPath("/");

    ASSERT_EQ(range0.begin()->getIndex(), 5);
    auto count = 0;
    for(auto& entry: range0) {
      count++;
      ASSERT_EQ(entry.getPath().find("A/Main_Page.html"), 0);
    }
    ASSERT_EQ(count, 1);

    ASSERT_EQ(range1.begin()->getIndex(), 78);
    count = 0;
    for(auto& entry: range1) {
      count++;
      std::cout << entry.getPath() << std::endl;
      ASSERT_EQ(entry.getPath().find("I/s/"), 0);
    }
    ASSERT_EQ(count, 31);

    ASSERT_EQ(range2.begin()->getIndex(), 2);
    count = 0;
    for(auto& entry: range2) {
      count++;
      ASSERT_EQ(entry.getPath().find("-/j/head.js"), 0);
    }
    ASSERT_EQ(count, 1);

    ASSERT_EQ(range3.begin()->getIndex(), 75);
    count = 0;
    for(auto& entry: range3) {
      count++;
      std::cout << entry.getPath() << std::endl;
      ASSERT_EQ(entry.getPath().find("I"), 0);
    }
    ASSERT_EQ(count, 34);

    ASSERT_EQ(range4.begin()->getIndex(), 75);
    count = 0;
    for(auto& entry: range4) {
      count++;
      std::cout << entry.getPath() << std::endl;
      ASSERT_EQ(entry.getPath().find("I/"), 0);
    }
    ASSERT_EQ(count, 34);

    count = 0;
    for(auto& entry: range5) {
      ASSERT_EQ(count, entry.getIndex());
      count++;
    }
    ASSERT_EQ(count, 118);

    count = 0;
    for(auto& entry: range6) {
      ASSERT_EQ(count, entry.getIndex());
      count++;
    }
    ASSERT_EQ(count, 118);
  }
}

// By Path
TEST(FindTests, ByPathNons)
{
  for(auto& testfile:getDataFilePath("wikibooks_be_all_nopic_2017-02.zim", "nons")) {
    zim::Archive archive (testfile.path);

    auto range0 = archive.findByPath("Першая_старонка.html");
    auto range1 = archive.findByPath("П");
    auto range2 = archive.findByPath("");
    auto range3 = archive.findByPath("/");

    auto count = 0;
    for(auto& entry: range0) {
      count++;
      ASSERT_EQ(entry.getPath().find("Першая_старонка.html"), 0);
    }
    ASSERT_EQ(count, 1);

    count = 0;
    for(auto& entry: range1) {
      count++;
      std::cout << entry.getPath() << std::endl;
      ASSERT_EQ(entry.getPath().find("П"), 0);
    }
    ASSERT_EQ(count, 2);

    count = 0;
    for(auto& entry: range2) {
      ASSERT_EQ(count, entry.getIndex());
      count++;
    }
    ASSERT_EQ(count, 109);

    count = 0;
    for(auto& entry: range3) {
      ASSERT_EQ(count, entry.getIndex());
      count++;
    }
    ASSERT_EQ(count, 109);
  }
}
#endif

} // namespace