第一题
package cn.wu.com; import java.util.Scanner; public class GCD{ public static int fun1(int a1,int b1){ int c; c=a1%b1; while (c>0) {a1=b1; b1=c; c=a1%b1; } return b1; } public static void main(String[] args) { int a,b,temp; int m; Scanner in=new Scanner(System.in); a=in.nextInt(); b=in.nextInt(); if(a<b) {temp=a;a=b;b=temp; } m=fun1(a,b); System.out.println("最大公约数为:"+m); } }
第二题
package cn.wu.com; public class LogicCoverage { /** * * 逻辑覆盖的简单练习 */ public static void t(int X,int Y){ if(X<4||Y>0){ if(Y>1){ Y=Y+1; System.out.println("Y="+Y); } else{ System.out.println("Y="+Y+","+"X="+X); return; } } else{ if(X>=5){ X=X-Y; System.out.println("X="+X); } else{ X=X+Y; System.out.println("X="+X); } } } }
JUnit编写测试用例
package cn.wu.com; import static org.junit.Assert.*; import java.util.Scanner; import org.junit.Test; public class LogicCoverageTest { @Test public void test() { LogicCoverageTest.t(); } private static void t() { // TODO Auto-generated method stub int X; int Y; Scanner s=new Scanner(System.in); System.out.println("请输入第一个数X: "); X=s.nextInt(); System.out.println("请输入第一个数Y: "); Y=s.nextInt(); } }
T1:X<4,T2:Y>0,T3:X>5,T4:Y>1,A1,A2,A3
语句覆盖的测试用例
用例编号 |
输入的数据 |
预期的输出 |
实际的输出 |
执行路径 |
条件覆盖 |
判定节点覆盖 |
1 |
X=6,Y=-1 |
X=7 |
X=7 |
1-2-5-7 |
T1,-T2, T3,-T4 |
-A1,A2 |
2 |
X=5,Y=1 |
X=4 |
X=4 |
1-2-4-7 |
-T1,-T2, -T3,-T4 |
-A1,-A2 |
3 |
X=3,Y=2 |
Y=3 |
Y=3 |
1-3-6-7 |
T1,T2 -T3,T4 |
A1,A3 |
分支覆盖的测试用例
用例编号 |
输入的数据 |
预期的输出 |
实际的输出 |
执行路径 |
条件覆盖 |
判定节点覆盖 |
1 |
X=6,Y=-1 |
X=7 |
X=7 |
1-2-5-7 |
T1,-T2, T3,-T4 |
-A1,A2 |
2 |
X=5,Y=1 |
X=4 |
X=4 |
1-2-4-7 |
-T1,-T2, -T3,-T4 |
-A1,-A2 |
3 |
X=3,Y=2 |
Y=3 |
Y=3 |
1-3-6-7 |
T1,T2 -T3,T4 |
A1,A3 |
4 |
X=3,Y=1 |
|
|
1-3-7 |
T1,T2 -T3,-T4 |
A1,-A3 |