文章摘要
本文主要是分享一下慕测平台ISTC 2022 Practice的答案。
(本人刚刚入门软件测试,主要是参加这个比赛,答案可能不对,仅供参考)
结果展示:
相对来说,MoreTriangle更简单一些;Calculator涉及到一些控制台输出检测(我只覆盖,没有断言),异常抛出等,而且好像有的代码根本执行不到,比如:
- 黄色的FALSEf分支以及红色的返回语句
- 红色部分也是执行不到
Calculator测试代码
package net.mooctest;
import static org.junit.Assert.*;
import org.junit.Test;
public class CalculatorTest {
Calculator c= new Calculator();
@Test
public void testCommand() {
CommandNotFoundException e =new CommandNotFoundException("c");
assertEquals("c", e.getCommand());
ExpressionParserException exception= new ExpressionParserException("eee");
assertEquals("eee", exception.getFaultyExpression());
FunctionNotFoundException ffException =new FunctionNotFoundException("ff");
assertEquals("ff", ffException.getFunc());
MissingOperandException mm = new MissingOperandException("an", "+");
assertEquals("+", mm.getOperator());
UnmatchedBracketsException unmatchedBracketsException= new UnmatchedBracketsException("ab", 1);
assertEquals(1, unmatchedBracketsException.getIndexOfBracket());
VariableNotFoundException vv1 =new VariableNotFoundException("vva");
assertEquals("vva", vv1.getVar());
VariableNotFoundException vv2 =new VariableNotFoundException("v","vva");
assertEquals("vva", vv1.getVar());
}
@Test(expected=NullExpressionException.class)
public void testEvaluate00() throws Throwable {
Calculator.expParser=new ExpressionParser(2);
Calculator.evaluate("");
}
@Test
public void testEvaluate01() throws Throwable {
Calculator.expParser=new ExpressionParser(2);
Calculator.evaluate("8");
Calculator.evaluate(" 8 = 8");
Calculator.parseCommand("list vars");
}
@Test(expected=ExpressionParserException.class)
public void testEvaluate02() throws Throwable {
Calculator.expParser=new ExpressionParser(2);
Calculator.evaluate("1+2=3");
}
@Test
public void testEvaluate03() throws Throwable {
Calculator.expParser=new ExpressionParser(2);
String exp="8 = 8";
Calculator.evaluate(exp);
Calculator.expParser.addVariable("8", "10");
Calculator.expParser.addVariable("9", "10");
}
@Test
public void testParseCommand00() throws Throwable {
Calculator.parseCommand("exit");
Calculator.parseCommand("help");
Calculator.parseCommand("help vars");
Calculator.parseCommand("help cmds");
Calculator.parseCommand("help funcs");
Calculator.parseCommand("list");
Calculator.parseCommand("list cmds");
Calculator.parseCommand("list funcs");
}
@Test(expected=CommandNotFoundException.class)
public void testParseCommand01() throws Throwable {
Calculator.parseCommand("nono");
}
@Test
public void testMathParserFactorial() {
Calculator.expParser = new ExpressionParser(10);
assertEquals(1, Calculator.expParser.factorial(1),0);
assertEquals(2, Calculator.expParser.factorial(2),0);
assertEquals(16, Calculator.expParser.solveBinaryOperation(4, "^", 2),0);
assertEquals(0, Calculator.expParser.solveBinaryOperation(4, "%", 2),0);
assertEquals(2, Calculator.expParser.solveBinaryOperation(4, "/", 2),0);
assertEquals(8, Calculator.expParser.solveBinaryOperation(4, "*", 2),0);
assertEquals(6, Calculator.expParser.solveBinaryOperation(4, "+", 2),0);
assertEquals(2, Calculator.expParser.solveBinaryOperation(4, "-", 2),0);
assertEquals(0, Calculator.expParser.solveBinaryOperation(4, "--", 2),0);
}
@Test
public void testMathParserSolveUnaryFunction00() throws Throwable {
assertEquals(Math.sin(30), Calculator.expParser.solveUnaryFunction("sin", 30),0);
assertEquals(Math.cos(30), Calculator.expParser.solveUnaryFunction("cos", 30),0);
assertEquals(Math.tan(30), Calculator.expParser.solveUnaryFunction("tan", 30),0);
assertEquals(1.0/Math.sin(30), Calculator.expParser.solveUnaryFunction("csc", 30),0);
assertEquals(1.0/Math.cos(30), Calculator.expParser.solveUnaryFunction("sec", 30),0);
assertEquals(1.0/Math.tan(30), Calculator.expParser.solveUnaryFunction("ctn", 30),0);
assertEquals(Math.toRadians(30), Calculator.expParser.solveUnaryFunction("rad", 30),0);
assertEquals(Math.toDegrees(30), Calculator.expParser.solveUnaryFunction("deg", 30),0);
assertEquals(Calculator.expParser.factorial(30), Calculator.expParser.solveUnaryFunction("fct", 30),0);
assertEquals(Math.abs(30), Calculator.expParser.solveUnaryFunction("abs", 30),0);
assertEquals(Math.exp(30), Calculator.expParser.solveUnaryFunction("exp", 30),0);
assertEquals(Math.log(30), Calculator.expParser.solveUnaryFunction("log", 30),0);
}
@Test(expected=FunctionNotFoundException.class)
public void testMathParserSolveUnaryFunction01() throws Throwable {
assertEquals(Math.sin(30), Calculator.expParser.solveUnaryFunction("ssin", 30),0);
}
@Test
public void testExpParseVariables00() throws Throwable {
Calculator.expParser=new ExpressionParser(10);
Calculator.expParser.numberOfVars=1;
Calculator.expParser.parseVariables("1+3");
Calculator.expParser.parseVariables("<1+3");
Calculator.expParser.parseVariables(">1+3<");
}
@Test(expected=VariableNotFoundException.class)
public void testExpParseVariables01() throws Throwable {
Calculator.expParser=new ExpressionParser(10);
Calculator.expParser.numberOfVars=1;
Calculator.expParser.parseVariables("<11>");
}
@Test
public void testExpParseParenthesis() throws Throwable{
Calculator.expParser=new ExpressionParser(10);
Calculator.expParser.parseParenthesis("(1+2)*3=9");
Calculator.expParser.parseParenthesis("-(1+2)*3=9");
}
@Test(expected=ExpressionParserException.class)
public void testExpParseFunctions00() throws Throwable{
Calculator.expParser=new ExpressionParser(10);
Calculator.expParser.parseFunctions("[1+2]*3=9");
}
@Test(expected=FunctionNotFoundException.class)
public void testExpParseFunctions01() throws Throwable{
Calculator.expParser=new ExpressionParser(10);
Calculator.expParser.parseFunctions("4+5+[1+2]*3=9");
}
@Test
public void testExpParseFunctions02() throws Throwable{
Calculator.expParser=new ExpressionParser(10);
Calculator.expParser.parseFunctions("sin[30]");
Calculator.expParser.parseFunctions("-sin[30]");
}
@Test(expected=MissingOperandException.class)
public void testExpParseFunctions03() throws Throwable{
Calculator.expParser=new ExpressionParser(10);
Calculator.expParser.parseFunctions("a+b[]");
}
@Test(expected=ExpressionParserException.class)
public void testExpParseFunctions04() throws Throwable{
Calculator.expParser=new ExpressionParser(10);
Calculator.expParser.parseFunctions("sin[1+2=3]");
}
@Test
public void testExpParseOperators00() throws Throwable{
Calculator.expParser=new ExpressionParser(10);
Calculator.expParser.parseOperators("(1 - 2 + 3)");
}
@Test(expected=MissingOperandException.class)
public void testExpParseOperators01() throws Throwable{
Calculator.expParser=new ExpressionParser(10);
Calculator.expParser.parseOperators("1 +");
}
@Test(expected=MissingOperandException.class)
public void testExpParseOperators03() throws Throwable{
Calculator.expParser=new ExpressionParser(10);
Calculator.expParser.parseOperators("+ 1");
}
@Test
public void testExpParseOperators02() throws Throwable{
String exp=" a+a ";
String numberRegex = "(([+-]?)\\d+(\\.\\d+)?([eE](-?)\\d+)?)";
while (exp.matches("(.*)" + numberRegex + "\\s+-\\s+" + numberRegex + "(.*)")) {
exp = exp.replaceAll(numberRegex + "\\s+-\\s+" + numberRegex, " $1 + -($6) ");
exp = Calculator.expParser.parseParenthesis(exp);
}
System.out.println("ggggghhh:"+exp);
String[] stack = exp.split("\\s+");
System.out.println("sss_stcak:"+stack[1]+"__"+stack.length);
}
@Test(timeout=1000)
public void testExpindexOfMatchingBracket00() throws Throwable{
Calculator.expParser=new ExpressionParser(10);
System.out.println("VVV"+Calculator.expParser.indexOfMatchingBracket("(())(",2,'(',')'));
Calculator.expParser.indexOfMatchingBracket("(())(",1,'(',')');
}
@Test(expected=UnmatchedBracketsException.class)
public void testExpindexOfMatchingBracket01() throws Throwable{
Calculator.expParser=new ExpressionParser(10);
Calculator.expParser.indexOfMatchingBracket("(-4)",4,'a','b');
}
}
MoreTriangle测试代码
package net.mooctest;
import static org.junit.Assert.*;
import org.junit.Test;
public class MoreTriangleTest {
MoreTriangle t1=new MoreTriangle();
MoreTriangle t2=new MoreTriangle(1,1,4,5,7,1);
MoreTriangle t3=new MoreTriangle("t3",1,1,4,5,7,1);
MoreTriangle t4=new MoreTriangle(7,1,1,1,4,5);
MoreTriangle t5=new MoreTriangle(4,5,7,1,1,1);
MoreTriangle t6=new MoreTriangle(1,1,1,4,5,1);
MoreTriangle t7=new MoreTriangle(5,1,1,1,1,4);
MoreTriangle t8=new MoreTriangle(1,4,5,1,1,1);
Vector v1=new Vector();
Vector v2=new Vector(2,7);
Node n1=new Node(3,4);
Node n2=new Node("nn2",4,5);
Edge e1= new Edge();
Edge e2= new Edge(n1,n2);
@Test
public void testGetCentroidX() {
assertEquals(4, t2.getCentroidX(),0);
}
@Test
public void testGetCentroidY() {
assertEquals(7/3.0, t2.getCentroidY(),0);
}
@Test
public void testPerimeter() {
assertEquals(16, t2.perimeter(),0);
}
@Test
public void testArea() {
assertEquals(12, t2.area(),0);
}
@Test
public void testPointInsideTriangle() {
assertTrue(t2.pointInsideTriangle(4, 2));
assertTrue(t4.pointInsideTriangle(4, 2));
assertTrue(t5.pointInsideTriangle(4, 2));
assertFalse(t2.pointInsideTriangle(1, 1));
assertFalse(t2.pointInsideTriangle(4, 5));
assertTrue(t2.pointInsideTriangle(5, 2));
assertTrue(t5.pointInsideTriangle(5, 2));
assertFalse(t2.pointInsideTriangle(1, 10));
assertFalse(t2.pointInsideTriangle(8, 2));
assertFalse(t6.pointInsideTriangle(1, 2));
assertFalse(t7.pointInsideTriangle(1, 2));
assertFalse(t8.pointInsideTriangle(1, 2));
}
@Test
public void testToString() {
String s3="";
assertFalse(s3==t3.toString());
}
@Test
public void testAdd() {
assertTrue(Math.sqrt(68)==t4.node1.add(t4.node2).length());
}
@Test
public void testVectortoString() {
assertFalse("Vector(" + 2 + ", " + 7 + ")"== v2.toString());
}
}
结
如果朋友您正好在做这两个题,希望交流交流,网上找不到答案,官方也没有给答案,他们的群也进不去,而且那个慕测平台还有bug…
|