aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/tests/predicate/predicate_interval_store_test.cpp
blob: 7a483a1fe516fffbf8d6ea19eca126d5b6b2e73a (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
// Unit tests for predicate_interval_store.

#include <vespa/log/log.h>
LOG_SETUP("predicate_interval_store_test");

#include <vespa/searchlib/predicate/predicate_interval_store.h>

#include <vespa/searchlib/predicate/predicate_index.h>
#include <vespa/vespalib/testkit/testapp.h>
#include <vector>

using namespace search;
using namespace search::predicate;
using std::vector;

namespace {

TEST("require that empty interval list gives invalid ref") {
    PredicateIntervalStore store;
    vector<Interval> interval_list;
    auto ref = store.insert(interval_list);
    ASSERT_FALSE(ref.valid());
}

Interval single_buf;

template <typename IntervalT>
void testInsertAndRetrieve(const std::vector<IntervalT> &interval_list) {
    std::ostringstream ost;
    ost << "Type name: " << typeid(IntervalT).name() << ", intervals:";
    for (auto &i : interval_list) {
        ost << " 0x" << std::hex << i.interval;
    }
    TEST_STATE(ost.str().c_str());
    PredicateIntervalStore store;
    auto ref = store.insert(interval_list);
    ASSERT_TRUE(ref.valid());

    uint32_t size;
    IntervalT single;
    const IntervalT *intervals = store.get(ref, size, &single);
    EXPECT_EQUAL(interval_list.size(), size);
    ASSERT_TRUE(intervals);
    for (size_t i = 0; i < interval_list.size(); ++i) {
        EXPECT_EQUAL(interval_list[i], intervals[i]);
    }
}

TEST("require that single interval entry can be inserted") {
    testInsertAndRetrieve<Interval>({{0x0001ffff}});
    testInsertAndRetrieve<IntervalWithBounds>({{0x0001ffff, 0x3}});
}

TEST("require that multi-interval entry can be inserted") {
    testInsertAndRetrieve<Interval>({{0x00010001}, {0x0002ffff}});
    testInsertAndRetrieve<Interval>(
        {{0x00010001}, {0x00020002}, {0x0003ffff}});
    testInsertAndRetrieve<Interval>(
        {{0x00010001}, {0x00020002}, {0x00030003}, {0x00040004},
         {0x00050005}, {0x00060006}, {0x00070007}, {0x00080008},
         {0x0009ffff}});
    testInsertAndRetrieve<IntervalWithBounds>(
        {{0x00010001, 0x4}, {0x0002ffff, 0x10}});
    testInsertAndRetrieve<IntervalWithBounds>(
        {{0x00010001, 0x4}, {0x00020002, 0x10}, {0x00030003, 0x20},
         {0x00040004, 0x6}, {0x0005ffff, 0x7}});
}

TEST("require that multiple multi-interval entries can be retrieved") {
    PredicateIntervalStore store;
    auto ref = store.insert<Interval>({{1}, {2}});
    ASSERT_TRUE(ref.valid());
    ref = store.insert<Interval>({{3}, {4}});
    ASSERT_TRUE(ref.valid());

    uint32_t size;
    const Interval *intervals = store.get(ref, size, &single_buf);
    EXPECT_EQUAL(2u, size);
    ASSERT_TRUE(intervals);
    EXPECT_EQUAL(3u, intervals[0].interval);
    EXPECT_EQUAL(4u, intervals[1].interval);
}

/*
TEST("require that entries can be removed and reused") {
    GenerationHandler gen_handler;
    PredicateIntervalStore store(gen_handler);
    auto ref = store.insert<IntervalWithBounds>({{0x0001ffff, 5}});
    ASSERT_TRUE(ref.valid());
    store.remove(ref);

    auto ref2 = store.insert<Interval>({{1}, {2}, {3}, {4}, {5},
                                        {6}, {7}, {8}, {9}});
    ASSERT_TRUE(ref2.valid());
    store.remove(ref2);
    store.commit();

    auto ref3 = store.insert<IntervalWithBounds>({{0x0002ffff, 10}});
    ASSERT_EQUAL(ref.ref(), ref3.ref());

    uint32_t size;
    IntervalWithBounds single;
    const IntervalWithBounds *bounds = store.get(ref3, size, &single);
    EXPECT_EQUAL(1u, size);
    EXPECT_EQUAL(0x0002ffffu, bounds->interval);
    EXPECT_EQUAL(10u, bounds->bounds);

    auto ref4 = store.insert<Interval>({{2}, {3}, {4}, {5},
                                        {6}, {7}, {8}, {9}, {10}});
    ASSERT_EQUAL(ref2.ref(), ref4.ref());

    const Interval *intervals = store.get(ref4, size, &single_buf);
    EXPECT_EQUAL(9u, size);
    EXPECT_EQUAL(2u, intervals[0].interval);
    EXPECT_EQUAL(10u, intervals[8].interval);
}
*/

TEST("require that single interval entries are optimized") {
    PredicateIntervalStore store;
    auto ref = store.insert<Interval>({{0x0001ffff}});
    ASSERT_TRUE(ref.valid());
    ASSERT_EQUAL(0x0001ffffu, ref.ref());

    uint32_t size;
    const Interval *intervals = store.get(ref, size, &single_buf);
    ASSERT_EQUAL(intervals, &single_buf);
    EXPECT_EQUAL(0x0001ffffu, single_buf.interval);

    store.remove(ref);  // Should do nothing
}

TEST("require that interval refs are reused for identical data.") {
    PredicateIntervalStore store;
    auto ref = store.insert<Interval>({{0x00010001}, {0x0002ffff}});
    ASSERT_TRUE(ref.valid());
    ASSERT_EQUAL(0x02000001u, ref.ref());

    auto ref2 = store.insert<Interval>({{0x00010001}, {0x0002ffff}});
    EXPECT_EQUAL(ref.ref(), ref2.ref());

    uint32_t size;
    const Interval *intervals = store.get(ref, size, &single_buf);
    EXPECT_EQUAL(0x00010001u, intervals[0].interval);
    EXPECT_EQUAL(0x0002ffffu, intervals[1].interval);
}

}  // namespace

TEST_MAIN() { TEST_RUN_ALL(); }