第二次作业

JUnit单元测试

  本次作业我采用的环境是eclipse,所测试的是封装了一个用于获取数组最大子列的函数的类。

1. 需要测试的类的代码及截图:

代码如下

package wang.JUnitPractice;

import java.util.Scanner;

public class GetMaxSubArray {
	public int FindMaxSubArray(int[] array) {
        if (array == null || array.length == 0) {
            return Integer.MIN_VALUE;
        }
        int currSum = Integer.MIN_VALUE;
        int maxSum = Integer.MIN_VALUE;
        for (int i = 0; i < array.length; i++) {
            currSum = (currSum < 0) ? array[i] : currSum + array[i];
            if (currSum > maxSum) maxSum = currSum;
        }
        return maxSum;
    }
	
	public static void main(String[] args) {		
		Scanner scanner = new Scanner(System.in);
		String str = scanner.nextLine().toString();
		String arrString[] = str.split(",");
		int[] arr= new int[arrString.length];
		for(int i = 0; i < arrString.length; i++){
			arr[i] = Integer.parseInt(arrString[i]);
		}
		scanner.close();
		
		GetMaxSubArray g = new GetMaxSubArray();
		int result = g.FindMaxSubArray(arr);
		System.out.println("最大子列的值为:"+result);
	}
}

eclipse截图

2. 测试类的代码及截图:

代码如下

package wang.JUnitPractice.test;

import static org.junit.Assert.assertEquals;

import org.junit.jupiter.api.Test;

import wang.JUnitPractice.GetMaxSubArray;

class GetMaxSubArrayTest {

	@Test
	void testFindMaxSubArray() {
		int arr[] = new int[]{1,5,78,6,-9,5,-8,8,9,9,4,-1,0,6};
		int result = new GetMaxSubArray().FindMaxSubArray(arr);
		assertEquals(113,result);
	}
}

eclipse截图

3. 测试结果截图:

posted @ 2019-04-13 18:07  王肖男  阅读(110)  评论(0编辑  收藏  举报