/*
 *  @author Ranides Atterwim <ranides@gmail.com>
 *  @copyright Ranides Atterwim
 *  @license WTFPL
 *  @url http://ranides.net/projects/assira
 */
package net.ranides.assira.collection.map;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import net.ranides.assira.generic.Serializer;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author ranides
 */
public class MultiHashMapTest {
	
	public MultiHashMapTest() {
	}
	
	private MultiHashMap<Integer, Integer> make() {
		MultiHashMap<Integer, Integer> imap = new MultiHashMap<>();
		imap.putItem(1, 1000);
		imap.putItem(2, 2000);
		imap.putItem(3, 3000);
		imap.putItem(1, 4000);
		return imap;
	}
	
	@Test
	public void testSerialization() throws IOException {
		MultiHashMap<Integer, Integer> imap = make();
		MultiHashMap<Integer, Integer> omap = Serializer.copy(imap);
		
		assertNotSame(imap, omap);
		assertEquals(imap, omap);
	}
	
	@Test
	public void testConstruct() {
		MultiHashMap<Integer, Integer> imap = make();
		MultiHashMap<Integer,Integer> mmap = new MultiHashMap<Integer,Integer>(imap);
		
		assertEquals(imap, mmap);
	}
	
	@Test
	public void testPutItems() {
		MultiHashMap<Integer, Integer> map1 = new MultiHashMap<>();
		map1.putItem(1, 1000);
		map1.putItem(1, 1001);
		map1.putItem(1, 1002);
		map1.putItem(2, 2000);
		map1.putItem(3, 3000);
		
		Map<Integer, Integer> map2 = new HashMap<>();
		map2.put(1, 60);
		map2.put(5, 5000);
				
		MultiHashMap<Integer, Integer> map3 = new MultiHashMap<>();
		map3.putItem(1, 70);
		map3.putItem(1, 71);
		map3.putItem(1, 71);
		map3.putItem(1, 1000);
		map3.putItem(6, 4000);
		
		MultiHashMap<Integer, Integer> omap = new MultiHashMap<>();
		omap.putItems(map1);
		omap.putItems(map2);
		omap.putItems(map3);
		
		assertEquals(10, omap.multiSize());
		assertEquals(5, omap.size());
		
		assertTrue(omap.containsItem(70));
		assertTrue(omap.containsItem(1000));
		assertTrue(omap.containsItem(5000));
		assertFalse(omap.containsItem(10));
	}
	
	@Test
	public void testRemove() {
		MultiHashMap<Integer, Integer> omap = new MultiHashMap<>();
		omap.putItem(1, 10);
		omap.putItem(1, 11);
		omap.putItem(1, 12);
		omap.putItem(2, 20);
		omap.putItem(2, 21);
		omap.putItem(3, 30);
		
		assertNull(omap.removeFirst(7));
		
		Integer v1 = omap.getLast(2);
		assertEquals(v1, omap.removeLast(2));
		
		Integer v2 = omap.getFirst(1);
		assertEquals(v2, omap.removeFirst(1));
		
		assertTrue( omap.containsKey(2) );
		omap.removeFirst(2);
		assertFalse( omap.containsKey(2) );
		assertNull( omap.getFirst(2) );
		
		assertTrue( omap.containsKey(1) );
		omap.removeLast(1);
		omap.removeLast(1);
		assertFalse( omap.containsKey(1) );
		assertNull( omap.getFirst(1) );
		
		assertTrue(omap.containsKey(3));
		assertFalse(omap.removeItem(3, 100));
		assertTrue(omap.containsKey(3));
		assertTrue(omap.removeItem(3, 30));
		assertFalse(omap.containsKey(3));
	}
}
