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
|
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/searchcore/proton/matching/attribute_operation.h>
#include <vespa/searchlib/attribute/attributefactory.h>
#include <vespa/searchlib/attribute/singlenumericattribute.h>
#include <vespa/vespalib/testkit/testapp.h>
#include <vespa/log/log.h>
LOG_SETUP("attribute_operation_test");
using proton::matching::AttributeOperation;
using search::attribute::BasicType;
using search::AttributeVector;
using search::AttributeFactory;
using search::attribute::CollectionType;
using search::attribute::Config;
TEST("test legal operations on integer attribute") {
const std::vector<uint32_t> docs;
for (auto operation : {"++", "--", "+=7", "+= 7", "-=7", "*=8", "/=6", "%=7", "=3", "=-3"}) {
EXPECT_TRUE(AttributeOperation::create(BasicType::INT64, operation, docs));
}
}
TEST("test illegal operations on integer attribute") {
const std::vector<uint32_t> docs;
for (auto operation : {"", "-", "+", "+=7.1", "=a", "*=8.z", "=", "=.7", "/=0", "%=0"}) {
EXPECT_FALSE(AttributeOperation::create(BasicType::INT64, operation, docs));
}
}
TEST("test legal operations on float attribute") {
const std::vector<uint32_t> docs;
for (auto operation : {"++", "--", "+=7", "+= 7", "-=7", "*=8", "*=8.7", "*=.7", "/=6", "%=7", "=3", "=-3"}) {
EXPECT_TRUE(AttributeOperation::create(BasicType::DOUBLE, operation, docs));
}
}
TEST("test illegal operations on float attribute") {
const std::vector<uint32_t> docs;
for (auto operation : {"", "-", "+", "=a", "*=8.z", "=", "/=0", "%=0"}) {
EXPECT_FALSE(AttributeOperation::create(BasicType::DOUBLE, operation, docs));
}
}
AttributeVector::SP
createAttribute(BasicType basicType, const vespalib::string &fieldName)
{
Config cfg(basicType, CollectionType::SINGLE);
auto av = search::AttributeFactory::createAttribute(fieldName, cfg);
while (20 >= av->getNumDocs()) {
AttributeVector::DocId checkDocId(0u);
ASSERT_TRUE(av->addDoc(checkDocId));
}
av->commit();
return av;
}
template <typename T, typename A>
void verify(vespalib::stringref operation, AttributeVector & attr, T initial, T expected) {
auto & attrT = dynamic_cast<A &>(attr);
for (uint32_t docid(0); docid < attr.getNumDocs(); docid++) {
attrT.set(docid, initial);
}
std::vector<uint32_t> docs = {1,7,9,10,17,19};
auto op = AttributeOperation::create(attr.getBasicType(), operation, docs);
EXPECT_TRUE(op);
op->operator()(attr);
for (uint32_t docid(0); docid < attr.getNumDocs(); docid++) {
if (docs.empty() || (docid < docs.front())) {
EXPECT_EQUAL(initial, attrT.get(docid));
} else {
EXPECT_EQUAL(expected, attrT.get(docid));
docs.erase(docs.begin());
}
}
}
template <typename T>
void verify(vespalib::stringref operation, AttributeVector & attr, T initial, T expected) {
BasicType::Type type = attr.getBasicType();
if (type == BasicType::INT64) {
verify<int64_t, search::SingleValueNumericAttribute<search::IntegerAttributeTemplate<int64_t>>>(operation, attr, initial, expected);
} else if (type == BasicType::INT32) {
verify<int32_t, search::SingleValueNumericAttribute<search::IntegerAttributeTemplate<int32_t>>>(operation, attr, initial, expected);
} else if (type == BasicType::DOUBLE) {
verify<double , search::SingleValueNumericAttribute<search::FloatingPointAttributeTemplate<double >>>(operation, attr, initial, expected);
} else if (type == BasicType::FLOAT) {
verify<float , search::SingleValueNumericAttribute<search::FloatingPointAttributeTemplate<float >>>(operation, attr, initial, expected);
} else {
ASSERT_TRUE(false);
}
}
TEST("test all integer operations") {
auto attr = createAttribute(BasicType::INT64, "ai");
const std::vector<std::pair<const char *, int64_t>> expectedOperation = {
{"++", 8}, {"--", 6}, {"+=7", 14}, {"-=9", -2}, {"*=3", 21}, {"/=3", 2}, {"%=3", 1}
};
for (auto operation : expectedOperation) {
TEST_DO(verify<int64_t>(operation.first, *attr, 7, operation.second));
}
}
TEST("test all float operations") {
auto attr = createAttribute(BasicType::DOUBLE, "af");
const std::vector<std::pair<const char *, double>> expectedOperation = {
{"++", 8}, {"--", 6}, {"+=7.3", 14.3}, {"-=0.9", 6.1}, {"*=3.1", 21.7}, {"/=2", 3.5}, {"%=3", 7}
};
for (auto operation : expectedOperation) {
TEST_DO(verify<double>(operation.first, *attr, 7, operation.second));
}
}
TEST_MAIN() { TEST_RUN_ALL(); }
|