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

import static com.yahoo.tensor.TensorAddress.of;
import static com.yahoo.tensor.TensorAddress.ofLabels;

import org.junit.jupiter.api.Test;

import java.util.Arrays;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;

/**
 * Test for tensor address.
 *
 * @author baldersheim
 */
public class TensorAddressTestCase {
    public static void equal(TensorAddress a, TensorAddress b) {
        assertEquals(a.hashCode(), b.hashCode());
        assertEquals(a, b);
        assertEquals(a.size(), b.size());
        for (int i = 0; i < a.size(); i++) {
            assertEquals(a.label(i), b.label(i));
            assertEquals(a.numericLabel(i), b.numericLabel(i));
        }
    }
    public static void notEqual(TensorAddress a, TensorAddress b) {
        assertNotEquals(a.hashCode(), b.hashCode()); // This might not hold, but is bad if not very rare
        assertNotEquals(a, b);
    }
    @Test
    void testStringVersusNumericAddressEquality() {
        equal(ofLabels("1"), of(1));
    }
    @Test
    void testInEquality() {
        notEqual(ofLabels("1"), ofLabels("2"));
        notEqual(of(1), of(2));
    }
    @Test
    void testDimensionsEffectsEqualityAndHash() {
        notEqual(ofLabels("1"), ofLabels("1", "1"));
        notEqual(of(1), of(1, 1));
    }
    @Test
    void testAllowNullDimension() {
        TensorAddress s1 = ofLabels("1", null, "2");
        TensorAddress s2 = ofLabels("1", "2");
        assertNotEquals(s1, s2);
        assertEquals(-1, s1.numericLabel(1));
        assertEquals(null, s1.label(1));
    }

    private static void verifyWithLabel(int dimensions) {
        int [] indexes = new int[dimensions];
        Arrays.fill(indexes, 1);
        TensorAddress next = of(indexes);
        for (int i = 0; i < dimensions; i++) {
            indexes[i] = 3;
            assertEquals(of(indexes), next = next.withLabel(i, 3));
        }
    }
    @Test
    void testWithLabel() {
        for (int i=0; i < 10; i++) {
            verifyWithLabel(i);
        }
    }

}