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

📄 exprevaltest.java

📁 基于算符优先关心的
💻 JAVA
字号:
/**
 * @Copyright(C) 2008 Software Engineering Laboratory (SELAB), Department of Computer 
 * Science, SUN YAT-SEN UNIVERSITY. All rights reserved.
**/

package test;

import parser.*;
import exceptions.*;
import java.io.*;
import java.text.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.SAXException;

/**
 * Testing for ExprEval. All predefined test cases are stored in an XML document, and 
 * will be retrieved and performed during the testing procedure. 
 *
 * @author Dr. Wen-jun LI
 * @author Da LUO
 * @version 1.00 (Last update: March 4, 2008)
**/
public class ExprEvalTest
{
	/**
	 * The file name of the XML document, which storing all test cases. 
	**/
	public static String fileName;

	/**
	 * The total number of test cases. 
	**/
	public static int total;

	/**
	 * The number of warnings on test cases. A warning is the testing result 
	 * indicating that if the expected output is a evaluated value, the running of 
	 * the test case must output exactly the same value; if the expected output is 
	 * an exception, the running of the test case must throw an exception, even the 
	 * thrown exception is different from the expected one. 
	**/
	public static int warnings;

	/**
	 * The number of errors on test cases. 
	**/
	public static int errors;


	/**
	 * Main entry to the testing of ExprEval. 
	 * 
	 * @param args  a string indicating the name of an XML file, which contains data 
	 *              of test cases. If no args are provided, a default file name will 
	 *              be used. 
	 * @throws Exception  when exceptions occurs while testing. 
	**/
	public static void main(String[] args) throws Exception {
		// Set counters
		total = warnings = errors = 0;

		// Get XML file name
		fileName = "../testcases/standard.xml";
		if (args.length >= 1)  fileName=args[0];

		// Run all test cases
		System.out.println("Testing Procedure");
		System.out.println("========================================================");
		Document doc = getDoc();
		test(doc);
		System.out.println("--------------------------------------------------------");

		// Show the testing result
		System.out.println("Statistics Report ("  + total + " test cases):\n");
		if (total == 0)  return;
		System.out.println("\tPassed case(s): " + (total - warnings - errors) + " (" + ((total - warnings - errors) * 100.0 / total) + "%)");
		System.out.println("\tWarning case(s): " + warnings + " (" + (warnings * 100.0 / total) + "%)");
		System.out.println("\tFailed case(s): " + errors + " (" + (errors * 100.0 / total) + "%)");
		System.out.println("========================================================\n");
	}

	/**
	 * Create an instance of Document from the XML document. 
	 * 
	 * @return  the newly created Document. 
	**/
	public static Document getDoc() throws SAXException {
        Document doc = null; 
        try { 
			doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(fileName); 
		} catch (ParserConfigurationException ex) {
			System.out.println("XML parser configuration failed: " + ex);
			System.exit(1);
		} catch(FileNotFoundException ex) {
			System.out.println("XML file not found: " + fileName);
			System.exit(1);
		} catch (DOMException ex) {
			System.out.println("XML parser finds an error in the document: " + ex);
			System.exit(1);
		} catch (Exception ex) {
			System.out.println("XML parser encounters an error: " + ex);
			System.exit(1);
		}
		return doc;
	}

	/**
	 * Run all test case specified in the Document. 
	 * 
	 * @param doc  the XML document containing all test cases. 
	 * @throws ExpressionException  result of running test case. 
	**/
	public static void test(Document doc) throws ExpressionException
	{
		// Get the list of all test Cases
		Element root = (Element) doc.getDocumentElement(); 
		root.normalize();
		NodeList tclist = root.getElementsByTagName("test-case"); 

		// Read in a test case and call oneTesting() to perform its testing procedure
		for (int i = 0; i < tclist.getLength(); i++) {
			// Get a test case
			Element tc = (Element) tclist.item(i);
			// Get the ID of the test case
			NodeList idTags = tc.getElementsByTagName("id");
			if (idTags.getLength() != 1) {
				System.out.println("Test case format error in XML document: each test case must have one and only one ID. ");
				continue;
			}
			String id = ((Text)((Element)(idTags.item(0))).getFirstChild()).getNodeValue();
			// Get the description of the test case
			NodeList descTags = tc.getElementsByTagName("description");
			if (descTags.getLength() != 1) {
				System.out.println("Test case format error in XML document: each test case must have one and only one description. ");
				continue;
			}
			String desc = ((Text)((Element)(descTags.item(0))).getFirstChild()).getNodeValue();
			// Get the input of the test case
			NodeList inputTags = tc.getElementsByTagName("input");
			if (inputTags.getLength() != 1) {
				System.out.println("Test case format error in XML document: each test case must have one and only one input. ");
				continue;
			}
			String input = ((Text)((Element)(inputTags.item(0))).getFirstChild()).getNodeValue();
			// Get the expected output of the test case
			String output = "";
			boolean isExc = false;
			NodeList outputTags = tc.getElementsByTagName("output");
			if (outputTags.getLength() > 1) {
				System.out.println("Test case format error in XML document: each test case must have no more than one output. ");
				continue;
			} else if (outputTags.getLength() == 1) {
				output = ((Text)((Element)(outputTags.item(0))).getFirstChild()).getNodeValue();
			} else {
				NodeList excTags = tc.getElementsByTagName("exception");
				if (excTags.getLength() > 1) {
					System.out.println("Test case format error in XML document: each test case must have no more than one exception. ");
					continue;
				} else if (excTags.getLength() == 1) {
					output = ((Text)((Element)(excTags.item(0))).getFirstChild()).getNodeValue();
				} else {
					System.out.println("Test case format error in XML document: each test case must have one expected output or exception. ");
					continue;
				}
			}
			// 调用测试
            test(new TestCase(id, desc, input, output, isExc));
        } 
		System.out.println("");
	}

	/**
	 * Run a test case. 
	 * 
	 * @param tc  test case to be performed. 
	 * @throws ExpressionException  result of running test case. 
	**/
	public static void test(TestCase tc) throws ExpressionException {
		String formatter = "###,###,###,###.####";  // precision is limited to only 4 digits of decimal fraction
		String errorMessage = "";

		// The program under testing may throw an exception that indicating errors 
		// in the input expression. 
		try {
			// Perform the test case and get the result
			double result = new Calculator().calculate(tc.getInput());
			try {
				// Compare the result with the expected output
				String output = (new DecimalFormat(formatter)).format(result);
				String expected = (new DecimalFormat(formatter)).format(Double.parseDouble(tc.getOutput()));
				if (! output.equals(expected)) {
					errors++;
					errorMessage = "Error: evaluation result is not expected (output = " + output + ", expected = " + expected + "). ";
				}
			} catch(Exception ex) {
				// The result is not a legal value of double type
				errors++;
				errorMessage = "Error: evaluation result is not a double type. ";
			}
		} catch (ExpressionException ex) {
			// The result of running the test case is an exception
			if (tc.isException()) {
				errors++;
				errorMessage = "Error: wrong detection for a correct input (" + ex + "). ";
			}
			// Check if the exception is expected
			if ((ex instanceof DividedByZeroException) && tc.getOutput().equalsIgnoreCase("DividedByZeroException")) {
			} else if ((ex instanceof EmptyExpressionException) && tc.getOutput().equalsIgnoreCase("EmptyExpressionException")) {
			} else if ((ex instanceof ExpressionException) && tc.getOutput().equalsIgnoreCase("ExpressionException")) {
			} else if ((ex instanceof FunctionCallException) && tc.getOutput().equalsIgnoreCase("FunctionCallException")) {
			} else if ((ex instanceof IllegalDecimalException) && tc.getOutput().equalsIgnoreCase("IllegalDecimalException")) {
			} else if ((ex instanceof IllegalIdentifierException) && tc.getOutput().equalsIgnoreCase("IllegalIdentifierException")) {
			} else if ((ex instanceof IllegalSymbolException) && tc.getOutput().equalsIgnoreCase("IllegalSymbolException")) {
			} else if ((ex instanceof LexicalException) && tc.getOutput().equalsIgnoreCase("LexicalException")) {
			} else if ((ex instanceof MissingLeftParenthesisException) && tc.getOutput().equalsIgnoreCase("MissingLeftParenthesisException")) {
			} else if ((ex instanceof MissingOperandException) && tc.getOutput().equalsIgnoreCase("MissingOperandException")) {
			} else if ((ex instanceof MissingOperatorException) && tc.getOutput().equalsIgnoreCase("MissingOperatorException")) {
			} else if ((ex instanceof MissingRightParenthesisException) && tc.getOutput().equalsIgnoreCase("MissingRightParenthesisException")) {
			} else if ((ex instanceof SemanticException) && tc.getOutput().equalsIgnoreCase("SemanticException")) {
			} else if ((ex instanceof SyntacticException) && tc.getOutput().equalsIgnoreCase("SyntacticException")) {
			} else if ((ex instanceof TrinaryOperationException) && tc.getOutput().equalsIgnoreCase("TrinaryOperationException")) {
			} else if ((ex instanceof TypeMismatchedException) && tc.getOutput().equalsIgnoreCase("TypeMismatchedException")) {
			} else {
				warnings++;
				errorMessage = "Warning: exception is not an expected type (" + ex + "). ";
			}
		} catch (Exception ex) {
			// Not a user defined exception
			errors++;
			errorMessage = "Error: exception found in your program (" + ex + "). ";
		} finally {
			// Display statistics
			total++;
			if (errorMessage.equals("")) {
				System.out.println("\n" + tc + "\nPassed !");
			} else {
				System.out.println("\n" + tc);
				System.out.println(errorMessage);
			}
		} // try
	}
}

⌨️ 快捷键说明

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