/*******************************************************************************
*   Copyright 2015 Analog Devices, Inc.
*
*   Licensed under the Apache License, Version 2.0 (the "License");
*   you may not use this file except in compliance with the License.
*   You may obtain a copy of the License at
*
*       http://www.apache.org/licenses/LICENSE-2.0
*
*   Unless required by applicable law or agreed to in writing, software
*   distributed under the License is distributed on an "AS IS" BASIS,
*   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*   See the License for the specific language governing permissions and
*   limitations under the License.
********************************************************************************/

package com.analog.lyric.dimple.test.data;

import static com.analog.lyric.util.test.ExceptionTester.*;
import static org.junit.Assert.*;

import java.util.AbstractMap;

import org.junit.Test;

import com.analog.lyric.dimple.data.DataEntry;
import com.analog.lyric.dimple.data.IDatum;
import com.analog.lyric.dimple.model.core.IFactorGraphChild;
import com.analog.lyric.dimple.model.values.RealValue;
import com.analog.lyric.dimple.model.values.Value;
import com.analog.lyric.dimple.model.variables.Real;
import com.analog.lyric.dimple.model.variables.Variable;
import com.analog.lyric.dimple.test.DimpleTestBase;

/**
 * Unit tests for {@link DataEntry}
 * @since 0.08
 * @author Christopher Barber
 */
public class TestDataEntry extends DimpleTestBase
{
	@Test
	public void test()
	{
		Real a = new Real();
		
		DataEntry<Variable,Value> entry = new DataEntry<Variable,Value>(a, null);
		assertInvariants(entry);
		assertSame(a, entry.getKey());
		assertNull(entry.getValue());
		
		Value val = RealValue.create(2);
		DataEntry<Variable, ? extends IDatum> entry2 = new DataEntry<Variable,Value>(a, val);
		assertInvariants(entry2);
		assertNotEquals(entry, entry2);
	}
	
	private <K extends IFactorGraphChild, D extends IDatum> void assertInvariants(DataEntry<K,D> entry)
	{
		assertTrue(entry.equals(entry));
		assertFalse(entry.equals("foo"));
		
		assertTrue(entry.equals(new AbstractMap.SimpleEntry<>(entry.getKey(), entry.getValue())));
		assertFalse(entry.equals(new AbstractMap.SimpleEntry<>(new Real(), entry.getValue())));
		assertFalse(entry.equals(new AbstractMap.SimpleEntry<>(entry.getKey(), RealValue.create(Double.NaN))));
		
		assertEquals(entry.hashCode(), new AbstractMap.SimpleEntry<>(entry.getKey(), entry.getValue()).hashCode());
		
		expectThrow(UnsupportedOperationException.class, entry, "setValue", RealValue.create());
	}
}
