// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document.datatypes;

import com.yahoo.tensor.Tensor;
import com.yahoo.tensor.TensorType;
import org.junit.Test;

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


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

    private static TensorFieldValue createFieldValue(String tensorString) {
        return new TensorFieldValue(Tensor.from(tensorString));
    }

    @Test
    public void requireThatDifferentFieldValueTypesAreNotEqual() {
        assertFalse(createFieldValue("{{x:0}:2.0}").equals(new IntegerFieldValue(5)));
    }

    @Test
    public void requireThatDifferentTensorTypesWithEmptyValuesAreNotEqual() {
        TensorFieldValue field1 = new TensorFieldValue(new TensorType.Builder().mapped("x").build());
        TensorFieldValue field2 = new TensorFieldValue(new TensorType.Builder().indexed("y").build());
        assertFalse(field1.equals(field2));
    }

    @Test
    public void requireThatDifferentTensorValuesAreNotEqual() {
        TensorFieldValue field1 = createFieldValue("{{x:0}:2.0}");
        TensorFieldValue field2 = createFieldValue("{{x:0}:3.0}");
        assertFalse(field1.equals(field2));
        assertFalse(field1.equals(new TensorFieldValue(TensorType.empty)));
    }

    @Test
    public void requireThatSameTensorValueIsEqual() {
        Tensor tensor = Tensor.from("{{x:0}:2.0}");
        TensorFieldValue field1 = new TensorFieldValue(tensor);
        TensorFieldValue field2 = new TensorFieldValue(tensor);
        assertTrue(field1.equals(field1));
        assertTrue(field1.equals(field2));
        assertTrue(field1.equals(createFieldValue("{{x:0}:2.0}")));
    }

    @Test
    public void requireThatToStringWorks() {
        TensorFieldValue field1 = createFieldValue("{{x:0}:2.0}");
        assertEquals("tensor(x{}):{0:2.0}", field1.toString());
        TensorFieldValue field2 = new TensorFieldValue(TensorType.fromSpec("tensor(x{})"));
        assertEquals("null", field2.toString());
    }
}

@@ -241,16 +241,43 @@
 it.Test;
+%0Aimport java.util.Optional;
 %0A%0Aimport
@@ -2206,10 +2206,1945 @@
 ;%0A    %7D%0A
+%0A    @Test%0A    public void requireThatSerializationHappens() %7B%0A        TensorFieldValue orig = createFieldValue(%22%7B%7Bx:0%7D:2.0%7D%22);%0A        assertEquals(%22tensor(x%7B%7D):%7B0:2.0%7D%22, orig.toString());%0A        assertTrue(orig.getWrappedValue() != null);%0A        Optional%3Cbyte%5B%5D%3E serializedBytes = orig.getSerializedTensor();%0A        assertTrue(serializedBytes.isPresent());%0A        Optional%3Cbyte%5B%5D%3E otherSerialized = orig.getSerializedTensor();%0A        assertTrue(otherSerialized.isPresent());%0A        assertTrue(serializedBytes.get() == otherSerialized.get());%0A%0A        TensorFieldValue copy = new TensorFieldValue();%0A        assertTrue(copy.getSerializedTensor().isEmpty());%0A        copy.assignSerializedTensor(serializedBytes.get());%0A        assertFalse(copy.getSerializedTensor().isEmpty());%0A        otherSerialized = copy.getSerializedTensor();%0A        assertTrue(serializedBytes.get() == otherSerialized.get());%0A%0A        copy = new TensorFieldValue();%0A        assertTrue(copy.getTensorType().isEmpty());%0A        copy.assignSerializedTensor(serializedBytes.get());%0A        assertFalse(copy.getTensorType().isEmpty());%0A        assertEquals(copy.getTensorType().get(), orig.getTensorType().get());%0A%0A        copy = new TensorFieldValue();%0A        assertTrue(copy.getTensor().isEmpty());%0A        copy.assignSerializedTensor(serializedBytes.get());%0A        assertFalse(copy.getTensor().isEmpty());%0A%0A        copy = new TensorFieldValue();%0A        assertTrue(copy.getTensor().isEmpty());%0A        copy.assignSerializedTensor(serializedBytes.get());%0A        assertFalse(copy.getTensor().isEmpty());%0A        assertEquals(copy.getTensor().get(), orig.getTensor().get());%0A%0A        copy = new TensorFieldValue();%0A        assertTrue(copy.getWrappedValue() == null);%0A        copy.assignSerializedTensor(serializedBytes.get());%0A        assertFalse(copy.getWrappedValue() == null);%0A        assertEquals(copy.getWrappedValue(), orig.getWrappedValue());%0A    %7D%0A%0A
 %7D%0A

add unit testing of lazy deserialization