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
|
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/vespalib/testkit/test_kit.h>
#include <vespa/vespalib/eval/value_cache/constant_value_cache.h>
#include <vespa/vespalib/eval/value_cache/constant_value.h>
#include <vespa/vespalib/eval/value.h>
#include <vespa/vespalib/eval/value_type.h>
using namespace vespalib::eval;
struct MyValue : ConstantValue {
DoubleValue my_value;
ValueType my_type;
MyValue(double val) : my_value(val), my_type(ValueType::double_type()) {}
const ValueType &type() const override { return my_type; }
const Value &value() const override { return my_value; }
};
struct MyFactory : ConstantValueFactory {
mutable size_t create_cnt = 0;
ConstantValue::UP create(const vespalib::string &path, const vespalib::string &) const override {
++create_cnt;
return std::make_unique<MyValue>(double(atoi(path.c_str())));
}
};
TEST_FF("require that values can be created", MyFactory(), ConstantValueCache(f1)) {
ConstantValue::UP res = f2.create("1", "type");
EXPECT_TRUE(res->type().is_double());
EXPECT_EQUAL(1.0, res->value().as_double());
EXPECT_EQUAL(2.0, f2.create("2", "type")->value().as_double());
EXPECT_EQUAL(3.0, f2.create("3", "type")->value().as_double());
EXPECT_EQUAL(3, f1.create_cnt);
}
TEST_FF("require that underlying values can be shared", MyFactory(), ConstantValueCache(f1)) {
auto res1 = f2.create("1", "type");
auto res2 = f2.create("2", "type");
auto res3 = f2.create("2", "type");
auto res4 = f2.create("2", "type");
EXPECT_EQUAL(1.0, res1->value().as_double());
EXPECT_EQUAL(2.0, res2->value().as_double());
EXPECT_EQUAL(2.0, res2->value().as_double());
EXPECT_EQUAL(2.0, res2->value().as_double());
EXPECT_EQUAL(2, f1.create_cnt);
}
TEST_FF("require that unused values are evicted", MyFactory(), ConstantValueCache(f1)) {
EXPECT_EQUAL(1.0, f2.create("1", "type")->value().as_double());
EXPECT_EQUAL(2.0, f2.create("2", "type")->value().as_double());
EXPECT_EQUAL(2.0, f2.create("2", "type")->value().as_double());
EXPECT_EQUAL(2.0, f2.create("2", "type")->value().as_double());
EXPECT_EQUAL(4, f1.create_cnt);
}
TEST_FF("require that type spec is part of cache key", MyFactory(), ConstantValueCache(f1)) {
auto res1 = f2.create("1", "type");
auto res2 = f2.create("2", "type_a");
auto res3 = f2.create("2", "type_b");
auto res4 = f2.create("2", "type_b");
EXPECT_EQUAL(1.0, res1->value().as_double());
EXPECT_EQUAL(2.0, res2->value().as_double());
EXPECT_EQUAL(2.0, res2->value().as_double());
EXPECT_EQUAL(2.0, res2->value().as_double());
EXPECT_EQUAL(3, f1.create_cnt);
}
TEST_MAIN() { TEST_RUN_ALL(); }
|