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

  • 相关阅读:
    APIO 2010 特别行动队 斜率优化DP
    洛谷 P2747 Canada Tour 周游加拿大 动态规划
    VIJOS-P1282 佳佳的魔法照片 排序
    [APIO2012]派遣 可并堆
    可并堆模板题-mergeable heap
    可并堆总结
    分治思想及树上点分治
    拉丁字母(英语)缩写
    区块链是什么?跟比特币有什么关系?
    究竟什么比特币?如何理解比特币?
  • 原文地址:https://www.cnblogs.com/liquan/p/5910614.html
Copyright © 2011-2022 走看看