⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testharness.java

📁 简单的一个maze系统
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
package mazeImplementation;

/**
 * <b>Programming 2, Assignment 2</b><br/>
 * Maze System Implementation<p/>
 *
 * This class will test the maze system.
 *
 * USAGE
 *
 * Run it for an interactive test menu.
 *
 * Test #15 ("Test all") will dump any debugging output made by the test
 * program, your code, and the JRE, into a text file named "tests.txt".
 *
 * It will also estimate your mark (out of 7) for the "functionality" section.
 *
 * The test #15 can be invoked from the commandline with the "all" argument.
 *
 *
 * @author Peter Tilmanis
 * @version 1.0, 5 September 2007
 *
 */

// NOTE TO STUDENTS: you may need to include a package declaration here, so
// that you can include the test harness in your project.

import java.io.*;
import java.util.*;

public class TestHarness
{
   static Buildable b;
   static Traversable t;
   static BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
   
   /**
    * Initialise a maze system instance.
    *
    * NOTE TO STUDENTS: this is where you change the constructor name/parameters,
    * if necessary. This should be the ONLY thing you might need to modify.
    *
    * @param   size  the size of the maze system to create.
    */
   public static void initialiseEngine(MazeRef size)
   {
      System.out.println("Initialising maze engine of size " + size.getX() +
                         "," + size.getY() + "," + size.getZ() + "...");

      // this is the line that creates a maze engine instance
      b = new MazeEngine(size);

      t = (Traversable) b;
   }
   
   
// ************************************************************************
// ************************************************************************
// ************************************************************************
// ************************************************************************
// ************************************************************************

//              BE VERY CAREFUL CHANGING ANYTHING PAST HERE

//    You may add your own extra lines here and there while debugging
//                          your work, but ...

//      YOUR MAZE SYSTEM SHOULD BE ABLE TO WORK WITH ANYTHING AFTER
//                    THIS POINT, LEFT UNMODIFIED !!

// ************************************************************************
// ************************************************************************
// ************************************************************************
// ************************************************************************
// ************************************************************************



   // --- test1  ----------------------------------------------------------
   public static boolean test1() throws Exception
   {
      // create new maze system instance
      initialiseEngine(new MazeRef(3,3,3));

      // check player string representation
      String s = t.getPlayer().toString();
      System.out.print("Player string is ");
      System.out.print("\"" + s + "\"");
      if(s.toUpperCase().indexOf("UNKNOWN:50") != 0)
      {
         System.out.println(" ... not correct");
         return false;
      }
      else
      {
         System.out.println(" ... correct");
         return true;
      }
   }



   // --- test2  ----------------------------------------------------------
   public static boolean test2() throws Exception
   {
      // create new maze system instance
      initialiseEngine(new MazeRef(3,3,3));
      String[] expected = {"1,1,1:RoomA::", "2,2,2:RoomB::"};

      // add first room
      System.out.print("Creating first room...");
      if (failureCheck( b.createRoom(new MazeRef(1,1,1), "RoomA") ,true))
         return false;

      // add first room
      System.out.print("Creating second room...");
      if (failureCheck( b.createRoom(new MazeRef(2,2,2), "RoomB") ,true))
         return false;

      // find first room
      System.out.print("Finding first room with getRoom()...");
      if (failureCheck( b.getRoom(new MazeRef(1,1,1)).toString().equalsIgnoreCase("1,1,1:RoomA::") ,true))
         return false;

      // find first room
      System.out.print("Finding second room with getRoom()...");
      if (failureCheck( b.getRoom(new MazeRef(2,2,2)).toString().equalsIgnoreCase("2,2,2:RoomB::") ,true))
         return false;

      System.out.print("Checking rooms with getAllRooms()...");
      if (failureCheck( checkForRooms(b.getAllRooms(), expected) ,true))
         return false;

      return true;
   }



   // --- test3  ----------------------------------------------------------
   public static boolean test3() throws Exception
   {
      // create new maze system instance
      initialiseEngine(new MazeRef(3,3,3));
      String[] expected = {"ItemA", "ItemB"};

      // add first room
      System.out.print("Creating a room...");
      if (failureCheck( b.createRoom(new MazeRef(1,1,1), "RoomA") ,true))
         return false;

      // add first item
      System.out.print("Adding first item...");
      if (failureCheck( b.addItem(new MazeRef(1,1,1), "ItemA", 5) ,true))
         return false;

      // add second item
      System.out.print("Adding second item...");
      if (failureCheck( b.addItem(new MazeRef(1,1,1), "ItemB", 3) ,true))
         return false;

      // check for items
      System.out.print("Checking items in the room...");
      if (failureCheck( checkForItems(b.getRoom(new MazeRef(1,1,1)), expected) ,true))
         return false;

      return true;
   }



   // --- test4  ----------------------------------------------------------
   public static boolean test4() throws Exception
   {
      // create new maze system instance
      initialiseEngine(new MazeRef(3,3,3));
      String[] expected1 = {"ItemA", "ItemB"};
      String[] expected2 = {"ItemA"};

      // add first room
      System.out.print("Creating a room...");
      if (failureCheck( b.createRoom(new MazeRef(1,1,1), "RoomA") ,true))
         return false;

      // add first item
      System.out.print("Adding first item...");
      if (failureCheck( b.addItem(new MazeRef(1,1,1), "ItemA", 5) ,true))
         return false;

      // add second item
      System.out.print("Adding second item...");
      if (failureCheck( b.addItem(new MazeRef(1,1,1), "ItemB", 3) ,true))
         return false;

      // check for items
      System.out.print("Checking items in the room...");
      if (failureCheck( checkForItems(b.getRoom(new MazeRef(1,1,1)), expected1) ,true))
         return false;

      // remove item that doesn't exist
      System.out.print("Removing item that doesn't exist...");
      if (failureCheck( b.removeItem(new MazeRef(1,1,1), "ItemZ") ,false))
         return false;

      // check for items
      System.out.print("Checking items in the room...");
      if (failureCheck( checkForItems(b.getRoom(new MazeRef(1,1,1)), expected1) ,true))
         return false;

      // remove item that does exist
      System.out.print("Removing item that does exist...");
      if (failureCheck( b.removeItem(new MazeRef(1,1,1), "ItemB") ,true))
         return false;

      // check for items
      System.out.print("Checking items in the room...");
      if (failureCheck( checkForItems(b.getRoom(new MazeRef(1,1,1)), expected2) ,true))
         return false;

      return true;
   }



   // --- test5  ----------------------------------------------------------
   public static boolean test5() throws Exception
   {
      // create new maze system instance
      initialiseEngine(new MazeRef(3,3,3));
      String[] expected = {"ExitAtoB", "ExitAtoC"};

      // add first room
      System.out.print("Creating a room...");
      if (failureCheck( b.createRoom(new MazeRef(1,1,1), "RoomA") ,true))
         return false;

      // add second room
      System.out.print("Creating a room...");
      if (failureCheck( b.createRoom(new MazeRef(1,1,2), "RoomB") ,true))
         return false;

      // add third room
      System.out.print("Creating a room...");
      if (failureCheck( b.createRoom(new MazeRef(1,2,1), "RoomC") ,true))
         return false;

      // add first exit
      System.out.print("Adding first exit point...");
      if (failureCheck( b.addExitPoint(new MazeRef(1,1,1), "ExitAtoB", new MazeRef(1,1,2)) ,true))
         return false;

      // add second exit
      System.out.print("Adding second exit point...");
      if (failureCheck( b.addExitPoint(new MazeRef(1,1,1), "ExitAtoC", new MazeRef(1,2,1)) ,true))
         return false;

      // check for items
      System.out.print("Checking exits in the room...");
      if (failureCheck( checkForExits(b.getRoom(new MazeRef(1,1,1)), expected) ,true))
         return false;

      return true;
   }



   // --- test6  ----------------------------------------------------------
   public static boolean test6() throws Exception
   {
      // create new maze system instance
      initialiseEngine(new MazeRef(3,3,3));
      String[] expected1 = {"ExitAtoB", "ExitAtoC"};
      String[] expected2 = {"ExitAtoC"};

      // add first room
      System.out.print("Creating a room...");
      if (failureCheck( b.createRoom(new MazeRef(1,1,1), "RoomA") ,true))
         return false;

      // add second room
      System.out.print("Creating a room...");
      if (failureCheck( b.createRoom(new MazeRef(1,1,2), "RoomB") ,true))
         return false;

      // add third room
      System.out.print("Creating a room...");
      if (failureCheck( b.createRoom(new MazeRef(1,2,1), "RoomC") ,true))
         return false;

      // add first exit
      System.out.print("Adding first exit point...");
      if (failureCheck( b.addExitPoint(new MazeRef(1,1,1), "ExitAtoB", new MazeRef(1,1,2)) ,true))
         return false;

      // add second exit
      System.out.print("Adding second exit point...");
      if (failureCheck( b.addExitPoint(new MazeRef(1,1,1), "ExitAtoC", new MazeRef(1,2,1)) ,true))
         return false;

      // check for items
      System.out.print("Checking exits in the room...");
      if (failureCheck( checkForExits(b.getRoom(new MazeRef(1,1,1)), expected1) ,true))
         return false;

      // remove exit that doesn't exist
      System.out.print("Removing exit that doesn't exist...");
      if (failureCheck( b.removeExitPoint(new MazeRef(1,1,1), "ExitZ") ,false))
         return false;

      // check for items
      System.out.print("Checking exits in the room...");
      if (failureCheck( checkForExits(b.getRoom(new MazeRef(1,1,1)), expected1) ,true))
         return false;

      // remove item that does exist
      System.out.print("Removing exit that does exist...");
      if (failureCheck( b.removeExitPoint(new MazeRef(1,1,1), "ExitAtoB") ,true))
         return false;

      // check for items
      System.out.print("Checking exits in the room...");
      if (failureCheck( checkForExits(b.getRoom(new MazeRef(1,1,1)), expected2) ,true))
         return false;

      return true;
   }



   // --- test7  ----------------------------------------------------------
   public static boolean test7() throws Exception
   {
      // create new maze system instance
      initialiseEngine(new MazeRef(3,3,3));
      String[] expected1 = {"1,1,1:RoomA1::"};
      String[] expected2 = {"1,1,1:RoomA1::", "2,2,2:RoomB1::"};

      // add first room
      System.out.print("Creating first room...");
      if (failureCheck( b.createRoom(new MazeRef(1,1,1), "RoomA1") ,true))
         return false;

      // add a clashing room
      System.out.print("Creating a clashing room...");
      if (failureCheck( b.createRoom(new MazeRef(1,1,1), "RoomA2") ,false))
         return false;

      // check rooms
      System.out.print("Checking rooms with getAllRooms()...");
      if (failureCheck( checkForRooms(b.getAllRooms(), expected1) ,true))
         return false;

      // add another room
      System.out.print("Creating a second room...");
      if (failureCheck( b.createRoom(new MazeRef(2,2,2), "RoomB1") ,true))
         return false;

      // check rooms
      System.out.print("Checking rooms with getAllRooms()...");
      if (failureCheck( checkForRooms(b.getAllRooms(), expected2) ,true))
         return false;

      return true;
   }



   // --- test8  ----------------------------------------------------------
   public static boolean test8() throws Exception
   {
      // create new maze system instance
      initialiseEngine(new MazeRef(5,5,5));

      // check maze size
      MazeRef mr = b.getMazeSize();

      System.out.print("Checking maze size");      
      if (mr.getX() != 5 || mr.getY() != 5 || mr.getZ() != 5)      
      {
         System.out.println(" ... not correct");
         return false;
      }
      else
         System.out.println(" ... correct");

      // create new maze system instance
      initialiseEngine(new MazeRef(1,2,3));

      // check maze size
      mr = b.getMazeSize();

      System.out.print("Checking maze size");      
      if (mr.getX() != 1 || mr.getY() != 2 || mr.getZ() != 3)      
      {
         System.out.println(" ... not correct");
         return false;
      }
      else
         System.out.println(" ... correct");

      return true;
   }



   // --- test9  ----------------------------------------------------------
   public static boolean test9() throws Exception
   {
      // create new maze system instance
      initialiseEngine(new MazeRef(3,3,3));
      String[] expected = {"0,0,0:Origin::",
                           "2,0,0:MaxX::",
                           "0,2,0:MaxY::",
                           "0,0,2:MaxZ::",
                           "2,2,0:MaxXY::",
                           "0,2,2:MaxYZ::",
                           "2,0,2:MaxXZ::",
                           "2,2,2:MaxXYZ::"};

      // add rooms
      System.out.print("Creating origin room...");
      if (failureCheck( b.createRoom(new MazeRef(0,0,0), "Origin") ,true))
         return false;

      System.out.print("Creating MaxX room...");
      if (failureCheck( b.createRoom(new MazeRef(2,0,0), "MaxX") ,true))
         return false;

      System.out.print("Creating MaxY room...");
      if (failureCheck( b.createRoom(new MazeRef(0,2,0), "MaxY") ,true))
         return false;

      System.out.print("Creating MaxZ room...");
      if (failureCheck( b.createRoom(new MazeRef(0,0,2), "MaxZ") ,true))
         return false;

      System.out.print("Creating MaxXY room...");
      if (failureCheck( b.createRoom(new MazeRef(2,2,0), "MaxXY") ,true))
         return false;

      System.out.print("Creating MaxYZ room...");
      if (failureCheck( b.createRoom(new MazeRef(0,2,2), "MaxYZ") ,true))
         return false;

      System.out.print("Creating MaxXZ room...");
      if (failureCheck( b.createRoom(new MazeRef(2,0,2), "MaxXZ") ,true))
         return false;

      System.out.print("Creating MaxXYZ room...");
      if (failureCheck( b.createRoom(new MazeRef(2,2,2), "MaxXYZ") ,true))
         return false;

      // check rooms
      System.out.print("Checking rooms with getAllRooms()...");
      if (failureCheck( checkForRooms(b.getAllRooms(), expected) ,true))
         return false;

      // add an out of bounds room
      System.out.print("Creating a room out of bounds...");
      if (failureCheck( b.createRoom(new MazeRef(3,3,3), "OOB") ,false))
         return false;

      // check rooms
      System.out.print("Checking rooms with getAllRooms()...");
      if (failureCheck( checkForRooms(b.getAllRooms(), expected) ,true))
         return false;

      return true;
   }



   // --- test10 ----------------------------------------------------------
   public static boolean test10() throws Exception
   {
      // create new maze system instance
      initialiseEngine(new MazeRef(10,10,10));

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -