package debug.tests;
import static org.junit.Assert.*;
import java.util.Vector;
import org.junit.Test;
import general.datastructures.PriorityNode;
import general.datastructures.PriorityQueue;
import general.datastructures.Rectangle;
import general.datastructures.Vector2f;
import general.datastructures.Waypoint;
import general.exceptions.EmptyListException;
/**
* Testing class, which should test all Classes and Methods of the Package datastructures
*
* @author Tim
* @version 0.5.0
* @since 0.4.1
*/
public class Test_Datastructures {
/*
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
*/
@Test
public void testVector2f() {
Vector2f vec1 = new Vector2f(5, 7);
assertEquals(5,vec1.x(),0.0001);
assertEquals(7,vec1.y(),0.0001);
Vector2f vec2 = new Vector2f(5);
assertTrue(vec1.isInRange(vec2, 2.0001f));
assertFalse(vec1.isInRange(vec2, 1.9999f));
assertEquals(Math.sqrt(50), vec2.length(), 0.0001);
Vector2f vec3 = new Vector2f(vec1);
assertEquals(vec1, vec3);
vec3.add(vec1);
assertEquals(10, vec3.x(),0.0001);
assertEquals(14, vec3.y(),0.0001);
Vector2f vec4 = new Vector2f(4,0);
vec4.normalize();
assertEquals(1, vec4.length(), 0.0001);
vec4.scale(3f);
assertEquals(3, vec4.length(), 0.0001);
}
@Test
public void testRectangle()
{
Rectangle r;
r = new Rectangle(2, 3, 4, 5);
assertNotNull(r);
assertEquals(2, r.getLeft(),0.0001);
assertEquals(3, r.getRight(),0.0001);
assertEquals(4, r.getTop(),0.0001);
assertEquals(5, r.getBottom(),0.0001);
}
@Test
public void testWaypoint()
{
Waypoint wp1, wp2, wp3, wp4, wp5;
wp1 = new Waypoint(new Vector2f(3, 4), 0);
assertNotNull(wp1);
assertNull(wp1.getPredecessor());
assertEquals(new Vector2f(3,4),wp1.getVector2f());
assertEquals(0.0,wp1.getDistance(),1e-4);
wp2 = new Waypoint(new Vector2f(3,8), 4, wp1);
assertNotNull(wp2);
assertEquals(wp1,wp2.getPredecessor());
wp3 = new Waypoint(new Vector2f(1,8), 2, wp2);
assertEquals(6, wp3.getDistance(),1e-4);
Vector<Vector2f> points = new Vector<Vector2f>();
Vector<Vector2f> actual_points = new Vector<Vector2f>();
points = wp3.getRouteList(points);
assertNotSame(actual_points,points);
actual_points.add(wp3.getVector2f());
actual_points.add(wp2.getVector2f());
actual_points.add(wp1.getVector2f());
assertEquals(actual_points, points);
wp4 = new Waypoint(new Vector2f(3,4), 1, wp2);
wp5 = new Waypoint(new Vector2f(2,4), 0);
assertTrue(wp1.equals(wp1));
assertTrue(wp1.equals(wp4));
assertFalse(wp1.equals(wp2));
assertFalse(wp1.equals(wp5));
}
@Test
public void testPriorityNode()
{
Waypoint wp1,wp2,wp3,wp4;
wp1 = new Waypoint(new Vector2f(1, 1), 1);
wp2 = new Waypoint(new Vector2f(2, 2), 2);
wp3 = new Waypoint(new Vector2f(3, 3), 3);
wp4 = new Waypoint(new Vector2f(4, 4), 4);
PriorityNode n1, n2, n3, n4;
n1 = new PriorityNode(2, wp1);
assertNotNull(n1);
assertEquals(2, n1.getPriority(),1e-4);
assertEquals(wp1, n1.getWaypoint());
assertNull(n1.getNext());
assertTrue(n1.contains(wp1));
n3 = new PriorityNode(5, wp4);
n1.setNext(n3);
assertNotNull(n1.getNext());
assertTrue(n1.insert(0, wp2));
n2 = new PriorityNode(1, wp2, n1);
assertNotNull(n2.getNext());
assertTrue(n2.contains(wp1));
assertFalse(n2.insert(3, wp3));
assertTrue(n2.remove(wp1));
assertEquals(3, n2.getDistanceTo(wp3),1e-4);
assertEquals(Double.MAX_VALUE, n2.getDistanceTo(wp1),1);
assertEquals(1 , n1.compareTo(n2));
assertEquals(-1, n1.compareTo(n3));
assertEquals(0, n1.compareTo(n1));
n4 = new PriorityNode(5, wp1);
assertTrue(n1.equals(n4));
assertFalse(n1.equals(n2));
}
@Test(expected = EmptyListException.class)
public void testPriorityQueueEmpty() throws EmptyListException
{
PriorityQueue pq;
pq = new PriorityQueue();
assertNotNull(pq);
assertTrue(pq.isEmpty());
pq.getFirst();
}
@Test
public void testPriorityQueueInsert() throws EmptyListException
{
PriorityQueue pq = new PriorityQueue();
Waypoint wp1, wp2, wp3, wp4, wp5;
wp1 = new Waypoint(new Vector2f(1,1), 1);
wp2 = new Waypoint(new Vector2f(2,2), 2);
wp3 = new Waypoint(new Vector2f(3,3), 3);
wp4 = new Waypoint(new Vector2f(4,4), 4);
wp5 = new Waypoint(new Vector2f(5,5), 5);
pq.insert(wp2, 2);
assertFalse(pq.isEmpty());
pq.insert(wp4, 4);
assertFalse(pq.isEmpty());
pq.insert(wp5, 5);
assertFalse(pq.isEmpty());
pq.insert(wp3, 6);
assertFalse(pq.isEmpty());
pq.insert(wp1, 1);
assertFalse(pq.isEmpty());
pq.insert(wp3, 3);
assertEquals(wp1,pq.getFirst());
assertEquals(wp2,pq.getFirst());
assertEquals(wp3,pq.getFirst());
assertEquals(wp4,pq.getFirst());
assertEquals(wp5,pq.getFirst());
assertTrue(pq.isEmpty());
}
@Test
public void testPriorityQueueDublicates() throws EmptyListException
{
PriorityQueue pq = new PriorityQueue();
Waypoint wp1, wp2, wp3;
wp1 = new Waypoint(new Vector2f(1,1), 1);
wp2 = new Waypoint(new Vector2f(2,2), 2);
wp3 = new Waypoint(new Vector2f(3,3), 3);
pq.insert(wp1, 10);
pq.insert(wp1, 9);
pq.insert(wp2, 8);
pq.insert(wp2, 7);
pq.insert(wp3, 6);
pq.insert(wp3, 5);
pq.insert(wp1, 1);
pq.insert(wp1, 1);
pq.insert(wp2, 2);
pq.insert(wp2, 2);
pq.insert(wp3, 3);
pq.insert(wp3, 3);
assertEquals(wp1,pq.getFirst());
assertEquals(wp2,pq.getFirst());
assertEquals(wp3,pq.getFirst());
assertTrue(pq.isEmpty());
}
}