aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/test/java/com/yahoo/tensor/functions/SliceTestCase.java
blob: 94a06a416a5953ec1647fbe801ec4b76bc48e377 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright Vespa.ai. 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 SliceTestCase {

    private static final double delta = 0.000001;

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

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

    @Test
    public void testSliceFunctionGeneralFormToIndexedRank2to1() {
        Tensor input = Tensor.from("tensor(key{},x[2]):{ {key:foo,x:0}:1.3, {key:foo,x:1}:1.4, {key:bar,x:0}:2.3, {key:bar,x:1}:2.4 }");
        Tensor result = new Slice<>(new ConstantTensor<>(input),
                                    List.of(new Slice.DimensionValue<>("key", "bar")))
                                .evaluate();
        assertEquals(1, result.type().rank());
        assertEquals(Tensor.from("tensor(x[2]):[2.3, 2.4]]"), result);
    }

    @Test
    public void testSliceFunctionGeneralFormToMappedRank2to1() {
        Tensor input = Tensor.from("tensor(key{},x[2]):{ {key:foo,x:0}:1.3, {key:foo,x:1}:1.4, {key:bar,x:0}:2.3, {key:bar,x:1}:2.4 }");
        Tensor result = new Slice<>(new ConstantTensor<>(input),
                                    List.of(new Slice.DimensionValue<>("x", 0)))
                                .evaluate();
        assertEquals(1, result.type().rank());
        assertEquals(Tensor.from("tensor(key{}):{{key:foo}:1.3, {key:bar}:2.3}"), result);
    }

    @Test
    public void testSliceFunctionGeneralFormToMappedRank3to1() {
        Tensor input = Tensor.from("tensor(key{},x[2],y[1]):{ {key:foo,x:0,y:0}:1.3, {key:foo,x:1,y:0}:1.4, {key:bar,x:0,y:0}:2.3, {key:bar,x:1,y:0}:2.4 }");
        Tensor result = new Slice<>(new ConstantTensor<>(input),
                                    List.of(new Slice.DimensionValue<>("x", 1),
                                            new Slice.DimensionValue<>("y", 0)))
                                .evaluate();
        assertEquals(1, result.type().rank());
        assertEquals(Tensor.from("tensor(key{}):{{key:foo}:1.4, {key:bar}:2.4}"), result);
    }

    @Test
    public void testSliceFunctionGeneralFormToMappedRank3to1ReverseDimensionOrder() {
        Tensor input = Tensor.from("tensor(key{},x[2],y[1]):{ {key:foo,x:0,y:0}:1.3, {key:foo,x:1,y:0}:1.4, {key:bar,x:0,y:0}:2.3, {key:bar,x:1,y:0}:2.4 }");
        Tensor result = new Slice<>(new ConstantTensor<>(input),
                                    List.of(new Slice.DimensionValue<>("y", 0),
                                            new Slice.DimensionValue<>("x", 1)))
                                .evaluate();
        assertEquals(1, result.type().rank());
        assertEquals(Tensor.from("tensor(key{}):{{key:foo}:1.4, {key:bar}:2.4}"), result);
    }

    @Test
    public void testSliceFunctionGeneralFormToMappedRank3to2() {
        Tensor input = Tensor.from("tensor(key{},x[2],y[1]):{ {key:foo,x:0,y:0}:1.3, {key:foo,x:1,y:0}:1.4, {key:bar,x:0,y:0}:2.3, {key:bar,x:1,y:0}:2.4 }");
        Tensor result = new Slice<>(new ConstantTensor<>(input),
                                    List.of(new Slice.DimensionValue<>("x", 1)))
                                .evaluate();
        assertEquals(2, result.type().rank());
        assertEquals(Tensor.from("tensor(key{},y[1]):{{key:foo,y:0}:1.4, {key:bar,y:0}:2.4}"), result);
    }

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

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

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

    @Test
    public void testToString() {
        Tensor input = Tensor.from("tensor(key[3]):[1.1, 2.2, 3.3]");
        assertEquals("tensor(key[3]):[1.1, 2.2, 3.3][2]",
                     new Slice<>(new ConstantTensor<>(input),
                                 List.of(new Slice.DimensionValue<>(2)))
                     .toString());
    }

}