File: dirent_lookup.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 (188 lines) | stat: -rw-r--r-- 6,403 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
/*
 * Copyright (C) 2020 Matthieu Gautier <mgautier@kymeria.fr>
 *
 * 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 "../src/dirent_lookup.h"
#include "../src/_dirent.h"
#include <zim/zim.h>

#include "gtest/gtest.h"

#include <vector>
#include <string>
#include <utility>

namespace
{

const std::vector<std::pair<char, std::string>> articleurl = {
  {'A', "aa"},       //0
  {'A', "aaaa"},     //1
  {'A', "aaaaaa"},   //2
  {'A', "aaaabb"},   //3
  {'A', "aaaacc"},   //4
  {'A', "aabbaa"},   //5
  {'A', "aabbbb"},   //6
  {'A', "aabbcc"},   //7
  {'A', "cccccc"},   //8
  {'M', "foo"},      //9
  {'a', "aa"},       //10
  {'a', "bb"},       //11
  {'b', "aa"}        //12
};

struct GetDirentMock
{
  typedef GetDirentMock DirentAccessorType;
  typedef zim::entry_index_t index_t;
  static const std::string& getDirentKey(const zim::Dirent& d) {
    return d.getUrl();
  }

  zim::entry_index_t getDirentCount() const {
    return zim::entry_index_t(articleurl.size());
  }

  std::shared_ptr<const zim::Dirent> getDirent(zim::entry_index_t idx) const {
    auto info = articleurl.at(idx.v);
    auto ret = std::make_shared<zim::Dirent>();
    ret->setUrl(info.first, info.second);
    return ret;
  }
};

class NamespaceBoundaryTest : public :: testing::Test
{
  protected:
    GetDirentMock dirents;
};

TEST_F(NamespaceBoundaryTest, BeginOffset)
{
  ASSERT_EQ(zim::getNamespaceBeginOffset(dirents, 'a').v, 10);
  ASSERT_EQ(zim::getNamespaceBeginOffset(dirents, 'b').v, 12);
  ASSERT_EQ(zim::getNamespaceBeginOffset(dirents, 'c').v, 13);
  ASSERT_EQ(zim::getNamespaceBeginOffset(dirents, 'A'-1).v, 0);
  ASSERT_EQ(zim::getNamespaceBeginOffset(dirents, 'A').v, 0);
  ASSERT_EQ(zim::getNamespaceBeginOffset(dirents, 'M').v, 9);
  ASSERT_EQ(zim::getNamespaceBeginOffset(dirents, 'U').v, 10);
}

TEST_F(NamespaceBoundaryTest, EndOffset)
{
  ASSERT_EQ(zim::getNamespaceEndOffset(dirents, 'a').v, 12);
  ASSERT_EQ(zim::getNamespaceEndOffset(dirents, 'b').v, 13);
  ASSERT_EQ(zim::getNamespaceEndOffset(dirents, 'c').v, 13);
  ASSERT_EQ(zim::getNamespaceEndOffset(dirents, 'A'-1).v, 0);
  ASSERT_EQ(zim::getNamespaceEndOffset(dirents, 'A').v, 9);
  ASSERT_EQ(zim::getNamespaceEndOffset(dirents, 'M').v, 10);
  ASSERT_EQ(zim::getNamespaceEndOffset(dirents, 'U').v, 10);
}

TEST_F(NamespaceBoundaryTest, EndEqualsStartOfNext)
{
  for (char ns=32; ns<127; ns++){
    std::cout << "ns: " << ns << "|" << (int)ns << std::endl;
    ASSERT_EQ(zim::getNamespaceEndOffset(dirents, ns).v, zim::getNamespaceBeginOffset(dirents, ns+1).v);
  }
}


class DirentLookupTest : public :: testing::Test
{
  protected:
    GetDirentMock dirents;
};

typedef zim::DirentLookup<GetDirentMock> DirentLookupType;

// Provide access to protected functionality in order to unit-test it
struct UnprotectedDirentLookup : DirentLookupType
{
  template<typename... T> UnprotectedDirentLookup(const T&... args)
    : DirentLookupType(args...)
  {}

  using DirentLookupType::compareWithDirentAt;
};


TEST_F(DirentLookupTest, compareWithDirentAt)
{
  UnprotectedDirentLookup direntLookup(&dirents);

  // Dirent at index 9 is {'M', "foo"}
  EXPECT_LE(direntLookup.compareWithDirentAt('A', "foo", 9), 0);
  EXPECT_LE(direntLookup.compareWithDirentAt('M', "boo", 9), 0);
  EXPECT_EQ(direntLookup.compareWithDirentAt('M', "foo", 9), 0);
  EXPECT_GE(direntLookup.compareWithDirentAt('M', "for", 9), 0);
  EXPECT_GE(direntLookup.compareWithDirentAt('N', "foo", 9), 0);
}


#define CHECK_FIND_RESULT(expr, is_exact_match, expected_value) \
  { \
    const auto findResult = expr; \
    ASSERT_EQ(findResult.first, is_exact_match); \
    ASSERT_EQ(findResult.second.v, expected_value); \
  }

TEST_F(DirentLookupTest, ExactMatch)
{
  zim::DirentLookup<GetDirentMock> direntLookup(&dirents);
  zim::FastDirentLookup<GetDirentMock> fast_direntLookup(&dirents, 4);

#define CHECK_EXACT_MATCH(expr, expected_value)         \
  CHECK_FIND_RESULT(expr,        true, expected_value); \
  CHECK_FIND_RESULT(fast_##expr, true, expected_value);

  CHECK_EXACT_MATCH(direntLookup.find('A', "aa"), 0);
  CHECK_EXACT_MATCH(direntLookup.find('a', "aa"), 10);
  CHECK_EXACT_MATCH(direntLookup.find('A', "aabbbb"), 6);
  CHECK_EXACT_MATCH(direntLookup.find('b', "aa"), 12);

#undef CHECK_EXACT_MATCH
}


TEST_F(DirentLookupTest, NoExactMatch)
{
  zim::DirentLookup<GetDirentMock> direntLookup(&dirents);
  zim::FastDirentLookup<GetDirentMock> fast_direntLookup(&dirents, 4);

#define CHECK_NOEXACT_MATCH(expr, expected_value)        \
  CHECK_FIND_RESULT(expr,        false, expected_value); \
  CHECK_FIND_RESULT(fast_##expr, false, expected_value);

  CHECK_NOEXACT_MATCH(direntLookup.find('A', "ABC"), 0);
  CHECK_NOEXACT_MATCH(direntLookup.find('U', "aa"), 10); // No U namespace => return 10 (the index of the first item from the next namespace)
  CHECK_NOEXACT_MATCH(direntLookup.find('A', "aabb"), 5); // aabb is between aaaacc (4) and aabbaa (5) => 5
  CHECK_NOEXACT_MATCH(direntLookup.find('A', "aabbb"), 6); // aabbb is between aabbaa (5) and aabbbb (6) => 6
  CHECK_NOEXACT_MATCH(direntLookup.find('A', "aabbbc"), 7); // aabbbc is between aabbbb (6) and aabbcc (7) => 7
  CHECK_NOEXACT_MATCH(direntLookup.find('A', "bb"), 8); // bb is between aabbcc (7) and cccccc (8) => 8
  CHECK_NOEXACT_MATCH(direntLookup.find('A', "dd"), 9); // dd is after cccccc (8) => 9
  CHECK_NOEXACT_MATCH(direntLookup.find('M', "f"), 9); // f is before foo (9) => 9
  CHECK_NOEXACT_MATCH(direntLookup.find('M', "bar"), 9); // bar is before foo (9) => 9
  CHECK_NOEXACT_MATCH(direntLookup.find('M', "foo1"), 10); // foo1 is after foo (9) => 10
  CHECK_NOEXACT_MATCH(direntLookup.find('z', "zz"), 13);

#undef CHECK_NOEXACT_MATCH
}


}  // namespace