1. 程式人生 > >第三次軟件工程作業——覆蓋標準,自動單元測試復習,github之使用。

第三次軟件工程作業——覆蓋標準,自動單元測試復習,github之使用。

學習 unittest junit log 工程 roi 要求 sdn sum()

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,AC,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));
    }

}

技術分享圖片

3. 總結

重要的是,從第一次第二次以及未來的第n次~~~要學習到其中的精髓,學會操作的方法。

第三次軟件工程作業——覆蓋標準,自動單元測試復習,github之使用。