您的位置:首页 > 其它

简易四则运算生成程序——第一次改进后的单元测试

2016-09-28 21:05 381 查看
测试项目:减一四则运算生成程序

项目成员:张金生 张政

工程地址:https://coding.net/u/jx8zjs/p/paperOne/git

ssh://git@git.coding.net:jx8zjs/paperOne.git

测试单元概览

1. Fraction: 分数类,支持分数加减乘除法,约分,取相反数等

2.QuestionGen:题目生成类,支持生成各种难度的题目,和答案。

待测单元:

Fraction类:

public String generateQuestion(int numOfOperand, int rangeMin, int rangMax, SupportedOperation[] operation,
boolean isFractional, boolean hasbracket) {
String question = "";
int[] ioperands = null;
ArrayList<Fraction> af = new ArrayList<Fraction>();
SupportedOperation[] so = null;
if (numOfOperand < 2) {
System.out.println("操作数数量至少为2");
return "";
}
if (rangMax > 500) {
System.out.println("操作数数最大值不能超过500");
return "";
}
getBcPrint(numOfOperand);
if (!isFractional) {
ScriptEngine se = new ScriptEngineManager().getEngineByName("JavaScript");
ioperands = new int[numOfOperand];
for (int i = 0; i < numOfOperand; i++) {
ioperands[i] = (int) (Math.random() * rangMax / 2 + 1);

}
so = new SupportedOperation[numOfOperand - 1];
for (int i = 0; i < operation.length; i++) {
if (operation[i] == SupportedOperation.ALL) {
operation = new SupportedOperation[4];
operation[0] = SupportedOperation.ADD;
operation[1] = SupportedOperation.MINUS;
operation[2] = SupportedOperation.MULTIPLY;
operation[3] = SupportedOperation.DIVIDE;

}
}
// 除法運算,保证整除
int value = 0;
for (int j = numOfOperand - 1; j > 0; j--) {
so[numOfOperand - 1 - j] = operation[(int) (Math.random() * operation.length)];
}
for (int j = numOfOperand - 2; j >= 0; j--) {
if (so[j] == SupportedOperation.DIVIDE) {
if (value < 1) {
ioperands[j] = ioperands[j] * ioperands[j + 1];
value++;

} else {
so[j] = operation[(int) (Math.random() * (operation.length - 2))];
}
}
}
// 输出括号
for (int i = 0; i < numOfOperand - 1; i++) {
if (frequency.containsKey(i)) {
if (direction.get(i) == 0) {
for (int k = 0; k < frequency.get(i); k++) {
question += "(";
}
}
}
question += ioperands[i];
if (frequency.containsKey(i)) {
if (direction.get(i) == 1) {
for (int k = 0; k < frequency.get(i); k++) {
question += ")";
}
}
}
question += so[i];
}
if (frequency.containsKey(numOfOperand - 1)) {
if (direction.get(numOfOperand - 1) == 0) {
for (int k = 0; k < frequency.get(numOfOperand - 1); k++) {
question += "(";
}
}
}
question += ioperands[numOfOperand - 1];
if (frequency.containsKey(numOfOperand - 1)) {
if (direction.get(numOfOperand - 1) == 1) {
for (int k = 0; k < frequency.get(numOfOperand - 1); k++) {
question += ")";
}
}
}

try {
Integer d = (Integer) se.eval(question);
answer = "" + d;
} catch (Exception e) {
generateQuestion(numOfOperand, rangeMin, rangMax, operation, isFractional, hasbracket);
}

} else {
for (int i = 0; i < numOfOperand; i++) {
af.add(Fraction.getRandiom(rangMax));
}

so = new SupportedOperation[numOfOperand - 1];
for (int i = 0; i < operation.length; i++) {
if (operation[i] == SupportedOperation.ALL) {
operation = new SupportedOperation[4];
operation[0] = SupportedOperation.ADD;
operation[1] = SupportedOperation.MINUS;
operation[2] = SupportedOperation.MULTIPLY;
operation[3] = SupportedOperation.DIVIDE;

}
}
question += af.get(0);
for (int j = 0; j < numOfOperand - 1; j++) {
so[j] = operation[(int) (Math.random() * operation.length)];
question += (so[j] == SupportedOperation.DIVIDE ? "÷" : so[j].toString()) + af.get(j + 1);

}
answer = getanswer(af, so).toString();
try {
} catch (Exception e) {
e.printStackTrace();
}

}

return question;

}


View Code
测试类:

FractionTest

public class FractionTest {

Fraction f1 = new Fraction(9, 4);
Fraction f2 = new Fraction(16, 4);

@Test
public void testdown0() {
new Fraction(0, 0);
}

@Test
public void testGcd() {
assertEquals("4", f2.gcd(f2).toString());
}

@Test
public void testToString() {
assertEquals("2 1/4", f1.toString());
}

@Test
public void testAddFraction() {
assertEquals(new Fraction(25, 4).toString(), f1.add(f2).toString());
}

@Test
public void testMinusFraction() {
assertEquals(new Fraction(-7, 4).toString(), f1.minus(f2).toString());
}

@Test
public void testMultiplyFraction() {
assertEquals(new Fraction(144, 16).toString(), f1.multiply(f2).toString());
}

@Test
public void testDivideFraction() {
assertEquals(new Fraction(9, 16).toString(), f1.divide(f2).toString());
}

@Test
public void testChangeSign() {
assertEquals(new Fraction(-9, 4).toString(), f1.changeSign().toString());
}

@Test
public void testGetRandiomInt() {
Fraction a = Fraction.getRandiom(7);
System.out.println(a.toString());
}

@Test
public void testGetRandiomIntBoolean() {
Fraction a = Fraction.getRandiom(7, true);
Fraction b = Fraction.getRandiom(7, true);
System.out.println("true = " + a.toString() + "\nfalse = " + b.toString());
}

}


QuestionGenTest:

public class QuestionGenTest {

QuestionGen qg = new QuestionGen();
ArrayList<Fraction> flt = new ArrayList<Fraction>();
SupportedOperation[] sot = { SupportedOperation.ADD, SupportedOperation.MINUS, SupportedOperation.MULTIPLY,
SupportedOperation.DIVIDE };

@Test
public void testGetanswer() {

flt.add(new Fraction(1, 2));
flt.add(new Fraction(1, 3));
flt.add(new Fraction(1, 4));
flt.add(new Fraction(1, 3));
flt.add(new Fraction(1, 2));

Fraction r = qg.getanswer(flt, sot);
assertEquals("2/3", r.toString());

}

@Test
public void testGenerateSimpleQuestion() {
qg.generateSimpleQuestion();
}

@Test
public void testGenerateCommonQuestion() {
qg.generateCommonQuestion();
}

@Test
public void testGenerateMediumQuestion() {
qg.generateMediumQuestion();
}

@Test
public void testGenerateComplexQuestion() {
qg.generateComplexQuestion();
}

}


测试结果:

Fraction类测试结果:





QuestionGen类测试结果:



单元测试总结:

单元测试时发现了一些功能或方法定义了,但在使用的时候没有用到,有些可能预计不会再用到的则选择注释掉,这样可以增加代码覆盖率。测试过的单元和功能都能成功的达到想要的预期结果。

代码覆盖率:

Fraction类测试代码覆盖率:



QuestionGen类测试代码覆盖率:



总代吗覆盖率:



代码覆盖率总结:

Fraction类的代码覆盖率能达到97.4%,确实非常高,没有测试到的部分为一些复杂的随机分支,或者减少程序出错的校验内容。QuestionGen类的代码覆盖率为74.2%,一部分未测试部分也是由于随机程度够多的时候才能完全覆盖,本次测试也运行了多次测试用例,目前显示未覆盖的部分也是可达的,还有一部分未测试行是保证程序鲁棒性的代码。因此我们认为本次测试基本达到单元测试要求。

工程地址:https://coding.net/u/jx8zjs/p/paperOne/git

ssh://git@git.coding.net:jx8zjs/paperOne.git
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: