aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/tests/attribute/enum_comparator/enum_comparator_test.cpp
blob: 975a29180269f00c268d7cc186f734eba469d539 (plain) (blame)
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/searchlib/attribute/enumcomparator.h>
#include <vespa/searchlib/attribute/dfa_string_comparator.h>
#include <vespa/vespalib/btree/btreeroot.h>
#include <vespa/vespalib/gtest/gtest.h>
#include <vespa/vespalib/text/lowercase.h>
#include <vespa/vespalib/text/utf8.h>

#include <vespa/searchlib/attribute/enumstore.hpp>

using namespace vespalib::btree;

using vespalib::datastore::AtomicEntryRef;
using vespalib::LowerCase;
using vespalib::Utf8ReaderForZTS;

namespace vespalib::datastore {

std::ostream & operator << (std::ostream& os, const EntryRef& ref) {
    return os << "EntryRef(" << ref.ref() << ")";
}

}

namespace {

std::vector<uint32_t> as_utf32(const char* key)
{
    std::vector<uint32_t> result;
    Utf8ReaderForZTS reader(key);
    while (reader.hasMore()) {
        uint32_t code_point = reader.getChar();
        result.push_back(code_point);
    }
    return result;
}

}

namespace search {

using NumericEnumStore = EnumStoreT<int32_t>;
using FloatEnumStore = EnumStoreT<float>;
using StringEnumStore = EnumStoreT<const char*>;

using EnumIndex = IEnumStore::Index;

using TreeType = BTreeRoot<AtomicEntryRef, BTreeNoLeafData,
                           vespalib::btree::NoAggregated,
                           const vespalib::datastore::EntryComparatorWrapper>;
using NodeAllocator = TreeType::NodeAllocatorType;

using attribute::DfaStringComparator;
using vespalib::datastore::EntryComparator;


TEST(EnumComparatorTest, require_that_numeric_less_is_working)
{
    NumericEnumStore es(false, DictionaryConfig::Type::BTREE);
    EnumIndex e1 = es.insert(10);
    EnumIndex e2 = es.insert(30);
    const auto & cmp1 = es.get_comparator();
    EXPECT_TRUE(cmp1.less(e1, e2));
    EXPECT_FALSE(cmp1.less(e2, e1));
    EXPECT_FALSE(cmp1.less(e1, e1));
    auto cmp2 = es.make_comparator(20);
    EXPECT_TRUE(cmp2.less(EnumIndex(), e2));
    EXPECT_FALSE(cmp2.less(e2, EnumIndex()));
}

TEST(EnumComparatorTest, require_that_numeric_equal_is_working)
{
    NumericEnumStore es(false, DictionaryConfig::Type::BTREE);
    EnumIndex e1 = es.insert(10);
    EnumIndex e2 = es.insert(30);
    const auto & cmp1 = es.get_comparator();
    EXPECT_FALSE(cmp1.equal(e1, e2));
    EXPECT_FALSE(cmp1.equal(e2, e1));
    EXPECT_TRUE(cmp1.equal(e1, e1));
    auto cmp2 = es.make_comparator(20);
    EXPECT_FALSE(cmp2.equal(EnumIndex(), e2));
    EXPECT_FALSE(cmp2.equal(e2, EnumIndex()));
    EXPECT_TRUE(cmp2.equal(EnumIndex(), EnumIndex()));
}

TEST(EnumComparatorTest, require_that_float_less_is_working)
{
    FloatEnumStore es(false, DictionaryConfig::Type::BTREE);
    EnumIndex e1 = es.insert(10.5);
    EnumIndex e2 = es.insert(30.5);
    EnumIndex e3 = es.insert(std::numeric_limits<float>::quiet_NaN());
    const auto & cmp1 = es.get_comparator();
    EXPECT_TRUE(cmp1.less(e1, e2));
    EXPECT_FALSE(cmp1.less(e2, e1));
    EXPECT_FALSE(cmp1.less(e1, e1));
    EXPECT_TRUE(cmp1.less(e3, e1));  // nan
    EXPECT_FALSE(cmp1.less(e1, e3)); // nan
    EXPECT_FALSE(cmp1.less(e3, e3)); // nan
    auto cmp2 = es.make_comparator(20.5);
    EXPECT_TRUE(cmp2.less(EnumIndex(), e2));
    EXPECT_FALSE(cmp2.less(e2, EnumIndex()));
}

TEST(EnumComparatorTest, require_that_float_equal_is_working)
{
    FloatEnumStore es(false, DictionaryConfig::Type::BTREE);
    EnumIndex e1 = es.insert(10.5);
    EnumIndex e2 = es.insert(30.5);
    EnumIndex e3 = es.insert(std::numeric_limits<float>::quiet_NaN());
    const auto & cmp1 = es.get_comparator();
    EXPECT_FALSE(cmp1.equal(e1, e2));
    EXPECT_FALSE(cmp1.equal(e2, e1));
    EXPECT_TRUE(cmp1.equal(e1, e1));
    EXPECT_FALSE(cmp1.equal(e3, e1));  // nan
    EXPECT_FALSE(cmp1.equal(e1, e3)); // nan
    EXPECT_TRUE(cmp1.equal(e3, e3)); // nan
    auto cmp2 = es.make_comparator(20.5);
    EXPECT_FALSE(cmp2.equal(EnumIndex(), e2));
    EXPECT_FALSE(cmp2.equal(e2, EnumIndex()));
    EXPECT_TRUE(cmp2.equal(EnumIndex(), EnumIndex()));
}

TEST(EnumComparatorTest, require_that_string_less_is_working)
{
    StringEnumStore es(false, DictionaryConfig::Type::BTREE);
    EnumIndex e1 = es.insert("Aa");
    EnumIndex e2 = es.insert("aa");
    EnumIndex e3 = es.insert("aB");
    const auto & cmp1 = es.get_comparator();
    EXPECT_TRUE(cmp1.less(e1, e2)); // similar folded, fallback to regular
    EXPECT_FALSE(cmp1.less(e2, e1));
    EXPECT_FALSE(cmp1.less(e1, e1));
    EXPECT_TRUE(cmp1.less(e2, e3)); // folded compare
    EXPECT_TRUE(strcmp("aa", "aB") > 0); // regular
    auto cmp2 = es.make_comparator("AB");
    EXPECT_TRUE(cmp2.less(EnumIndex(), e3));
    EXPECT_FALSE(cmp2.less(e3, EnumIndex()));
}

TEST(EnumComparatorTest, require_that_string_equal_is_working)
{
    StringEnumStore es(false, DictionaryConfig::Type::BTREE);
    EnumIndex e1 = es.insert("Aa");
    EnumIndex e2 = es.insert("aa");
    EnumIndex e3 = es.insert("aB");
    const auto & cmp1 = es.get_comparator();
    EXPECT_FALSE(cmp1.equal(e1, e2)); // similar folded, fallback to regular
    EXPECT_FALSE(cmp1.equal(e2, e1));
    EXPECT_TRUE(cmp1.equal(e1, e1));
    EXPECT_FALSE(cmp1.equal(e2, e3)); // folded compare
    auto cmp2 = es.make_comparator("AB");
    EXPECT_FALSE(cmp2.equal(EnumIndex(), e3));
    EXPECT_FALSE(cmp2.equal(e3, EnumIndex()));
    EXPECT_TRUE(cmp2.equal(EnumIndex(), EnumIndex()));
}

TEST(EnumComparatorTest, require_that_comparator_with_tree_is_working)
{
    NumericEnumStore es(false, DictionaryConfig::Type::BTREE);
    vespalib::GenerationHandler g;
    TreeType t;
    NodeAllocator m;
    for (int32_t v = 100; v > 0; --v) {
        auto cmp = es.make_comparator(v);
        EXPECT_FALSE(t.find(AtomicEntryRef(), m, cmp).valid());
        EnumIndex idx = es.insert(v);
        t.insert(AtomicEntryRef(idx), BTreeNoLeafData(), m, cmp);
    }
    EXPECT_EQ(100u, t.size(m));
    int32_t exp = 1;
    for (TreeType::Iterator itr = t.begin(m); itr.valid(); ++itr) {
        EXPECT_EQ(exp++, es.get_value(itr.getKey().load_relaxed()));
    }
    EXPECT_EQ(101, exp);
    t.clear(m);
    m.freeze();
    m.assign_generation(g.getCurrentGeneration());
    g.incGeneration();
    m.reclaim_memory(g.get_oldest_used_generation());
}

using EnumIndexVector = std::vector<EnumIndex>;

void sort_enum_indexes(EnumIndexVector &vec, const EntryComparator &compare)
{
    std::stable_sort(vec.begin(), vec.end(), [&compare](auto& lhs, auto& rhs) { return compare.less(lhs, rhs); });
}

TEST(EnumComparatorTest, require_that_folded_less_is_working)
{
    StringEnumStore es(false, DictionaryConfig::Type::BTREE);
    EnumIndex e1 = es.insert("Aa");
    EnumIndex e2 = es.insert("aa");
    EnumIndex e3 = es.insert("aB");
    EnumIndex e4 = es.insert("Folded");
    const auto & cmp1 = es.get_folded_comparator();
    EXPECT_FALSE(cmp1.less(e1, e2)); // similar folded
    EXPECT_FALSE(cmp1.less(e2, e1)); // similar folded
    EXPECT_TRUE(cmp1.less(e2, e3)); // folded compare
    EXPECT_FALSE(cmp1.less(e3, e2)); // folded compare
    auto cmp2 = es.make_folded_comparator("fol");
    auto cmp3 = es.make_folded_comparator_prefix("fol");
    EXPECT_TRUE(cmp2.less(EnumIndex(), e4));
    EXPECT_FALSE(cmp2.less(e4, EnumIndex()));
    EXPECT_FALSE(cmp3.less(EnumIndex(), e4)); // similar when prefix
    EXPECT_FALSE(cmp3.less(e4, EnumIndex())); // similar when prefix
    // Full sort, CompareStrategy::UNCASED_THEN_CASED
    EnumIndexVector vec{e4, e3, e2, e1};
    sort_enum_indexes(vec, es.get_comparator());
    EXPECT_EQ((EnumIndexVector{e1, e2, e3, e4}), vec);
    // Partial sort, CompareStrategy::UNCASED
    EnumIndexVector vec2{e4, e3, e2, e1};
    sort_enum_indexes(vec2, cmp1);
    EXPECT_EQ((EnumIndexVector{e2, e1, e3, e4}), vec2);
    // Partial sort, CompareStrategy::UNCASED
    EnumIndexVector vec3{e4, e3, e1, e2};
    sort_enum_indexes(vec3, cmp1);
    EXPECT_EQ((EnumIndexVector{e1, e2, e3, e4}), vec3);
}

TEST(EnumComparatorTest, require_that_equal_is_working)
{
    StringEnumStore es(false, DictionaryConfig::Type::BTREE);
    EnumIndex e1 = es.insert("Aa");
    EnumIndex e2 = es.insert("aa");
    EnumIndex e3 = es.insert("aB");
    const auto & cmp1 = es.get_comparator();
    EXPECT_TRUE(cmp1.equal(e1, e1));
    EXPECT_FALSE(cmp1.equal(e1, e2));
    EXPECT_FALSE(cmp1.equal(e1, e3));
    EXPECT_FALSE(cmp1.equal(e2, e1));
    EXPECT_TRUE(cmp1.equal(e2, e2));
    EXPECT_FALSE(cmp1.equal(e2, e3));
    EXPECT_FALSE(cmp1.equal(e3, e1));
    EXPECT_FALSE(cmp1.equal(e3, e2));
    EXPECT_TRUE(cmp1.equal(e3, e3));
}

TEST(EnumComparatorTest, require_that_cased_less_is_working)
{
    StringEnumStore es(false, DictionaryConfig(DictionaryConfig::Type::BTREE, DictionaryConfig::Match::CASED));
    EnumIndex e1 = es.insert("Aa");
    EnumIndex e2 = es.insert("aa");
    EnumIndex e3 = es.insert("aB");
    EnumIndex e4 = es.insert("Folded");
    const auto & cmp1 = es.get_folded_comparator();
    EXPECT_TRUE(cmp1.less(e1, e2));
    EXPECT_FALSE(cmp1.less(e2, e1));
    EXPECT_FALSE(cmp1.less(e2, e3));
    EXPECT_TRUE(cmp1.less(e3, e2));
    auto cmp2 = es.make_folded_comparator("fol");
    auto cmp3 = es.make_folded_comparator_prefix("fol");
    EXPECT_FALSE(cmp2.less(EnumIndex(), e4)); // case mismatch
    EXPECT_TRUE(cmp2.less(e4, EnumIndex()));  // case mismatch
    EXPECT_FALSE(cmp3.less(EnumIndex(), e4)); // case mismatch
    EXPECT_TRUE(cmp3.less(e4, EnumIndex()));  // case mismatch
    auto cmp4 = es.make_folded_comparator("Fol");
    auto cmp5 = es.make_folded_comparator_prefix("Fol");
    EXPECT_TRUE(cmp4.less(EnumIndex(), e4));  // no match
    EXPECT_FALSE(cmp4.less(e4, EnumIndex())); // no match
    EXPECT_FALSE(cmp5.less(EnumIndex(), e4)); // prefix match
    EXPECT_FALSE(cmp5.less(e4, EnumIndex())); // prefix match
    // Full sort, CompareStrategy::CASED
    EnumIndexVector vec{e4, e3, e2, e1};
    sort_enum_indexes(vec, es.get_comparator());
    EXPECT_EQ((EnumIndexVector{e1, e4, e3, e2}), vec);
}

TEST(DfaStringComparatorTest, require_that_less_is_working)
{
    StringEnumStore es(false, DictionaryConfig::Type::BTREE);
    EnumIndex e1 = es.insert("Aa");
    EnumIndex e2 = es.insert("aa");
    EnumIndex e3 = es.insert("aB");
    auto aa_utf32 = as_utf32("aa");
    DfaStringComparator cmp1(es.get_data_store(), aa_utf32);
    EXPECT_FALSE(cmp1.less(EnumIndex(), e1));
    EXPECT_FALSE(cmp1.less(EnumIndex(), e2));
    EXPECT_TRUE(cmp1.less(EnumIndex(), e3));
    EXPECT_FALSE(cmp1.less(e1, EnumIndex()));
    EXPECT_FALSE(cmp1.less(e2, EnumIndex()));
    EXPECT_FALSE(cmp1.less(e3, EnumIndex()));
    auto Aa_utf32 = as_utf32("Aa");
    DfaStringComparator cmp2(es.get_data_store(), Aa_utf32);
    EXPECT_TRUE(cmp2.less(EnumIndex(), e1));
    EXPECT_TRUE(cmp2.less(EnumIndex(), e2));
    EXPECT_TRUE(cmp2.less(EnumIndex(), e3));
    EXPECT_FALSE(cmp2.less(e1, EnumIndex()));
    EXPECT_FALSE(cmp2.less(e2, EnumIndex()));
    EXPECT_FALSE(cmp2.less(e3, EnumIndex()));
}

}

GTEST_MAIN_RUN_ALL_TESTS()