Java 代码求最大公约数的测试用例设计与脚本编写
Java 代码求最大公约数的测试用例设计与脚本编写
代码
public int getGCD(int x, int y)
{
if(x<1 || x>100)
{ System.out.println("参数不正确!");
return -1; }
if(y<1 || y>100)
{ System.out.println("参数不正确!");
return -1; }
int max,min,result=1;
if(x>=y)
{ max=x;
min=y; }
else
{ max=x;
min=y; }
for(int n=1; n<=min; n++)
{ if(min%n==0 && max%n==0)
{ if(n>result)
result=n; }
}
System.out.println("最大公约数为 :" + result);
return result;
}
测试用例
- 输入 x=50, y=80,预期输出最大公约数为 10。
- 输入 x=0, y=50,预期输出参数不正确,返回值为 -1。
- 输入 x=50, y=0,预期输出参数不正确,返回值为 -1。
- 输入 x=101, y=50,预期输出参数不正确,返回值为 -1。
- 输入 x=50, y=101,预期输出参数不正确,返回值为 -1。
- 输入 x=50, y=50,预期输出最大公约数为 50。
测试脚本
public class GCDTest {
public static void main(String[] args) {
GCD gcd = new GCD();
// 测试用例1
int result = gcd.getGCD(50, 80);
if (result == 10) {
System.out.println("测试用例1通过");
} else {
System.out.println("测试用例1不通过");
}
// 测试用例2
result = gcd.getGCD(0, 50);
if (result == -1) {
System.out.println("测试用例2通过");
} else {
System.out.println("测试用例2不通过");
}
// 测试用例3
result = gcd.getGCD(50, 0);
if (result == -1) {
System.out.println("测试用例3通过");
} else {
System.out.println("测试用例3不通过");
}
// 测试用例4
result = gcd.getGCD(101, 50);
if (result == -1) {
System.out.println("测试用例4通过");
} else {
System.out.println("测试用例4不通过");
}
// 测试用例5
result = gcd.getGCD(50, 101);
if (result == -1) {
System.out.println("测试用例5通过");
} else {
System.out.println("测试用例5不通过");
}
// 测试用例6
result = gcd.getGCD(50, 50);
if (result == 50) {
System.out.println("测试用例6通过");
} else {
System.out.println("测试用例6不通过");
}
}
}
逻辑覆盖测试
- 输入 x=-1, y=50,预期输出参数不正确,返回值为 -1。
- 输入 x=50, y=-1,预期输出参数不正确,返回值为 -1。
- 输入 x=0, y=50,预期输出参数不正确,返回值为 -1。
- 输入 x=50, y=0,预期输出参数不正确,返回值为 -1。
- 输入 x=101, y=50,预期输出参数不正确,返回值为 -1。
- 输入 x=50, y=101,预期输出参数不正确,返回值为 -1。
- 输入 x=50, y=50,预期输出最大公约数为 50。
基本路径测试
我们将 GCD 方法的控制流图画出来,然后使用基本路径测试方法,覆盖所有基本路径。
控制流图:

基本路径:
- 1-2-3-4-5-6-7-8-9-10-11-12-13
- 1-2-3-4-5-6-7-8-9-10-11-14
- 1-2-3-4-5-6-7-8-9-15-16
- 1-2-3-4-5-6-7-8-9-10-11-17-18-19
- 1-2-3-4-5-6-7-8-9-10-11-17-18-20
- 1-2-3-4-5-6-7-8-9-10-11-17-21-22-23
- 1-2-3-4-5-6-7-8-9-10-11-17-21-22-24
- 1-2-3-4-5-6-7-8-9-10-11-17-21-25-26
- 1-2-3-4-5-6-7-8-9-10-11-17-21-25-27
- 1-2-3-4-5-6-7-8-9-10-11-17-21-22-23-28-29-30-13
- 1-2-3-4-5-6-7-8-9-10-11-17-21-22-24-31-29-30-13
- 1-2-3-4-5-6-7-8-9-10-11-17-21-25-26-31-29-30-13
- 1-2-3-4-5-6-7-8-9-10-11-17-21-25-27-31-29-30-13
测试脚本
public class GCDTest {
public static void main(String[] args) {
GCD gcd = new GCD();
// 逻辑覆盖测试
int result = gcd.getGCD(-1, 50);
if (result == -1) {
System.out.println("逻辑覆盖测试1通过");
} else {
System.out.println("逻辑覆盖测试1不通过");
}
result = gcd.getGCD(50, -1);
if (result == -1) {
System.out.println("逻辑覆盖测试2通过");
} else {
System.out.println("逻辑覆盖测试2不通过");
}
result = gcd.getGCD(0, 50);
if (result == -1) {
System.out.println("逻辑覆盖测试3通过");
} else {
System.out.println("逻辑覆盖测试3不通过");
}
result = gcd.getGCD(50, 0);
if (result == -1) {
System.out.println("逻辑覆盖测试4通过");
} else {
System.out.println("逻辑覆盖测试4不通过");
}
result = gcd.getGCD(101, 50);
if (result == -1) {
System.out.println("逻辑覆盖测试5通过");
} else {
System.out.println("逻辑覆盖测试5不通过");
}
result = gcd.getGCD(50, 101);
if (result == -1) {
System.out.println("逻辑覆盖测试6通过");
} else {
System.out.println("逻辑覆盖测试6不通过");
}
result = gcd.getGCD(50, 50);
if (result == 50) {
System.out.println("逻辑覆盖测试7通过");
} else {
System.out.println("逻辑覆盖测试7不通过");
}
// 基本路径测试
result = gcd.getGCD(-1, 50);
if (result == -1) {
System.out.println("基本路径测试1通过");
} else {
System.out.println("基本路径测试1不通过");
}
result = gcd.getGCD(50, -1);
if (result == -1) {
System.out.println("基本路径测试2通过");
} else {
System.out.println("基本路径测试2不通过");
}
result = gcd.getGCD(0, 50);
if (result == -1) {
System.out.println("基本路径测试3通过");
} else {
System.out.println("基本路径测试3不通过");
}
result = gcd.getGCD(50, 0);
if (result == -1) {
System.out.println("基本路径测试4通过");
} else {
System.out.println("基本路径测试4不通过");
}
result = gcd.getGCD(101, 50);
if (result == -1) {
System.out.println("基本路径测试5通过");
} else {
System.out.println("基本路径测试5不通过");
}
result = gcd.getGCD(50, 101);
if (result == -1) {
System.out.println("基本路径测试6通过");
} else {
System.out.println("基本路径测试6不通过");
}
result = gcd.getGCD(50, 50);
if (result == 50) {
System.out.println("基本路径测试7通过");
} else {
System.out.println("基本路径测试7不通过");
}
result = gcd.getGCD(50, 80);
if (result == 10) {
System.out.println("基本路径测试8通过");
} else {
System.out.println("基本路径测试8不通过");
}
result = gcd.getGCD(50, 50);
if (result == 50) {
System.out.println("基本路径测试9通过");
} else {
System.out.println("基本路径测试9不通过");
}
result = gcd.getGCD(50, 80);
if (result == 10) {
System.out.println("基本路径测试10通过");
} else {
System.out.println("基本路径测试10不通过");
}
}
}
原文地址: https://www.cveoy.top/t/topic/lJW8 著作权归作者所有。请勿转载和采集!