Calculator JUnit测试用例

news/2024/6/30 10:48:36

覆盖率

总结

1. 这道题是目前为止我做过的唯一一道,测试用例的覆盖率拖累被测文件覆盖率。因为要捕捉好几种类型的异常。目的是增加变异得分。

2. parseCommand这个函数的输出,其实就是提示要怎么写测试用例。

3. 这一题只需要测Calculator.java这一个类,因为它使用ExpressionParser.java,而后者又继承了MathParser.java,至于其余的七个异常文件,自然都是运算触发的。

待改进

1. 我没有分析异常间的继承关系,或许每个test的类都要expected那个父类异常才安全。

2. 还有个别分支因为不理解意思不知道怎么进入。

代码

package net.mooctest;import static org.junit.Assert.*;import org.junit.Test;public class CalculatorTest {private Calculator c = new Calculator();@Test(timeout = 4000)public void test00()  throws Throwable  {c.expParser = new ExpressionParser(10);c.expParser.addVariable("wxy", "330");assertEquals(1,c.expParser.numberOfVars);c.expParser.addVariable("wyc", "1225");c.expParser.addVariable("wyc", "1215");assertEquals(2,c.expParser.numberOfVars);c.expParser.addVariable("fd", "429");assertEquals(3,c.expParser.numberOfVars);c.expParser.addVariable("wlw", "918");//      c.parseCommand("list vars");}@Test(timeout = 4000)public void test01()  throws Throwable  {assertEquals("200.0",c.evaluate("100+100"));assertEquals(4,c.expParser.numberOfVars);c.evaluate("gjr = 922");assertEquals(5,c.expParser.numberOfVars);}@Test(timeout = 4000,expected=ExpressionParserException.class)public void test02()  throws Throwable  {c.evaluate("");}@Test(timeout = 4000,expected=ExpressionParserException.class)public void test03()  throws Throwable  {System.out.println(c.evaluate("wxy + wyc"));}@Test(timeout = 4000)public void test04()  throws Throwable  {assertEquals("35.0",c.evaluate("(3+4)*5"));}@Test(timeout = 4000,expected=ExpressionParserException.class)public void test05()  throws Throwable  {assertEquals("1024.0",c.evaluate(" 2^10 "));}@Test(timeout = 4000,expected=ExpressionParserException.class)public void test06()  throws Throwable  {assertEquals("1545.0",c.evaluate("<wxy> + <wyc>"));assertEquals("2.0",c.evaluate("1 + abs[2 - 3]"));assertEquals("1.0",c.evaluate("log[exp[1.0]]"));c.expParser.addVariable("pi", "3.1415926");assertEquals("179.99999692953102",c.evaluate("deg[ <pi> ]  "));assertEquals("3.141592653589793",c.evaluate("rad[ 180 ]"));c.expParser.addVariable("pi", "3.141592653589793");assertEquals("1.0",c.evaluate("sin[ <pi>/2 ]"));assertEquals("6.123233995736766E-17",c.evaluate("cos[ <pi>/2 ]"));assertEquals("-1.2246467991473532E-16",c.evaluate("tan[ <pi> ]"));assertEquals("Infinity",c.evaluate("ctn[ 0 ]"));}@Test(timeout = 4000,expected=ExpressionParserException.class)public void test07()  throws Throwable  {assertEquals("1.0",c.evaluate("fct[ 0 ]"));assertEquals("1.0",c.evaluate("fct[ 1 ]"));assertEquals("3628800.0",c.evaluate("fct[ 10 ]"));assertEquals("6.0",c.evaluate("13%7"));assertEquals("6.0",c.evaluate("13-7"));assertEquals("1.4142135623730951",c.evaluate("csc[ <pi>/4 ]"));assertEquals("1.414213562373095",c.evaluate("sec[ <pi>/4 ]"));System.out.println(c.evaluate("sec[ <pi>/4 ]"));}@Test(timeout = 4000,expected=UnmatchedBracketsException.class)public void test08()  throws Throwable  {System.out.println(c.evaluate("csc[ <pi>/4 "));}@Test(timeout = 4000,expected=ExpressionParserException.class)public void test09()  throws Throwable  {System.out.println(c.evaluate("<$>"));}@Test(timeout = 4000,expected=ExpressionParserException.class)public void test10()  throws Throwable  {System.out.println(c.evaluate("$100"));}@Test(timeout = 4000,expected=ExpressionParserException.class)public void test11()  throws Throwable  {System.out.println(c.evaluate("abc[100]"));}@Test(timeout = 4000,expected=CommandNotFoundException.class)public void test12()  throws Throwable  {c.parseCommand("lueluelue");}@Test(timeout = 4000,expected=CommandNotFoundException.class)public void test13()  throws Throwable  {assertEquals("-3628800.0",c.evaluate("-fct[ 10 ]"));}@Test(timeout = 4000,expected=NullPointerException.class)public void test14()  throws Throwable  {c.parseCommand("exit");c.parseCommand("help");c.parseCommand("help vars");c.parseCommand("help funcs");c.parseCommand("help cmds"); c.parseCommand("list"); c.parseCommand("list cmds"); c.parseCommand("list vars"); c.parseCommand("list funcs"); System.out.println(c.evaluate("<ans>=114"));}@Test(timeout = 4000,expected=CommandNotFoundException.class)public void test15()  throws Throwable  {assertEquals("1013.294133013707",c.evaluate("(<fd>^2 + <wlw>^2)^0.5"));c.expParser.variables[0][0] = "wxy";c.expParser.variables[0][1] = "200";c.expParser.variables[1][0] = "wyc";c.expParser.variables[1][1] = "50";c.expParser.variables[2][0] = "fd";c.expParser.variables[2][1] = "100";assertEquals("2.5",c.evaluate("(<wxy>+<wyc>)/<fd>"));}}


http://lihuaxi.xjx100.cn/news/240555.html

相关文章

ubuntu×anaconda使用总结[长更]

想让命令“透明”一些 1. 查看命令运行时间&#xff0c;在指令前加time 其实这个是linux所有命令通用的 2. 希望看到conda究竟在干啥&#xff0c;加参数 --debug 这个console跑得太快了&#xff0c;我不好截图orz 然后就是我能看到在干什么&#xff0c;但是我看不懂啊………

非凡推崇_2015年值得推崇的25位编码者

非凡推崇by freeCodeCamp通过freeCodeCamp 2015年值得推崇的25位编码者 (25 Coders Worth Following on Twitter in 2015) Our community upvoted the following 25 coders, in no particular order, as “Coders Worth Following in 2015”:我们的社区对以下25位编码员进行了…

2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest

A. Automatic Door 对于规律的点可以推公式计算&#xff0c;对于噪点则暴力计算&#xff0c;时间复杂度$O(m\log m)$。 #include<stdio.h> #include<iostream> #include<string.h> #include<string> #include<ctype.h> #include<math.h> #…

C#集合

//int[] arr1 new int[2] {1,2};//int[,] arr2 new int[2, 3] {// {0,1,2 },// {2,3,4 }// };//Console.WriteLine(arr2[1,1]);普通集合//ArrayList      //ArrayList alt new ArrayList(); 不需要规定长度和类型不能改变健值 //alt.Add("123");…

[vijos1234]口袋的天空最小生成树

题目链接&#xff1a;https://vijos.org/p/1234 白天刚刚写完prim的算法&#xff0c;晚上就心血来潮的打了一道最小生成树的题 虽然有题解说可以用prim做&#xff0c;但是这道题明显是加最小的边&#xff0c;感觉kruskal方便多了 但是愉快的是我竟然不是一次过&#xff0c;最后…

2014年数字:我的人生在命令行中

by freeCodeCamp通过freeCodeCamp 2014年数字&#xff1a;我的人生在命令行中 (2014 in Numbers: My Life Behind the Command Line) For 2014, I decided to simplify my life. Rather than pursuing a variety of human experiences, as I had previously, I wanted to focu…

linux 将文件的特定行写入另一个文件

问题背景 &#xff1a;博主处理了数据集&#xff0c;一个文件是class namefocal method&#xff0c;另一个文件是test case 对 test case 进行过滤之后得到了idx文件&#xff0c;然后根据idx文件提取出了对应的class namefocal method 现在想验证一下对应序号的class namefoc…

Java读取Properties配置文件

目录1.Properties类与Properties配置文件2.Properties中的主要方法3.示例1.Properties类与Properties配置文件Properties类继承自Hashtable类并且实现了Map接口&#xff0c;使用键值对的形式来保存属性集。不过Properties的键和值都是字符串类型。2.Properties中的主要方法(1)l…