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

import com.google.common.collect.Sets;
import junit.framework.TestCase;
import org.junit.Test;

import java.util.Set;

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

/**
 * 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();
        TestCase.assertTrue(empty instanceof MappedTensor);
        TestCase.assertTrue(empty.isEmpty());
        assertEquals("tensor(x{}):{}", empty.toString());
        Tensor emptyFromString = Tensor.from(type, "{}");
        assertEquals("tensor(x{}):{}", Tensor.from("tensor(x{}):{}").toString());
        TestCase.assertTrue(emptyFromString.isEmpty());
        TestCase.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(Sets.newHashSet("x"), tensor.type().dimensionNames());
        assertEquals("{{x:0}:1.0,{x: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(Sets.newHashSet("x", "y"), tensor.type().dimensionNames());
        assertEquals("{{x:0,y:0}:1.0,{x:1,y:0}:2.0}", tensor.toString());
    }

}