view src/test/java/jungle/core/graph/AbstractVertexesTest.java @ 5:07b26b4b21e0

modified AbstractVertexesTest
author shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
date Sat, 23 Jun 2012 01:54:20 +0900
parents 761d04aecfcb
children 1a5eaf5ce085
line wrap: on
line source

package jungle.core.graph;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;

import javax.swing.plaf.basic.BasicScrollPaneUI.VSBChangeListener;

import junit.framework.Assert;
import junit.framework.TestCase;

public abstract class AbstractVertexesTest extends TestCase
{
	public abstract Graph newInstance();
	
	public static final String RELATION = "RELATION";
	
	public void testSize()
	{
		Graph g = newInstance();
		Vertex v = g.createVertex();
		Vertexes vs = v.createVertexes(RELATION);
		
		// first must be zero.
		Assert.assertEquals(0,vs.size());
		
		// add single , size() will be one.
		vs.add(g.createVertex());
		Assert.assertEquals(1,vs.size());
		
		// add single , size() will be two.
		vs.add(g.createVertex());
		Assert.assertEquals(2,vs.size());
		
		Vertex c3 = g.createVertex();
		vs.add(c3);
		Assert.assertEquals(3,vs.size());
		
		// remove , will be two
		vs.removeFirst(c3);
		Assert.assertEquals(2,vs.size());
	}
	
	public void testAddFromOtherGraphExceptions()
	{
		Graph g1 = newInstance();
		Graph g2 = newInstance();
		
		Vertex v1 = g1.createVertex();
		Vertex v2 = g2.createVertex(); // belongs to g2
		
		Vertexes vs = v1.createVertexes(RELATION);
		try{
			// v2 belongs to g2 , but v1 is not belongs to g2
			vs.add(v2); // should be fail here.
			Assert.fail("add(Vertex) must fail when vertex from other graph is supplied");
		}catch(IllegalArgumentException _e){
			// do nothing.
		}
		
		try{
			Collection<Vertex> c = Arrays.asList(g2.createVertex(),g2.createVertex());
			vs.add(c);
			Assert.fail("add(Collection<Vertex>) must fail when vertex from other graph is supplied");
		}catch(IllegalArgumentException _e){
			// do nothing.
		}
		
		return;
	}
	
	public void testAddSingleVertex()
	{
		Graph g = newInstance();
		Vertex v = g.createVertex();
		Vertex c = g.createVertex();
		Vertexes vs = v.createVertexes(RELATION);
		
		vs.add(c);
		
		for(Vertex i : vs){
			Assert.assertEquals(c,i);
		}
	}
	
	public void testAddExceptions()
	{
		Graph g = newInstance();
		Vertex v = g.createVertex();
		Vertexes vs = v.createVertexes(RELATION);
		
		try{
			vs.add((Vertex)null);
			Assert.fail("vs.add(Vertex) didnt throw null pointer exception");
		}catch(NullPointerException _e){
			// do nothing.
		}
		
		try{
			vs.add((Collection<Vertex>)null);
			Assert.fail("vs.add(Collection<Vertex> didnt throw null pointer exception");
		}catch(NullPointerException _e){
			return;
		}
	}
	
	public void testAddVertexCollection()
	{
		Graph g = newInstance();
		Vertex v = g.createVertex();
		Vertexes vs = v.createVertexes(RELATION);
		
		HashSet<Vertex> set = new HashSet<Vertex>();
		set.add(g.createVertex());
		set.add(g.createVertex());
		set.add(g.createVertex());
		
		vs.add(set);
		Assert.assertEquals(3,vs.size());
		
		for(Vertex x : vs){
			Assert.assertTrue("adding collection to Vertexes , but verification was failed.",set.contains(x));
		}
	}
	
	public void testAt()
	{
		Graph g = newInstance();
		Vertex v = g.createVertex();
		Vertexes vs = v.createVertexes(RELATION);
		
		Vertex c = g.createVertex();
		
		vs.add(g.createVertex());  // 0
		vs.add(g.createVertex());  // 1
		vs.add(c);  // 2
		vs.add(g.createVertex());  // 3
		vs.add(g.createVertex());  // 4
		
		Vertex two = vs.at(2);
		Assert.assertEquals(c,two);
	}
	
	public void testAtExceptions()
	{
		Graph g = newInstance();
		Vertex v = g.createVertex();
		Vertexes vs = v.createVertexes(RELATION);
		
		vs.add(g.createVertex()); // 0
		vs.add(g.createVertex()); // 1
		vs.add(g.createVertex()); // 2
		vs.add(g.createVertex()); // 3
		
		try{
			// try muinus value
			vs.at(-1);
			Assert.fail("Vertexes.at() must throw IndexOutOfBoundsException when muinus value supplied.");
		}catch(IndexOutOfBoundsException _e){
			// ???
		}
		
		try{
			// try large value , maximum index is 3 
			vs.at(4);
			Assert.fail("Vertexes.at() must throw IndexOutOfBoundsException when large value supplied.");
		}catch(IndexOutOfBoundsException _e){
			// ???
		}
	}
	
	public void testReplace()
	{
		Graph g = newInstance();
		Vertex v = g.createVertex();
		Vertexes vs = v.createVertexes(RELATION);
		Vertex a = g.createVertex("a");
		Vertex c = g.createVertex("c");
		
		vs.add(Arrays.asList(g.createVertex(),g.createVertex(),c,g.createVertex()));
		Assert.assertEquals(c,vs.replace(a,2));
		Assert.assertEquals(a,vs.at(2));
	}
	
	public void testReplaceExceptions()
	{
		Graph g = newInstance();
		Vertex v = g.createVertex();
		Vertexes vs = v.createVertexes(RELATION);
		
		Vertex a = g.createVertex("a");
		vs.add(Arrays.asList(g.createVertex(),a,g.createVertex()));
		
		try{
			vs.replace(a,-1);
			Assert.fail("Vertex.replate(Vertex,int) must throw IndexOutOfBoundsException when muinus value supplied.");
		}catch(IndexOutOfBoundsException _e){
			// nothing
		}
		
		try{
			vs.replace(a,3);
			Assert.fail("Vertex.replate(Vertex,int) must throw IndexOutOfBoundsException when large value supplied.");
		}catch(IndexOutOfBoundsException _e){
			// nothing
		}
		
		try{
			vs.replace(null,0);
			Assert.fail("Vertex.replate(Vertex,int) must throw NullPointerException when null value supplied.");
		}catch(NullPointerException _e){
			// nothing
		}
	}
	
	public void testReplaceFirst()
	{
		Graph g = newInstance();
		Vertex v = g.createVertex();
		Vertexes vs = v.createVertexes(RELATION);
		Vertex a = g.createVertex("a");
		Vertex c = g.createVertex("c");
		
		vs.add(Arrays.asList(g.createVertex(),g.createVertex(),c,g.createVertex(),c,c));
		Assert.assertTrue(vs.replaceFirst(c,a));
		Assert.assertEquals(a,vs.at(2));
		Assert.assertEquals(c,vs.at(4));
		Assert.assertEquals(c,vs.at(5));
	}
	
	public void testReplaceFirstExceptions()
	{
		Graph g = newInstance();
		Vertex v = g.createVertex();
		Vertexes vs = v.createVertexes(RELATION);
		Vertex a = g.createVertex("a");
		Vertex c = g.createVertex("c");
		
		vs.add(Arrays.asList(g.createVertex(),g.createVertex(),c,g.createVertex(),c,c));
		
		try{
			vs.replaceFirst(null,c);
			Assert.fail("Vertexes.replaceFirst(Vertex,Vertex) must throw null when null value supplied.");
		}catch(NullPointerException _e){
			// nothing.
		}
		
		try{
			vs.replaceFirst(c,null);
			Assert.fail("Vertexes.replaceFirst(Vertex,Vertex) must throw null when null value supplied.");
		}catch(NullPointerException _e){
			// nothing.
		}
	}
	
	public void testCompareAndSwap()
	{
		Graph g = newInstance();
		Vertex v = g.createVertex();
		Vertexes vs = v.createVertexes(RELATION);
		Vertex a = g.createVertex("a");
		Vertex c = g.createVertex("c");
		
		vs.add(Arrays.asList(g.createVertex(),g.createVertex(),c,g.createVertex(),c,c));
		Assert.assertTrue(vs.compareAndSwap(2,c,a));
		Assert.assertEquals(a,vs.at(2));
		Assert.assertFalse(vs.compareAndSwap(2,c,a));
	}
	
	public void testCompareAndSwapExceptions()
	{
		Graph g = newInstance();
		Vertex v = g.createVertex();
		Vertexes vs = v.createVertexes(RELATION);
		Vertex a = g.createVertex("a");
		Vertex c = g.createVertex("c");
		
		vs.add(Arrays.asList(g.createVertex(),g.createVertex(),c,g.createVertex(),c,c));
		try{
			vs.compareAndSwap(-1,a,c);
			Assert.fail("Vertexes.compareAndSwap(int,Vertex,Vertex) must throw IndexOutOfBoundsException when muinus value supplied.");
		}catch(IndexOutOfBoundsException _e){
			// nothing.
		}
		
		try{
			vs.compareAndSwap(6,a,c);
			Assert.fail("Vertexes.compareAndSwap(int,Vertex,Vertex) must throw IndexOutOfBoundsException when large value supplied.");
		}catch(IndexOutOfBoundsException _e){
			// nothing.
		}
		
		try{
			vs.compareAndSwap(2,null,c);
			Assert.fail("Vertexes.compareAndSwap(int,Vertex,Vertex) must throw NullPointerException when null value supplied.");
		}catch(NullPointerException _e){
			// nothing.
		}
		
		try{
			vs.compareAndSwap(2,c,null);
			Assert.fail("Vertexes.compareAndSwap(int,Vertex,Vertex) must throw NullPointerException when null value supplied.");
		}catch(NullPointerException _e){
			// nothing.
		}
	}
	
	public void testGetGraph()
	{
		Graph g = newInstance();
		Vertex v = g.createVertex();
		Vertexes vs = v.createVertexes(RELATION);
		
		Assert.assertEquals(g,vs.getGraph());
	}
	
	public void testRemoveSingleVertex()
	{
		Graph g = newInstance();
		Vertex v = g.createVertex();
		Vertexes vs = v.createVertexes("RELATIONS");
		
		HashSet<Vertex> set = new HashSet<Vertex>();
		set.add(g.createVertex());
		set.add(g.createVertex());
		set.add(g.createVertex());
		
		Vertex c = g.createVertex();
		vs.add(c);
		Assert.assertEquals(4,vs.size());
		
		vs.removeFirst(c);
		Assert.assertEquals(3,vs.size());
		
		for(Vertex x : vs){
			Assert.assertTrue(set.contains(x));
		}
		
		Assert.assertEquals(0,set.size());
	}
	
	public void testRemoveVertexExceptions()
	{
		Graph g = newInstance();
		Vertexes vs = g.createVertex().createVertexes("RELATIONS");
		
		vs.add(Arrays.asList(g.createVertex(),g.createVertex(),g.createVertex()));
		try{
			vs.remove(-1);
			Assert.fail("Vertexes.remove(int) must throw IndexOutOfBoundsException when muinus value supplied.");
		}catch(IndexOutOfBoundsException _e){
			// do nothing.
		}
		
		try{
			vs.remove(10);
			Assert.fail("Vertexes.remove(int) must throw IndexOutOfBoundsException when large value supplied.");
		}catch(IndexOutOfBoundsException _e){
			// do nothing.
		}
		
		try{
			vs.removeFirst((Vertex)null);
			Assert.fail("vs.remove(Vertex) didnt throw null pointer exception");
		}catch(NullPointerException _e){
			// do nothing.
		}
		
		try{
			vs.removeFirst((Vertexes)null);
			Assert.fail("vs.remove(Vertexes) didnt throw null pointer exception");
		}catch(NullPointerException _e){
			// do nothing.
		}
		
		try{
			vs.removeFirst((Collection<Vertex>)null);
			Assert.fail("vs.remove(Collection<Vertex> didnt throw null pointer exception");
		}catch(NullPointerException _e){
			return;
		}
	}
	
	public void testRemoveVertexCollection()
	{
		Graph g = newInstance();
		Vertexes vs = g.createVertex().createVertexes("RELATIONS");
		
		HashSet<Vertex> set = new HashSet<Vertex>();
		set.add(g.createVertex());
		set.add(g.createVertex());
		set.add(g.createVertex());
		
		vs.add(set);
		Assert.assertEquals(3,vs.size());
		
		vs.removeFirst(set);
		Assert.assertEquals(0,vs.size());
	}
	
	public void testContains()
	{
		Graph g = newInstance();
		Vertexes vs = g.createVertex().createVertexes("RELATIONS");
		
		Vertex v = g.createVertex();
		vs.add(v);
		vs.add(g.createVertex());
		
		Assert.assertTrue(vs.contains(v));
	}
	
	public void testContainsExceptions()
	{
		Graph g = newInstance();
		Vertexes vs = g.createVertex().createVertexes("RELATIONS");
		
		try{
			vs.contains(null);
			Assert.fail("Vertexes.contains(Vertex) must throw NullPointerException when null value supplied.");
		}catch(NullPointerException _e){
			return;
		}
	}
	
	public void testVertexType()
	{
		Graph g = newInstance();
		Vertexes vs = g.createVertex().createVertexes("RELATIONS");
		
		Assert.assertEquals("RELATIONS",vs.type());
	}
}