aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/test/java/com/yahoo/tensor/functions/ValueTestCase.java
blob: ffb5e1433ca897c408079a250577a0b3cc671496 (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
// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.tensor.functions;

import com.yahoo.tensor.Tensor;
import org.junit.Test;

import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

/**
 * @author bratseth
 */
public class ValueTestCase {

    private static final double delta = 0.000001;

    @Test
    public void testValueFunctionGeneralForm() {
        Tensor input = Tensor.from("tensor(key{},x{}):{ {key:foo,x:0}:1.4, {key:bar,x:0}:2.3 }");
        Tensor result = new Value(new ConstantTensor(input),
                                  List.of(new Value.DimensionValue("key", "bar"),
                                          new Value.DimensionValue("x", 0)))
                                .evaluate();
        assertEquals(0, result.type().rank());
        assertEquals(2.3, result.asDouble(), delta);
    }

    @Test
    public void testValueFunctionSingleMappedDimension() {
        Tensor input = Tensor.from("tensor(key{}):{ {key:foo}:1.4, {key:bar}:2.3 }");
        Tensor result = new Value(new ConstantTensor(input),
                                  List.of(new Value.DimensionValue("foo")))
                                .evaluate();
        assertEquals(0, result.type().rank());
        assertEquals(1.4, result.asDouble(), delta);
    }

    @Test
    public void testValueFunctionSingleIndexedDimension() {
        Tensor input = Tensor.from("tensor(key[3]):[1.1, 2.2, 3.3]");
        Tensor result = new Value(new ConstantTensor(input),
                                  List.of(new Value.DimensionValue(2)))
                                .evaluate();
        assertEquals(0, result.type().rank());
        assertEquals(3.3, result.asDouble(), delta);
    }

    @Test
    public void testValueFunctionShortFormWithMultipleDimensionsIsNotAllowed() {
        try {
            Tensor input = Tensor.from("tensor(key{},x{}):{ {key:foo,x:0}:1.4, {key:bar,x:0}:2.3 }");
            new Value(new ConstantTensor(input),
                      List.of(new Value.DimensionValue("bar"),
                              new Value.DimensionValue(0)))
                    .evaluate();
            fail("Expected exception");
        }
        catch (IllegalArgumentException e) {
            assertEquals("Short form of cell addresses is only supported with a single dimension: Specify dimension names explicitly",
                         e.getMessage());
        }
    }

}