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 quiz7答案
2星 需积分: 9 98 浏览量
2008-11-28
10:17:44
上传
评论
收藏 8KB RAR 举报
liufuqi
- 粉丝: 0
- 资源: 1
最新资源
- 基于python+opencv实现的暗通道先验的单幅图像去雾算法python仿真源代码+文档说明+截图演示(高分课程设计)
- 基于STM32G4与智能摄像头的疲劳驾驶检测系统源代码(96分课程设计)
- 单片机课后答案.doc
- 单自由度系统自由振动matlab程序.docx
- 高分课程设计-基于opencvdlib的疲劳驾驶检测系统python源码+文档说明+界面演示(带ui界面)
- 哈工大c语言-练习题.doc
- Springboot集成Netflix-ribbon、Consul实现负载均衡调用-源码
- 大一大二常用命令大全笔记demo
- Vision语言文件读写操作代码笔记
- 大数据、云计算和人工智能的深度剖析与相互关系-PPT.ppt
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈