第三次软件工程作业——覆盖标准,自动单元测试复习,github之使用。

github链接:https://github.com/chailixuan/hellofirst/tree/master/AndroidStudioUnitTest

0.背景

问题: 给定n个整数(可能为负数)组成的序列a[1],a[2],a[3],…,a[n],求该序列如a[i]+a[i+1]+…+a[j]的子段和的最大值。当所给的整数均为负数时定义子段和为0,依此定义,所求的最优值为: Max{0,a[i]+a[i+1]+…+a[j]},1<=i<=j<=n
例如,当(a[1],a[2],a[3],a[4],a[5],a[6])=(-2,11,-4,13,-5,-2)时,最大子段和为20。
-- 引用自《百度百科》

1.程序与流程图

a.算法与覆盖标准

根据老师提供的资料,采取了第四种算法“在线算法”进行判定/条件覆盖,白盒测试-覆盖标准详解


注:编程能力不属于本次博客的主要内容。

b.覆盖表格

| 测试用例 |覆盖路径
|--|--|--|
| {10,-2,-1,-8} | AD,BC,BD
疑问:AC路径存在矛盾,是否不存在组合覆盖?

c.代码实现如下:

package com.example.asus.androidstudiounittest;

/**
 * Created by ASUS on 2018/3/31.
 */

public class MaxSum {
    public int get_maxSum(int arr[]){
        int len = arr.length;
        int thisSum = 0;
        int maxSum = 0;
        for(int i=0;i < len;i++){
            thisSum += arr[i];
            if(thisSum>maxSum) maxSum=thisSum;
            else if (thisSum<0)thisSum = 0;
        }
        return maxSum;
    }
}

2.自动单元测试结果

本次作业与上次相比单元测试更复杂了,出现了数组,这就要求学习更多的知识,具体怎么做请戳下面的链接。
JUnit成组测试详解
自动单元测试代码如下:

package com.example.asus.androidstudiounittest;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collection;
import java.util.Objects;

import static org.junit.Assert.*;

/**
 * Created by ASUS on 2018/3/31.
 */
@RunWith(Parameterized.class)//必须
public class MaxSumTest {
    private int expected;
    private int[] input;
    public MaxSumTest(int expected,int [] input){
        this.expected = expected;
        this.input = input;
    }
    @Parameterized.Parameters//用此语句去修饰一个public static Collection xxx()
    public static Collection data(){
        return Arrays.asList(new Object[][]{
            {11,new int[]{-6,10,-5,6,-7,-1,-1}},
            {10, new int[]{10,-2,-1,-8}},
        });
    }
    @Test
    public void get_maxSum() throws Exception {
        assertEquals(expected,new MaxSum().get_maxSum(input));
    }

}

  1. 总结
    --
    重要的是,从第一次第二次以及未来的第n次~~~要学习到其中的精髓,学会操作的方法。
posted @ 2018-03-31 21:41  麦麦特雷西  阅读(268)  评论(0编辑  收藏  举报