import java.io.*;
import java.util.*;
/**
* Test the class <code>StudentArrayList</code>
*
* @author iCarnegie
* @version 1.0.0
* @see Student
* @see StudentArrayList
*/
public class TestStudentArrayList {
/* Standard output stream */
private static PrintWriter stdOut =
new PrintWriter(System.out, true);
/* Standard error stream */
private static PrintWriter stdErr =
new PrintWriter(System.err, true);
/* Variables that contains the test objects */
private Student first;
private Student second;
private Student third;
private Student fourth;
private Student fifth;
private Student firstArrayListTwo;
private Student secondArrayListTwo;
private Student thirdArrayListTwo;
private Student fourthArrayListTwo;
private Student fifthArrayListTwo;
private ArrayList<Student> emptyArrayList;
private ArrayList<Student> arrayList;
private ArrayList<Student> arrayListTwo;
/**
* Tests methods of class {@link StudentArrayList}
*
* @param args not used
*/
public static void main (String args[]) {
stdOut.println("");
stdOut.println("Testing class StudentArrayList...");
TestStudentArrayList tester = new TestStudentArrayList();
if (tester.testMakeArrayList() &
tester.testMakeArrayListFromArray() &
tester.testHasStudent() &
tester.testCountGradeGreaterOrEqual() &
tester.testGetMinGrade() &
tester.testGetGradeAverage() &
tester.testRemoveGradeLess() &
tester.testDisplayAll()) {
stdOut.println("All tests passed");
}
}
/**
* Displays a message in the standard error stream if the value specified
* by parameter <code>condition<code> is <code>false</code>.
*
* @param message the error message.
* @param condition the test condition.
* @return the value of <code>condition</code>
*/
public static boolean assertTrue(String message, boolean condition) {
if (! condition) {
stdErr.print("** Test failure ");
stdErr.println(message);
return false;
} else {
return true;
}
}
/**
* Displays a message in the standard error stream.
*
* @param message the error message.
* @return false
*/
public static boolean fail(String message) {
stdErr.print("** Test failure ");
stdErr.println(message);
return false;
}
/** Assign the initial value to the test variables */
private void setUp() {
first = new Student(10, "First", 100);
second = new Student(20, "Second", 90);
third = new Student(30, "Third", 80);
fourth = new Student(40, "Fourth", 90);
firstArrayListTwo = new Student(10, "First", 50);
secondArrayListTwo = new Student(20, "Second", 90);
thirdArrayListTwo = new Student(30, "Third", 80);
fourthArrayListTwo = new Student(40, "Fourth", 80);
fifthArrayListTwo = new Student(50, "Fifth", 90);
emptyArrayList = new ArrayList<Student>();
arrayList = new ArrayList<Student>();
arrayList.add(first);
arrayList.add(second);
arrayList.add(third);
arrayList.add(fourth);
arrayListTwo = new ArrayList<Student>();
arrayListTwo.add(firstArrayListTwo);
arrayListTwo.add(secondArrayListTwo);
arrayListTwo.add(thirdArrayListTwo);
arrayListTwo.add(fourthArrayListTwo);
arrayListTwo.add(fifthArrayListTwo);
}
/**
* Test the method <code>makeArrayList3</code>
*
* @return <code>true</code> if all test passed; otherwise returns
* <code>false</code>.
*/
public boolean testMakeArrayList() {
setUp();
boolean test = true;
ArrayList<Student> result =
StudentArrayList.makeArrayList(first, second, third);
test = test && assertTrue("1, testing method makeArrayList",
result instanceof ArrayList);
test = test && assertTrue("2, testing method makeArrayList",
result.size() == 3);
test = test && assertTrue("3, testing method makeArrayList",
result.get(0) == first);
test = test && assertTrue("4, testing method makeArrayList",
result.get(1) == second);
test = test && assertTrue("5, testing method makeArrayList",
result.get(2) == third);
return test;
}
/**
* Test the method <code>makeArrayListFromArray</code>
*
* @return <code>true</code> if all test passed; otherwise returns
* <code>false</code>.
*/
public boolean testMakeArrayListFromArray() {
setUp();
boolean test = true;
ArrayList<Student> listFromEmptyArray =
StudentArrayList.makeArrayListFromArray(new Student[0]);
test = test && assertTrue("1, testing method makeArrayListFromArray",
listFromEmptyArray instanceof ArrayList);
test = test && assertTrue("2, testing method makeArrayListFromArray",
listFromEmptyArray.size() == 0);
Student array[] = {first, second, third};
ArrayList<Student> result =
StudentArrayList.makeArrayListFromArray(array);
test = test && assertTrue("3, testing method makeArrayListFromArray",
result instanceof ArrayList);
test = test && assertTrue("4, testing method makeArrayListFromArray",
result.size() == 3);
test = test && assertTrue("5, testing method makeArrayListFromArray",
result.get(0) == first);
test = test && assertTrue("6, testing method makeArrayListFromArray",
result.get(1) == second);
test = test && assertTrue("7, testing method makeArrayListFromArray",
result.get(2) == third);
return test;
}
/**
* Test the method <code>hasStudent</code>.
*
* @return <code>true</code> if all test passed; otherwise returns
* <code>false</code>.
*/
public boolean testHasStudent() {
setUp();
boolean test = true;
test = test && assertTrue("1, testing method hasStudent",
! StudentArrayList.hasStudent(emptyArrayList, first.getId()));
test = test && assertTrue("2, testing method hasStudent",
StudentArrayList.hasStudent(arrayList, first.getId()));
test = test && assertTrue("3, testing method hasStudent",
StudentArrayList.hasStudent(arrayList, second.getId()));
test = test && assertTrue("4, testing method hasStudent",
StudentArrayList.hasStudent(arrayList, third.getId()));
test = test && assertTrue("5, testing method hasStudent",
StudentArrayList.hasStudent(arrayList, fourth.getId()));
test = test && assertTrue("6, testing method hasStudent",
! StudentArrayList.hasStudent(arrayList, 555));
return test;
}
/**
* Test the method <code>countGradeGreaterOrEqual</code>.
*
* @return <code>true</code> if all test passed; otherwise returns
* <code>false</code>.
*/
public boolean testCountGradeGreaterOrEqual() {
setUp();
boolean test = true;
test = test && assertTrue("1, testing method countGradeGreaterOrEqual",
StudentArrayList.countGradeGreaterOrEqual(arrayList, 50) == 4);
test = test && assertTrue("2, testing method countGradeGreaterOrEqual",
StudentArrayList.countGradeGreaterOrEqual(arrayList, 100) == 1);
return test;
}
/**
* Test the method <code>getMinGrade</code>.
*
* @return <code>true</code> if test passed; otherwise returns
* <code>false</code>.
*/
public boolean testGetMinGrade() {
setUp();
boolean test = true;
test = test && assertTrue("1, testing method getMinGrade",
StudentArrayList.getMinGrade(arrayList) == 80);
test = test && assertTrue("2, testing method getMinGrade",
StudentArrayList.getMinGrade(arrayListTwo) == 50);
return test;
}
/**
* Test the method <code>getGradeAverage</code>.
*
* @return <code>true</code> if test passed; otherwise returns
* <code>false</code>.
*/
public boolean testGetGradeAverage() {
setUp();
return assertTrue("1, testing method getGradeAverage",
StudentArrayList.getGradeAverage(arrayList) ==
(first.getGrade() + second.getGrade() + third.getGrade() +
fourth.getGrade()) / 4.0);
}
/**
* Test the method <code>removeGrad
ssd3 practical quiz 8
1星 需积分: 10 136 浏览量
2011-05-07
22:31:24
上传
评论
收藏 10KB ZIP 举报
jazwoo
- 粉丝: 268
- 资源: 39
最新资源
- 877668389368492摩托车队经理.apk
- IDM下载器官网免安装包,注册CMD文件,突破30天的限制
- qud.win.1.1-installer-10053.9.zip
- adb-r-linux.zip adb-r-linux.zip
- 对光伏出力预测的优化设计,实现了部分功能(注释完全,可直接运行)(文档加Matlab源码)
- 70-android.rules
- 可用于光伏出力预测,引入气象影响因子,效果良好(注释完全,可直接运行)(文档加Matlab源码)
- 利用白噪声特性和ARMR模型,通过求解模型参数生成随机风速(注释完全,可直接运行)(文档加Matlab源码)
- 51-android.rules
- RecationTest.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈