zoukankan      html  css  js  c++  java
  • 四则运算单元测试

     项目成员:武志远李权

       对于四则运算中的单元测试以两个不同功能的函数为示例展示。仍然采用JUnit4的单元测试框架

      下面展示的是没有括号和分数情况下的计算函数 doCalculation的代码片段

    // 对生成的4则运算进行计算
        public static Double doCalculation(String formula) {
            ArrayList<Double> numList = new ArrayList<Double>();// 存储运算数
            ArrayList<String> opList = new ArrayList<String>();// 存储运算符
            int opIndex = 0;// 定位运算符的位置
            int preIndex = 0;// 紧挨运算符左边数字的起始位置
            for (char s : formula.toCharArray()) {
                if (s == '+' || s == '-' || s == '*' || s == '/') {
                    numList.add(Double.parseDouble(formula.substring(preIndex, opIndex)));
                    opList.add("" + s);
                    preIndex = opIndex + 1;
                }
                opIndex++;
            }
            numList.add(Double.parseDouble(formula.substring(preIndex))); 
            // 下面的for循环用来先计算*/法
            for (opIndex = 0; opIndex < opList.size(); opIndex++) {
                switch (opList.get(opIndex)) {
                case "*":
                    numList.add(opIndex, numList.get(opIndex) * numList.get(opIndex + 1));
                    numList.remove(opIndex + 1);
                    numList.remove(opIndex + 1);
                    opList.remove(opIndex);
                    opIndex = -1;
                    break;
                case "/":
                    numList.add(opIndex, numList.get(opIndex) / numList.get(opIndex + 1));
                    numList.remove(opIndex + 1);
                    numList.remove(opIndex + 1);
                    opList.remove(opIndex);
                    opIndex = -1;
                    break;
                }
            } 
            // 下面的for循环用来先计算+-法
            for (opIndex = 0; opIndex < opList.size(); opIndex++) {
                switch (opList.get(opIndex)) {
                case "+":
                    numList.add(opIndex, numList.get(opIndex) + numList.get(opIndex + 1));
                    numList.remove(opIndex + 1);
                    numList.remove(opIndex + 1);
                    opList.remove(opIndex);
                    opIndex = -1;
                    break;
                case "-":
                    numList.add(opIndex, numList.get(opIndex) - numList.get(opIndex + 1));
                    numList.remove(opIndex + 1);
                    numList.remove(opIndex + 1);
                    opList.remove(opIndex);
                    opIndex = -1;
                    break;
                }
            }
            return numList.get(0).doubleValue();
        } 

    下面是单元测试函数的代码

    @RunWith(Parameterized.class)
    public class CalculateMachine01Test {
        private String param;;
        private Double result;
        public CalculateMachine01Test(String param,double result){
            this.param=param;
            this.result=result;
        }
        
        @Parameters  
        public static Collection data(){    //下面是一些测试样例
            return Arrays.asList(new Object[][]{  
                {"1+2",3}, 
                {"1+2*3/4",2.5},
                {"1/0",-1},  
                {"1/3",0.3},
          });  
        }
            
        @Test  
        public void calTest() {  
            CalculateMachine01 cm=new CalculateMachine01();
            Double temp=cm.doCalculation(param);
         // Assert.assertEquals(result, temp);  //对比期望的结果和实际执行的结果
            Assert.assertEquals(temp, result);
        }  
    }

    测试结果如下

    其中第三行数据{"1/0",-1}和第四行数据{"1/3",0.3}未通过测试。但是这种示例可以再处理结果的的函数中过滤掉。

    针对函数之间有依赖有关mock的的暂时没做。

    感悟:单元测试感觉简单,实则不易。

    类别

    内容

    开始时间

    结束时间

    间断时间

    净时间

    学习

    对mock和stub的研究

    18:33

    19:23

    4

    46

    测试编码

    四则算式的函数测试

    19:30

    20:40

    20

    50

    总结

    mock的具体用法尚不明确,没有很好的例子

    20:40

    20:45

    0

    5

     工作照:

    https://git.coding.net/muziliquan/classwork03.git

    git@git.coding.net:muziliquan/classwork03.git

    git@git.coding.net:muziliquan/classwork03.git

  • 相关阅读:
    Java编码思想之什么是高内聚低耦合?
    hadoop中block副本的放置策略
    服务器组网的过程
    服务器的类型
    hadoop学习笔记(一):NameNade持久化和DataNode概念
    js中的局部函数和全局函数的调用
    对于javaScript中的alert和document.write()的执行顺序的记录
    常用工具api等链接
    js函数声明外面使用小括号括起来再接一个小括号的写法
    LVS的概念和重要性
  • 原文地址:https://www.cnblogs.com/liquan/p/5910614.html
Copyright © 2011-2022 走看看