AStarAlgorithmTests.java :  » Game » pageley-games » org » pageley » games » sanctuary » domain » environment » pathfinding » Java Open Source

Java Open Source » Game » pageley games 
pageley games » org » pageley » games » sanctuary » domain » environment » pathfinding » AStarAlgorithmTests.java
package org.pageley.games.sanctuary.domain.environment.pathfinding;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;

import junit.framework.TestCase;

import org.pageley.games.sanctuary.domain.Location;
import org.pageley.games.sanctuary.domain.SimpleLocation;

public class AStarAlgorithmTests extends TestCase {
  public void testFindPath() {
    Collection<Location> nodes = new HashSet<Location>();
    nodes.add(new SimpleLocation(0, 0));
    nodes.add(new SimpleLocation(2, 0));
    nodes.add(new SimpleLocation(0, 1));
    nodes.add(new SimpleLocation(2, 1));
    nodes.add(new SimpleLocation(0, 2));
    nodes.add(new SimpleLocation(1, 2));
    nodes.add(new SimpleLocation(2, 2));
    
    PathFindingAlgorithm aStar = new AStarAlgorithm(new ManhattanHeuristic());
    
    List<Location> route = aStar.findPath(new SimpleLocation(0, 0), new SimpleLocation(2, 0), nodes);
    assertNotNull(route);
    assertEquals(4, route.size());
    assertEquals(new SimpleLocation(0, 1), route.get(0));
    assertEquals(new SimpleLocation(1, 2), route.get(1));
    assertEquals(new SimpleLocation(2, 1), route.get(2));
    assertEquals(new SimpleLocation(2, 0), route.get(3));
  }
  
  public void testFindPathUnreachable() {
    Collection<Location> nodes = new HashSet<Location>();
    nodes.add(new SimpleLocation(0, 0));
    nodes.add(new SimpleLocation(2, 0));
    nodes.add(new SimpleLocation(0, 1));
    nodes.add(new SimpleLocation(2, 1));
    nodes.add(new SimpleLocation(0, 2));
    nodes.add(new SimpleLocation(2, 2));
    
    PathFindingAlgorithm aStar = new AStarAlgorithm(new ManhattanHeuristic());
    
    List<Location> route = aStar.findPath(new SimpleLocation(0, 0), new SimpleLocation(2, 0), nodes);
    assertNotNull(route);
    assertEquals(0, route.size());
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.