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

📄 testharness.java

📁 简单的一个maze系统
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
            anyFail = true;
            count++;
         }
      }
      catch (Exception e)
      {
         realOut.println("Test 10 **FAIL** (unexpected exception occurred)");
         anyFail = true;
         count++;
      }


      try
      {
         System.out.println("--- Test 11 --------------------------");
         if (!test11())
         {
            realOut.println("Test 11 **FAIL** (incorrect implementation)");
            anyFail = true;
            count++;
         }
      }
      catch (Exception e)
      {
         realOut.println("Test 11 **FAIL** (unexpected exception occurred)");
         anyFail = true;
         count++;
      }


      try
      {
         System.out.println("--- Test 12 --------------------------");
         if (!test12())
         {
            realOut.println("Test 12 **FAIL** (incorrect implementation)");
            anyFail = true;
            count++;
         }
      }
      catch (Exception e)
      {
         realOut.println("Test 12 **FAIL** (unexpected exception occurred)");
         anyFail = true;
         count++;
      }


      try
      {
         System.out.println("--- Test 13 --------------------------");
         if (!test13())
         {
            realOut.println("Test 13 **FAIL** (incorrect implementation)");
            anyFail = true;
            count++;
         }
      }
      catch (Exception e)
      {
         realOut.println("Test 13 **FAIL** (unexpected exception occurred)");
         anyFail = true;
         count++;
      }


      try
      {
         System.out.println("--- Test 14 --------------------------");
         if (!test14())
         {
            realOut.println("Test 14 **FAIL** (incorrect implementation)");
            anyFail = true;
            count++;
         }
      }
      catch (Exception e)
      {
         realOut.println("Test 14 **FAIL** (unexpected exception occurred)");
         anyFail = true;
         count++;
      }
      
      // set the output streams back to normal, and close the file
      System.setErr(realErr);
      System.setOut(realOut);
      outFile.close();

      if (!anyFail)
         System.out.println("NONE -- Working maze engine.");
      else
         System.out.println("Some tests failed...");
      
      System.out.println("\nTests failed:  " + count);
      System.out.println("Tests passed:  " + (14 - count));
      System.out.println();
      System.out.println("\nFunctionality Mark: " + (0.5 * (14 - count)));
   }



   /**
    * Display the test menu.
    */
   public static int mainMenu()
   {
      // variables for menu operation
      int answer = -1;
      boolean valid = false;
      
      // continue prompting until valid data given
      while (!valid)
      {
         // display menu and prompt for entry
         System.out.println();
         System.out.println("===============================================================================");
         System.out.println("Tests");
         System.out.println("===============================================================================");
         System.out.println();
         System.out.println("MAZE BUILDING TESTS                      MAZE TRAVERSAL TESTS");
         System.out.println(" 1. blank maze                           10. maze reset");
         System.out.println(" 2. room creation                        11. move operation");
         System.out.println(" 3. adding an item                       12. multiple movements");
         System.out.println(" 4. removing an item                     13. completing a maze");
         System.out.println(" 5. adding an exit point                 14. dying in a maze");
         System.out.println(" 6. removing an exit point");
         System.out.println(" 7. managing multiple rooms              15. test all");
         System.out.println(" 8. maze size");
         System.out.println(" 9. maze boundary checking               16. quit");

         System.out.println();
         System.out.print("Enter an option: ");
         
         // try to read and validate entered data
         try
         {
            answer = Integer.parseInt(stdin.readLine());
            System.out.println();
            if ((answer >= 1) && (answer <= 16))
               valid = true;
         }
         catch (NumberFormatException e)
         {
            System.out.println("Unparsable number entered");
         }
         catch (IOException e)
         {
            System.out.println("I/O Exception. Goodbye");
            System.exit(1);
         }
         
         // if data was invalid, print an error
         if (!valid)
         {
            System.out.println();
            System.out.println("Please enter a valid option (1-16).");
         }
      }
      
      // return the entered data
      return answer;
   }

   public static void main(String[] args)
   {
      if (args.length > 0)
      {
         if (args[0].equalsIgnoreCase("all"))
         {
            test15();
            System.exit(0);
         }
      }
      
      // menu and data declarations
      int menuOption = -1;
      
      // repeat processing menu items until quit is entered
      while (menuOption != 16)
      {
         menuOption = mainMenu();
         
         try
         {
            switch (menuOption)
            {
               case 1:
                  if (test1())
                     System.out.println("**PASS**");
                  else
                     System.out.println("**FAIL**");
                  break;
               case 2:
                  if (test2())
                     System.out.println("**PASS**");
                  else
                     System.out.println("**FAIL**");
                  break;
               case 3:
                  if (test3())
                     System.out.println("**PASS**");
                  else
                     System.out.println("**FAIL**");
                  break;
               case 4:
                  if (test4())
                     System.out.println("**PASS**");
                  else
                     System.out.println("**FAIL**");
                  break;
               case 5:
                  if (test5())
                     System.out.println("**PASS**");
                  else
                     System.out.println("**FAIL**");
                  break;
               case 6:
                  if (test6())
                     System.out.println("**PASS**");
                  else
                     System.out.println("**FAIL**");
                  break;
               case 7:
                  if (test7())
                     System.out.println("**PASS**");
                  else
                     System.out.println("**FAIL**");
                  break;
               case 8:
                  if (test8())
                     System.out.println("**PASS**");
                  else
                     System.out.println("**FAIL**");
                  break;
               case 9:
                  if (test9())
                     System.out.println("**PASS**");
                  else
                     System.out.println("**FAIL**");
                  break;
               case 10:
                  if (test10())
                     System.out.println("**PASS**");
                  else
                     System.out.println("**FAIL**");
                  break;
               case 11:
                  if (test11())
                     System.out.println("**PASS**");
                  else
                     System.out.println("**FAIL**");
                  break;
               case 12:
                  if (test12())
                     System.out.println("**PASS**");
                  else
                     System.out.println("**FAIL**");
                  break;
               case 13:
                  if (test13())
                     System.out.println("**PASS**");
                  else
                     System.out.println("**FAIL**");
                  break;
               case 14:
                  if (test14())
                     System.out.println("**PASS**");
                  else
                     System.out.println("**FAIL**");
                  break;
               case 15:
                  test15(); 
            }
         }
         catch (Exception e)
         {
            System.out.println("Unexpected exception! " + e.getMessage());
            e.printStackTrace();
            System.out.println("**FAIL**");
         }
      }
   }

   
   public static boolean checkForRooms(Room[] rooms, String[] expected) throws Exception
   {
      try
      {
         // if number of rooms is different from expected, return failure
         if (rooms.length != expected.length)
            return false;
   
         // make some spots to tick
         boolean[] tickbox = new boolean[expected.length];
   
         // for each expected room string...
         for (int i = 0; i < expected.length; i++)
         {
            // try to find a match in the actual room string list.
            boolean found = false;
            for (int j = 0; (j < rooms.length) && (!found); j++)
            {
               if (expected[i].equalsIgnoreCase(rooms[j].toString()))
                  found = true;
            }
   
            // store result for this room string in the tick box
            tickbox[i] = found;
         }
   
         // check there's a tick in every box
         for (int i = 0; i < tickbox.length; i++)
         {
            if (!tickbox[i])
               return false;
         }
      }
      catch (Exception e)
      {
         System.out.println("Parsing error: could not check room list successfully!");
         return false;
      }

      return true;
   }

   public static boolean failureCheck(boolean check, boolean expected)
   {
      if (check == expected)
      {
         System.out.println(" OK");
         return false;
      }
      else
      {
         System.out.println(" not OK!");
         return true;
      }
   }


   public static boolean checkForItems(Room room, String[] expected) throws Exception
   {
      try
      {
         // make some spots to tick
         boolean[] tickbox = new boolean[expected.length];

         // split the room string into its parts
         String[] split = room.toString().split(":");

         // check the correct number of parts are there
         if (split.length != 3)
            return false;

         // take the exit part and split it into individual exits
         String[] itemList = split[2].split(",");

         // for each expected item string...
         for (int i = 0; i < expected.length; i++)
         {
            // try to find a match in the actual item list.
            boolean found = false;
            for (int j = 0; (j < itemList.length) && (!found); j++)
            {
               if (expected[i].equalsIgnoreCase(itemList[j].toString()))
                  found = true;
            }
   
            // store result for this room string in the tick box
            tickbox[i] = found;
         }
   
         // check there's a tick in every box
         for (int i = 0; i < tickbox.length; i++)
         {
            if (!tickbox[i])
               return false;
         }
      }
      catch (Exception e)
      {
         return false;
      } 

      return true;
   }

   public static boolean checkForExits(Room room, String[] expected) throws Exception
   {
      try
      {
         // make some spots to tick
         boolean[] tickbox = new boolean[expected.length];

         // split the room string into its parts
         String[] split = room.toString().split(":");

         // check the correct number of parts are there
         if (split.length != 4)
            return false;

         // take the exit part and split it into individual exits
         String[] exitList = split[3].split(",");
         

         // for each expected item string...
         for (int i = 0; i < expected.length; i++)
         {
            // try to find a match in the actual item list.
            boolean found = false;
            for (int j = 0; (j < exitList.length) && (!found); j++)
            {
               if (expected[i].equalsIgnoreCase(exitList[j].toString()))
                  found = true;
            }
   
            // store result for this room string in the tick box
            tickbox[i] = found;
         }
   
         // check there's a tick in every box
         for (int i = 0; i < tickbox.length; i++)
         {
            if (!tickbox[i])
               return false;
         }
      }
      catch (Exception e)
      {
         return false;
      } 

      return true;
   }

   public static void buildTestMaze()
   {
      System.out.println("Building test maze...");
      // create rooms
      b.createRoom(new MazeRef(1,2,3),"Start");
      b.createRoom(new MazeRef(1,2,4),"Test1");
      b.createRoom(new MazeRef(1,2,5),"Test2");
      b.createRoom(new MazeRef(2,2,3),"Win1");
      b.createRoom(new MazeRef(2,2,4),"Win2");
      b.createRoom(new MazeRef(2,2,5),"Win3");
      b.createRoom(new MazeRef(0,2,3),"Lose1");
      b.createRoom(new MazeRef(0,2,4),"Lose2");
      b.createRoom(new MazeRef(0,2,5),"Lose3");

      // add items
      b.addItem(new MazeRef(1,2,4),"ItemA",-2);
      b.addItem(new MazeRef(1,2,5),"ItemB",3);
      b.addItem(new MazeRef(1,2,5),"ItemC",-5);
      b.addItem(new MazeRef(0,2,3),"ItemD",-20);
      b.addItem(new MazeRef(0,2,4),"ItemE",10);
      b.addItem(new MazeRef(0,2,4),"ItemF",-20);
      b.addItem(new MazeRef(0,2,5),"ItemG",10);
      b.addItem(new MazeRef(0,2,5),"ItemH",-25);

      // add exits
      b.addExitPoint(new MazeRef(1,2,3),"test",new MazeRef(1,2,4));
      b.addExitPoint(new MazeRef(1,2,4),"test",new MazeRef(1,2,5));

      b.addExitPoint(new MazeRef(1,2,3),"win",new MazeRef(2,2,3));
      b.addExitPoint(new MazeRef(2,2,3),"win",new MazeRef(2,2,4));
      b.addExitPoint(new MazeRef(2,2,4),"win",new MazeRef(2,2,5));

      b.addExitPoint(new MazeRef(1,2,3),"lose",new MazeRef(0,2,3));
      b.addExitPoint(new MazeRef(0,2,3),"lose",new MazeRef(0,2,4));
      b.addExitPoint(new MazeRef(0,2,4),"lose",new MazeRef(0,2,5));
   }

}

⌨️ 快捷键说明

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