Junit白盒測試條件組合覆蓋
阿新 • • 發佈:2019-04-20
語句 技術分享 img 覆蓋 table 白盒 最大 題目 print
A= F B= T
A= F B= F
即: M=max?b[j]?1≤j≤n
題目介紹
給定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。
條件組合覆蓋
設計測試用例時,使得每個判斷語句中條件結果的所有可能組合至少出現一次
測試用例條件:
A= T B= T
A= T B= F
A= F B= F
![技術分享圖片](http://image.bubuko.com/info/201904/20190420210848194578.png)
測試思路
條件組合 | 執行情況 |
---|---|
sum>0 sum>max | sum+=a[i] max=sum |
sum>0 sum<=max | sum+=a[i] max不變 |
sum<0 sum>max | sum=a[i] max=sum |
sum<0 sum<=max | sum=a[i] max不變 |
動態規劃算法
假設最大字段和我們設為 M
我們設1?j中包括a[j]最大字段和為 b[j]
即: b[j]=max{0,b[j?1]}+a[j]}
我們求出包括a[i]?a[n]a[i]?a[n]的所有最大字段和後,只需要求出這些最大字段和中最大的那個就可以了!
執行代碼
public class MaxArray{ public static void main(String[] args){ int arr[] = {-2,11,-4,13,-5,-5,-2}; int result = maxSumArray(arr.length,arr); System.out.println("最大子段和為:"+result); } public static int maxSumArray(int len, int[] a){ int sum = 0, max = 0; for (int i = 0; i < len; i++){ if (sum > 0){ sum += a[i]; } else{ sum = a[i]; } if (sum > max){ max = sum; } } return max; } }
測試代碼
import static org.junit.Assert.*;
import org.junit.Test;
public class Maxtest {
@Test
public void ArrayOne() {
int[] a= {};
assertEquals(0,new MaxArray().maxSumArray(a.length, a));
}
@Test
public void ArrayTwo() {
int[] b= {-2,-11,-4,-13,-5,-2};
assertEquals(0,new MaxArray().maxSumArray(b.length, b));
}
@Test
public void ArrayThree() {
int[] c= {-2,11,-4,13,-5,-2};
assertEquals(20,new MaxArray().maxSumArray(c.length, c));
}
}
執行情況
Junit白盒測試條件組合覆蓋