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
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document.update;
import com.yahoo.document.datatypes.TensorFieldValue;
import com.yahoo.tensor.Tensor;
import com.yahoo.tensor.TensorType;
import com.yahoo.yolean.Exceptions;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
public class TensorRemoveUpdateTest {
@Test
public void apply_remove_update_operations() {
assertApplyTo("{{x:0,y:0}:2, {x:0,y:1}:3}", "{{x:0,y:1}:1}", "{{x:0,y:0}:2}");
}
private void assertApplyTo(String init, String update, String expected) {
String spec = "tensor(x{},y{})";
TensorFieldValue initialFieldValue = new TensorFieldValue(Tensor.from(spec, init));
TensorRemoveUpdate removeUpdate = new TensorRemoveUpdate(new TensorFieldValue(Tensor.from(spec, update)));
TensorFieldValue updatedFieldValue = (TensorFieldValue) removeUpdate.applyTo(initialFieldValue);
assertEquals(Tensor.from(spec, expected), updatedFieldValue.getTensor().get());
}
@Test
public void verify_compatible_type_throws_on_mismatch() {
// Contains an indexed dimension, which is not allowed.
illegalTensorRemoveUpdate("tensor(x{},y[1])", "{{x:a,y:0}:1}", "tensor(x{},y[1])",
"Unexpected type 'tensor(x{},y[1])' in remove update. Expected dimensions to be a subset of 'tensor(x{})'");
// Sparse dimension is not found in the original type.
illegalTensorRemoveUpdate("tensor(y{})", "{{y:a}:1}", "tensor(x{},z[2])",
"Unexpected type 'tensor(y{})' in remove update. Expected dimensions to be a subset of 'tensor(x{})'");
}
private void illegalTensorRemoveUpdate(String updateType, String updateTensor, String originalType, String expectedMessage) {
try {
var value = new TensorFieldValue(Tensor.from(updateType, updateTensor));
new TensorRemoveUpdate(value).verifyCompatibleType(TensorType.fromSpec(originalType));
fail("Expected exception");
}
catch (IllegalArgumentException expected) {
assertEquals(expectedMessage, Exceptions.toMessageString(expected));
}
}
}
|