package VacuumWorld.testing;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import VacuumWorld.Environment;
import VacuumWorld.Location;
import VacuumWorld.VacuumSearchAgent;
import dataStructures.HashMap;
import dataStructures.Iterator;
/**
* Class VacuumWorldDLSTester.
* <p>
* This class runs test cases for testing a depth-limited search for the
* VacuumWorld problem.
* <p>
* Written by: Roger West
* University of Illinois at Springfield
* 01/2006
*/
public class VacuumWorldDLSTester
{
/************
* constants
***********/
/** indicates all available tests should be run in batch mode */
private final static String RUN_ALL_TESTS = "ALL";
/*************
* attributes
************/
/** the collection of TestCase objects available for testing */
private HashMap testCases;
/***************
* constructors
**************/
/**
* Create a new VacuumWorldDLSTester with an empty collection of test cases.
*/
public VacuumWorldDLSTester()
{
// create HashMap for storing test cases
testCases = new HashMap();
}
/**********
* methods
*********/
/**
* Query user for depth limit to use for testing.
*/
private int queryDepth()
{
int depth = 0;
try
{
BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));
System.out.print("\nEnter depth limit for DepthLimitedSearch: ");
String input = bfr.readLine();
depth = Integer.parseInt(input);
}
catch (Exception e)
{
depth = 0;
}
return depth;
}
/**
* Populate collection of test cases. Currently all test cases are hard-
* coded inside this method.
*
* @param depth depth limit for testing the depth-limited search
*/
private void populateVacuumWorldDLSTestCases(int depth)
{
// test case object
VacuumWorldDLSTestCase tc;
// test case id
String id;
// test case description
String description;
// the test case
Environment test;
// test case expected result
String expectedResult;
// Test Case #1:
id = "1";
description = "D-" + depth + " LEFT (dirty) (vacuum agent present) | RIGHT (dirty)";
expectedResult = "Optimal solution: Suck --> Move Right --> Suck";
test = new Environment();
test.addLocation(Location.LEFT, new Location(Location.LEFT, true));
test.addLocation(Location.RIGHT, new Location(Location.RIGHT, true));
test.setVacuumAgentLocation(Location.LEFT);
tc = new VacuumWorldDLSTestCase(id, description, test, depth, expectedResult);
testCases.add(id, tc);
// Test Case #2:
id = "2";
description = "D-" + depth + " LEFT (dirty) | RIGHT (dirty) (vacuum agent present)";
expectedResult = "Optimal solution: Suck --> Move Left --> Suck";
test = new Environment();
test.addLocation(Location.LEFT, new Location(Location.LEFT, true));
test.addLocation(Location.RIGHT, new Location(Location.RIGHT, true));
test.setVacuumAgentLocation(Location.RIGHT);
tc = new VacuumWorldDLSTestCase(id, description, test, depth, expectedResult);
testCases.add(id, tc);
// Test Case #3:
id = "3";
description = "D-" + depth + " LEFT (clean) (vacuum agent present) | RIGHT (clean)";
expectedResult = "Optimal solution: Do nothing";
test = new Environment();
test.addLocation(Location.LEFT, new Location(Location.LEFT, false));
test.addLocation(Location.RIGHT, new Location(Location.RIGHT, false));
test.setVacuumAgentLocation(Location.LEFT);
tc = new VacuumWorldDLSTestCase(id, description, test, depth, expectedResult);
testCases.add(id, tc);
// Test Case #4:
id = "4";
description = "D-" + depth + " LEFT (clean) | RIGHT (clean) (vacuum agent present)";
expectedResult = "Optimal solution: Do nothing";
test = new Environment();
test.addLocation(Location.LEFT, new Location(Location.LEFT, false));
test.addLocation(Location.RIGHT, new Location(Location.RIGHT, false));
test.setVacuumAgentLocation(Location.RIGHT);
tc = new VacuumWorldDLSTestCase(id, description, test, depth, expectedResult);
testCases.add(id, tc);
// Test Case #5:
id = "5";
description = "D-" + depth + " LEFT (clean) (vacuum agent present) | RIGHT (dirty)";
expectedResult = "Optimal solution: Move Right --> Suck";
test = new Environment();
test.addLocation(Location.LEFT, new Location(Location.LEFT, false));
test.addLocation(Location.RIGHT, new Location(Location.RIGHT, true));
test.setVacuumAgentLocation(Location.LEFT);
tc = new VacuumWorldDLSTestCase(id, description, test, depth, expectedResult);
testCases.add(id, tc);
// Test Case #6:
id = "6";
description = "D-" + depth + " LEFT (clean) | RIGHT (dirty) (vacuum agent present)";
expectedResult = "Optimal solution: Suck";
test = new Environment();
test.addLocation(Location.LEFT, new Location(Location.LEFT, false));
test.addLocation(Location.RIGHT, new Location(Location.RIGHT, true));
test.setVacuumAgentLocation(Location.RIGHT);
tc = new VacuumWorldDLSTestCase(id, description, test, depth, expectedResult);
testCases.add(id, tc);
// Test Case #7:
id = "7";
description = "D-" + depth + " LEFT (dirty) (vacuum agent present) | RIGHT (clean)";
expectedResult = "Optimal solution: Suck";
test = new Environment();
test.addLocation(Location.LEFT, new Location(Location.LEFT, true));
test.addLocation(Location.RIGHT, new Location(Location.RIGHT, false));
test.setVacuumAgentLocation(Location.LEFT);
tc = new VacuumWorldDLSTestCase(id, description, test, depth, expectedResult);
testCases.add(id, tc);
// Test Case #8:
id = "8";
description = "D-" + depth + " LEFT (dirty) | RIGHT (clean) (vacuum agent present)";
expectedResult = "Optimal solution: Move Left --> Suck";
test = new Environment();
test.addLocation(Location.LEFT, new Location(Location.LEFT, true));
test.addLocation(Location.RIGHT, new Location(Location.RIGHT, false));
test.setVacuumAgentLocation(Location.RIGHT);
tc = new VacuumWorldDLSTestCase(id, description, test, depth, expectedResult);
testCases.add(id, tc);
}
/**
* Display menu of available test cases and allow user to choose test case
* to run.
*/
public void test()
{
int maxDepth = queryDepth();
populateVacuumWorldDLSTestCases(maxDepth);
System.out.println("\nChoose a test case:");
for (Iterator itr = testCases.iterator(); itr.hasNext(); itr.next())
{
VacuumWorldDLSTestCase tc = (VacuumWorldDLSTestCase)itr.getCurrent();
System.out.println( " " + tc.getID() + ": "
+ tc.getDescription());
}
System.out.println();
try
{
BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));
String testID = bfr.readLine();
testID.trim();
runTest(testID);
test();
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* Execute the test case corresponding to the specified ID.
*
* @param id the unique identifier of the test case to run
*/
private void runTest(String id)
{
VacuumWorldDLSTestCase tc = (VacuumWorldDLSTestCase)testCases.get(id);
if (tc != null)
{
VacuumSearchAgent agent = tc.getAgent();
agent.solveProblem();
}
else
{
System.out.println("Error - chosen ID does not match any existing test cases!");
}
}
/**
* Execute all test cases in batch mode.
*
* Currently not implemented.
*/
private void runAllTests()
{
}
}