aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/test/java/com/yahoo/tensor/MappedTensorTestCase.java
blob: 09b989e4380111cdc60feada3a058bda8055ed5d (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.tensor;

import org.junit.Test;

import java.util.Set;

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

/**
 * Basic tensor tests. Tensor operations are tested in EvaluationTestCase
 *
 * @author bratseth
 */
public class MappedTensorTestCase {

    @Test
    public void testEmpty() {
        TensorType type = new TensorType.Builder().mapped("x").build();
        Tensor empty = Tensor.Builder.of(type).build();
        assertTrue(empty instanceof MappedTensor);
        assertTrue(empty.isEmpty());
        assertEquals("tensor(x{}):{}", empty.toString());
        Tensor emptyFromString = Tensor.from(type, "{}");
        assertEquals("tensor(x{}):{}", Tensor.from("tensor(x{}):{}").toString());
        assertTrue(emptyFromString.isEmpty());
        assertTrue(emptyFromString instanceof MappedTensor);
        assertEquals(empty, emptyFromString);
    }

    @Test
    public void testOneDimensionalBuilding() {
        TensorType type = new TensorType.Builder().mapped("x").build();
        Tensor tensor = Tensor.Builder.of(type).
                cell().label("x", "0").value(1).
                cell().label("x", "1").value(2).build();
        assertEquals(Set.of("x"), tensor.type().dimensionNames());
        assertEquals("tensor(x{}):{0:1.0, 1:2.0}", tensor.toString());
    }

    @Test
    public void testTwoDimensionalBuilding() {
        TensorType type = new TensorType.Builder().mapped("x").mapped("y").build();
        Tensor tensor = Tensor.Builder.of(type).
                cell().label("x", "0").label("y", "0").value(1).
                cell().label("x", "1").label("y", "0").value(2).build();
        assertEquals(Set.of("x", "y"), tensor.type().dimensionNames());
        assertEquals("tensor(x{},y{}):{{x:0,y:0}:1.0, {x:1,y:0}:2.0}", tensor.toString());
    }

}