📄 driver.java
字号:
/*
* Created on 2005-6-26
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package test;
import junit.framework.Assert;
import junit.framework.TestCase;
import java.util.EmptyStackException;
import util.Stack;
/**
* @author
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class Driver extends TestCase {
/* This the ClassBench framework
* It is base on the path cover
* In this Test Class we used seven paths
*
for each test suite parameter value do
for each path P required for arc coverage of the
testgraph do
invoke driver's reset() method
for each node/arc Y in P do
if Y is an arc then
invoke driver's arc() method
if Y is an node then
invoke driver's node() method
*
*
*/
//final int data as indentify that used in method arc()
private final int PUSH = 1;
private final int POP = -1;
//stub; used in this Driver;
private Stack s; //test target
private Oracle ora; //stub class used to check the output
//inner data used in class cast
private Integer sInt;
private Integer oraInt;
//test path; every path hava a different max elemnt number
private int[] path0 = {-1}; //max number is 0
private int[] path1 = {1,-1}; //max number is 1
private int[] path2 = {1,1,-1}; //max number is 2
private int[] path3 = {1,1,1,-1}; //max number is 3
/*
private int[] path4 = {1,-1}; //max number is 4
private int[] path5 = {1,1,-1}; //max number is 5
private int[] path6 = {1,1,1,-1}; //max number is 6
*/
//non-null test data used in push operation
private Integer[] data1 = {new Integer(1)};
private Integer[] data2 = {new Integer(1),new Integer(2)};
private Integer[] data3 = {new Integer(1),new Integer(2),new Integer(3)};
//iclude some null test data used in push operation
private Integer[] data4 = {null};
private Integer[] data5 = {null,new Integer(1)};
private Integer[] data6 = {null,new Integer(1),new Integer(2)};
//test data index that point out that the next push element
private int index;
public Driver(){
};
public void reset(){ //reinitialize all the instence and para
s = new Stack();
ora = new Oracle();
index = 0;
}
public void arc(int arcTag,int pathNum){ //cause the stute change
switch(arcTag){
case PUSH:
TPush(pathNum);
break;
case POP:
TPop();
break;
}
}
public void nodeCheck(){ //Check the operation on this state is right
TPeek();
TEmpty();
TSearch();
}
public static void main(String[] args) {
junit.textui.TestRunner.run(Driver.class);
}
/*
* @see TestCase#setUp()
*/
protected void setUp() throws Exception {
super.setUp();
//do noting
}
/*
* @see TestCase#tearDown()
*/
protected void tearDown() throws Exception {
super.tearDown();
//do noting
}
public void TPush(int pathNum) { //impelement the push operation
switch(pathNum){
case 1:
sInt = (Integer)s.push(data1[index]);
oraInt = (Integer)ora.push(data1[index++]);
if(oraInt != null){
Assert.assertEquals(oraInt.intValue(),sInt.intValue());
}else{
Assert.assertEquals(oraInt,null);
Assert.assertEquals(oraInt,sInt);
}
break;
case 2:
sInt = (Integer)s.push(data2[index]);
oraInt = (Integer)ora.push(data2[index++]);
if(oraInt != null){
Assert.assertEquals(oraInt.intValue(),sInt.intValue());
}else{
Assert.assertEquals(oraInt,null);
Assert.assertEquals(oraInt,sInt);
}
break;
case 3:
sInt = (Integer)s.push(data3[index]);
oraInt = (Integer)ora.push(data3[index++]);
if(oraInt != null){
Assert.assertEquals(oraInt.intValue(),sInt.intValue());
}else{
Assert.assertEquals(oraInt,null);
Assert.assertEquals(oraInt,sInt);
}
break;
case 4:
sInt = (Integer)s.push(data4[index]);
oraInt = (Integer)ora.push(data4[index++]);
if(oraInt != null){
Assert.assertEquals(oraInt.intValue(),sInt.intValue());
}else{
Assert.assertEquals(oraInt,null);
Assert.assertEquals(oraInt,sInt);
}
break;
case 5:
sInt = (Integer)s.push(data5[index]);
oraInt = (Integer)ora.push(data5[index++]);
if(oraInt != null){
Assert.assertEquals(oraInt.intValue(),sInt.intValue());
}else{
Assert.assertEquals(oraInt,null);
Assert.assertEquals(oraInt,sInt);
}
break;
case 6:
sInt = (Integer)s.push(data6[index]);
oraInt = (Integer)ora.push(data6[index++]);
if(oraInt != null){
Assert.assertEquals(oraInt.intValue(),sInt.intValue());
}else{
Assert.assertEquals(oraInt,null);
Assert.assertEquals(oraInt,sInt);
}
break;
//if there is some more path shoulde change here
}
nodeCheck();
}
public void TPop() { //Check the pop oparation is right
if(ora.empty()){
try{
s.pop();
Assert.fail("Should not step into here!");
}catch(EmptyStackException ese){
Assert.assertTrue(true);
}
}else{
sInt = (Integer)s.pop();
oraInt = (Integer)ora.pop();
if(oraInt != null){
Assert.assertEquals(oraInt.intValue(),sInt.intValue());
}else{
Assert.assertEquals(oraInt,null);
Assert.assertEquals(oraInt,sInt);
}
nodeCheck();
}
}
public void TPeek() { //Check the peek oparation is right
if(ora.empty()){
try{
s.peek();
Assert.fail("Should not step into here!");
}catch(EmptyStackException ese){
Assert.assertTrue(true);
}
}else{
sInt = (Integer)s.peek();
oraInt = (Integer)ora.peek();
if(oraInt != null){
Assert.assertEquals(oraInt.intValue(),sInt.intValue());
}else{
Assert.assertEquals(oraInt,null);
Assert.assertEquals(oraInt,sInt);
}
}
}
public void TEmpty() { //Check the empty operation is right
Assert.assertEquals(s.empty(),ora.empty());
}
public void TSearch() { //Check the search operation is right
for(int i = ora.getSize(); i > 0; i--){
Assert.assertEquals(ora.search(ora.get(i)),s.search(ora.get(i)));
}
Assert.assertEquals(-1,ora.search(new Integer(100)));
Assert.assertEquals(ora.search(new Integer(100)),s.search(new Integer(100)));
}
public void testPath0(){ //first test case : the stack should contain noting
reset();
arc(path0[0],0);
};
public void testPath1(){ //first test case : the stack should contain
//at most one element
reset();
for(int i = 0; i < path1.length; i++){
arc(path1[i],1);
}
};
public void testPath2(){ //first test case : the stack should contain
//at most two element
reset();
for(int i = 0; i < path2.length; i++){
arc(path2[i],2);
}
};
public void testPath3(){ //first test case : the stack should contain
//at most three element
reset();
for(int i = 0; i < path3.length; i++){
arc(path3[i],3);
}
};
public void testPath4(){ //first test case : the stack should contain
//at most one element
reset();
for(int i = 0; i < path1.length; i++){
arc(path1[i],4);
}
};
public void testPath5(){ //first test case : the stack should contain
//at most one element
reset();
for(int i = 0; i < path1.length; i++){
arc(path1[i],5);
}
};
public void testPath6(){ //first test case : the stack should contain
//at most one element
reset();
for(int i = 0; i < path1.length; i++){
arc(path1[i],6);
}
};
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -