结对编程之附加题:单元测试

我的学号:201421122044

队友学号:201421122040

coding地址:https://coding.net/u/gebeelaowang/p/FourArithmeticOperation/git

 

1.把计算模块提取出来,单独创建一个类。

  提取计算模块,封装成单独的一个计算类NPR.java

   类中主要方法为

     1.中缀表达式转后缀表达式

        2.后缀表达式计算生成结果

  1 public class NPR {
  2     public String getFindTheSame() {
  3         return findTheSame;
  4     }
  5     public void setFindTheSame(String findTheSame) {
  6         this.findTheSame = findTheSame;
  7     }
  8     //判断ch是否为运算符
  9     public boolean inOp(char c){
 10         if (c == '(' || c == ')' || c == '+' || c == '-' || c == '*' || c == '÷'){
 11             return true;
 12         }
 13         else{
 14             return false;
 15         }
 16     }
 17     //判断ch是否为运算符
 18     public int precede(int op1,int op2){
 19         if (op1 == op2){
 20             return 0;
 21         }
 22         else if (op1 < op2){
 23             return -1;
 24         }
 25         else{
 26             return 1;
 27         }
 28     }
 29     
 30     //逆波兰式
 31     public String exchangeNPR(String formula) {
 32         //用哈希键值对映射左运算符的优先级
 33         HashMap<String, Integer> lpri = new HashMap<String,Integer>();
 34         lpri.put("=",0);
 35         lpri.put("(",1);
 36         lpri.put("*",5);
 37         lpri.put("÷",5);
 38         lpri.put("+",3);
 39         lpri.put("-",3);
 40         lpri.put(")",6);
 41         //用哈希键值对映射右运算符的优先级
 42         HashMap<String, Integer> rpri = new HashMap<String,Integer>();
 43         rpri.put("=",0);
 44         rpri.put("(",6);
 45         rpri.put("*",4);
 46         rpri.put("÷",4);
 47         rpri.put("+",2);
 48         rpri.put("-",2);
 49         rpri.put(")",1);
 50         //创建栈
 51         Stack op = new Stack();
 52         op.data = new char[200];
 53         op.top=-1;
 54         op.top++;
 55         op.data[op.top]='=';
 56         //把传入的表达式转化成字符数组
 57         char[] exp = formula.toCharArray();
 58         //用来存储后缀表达式
 59         String postexp = ""; 
 60         int j=0;
 61         while(j<exp.length) {
 62             if(!inOp(exp[j])) {
 63                 while(j<exp.length&&((exp[j]>='0'&& exp[j]<='9')||(exp[j]=='\'')||(exp[j]=='/'))) {
 64 //                    System.out.println(exp.length);
 65                     postexp+=(exp[j]);
 66                     j++;
 67                 }
 68                 postexp+=('#');
 69             }else {
 70                 switch(precede(lpri.get(op.data[op.top]+""),rpri.get(exp[j]+""))){
 71                 case -1:
 72                     op.top++;
 73                     op.data[op.top]=exp[j];
 74                     j++;
 75                     break;
 76                 case 0:
 77                     op.top--;
 78                     j++;
 79                     break;
 80                 case 1:
 81                     postexp+=(op.data[op.top]);
 82                     op.top--;
 83                     break;
 84                 }
 85             }    
 86         }
 87         while(op.data[op.top]!='=') {
 88             postexp+=(op.data[op.top]);
 89             op.top--;
 90         }
 91         return postexp;
 92     }
 93     //把运算数转换成数组[分子,分母]
 94     public int[] numberChangeArr(String number){
 95         int num[] = new int[2];
 96         if(number.indexOf('/')!=-1) {
 97             if(number.indexOf('\'')!=-1) {
 98                 int theTimes = Integer.parseInt(number.split("\'")[0]);
 99                 int numerator = Integer.parseInt((number.split("\'")[1]).split("/")[0]);
100                 int denominator = Integer.parseInt((number.split("\'")[1]).split("/")[1]);
101                 num[0] = theTimes*denominator+numerator;
102                 num[1] = denominator;
103             }else {
104                 num[0] = Integer.parseInt(number.split("/")[0]);
105                 num[1] = Integer.parseInt(number.split("/")[1]);
106             }
107         }else {
108             num[0] = Integer.parseInt(number);
109             num[1] = 1;
110         }
111         return num;
112     }
113     //后缀表达式的计算
114     public String compvalue(String nprFormula) {
115         this.findTheSame = ""; //查重用字符串
116         Stack st = new Stack();
117         st.top=-1;
118         st.data2 = new String[200];
119         int i=0;
120         char[] postexp = nprFormula.toCharArray();
121         while(i<postexp.length) {
122             switch(postexp[i]) {
123             case '+':
124                 String a = st.data2[st.top];
125                 st.top--;
126                 String b = st.data2[st.top];
127                 st.top--;
128                 String c = "";
129                 int numeratorA = numberChangeArr(a)[0];
130                 int denominatorA = numberChangeArr(a)[1];
131                 int numeratorB = numberChangeArr(b)[0];
132                 int denominatorB = numberChangeArr(b)[1];
133                 int theMinTimes = MathUtils.minTimes(denominatorA,denominatorB);
134                 numeratorA=(theMinTimes/denominatorA)*numeratorA;
135                 numeratorB=(theMinTimes/denominatorB)*numeratorB;
136                 int theAdd = numeratorA+numeratorB;
137                 c+=MathUtils.simpleNum(theAdd,theMinTimes);
138                 st.top++;
139                 st.data2[st.top]=c;
140                 //查重用
141                 if(numeratorA>numeratorB) {
142                     this.findTheSame+=b;
143                     this.findTheSame+=a;
144                 }else {
145                     this.findTheSame+=a;
146                     this.findTheSame+=b;
147                 }
148                 this.findTheSame+=c+" ";
149                 break;
150             case '-':
151                 String a2 = st.data2[st.top];
152                 st.top--;
153                 String b2 = st.data2[st.top];
154                 st.top--;
155                 String c2 = "";
156                 int numeratorA2 = numberChangeArr(a2)[0];
157                 int denominatorA2 = numberChangeArr(a2)[1];
158                 int numeratorB2 = numberChangeArr(b2)[0];
159                 int denominatorB2 = numberChangeArr(b2)[1];
160                 int theMinTimes2 = MathUtils.minTimes(denominatorA2,denominatorB2);
161                 int theReduct = 0;
162                 theReduct = numeratorB2-numeratorA2;
163                 numeratorA2=(theMinTimes2/denominatorA2)*numeratorA2;
164                 numeratorB2=(theMinTimes2/denominatorB2)*numeratorB2;
165                 theReduct = numeratorB2-numeratorA2;
166                 if(theReduct==0) {
167                     c2+="0";
168                 }else if(theReduct>0) {
169                     c2+=MathUtils.simpleNum(theReduct,theMinTimes2);
170                 }else {
171                     theReduct=Math.abs(theReduct);
172                     c2+="-"+MathUtils.simpleNum(theReduct,theMinTimes2);
173                 }
174                 st.top++;
175                 st.data2[st.top]=c2;
176             case '÷':
177                 String a3 = st.data2[st.top];
178                 st.top--;
179                 String b3 = st.data2[st.top];
180                 st.top--;
181                 String c3 = "";
182                 int numeratorA3 = numberChangeArr(a3)[0];
183                 int denominatorA3 = numberChangeArr(a3)[1];
184                 int numeratorB3 = numberChangeArr(b3)[0];
185                 int denominatorB3 = numberChangeArr(b3)[1];
186                 if(numeratorA3!=0) {
187                     int theNumerator = numeratorB3*denominatorA3;
188                     int theDenominator = denominatorB3*numeratorA3;
189                     c3 += MathUtils.simpleNum(theNumerator,theDenominator);
190                     st.top++;
191                     st.data2[st.top]=c3;
192                 }else {
193                     c3="Error! divide zero exception!";
194                     return c3;
195                 }
196                 break;
197             case '*':
198                 String a4 = st.data2[st.top];
199                 st.top--;
200                 String b4 = st.data2[st.top];
201                 st.top--;
202                 String c4 = "";
203                 int numeratorA4 = numberChangeArr(a4)[0];
204                 int denominatorA4 = numberChangeArr(a4)[1];
205                 int numeratorB4 = numberChangeArr(b4)[0];
206                 int denominatorB4 = numberChangeArr(b4)[1];
207                 int numerator = numeratorA4*numeratorB4;
208                 int denominator = denominatorA4*denominatorB4;
209                 c4+=MathUtils.simpleNum(numerator,denominator);
210                 st.top++;
211                 st.data2[st.top]=c4;
212                 break;
213             default:
214                 String d = "";
215                 while((postexp[i]>='0'&&postexp[i]<='9')||(postexp[i]=='\'')||(postexp[i]=='/')) {
216                     d+=postexp[i];
217                     i++;
218                 }
219                 st.top++;
220                 st.data2[st.top]=d;
221                 break;
222             }
223             i++;
224         }
225         return st.data2[st.top];
226     }
227     
228     
229 
230 }

2.需求分析:测试上有哪些详细的需求?

  1. 通过单元测试代码,测试加法是否能正确工作;
  2. 通过单元测试代码,测试加减乘除功能。
  3. 通过单元测试代码,测试计算类对于各种参数的支持。
  4. 通过增量修改的方式,改进程序, 完成对各种错误情况的处理。

3.设计测试框架, 模拟测试数据

  1.加法测试

    测试数据:3+4+5

    预期结果:12

  2.减法测试

    测试数据:3-4-5-1/2

    预期结果:-6'1/2

  3.乘法测试

    测试数据:2*5*1/2

    预期结果:5

  4.除法测试

    测试数据:2÷5÷1/2

    预期结果:4/5

  5.混合参数类型,混合运算符类型测试

    测试数据:1/3+1'1/3*2÷1/5-(8-4)

    预期结果:9'2/3

  6.除零异常测试

    测试数据:7÷(4-4)

    预期结果:Error! divide zero exception!

 

  Junit类测试代码

 1 package com.jmu.test;
 2 import static org.junit.Assert.*;
 3 import org.junit.Before;
 4 import org.junit.Test;
 5 import com.jmu.model.NPR;
 6 public class NPRTest {
 7 
 8     @Before
 9     public void setUp() throws Exception {
10     }
11     
12     //加法测试
13     @Test
14     public void testAdd() {
15         NPR testNpr = new NPR();
16         String testOutcome = testNpr.compvalue(testNpr.exchangeNPR("3+4+5"));
17         assertEquals(testOutcome,"12");
18     }
19     
20     //减法测试
21     @Test
22     public void testMinus() {
23         NPR testNpr = new NPR();
24         String testOutcome = testNpr.compvalue(testNpr.exchangeNPR("3-4-5-1/2"));
25         assertEquals(testOutcome,"-6'1/2");
26     }
27     
28     //乘法测试
29     @Test
30     public void testMultiply() {
31         NPR testNpr = new NPR();
32         String testOutcome = testNpr.compvalue(testNpr.exchangeNPR("2*5*1/2"));
33         assertEquals(testOutcome,"5");
34     }
35     
36     //除法测试
37     @Test
38     public void testDivede() {
39         NPR testNpr = new NPR();
40         String testOutcome = testNpr.compvalue(testNpr.exchangeNPR("2÷5÷1/2"));
41         assertEquals(testOutcome,"4/5");
42     }
43     
44     //计算类对于各种参数的支持,各类型运算元混合测试
45     @Test
46     public void testMess() {
47         NPR testNpr = new NPR();
48         String testOutcome = testNpr.compvalue(testNpr.exchangeNPR("1/3+1'1/3*2÷1/5-(8-4)"));
49         assertEquals(testOutcome,"9'2/3");
50     }
51     
52     //除零异常测试
53     @Test
54     public void zeroException() {
55         NPR testNpr = new NPR();
56         String testOutcome = testNpr.compvalue(testNpr.exchangeNPR("7÷(4-4)"));
57         assertEquals(testOutcome,"Error! divide zero exception!");
58     }
59 }

 

  2.测试结果

  

  如图,通过Junit单元测试,可以直观地看出所有函数运行结果与预期出现的结果一致

    coding提交记录

    

 

4.小结与感受:通过测试,是否有效发现了程序计算模块的问题,并给予改进?

  实验一的时候不是使用Junit测试,而是自己单独创建一个类Test.java

  然后用类里面的main函数实例化出这个计算类,对每个函数进行测试

  计算过程中的bug已经在实验一测试的时候排除

  然而,使用Junit可以更加直观地看出测试结果

  在后面的编码过程中,我打算更多地使用Junit来进行单元测试,测试过程更加方便,测试结果更加直观

5.PSP

  

PSP2.1 Personal Software Process Stages Time Senior Student Time
Planning 计划 3 1
· Estimate 估计这个任务需要多少时间 24 24
Development 开发 7 12
· Analysis 需求分析 (包括学习新技术) 10 3
· Design Spec 生成设计文档 NaN 0
· Design Review 设计复审 NaN 0
· Coding Standard 代码规范 NaN 0
· Design 具体设计 NaN 20
· Coding 具体编码 NaN 20
· Code Review 代码复审 NaN 10
· Test 测试(自我测试,修改代码,提交修改) NaN 2
Reporting 报告 NaN 10
· 测试报告 NaN 0
· 计算工作量 NaN 0
· 并提出过程改进计划 NaN 0
posted @ 2017-10-28 11:08  xanxuskkk  阅读(256)  评论(0编辑  收藏  举报