summaryrefslogtreecommitdiffstats
path: root/eval/src/tests/tensor/tensor_modify_operation/tensor_modify_operation_test.cpp
blob: c3c817d28c483702d9c6bfb3445f6863fd68d7ae (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
// Copyright 2019 Oath 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/util/stringfmt.h>
#include <vespa/eval/tensor/cell_values.h>
#include <vespa/eval/tensor/sparse/sparse_tensor.h>
#include <vespa/eval/tensor/default_tensor_engine.h>
#include <vespa/eval/eval/tensor_spec.h>

using vespalib::eval::Value;
using vespalib::eval::TensorSpec;
using namespace vespalib::tensor;

namespace {

double
replace(double, double b)
{
    return b;
}

template <typename Tensor>
const Tensor *asTensor(Value &value)
{
    auto *tensor = dynamic_cast<const Tensor *>(value.as_tensor());
    ASSERT_TRUE(tensor);
    return tensor;
}

}

void checkUpdate(const TensorSpec &source, const TensorSpec &update, const TensorSpec &expect) {
    auto sourceValue = DefaultTensorEngine::ref().from_spec(source);
    auto sourceTensor = asTensor<Tensor>(*sourceValue);
    auto updateValue = DefaultTensorEngine::ref().from_spec(update);
    auto updateTensor = asTensor<SparseTensor>(*updateValue);
    const CellValues cellValues(*updateTensor);
    auto actualTensor = sourceTensor->modify(replace, cellValues);
    auto actual = actualTensor->toSpec();
    auto expectValue = DefaultTensorEngine::ref().from_spec(expect);
    auto expectTensor = asTensor<Tensor>(*expectValue);
    auto expectPadded = expectTensor->toSpec();
    EXPECT_EQUAL(actual, expectPadded);
}

TEST("require that sparse tensors can be modified") {
    checkUpdate(TensorSpec("tensor(x{},y{})")
                .add({{"x","8"},{"y","9"}}, 11)
                .add({{"x","9"},{"y","9"}}, 11),
                TensorSpec("tensor(x{},y{})")
                .add({{"x","8"},{"y","9"}}, 2),
                TensorSpec("tensor(x{},y{})")
                .add({{"x","8"},{"y","9"}}, 2)
                .add({{"x","9"},{"y","9"}}, 11));
}

TEST("require that dense tensors can be modified") {
    checkUpdate(TensorSpec("tensor(x[10],y[10])")
                .add({{"x",8},{"y",9}}, 11)
                .add({{"x",9},{"y",9}}, 11),
                TensorSpec("tensor(x{},y{})")
                .add({{"x","8"},{"y","9"}}, 2),
                TensorSpec("tensor(x[10],y[10])")
                .add({{"x",8},{"y",9}}, 2)
                .add({{"x",9},{"y",9}}, 11));
}

TEST("require that sparse tensors ignore updates to missing cells") {
    checkUpdate(TensorSpec("tensor(x{},y{})")
                .add({{"x","8"},{"y","9"}}, 11)
                .add({{"x","9"},{"y","9"}}, 11),
                TensorSpec("tensor(x{},y{})")
                .add({{"x","7"},{"y","9"}}, 2)
                .add({{"x","8"},{"y","9"}}, 2),
                TensorSpec("tensor(x{},y{})")
                .add({{"x","8"},{"y","9"}}, 2)
                .add({{"x","9"},{"y","9"}}, 11));
}

TEST("require that dense tensors ignore updates to out of range cells") {
    checkUpdate(TensorSpec("tensor(x[10],y[10])")
                .add({{"x",8},{"y",9}}, 11)
                .add({{"x",9},{"y",9}}, 11),
                TensorSpec("tensor(x{},y{})")
                .add({{"x","8"},{"y","9"}}, 2)
                .add({{"x","10"},{"y","9"}}, 2),
                TensorSpec("tensor(x[10],y[10])")
                .add({{"x",8},{"y",9}}, 2)
                .add({{"x",9},{"y",9}}, 11));
}

TEST_MAIN() { TEST_RUN_ALL(); }