/*******************************************************************************
*   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.collect.tests;

import static org.junit.Assert.*;

import org.junit.Test;

import com.analog.lyric.collect.ExtendedArrayList;

/**
 * 
 * @since 0.08
 * @author Christopher Barber
 */
public class TestExtendedArrayList
{
	@Test
	public void test()
	{
		ExtendedArrayList<String> list = new ExtendedArrayList<>();
		
		assertTrue(list.isEmpty());
		assertInvariants(list);
		
		list.setSize(3);
		assertEquals(3, list.size());
		for (int i = list.size(); --i>=0;)
		{
			assertNull(list.getOrNull(i));
		}
		assertInvariants(list, null, null, null);
		
		list.set(0, "a");
		list.set(1, "b");
		list.set(2, "c");
		assertInvariants(list, "a", "b", "c");
		
		list.set(1, "B");
		assertEquals(3, list.size());
		list.set(5, "f");
		assertEquals(6, list.size());
		assertInvariants(list, "a", "B", "c", null, null, "f");
		
		list.setSize(2);
		assertEquals(2, list.size());
		assertInvariants(list, "a", "B");
		
		list.add(3, "D");
		assertEquals(4, list.size());
		assertInvariants(list, "a", "B", null, "D");
		
		list.add(2,"C");
		assertEquals(5, list.size());
		assertInvariants(list, "a","B","C",null,"D");
	}
	
	private <T> void assertInvariants(ExtendedArrayList<T> list, @SuppressWarnings("unchecked") T ... expectedContents)
	{
		int size = list.size();
		
		list.setSize(size);
		assertEquals(size, list.size());
		
		assertNull(list.getOrNull(-1));
		assertNull(list.getOrNull(size));
		
		for (int i = size; --i>=0;)
		{
			assertSame(list.get(i), list.getOrNull(i));
			assertSame(expectedContents[i], list.get(i));
		}
		
		ExtendedArrayList<T> list2 = new ExtendedArrayList<T>(list);
		assertEquals(list, list2);
	}
}
