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

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

#include <vespa/document/predicate/predicate.h>
#include <vespa/document/predicate/predicate_builder.h>
#include <vespa/vespalib/data/slime/slime.h>
#include <vespa/vespalib/testkit/testapp.h>

using std::string;
using vespalib::Slime;
using vespalib::slime::Cursor;
using namespace document;

namespace {

TEST("require that a predicate tree can be built from a slime object") {
    const string feature_name = "feature name";
    Slime input;
    Cursor &obj = input.setObject();
    obj.setLong(Predicate::NODE_TYPE, Predicate::TYPE_DISJUNCTION);
    Cursor &children = obj.setArray(Predicate::CHILDREN);
    {
        Cursor &child = children.addObject();
        child.setLong(Predicate::NODE_TYPE, Predicate::TYPE_FEATURE_SET);
        child.setString(Predicate::KEY, feature_name);
        Cursor &arr = child.setArray(Predicate::SET);
        arr.addString("foo");
        arr.addString("bar");
    }
    {
        Cursor &child = children.addObject();
        child.setLong(Predicate::NODE_TYPE, Predicate::TYPE_CONJUNCTION);
        Cursor &and_children = child.setArray(Predicate::CHILDREN);
        {
            Cursor &and_child = and_children.addObject();
            and_child.setLong(Predicate::NODE_TYPE,
                              Predicate::TYPE_FEATURE_RANGE);
            and_child.setString(Predicate::KEY, feature_name);
            and_child.setLong(Predicate::RANGE_MIN, 42);
        }
        {
            Cursor &and_child = and_children.addObject();
            and_child.setLong(Predicate::NODE_TYPE, Predicate::TYPE_NEGATION);
            Cursor &not_child =
                and_child.setArray(Predicate::CHILDREN).addObject();
            {
                not_child.setLong(Predicate::NODE_TYPE,
                                  Predicate::TYPE_FEATURE_SET);
                not_child.setString(Predicate::KEY, feature_name);
                Cursor &arr = not_child.setArray(Predicate::SET);
                arr.addString("baz");
                arr.addString("qux");
            }
        }
    }

    PredicateNode::UP node = PredicateBuilder().build(input.get());
    Disjunction *disjunction = dynamic_cast<Disjunction *>(node.get());
    ASSERT_TRUE(disjunction);
    ASSERT_EQUAL(2u, disjunction->getSize());
    FeatureSet *feature_set = dynamic_cast<FeatureSet *>((*disjunction)[0]);
    ASSERT_TRUE(feature_set);
    Conjunction *conjunction = dynamic_cast<Conjunction *>((*disjunction)[1]);
    ASSERT_TRUE(conjunction);
    ASSERT_EQUAL(2u, conjunction->getSize());
    FeatureRange *feature_range =
        dynamic_cast<FeatureRange *>((*conjunction)[0]);
    ASSERT_TRUE(feature_range);
    Negation *negation = dynamic_cast<Negation *>((*conjunction)[1]);
    ASSERT_TRUE(negation);
    feature_set = dynamic_cast<FeatureSet *>(&negation->getChild());
    ASSERT_TRUE(feature_set);
}

}  // namespace

TEST_MAIN() { TEST_RUN_ALL(); }