// Copyright 2016 Yahoo Inc. 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 static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;

/**
 * @author geirst
 */
public class TensorTypeTestCase {

    @Test
    public void requireThatAnEmptyTensorTypeCanBeSpecified() {
        assertTensorType("tensor()");
    }

    @Test
    public void requireThatBoundIndexedDimensionsCanBeSpecified() {
        assertTensorType("tensor(x[5])");
        assertTensorType("tensor(x[5],y[10],z[100])");
        assertTensorType("tensor(x[5],y[10],z[100])", "tensor( x[5] , y[10] , z[100] )");
        assertTensorType("tensor(baR_09[10])");
    }

    @Test
    public void requireThatUnboundIndexedDimensionsCanBeSpecified() {
        assertTensorType("tensor(x[])");
        assertTensorType("tensor(x[],y[],z[])");
        assertTensorType("tensor(x[],y[],z[])", "tensor( x[] , y[] , z[] )");
        assertTensorType("tensor(baR_09[])");
    }

    @Test
    public void requireThatMappedDimensionsCanBeSpecified() {
        assertTensorType("tensor(x{})");
        assertTensorType("tensor(x{},y{},z{})");
        assertTensorType("tensor(x{},y{},z{})", "tensor( x{} , y{} , z{} )");
        assertTensorType("tensor(baR_09{})");
    }

    @Test
    public void requireThatIndexedBoundDimensionMustHaveNonZeroSize() {
        assertIllegalTensorType("tensor(x[0])", "Size of bound dimension 'x' must be at least 1");
    }

    @Test
    public void requireThatDimensionsMustHaveUniqueNames() {
        assertIllegalTensorType("tensor(x[10],y[20],x[30])", "'x[10]' and 'x[30]' have the same name");
        assertIllegalTensorType("tensor(x{},y{},x{})", "'x{}' and 'x{}' have the same name");
    }

    @Test
    public void requireThatDimensionsAreOfSameType() {
        assertIllegalTensorType("tensor(x[10],y[])", "'x[10]' does not have the same type as 'y[]'");
        assertIllegalTensorType("tensor(x[10],y{})", "'x[10]' does not have the same type as 'y{}'");
        assertIllegalTensorType("tensor(x[10],y[20],z{})", "'y[20]' does not have the same type as 'z{}'");
        assertIllegalTensorType("tensor(x[],y{})", "'x[]' does not have the same type as 'y{}'");
    }

    @Test
    public void requireThatIllegalSyntaxInSpecThrowsException() {
        assertIllegalTensorType("foo(x[10])", "Tensor type spec must start with 'tensor(' and end with ')', but was 'foo(x[10])'");
        assertIllegalTensorType("tensor(x_@[10])", "Failed parsing element 'x_@[10]' in type spec 'tensor(x_@[10])'");
        assertIllegalTensorType("tensor(x[10a])", "Failed parsing element 'x[10a]' in type spec 'tensor(x[10a])'");
        assertIllegalTensorType("tensor(x{10})", "Failed parsing element 'x{10}' in type spec 'tensor(x{10})'");
    }

    private static void assertTensorType(String typeSpec) {
        assertTensorType(typeSpec, typeSpec);
    }

    private static void assertTensorType(String expected, String typeSpec) {
        assertEquals(expected, TensorType.fromSpec(typeSpec).toString());
    }

    private static void assertIllegalTensorType(String typeSpec, String messageSubstring) {
        try {
            TensorType.fromSpec(typeSpec);
            fail("Exception exception to be thrown with message: '" + messageSubstring + "'");
        } catch (IllegalArgumentException e) {
            assertThat(e.getMessage(), containsString(messageSubstring));
        }
    }

}

@@ -1754,46 +1754,74 @@
 %22, %22
-'x%5B10%5D' and 'x%5B30%5D' have the same name
+Could not add dimension x%5B30%5D as this dimension is already present
 %22);%0A
@@ -1880,42 +1880,72 @@
 %22, %22
-'x%7B%7D' and 'x%7B%7D' have the same name
+Could not add dimension x%7B%7D as this dimension is already present
 %22);%0A
@@ -2062,372 +2062,99 @@
 r(x%5B
-10
 %5D,y
-%5B%5D
+%7B%7D
 )%22, %22
-'x%5B10%5D' does not have the same type as 'y%5B%5D'%22);%0A        assertIllegalTensorType(%22tensor(x%5B10
+%5Bx%5B
 %5D,
+ 
 y%7B%7D
-)%22, %22'x%5B10%5D' does not have the same type as 'y%7B%7D'%22);%0A        assertIllegalTensorType(%22tensor(x%5B10%5D,y%5B20%5D,z%7B%7D)%22, %22'y%5B20%5D' does not have the same type as 'z%7B%7D'%22);%0A        assertIllegalTensorType(%22tensor(x%5B%5D,y%7B%7D)%22, %22'x%5B%5D' does not have the same type as 'y%7B%7D'
+%5D contains both indexed and mapped dimensions, this is not supported yet
 %22);%0A
@@ -3163,23 +3163,23 @@
 fail(%22Ex
-ception
+poected
  excepti

Fix tests