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

📄 testprocesscontainers.java

📁 ssd3期末考试答案 经典答案!!! 说明了用JAVA开发的东西
💻 JAVA
字号:
import  java.io.*;
import java.util.*;

/**
 * Test driver for class {@link ProcessContainers}
 *
 * @author  iCarnegie
 * @version 1.0.0
 * @see  ProcessContainers
 * @see MyObject
 * @see MyObjectFormatException
 */
public class  TestProcessContainers {
	
	private final static BufferedReader  stdIn = 
		new  BufferedReader(new  InputStreamReader(System.in));
	private final static PrintWriter  stdOut = 
		new  PrintWriter(System.out, true);
	private final static PrintWriter  stdErr = 
		new  PrintWriter(System.err, true);

	/**
	 * Test each method
	 *
	 * @param args not used
	 */
	public static void  main(String[]  args)  {
	
		stdOut.println("Testing started...");
		stdOut.println();
		
		boolean question3 = TestProcessContainers.testRemoveLess();
		stdErr.println();
		
		stdErr.println("b) Method readPositives.");
		stdErr.println();
		boolean question4a = TestProcessContainers.testReadPositives();
		stdErr.println();
		boolean question4b = TestProcessContainers.testInsufficientData();
		stdErr.println();
		boolean question4c = TestProcessContainers.testAbundantData();
		stdErr.println();
		boolean question4d = TestProcessContainers.testIncorrectNumberFormat();
		
		String msg = (question3 && question4a &&
		              question4b && question4c && question4d)?
			"all driver tests passed." :
			"your methods have errors.";
		
		stdOut.println();
		stdOut.println();
		stdOut.println("Done, " + msg);
		stdOut.println();
	}
	
	
	/* Test the method removeLess */
	private static boolean testRemoveLess() {

		MyObject obj;

		// empty ArrayList<MyObject>
		
		ArrayList<MyObject> list0 = new ArrayList<MyObject>();
		
		ProcessContainers.removeLess(list0);
		
		if (! (list0.size() == 0))  {
			stdErr.println("a) Method removeLess, error 1.");
			return false;
		}
		
		// removes all
		
		MyObject[] arr1 = new MyObject[3];
		
		arr1[0] = new MyObject(10.0, 20.0);
		arr1[1] = new MyObject(20.0, 30.0);
		arr1[2] = new MyObject(30.0, 40.0);
		
		ArrayList<MyObject> list1 = 
			new ArrayList<MyObject>(Arrays.asList(arr1));
		
		ProcessContainers.removeLess(list1);
		
		if (! (list1.size() == 0))  {
			stdErr.println("a) Method removeLess, error 2.");

			return false;
		}
		
		// removes some
		
		MyObject[] arr2 = new MyObject[8];
		
		arr2[0] = new MyObject(10.0, 20.0);
		arr2[1] = new MyObject(20.0, 30.0);
		arr2[2] = new MyObject(30.0, 40.0);
		arr2[3] = new MyObject(50.0, 50.0);
		arr2[4] = new MyObject(60.0, 50.0);
		arr2[5] = new MyObject(20.0, 30.0);
		arr2[6] = new MyObject(80.0, 70.0);
		arr2[7] = new MyObject(90.0, 80.0);
		
		ArrayList<MyObject> list2 =
			new ArrayList<MyObject>(Arrays.asList(arr2));
		
		ProcessContainers.removeLess(list2);
		
		if (! (list2.size() == 4))  {
			stdErr.println("a) Method removeLess, error 3.");

			return false;
		}
		obj = (MyObject) list2.get(0);
		if (! (obj.getFirst() == 50.0 && obj.getSecond() == 50.0)) {
			stdErr.println("a) Method removeLess, error 4.");

			return false;
		}
		obj = (MyObject) list2.get(1);
		if (! (obj.getFirst() == 60.0 && obj.getSecond() == 50.0)) {
			stdErr.println("a) Method removeLess, error 5.");

			return false;
		}
		obj = (MyObject) list2.get(2);
		if (!(obj.getFirst() == 80.0 && obj.getSecond() == 70.0)) {
			stdErr.println("a) Method removeLess, error 6.");

			return false;
		}
		obj = (MyObject) list2.get(3);
		if (!(obj.getFirst() == 90.0 && obj.getSecond() == 80.0)) {
			stdErr.println("a) Method removeLess, error 7.");

			return false;
		}
			
		stdErr.println("a) Method removeLess, all driver tests passed");
		
		return true;

	}
	
	/* Test method readPositives, testing correct input data */
	private static boolean testReadPositives() {
		
		MyObject obj;
		
		try {
		
			// testing reader with no positives
		
		 	String delim = "_";
		 	
			/*
			file test1.dat
			
			-101.0_-0.1
			-130.0_-0.3
			-200.0_-0.5
			*/
			
			ArrayList<MyObject> list1 = ProcessContainers.readPositives(
				new  BufferedReader(new FileReader("test1.dat")), delim);

			if (list1 == null)  {
				stdErr.println("   Testing correct input data, error 1.");

				return false;
			}
			if (! (list1.size() == 0))  {
				stdErr.println("   Testing correct input data, error 2.");

				return false;
			}
			
			// testing reader with some positives
			
			/*
			file test2.dat
			
			-0.5_1.1
			0.5_-1.1
			0.1_100.0
			0.2_200.0
			-0.5_-1.1
			0.3_300.0
			*/
			
			list1 = ProcessContainers.readPositives(
				new  BufferedReader(new FileReader("test2.dat")), delim);
			if (list1 == null)  {
				stdErr.println("   Testing correct input data, error 3.");

				return false;
			}
			if (! (list1.size() == 3))  {
				stdErr.println("   Testing correct input data, error 4.");

				return false;
			}
			obj = (MyObject) list1.get(0);
			if (! (obj.getFirst() == 0.1 && obj.getSecond() == 100)) {
				stdErr.println("   Testing correct input data, error 5.");

				return false;
			}
			obj = (MyObject) list1.get(1);
			if (! (obj.getFirst() == 0.2 && obj.getSecond() == 200)) {
				stdErr.println("   Testing correct input data, error 6.");

				return false;
			}
			obj = (MyObject) list1.get(2);
			if (! (obj.getFirst() == 0.3 && obj.getSecond() == 300)) {
				stdErr.println("   Testing correct input data, error 7.");

				return false;
			}
			
		} catch (MyObjectFormatException mofe) {
			stdErr.println("   Testing correct input data, error 8: "
				+ mofe.toString());

			return false;
		} catch (IOException ioe) {
			stdErr.println("   Testing correct input data, error 9: " + ioe);

			return false;
		} catch (Exception e) {
			stdErr.println("   Testing correct input data, error 10: " + e);

			return false;
		}
		stdErr.println("   Testing correct input data, all driver tests passed");
		
		return true;
	}
	
	/* Test method readPositives, testing insufficient data in a line */
	private static boolean testInsufficientData() {
		
		MyObject obj;
		ArrayList<MyObject> list1;

		String delim = "_";

		boolean exception = false;
		
		String data1 =	"-0.5_1.1\n" +
						"0.5_-1.1\n" +
						"_100.0\n" +
						"0.2_200.0_\n" +
						"-0.5_-1.1\n" +
						"0.3 _300.0";
		
		try {

			list1 = ProcessContainers.readPositives(
				new  BufferedReader(new StringReader(data1)), delim);
		} catch (MyObjectFormatException mofe) {
			exception = true;
		} catch (Exception e) {
			stdErr.println("   Testing insufficient data in a line, error 1: " + e);
			return false;
		}
		if (!exception) {
			stdErr.println("   Testing insufficient data in a line, error 2. Doesn't check insufficient data.");
			return false;
		}
		
		exception = false;
		data1 =		"-0.5_1.1\n" +
					"0.5_-1.1\n" +
					"0.3_100.0\n" +
					"0.2_200.0\n" +
					"-0.5_\n" +
					"0.3 _300.0";
		try {
			list1 = ProcessContainers.readPositives(
				new  BufferedReader(new StringReader(data1)), delim);
		} catch (MyObjectFormatException mofe) {
			exception = true;
		} catch (Exception e) {
			stdErr.println("   Testing insufficient data in a line, error 3: " + e);
			return false;
		}
		if (! exception) {
			stdErr.println("   Testing insufficient data in a line, error 4. Doesn't check insufficient data.");
			return false;
		}
		
		stdErr.println("   Testing insufficient data in a line, all driver tests passed");
		
		return true;
	}
	
	/* Test method readPositives, testing abundant data in a line */
	private static boolean testAbundantData() {
		
		MyObject obj;
		ArrayList<MyObject> list1;
		
		String delim = "_";
		boolean exception = false;
		
		String data1 =	"-0.5_1.1\n" +
						"0.5_-1.1\n" +
						"0.3_100.0\n" +
						"0.2_200.0_0.0\n" +
						"-0.5_0.3\n" +
						"0.3 _300.0";

		try {
			list1 = ProcessContainers.readPositives(
				new  BufferedReader(new StringReader(data1)), delim);
		} catch (MyObjectFormatException mofe) {
			exception = true;
		} catch (Exception e) {
			stdErr.println("   Testing abundant data in a line, error 1. Doesn't check abundant data: " + e);

			return false;
		}
		if (!exception) {
			stdErr.println("   Testing abundant data in a line, error 2. Doesn't check abundant data.");

			return false;
		}
		
		stdErr.println("   Testing abundant data in a line, all driver tests passed");
		
		return true;
	}
	
	/* Test method readPositives, testing incorrect number format in a line */
	private static boolean testIncorrectNumberFormat() {
		
		MyObject obj;
		ArrayList<MyObject> list1;
		
		String delim = "_";
		boolean exception = false;
		
		String data1 =	"-0.5_1.1\n" +
						"0.5_-1.1\n" +
						"0.1_100.0\n" +
						"0.2_20.0.0\n" +
						"-0.5_-1.1\n" +
						"0.3 _300.0";
		
		try {
			list1 = ProcessContainers.readPositives(
				new  BufferedReader(new StringReader(data1)), delim);
		} catch (MyObjectFormatException sfe) {
			exception = true;
		} catch (Exception e) {
			stdErr.println("   Testing incorrect number format in a line, error 1: " + e);
			return false;
		}
		if (!exception) {
			stdErr.println("   Testing incorrect number format in a line, error 2. Doesn't check incorrect numbers.");
			return false;
		}
		
		exception = false;
		
		data1 =		"-0.5_1.1\n" +
					"0.5_-1.1\n" +
					"0.1,9_100.0\n" +
					"0.2_20.0\n" +
					"-0.5_-1.1\n" +
					"0.3 _300.0";
		
		try {
			list1 = ProcessContainers.readPositives(
				new  BufferedReader(new StringReader(data1)), delim);
		} catch (MyObjectFormatException sfe) {
			exception = true;
		} catch (Exception e) {
			stdErr.println("   Testing incorrect number format in a line, error 3: " + e);
			return false;
		}
		if (!exception) {
			stdErr.println("   Testing incorrect number format in a line, error 4. Doesn't check incorrect numbers.");
			return false;
		}
		
		stdErr.println("   Testing incorrect number format in a line, all driver tests passed");
		
		return true;
	}
}

⌨️ 快捷键说明

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