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

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

#include <vespa/searchlib/query/tree/intermediatenodes.h>
#include <vespa/searchlib/query/tree/templatetermvisitor.h>
#include <vespa/searchlib/query/tree/simplequery.h>
#include <vespa/searchlib/query/tree/termnodes.h>
#include <vespa/vespalib/testkit/testapp.h>

using namespace search::query;

namespace {

class MyVisitor;

class Test : public vespalib::TestApp {
    void requireThatAllTermsCanBeVisited();

public:
    int Main() override;
};

int
Test::Main()
{
    TEST_INIT("templatetermvisitor_test");

    TEST_DO(requireThatAllTermsCanBeVisited());

    TEST_DONE();
}

class MyVisitor : public TemplateTermVisitor<MyVisitor, SimpleQueryNodeTypes>
{
public:
    template <typename T>
    bool &isVisited() {
        static bool b;
        return b;
    }

    template <class TermType>
    void visitTerm(TermType &) { isVisited<TermType>() = true; }
};

template <class T>
bool checkVisit(T *q) {
    Node::UP query(q);
    MyVisitor visitor;
    visitor.isVisited<T>() = false;
    query->accept(visitor);
    return visitor.isVisited<T>();
}

template <class T>
bool checkVisit() {
    return checkVisit(new T(typename T::Type(), "field", 0, Weight(0)));
}

void Test::requireThatAllTermsCanBeVisited() {
    EXPECT_TRUE(checkVisit<SimpleNumberTerm>());
    EXPECT_TRUE(checkVisit<SimpleLocationTerm>());
    EXPECT_TRUE(checkVisit<SimplePrefixTerm>());
    EXPECT_TRUE(checkVisit<SimpleRangeTerm>());
    EXPECT_TRUE(checkVisit<SimpleStringTerm>());
    EXPECT_TRUE(checkVisit<SimpleSubstringTerm>());
    EXPECT_TRUE(checkVisit<SimpleSuffixTerm>());
    EXPECT_TRUE(checkVisit<SimplePredicateQuery>());
    EXPECT_TRUE(checkVisit<SimpleRegExpTerm>());
    EXPECT_TRUE(checkVisit(new SimplePhrase("field", 0, Weight(0))));
    EXPECT_TRUE(checkVisit(new SimpleSameElement("foo", 0, Weight(0))));
    EXPECT_TRUE(!checkVisit(new SimpleAnd));
    EXPECT_TRUE(!checkVisit(new SimpleAndNot));
    EXPECT_TRUE(!checkVisit(new SimpleEquiv(17, Weight(100))));
    EXPECT_TRUE(!checkVisit(new SimpleNear(2)));
    EXPECT_TRUE(!checkVisit(new SimpleONear(2)));
    EXPECT_TRUE(!checkVisit(new SimpleOr));
    EXPECT_TRUE(!checkVisit(new SimpleRank));
}

}  // namespace

TEST_APPHOOK(Test);