zoukankan      html  css  js  c++  java
  • 第二次作业

    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. 测试结果截图:

  • 相关阅读:
    背水一战 Windows 10 (26)
    背水一战 Windows 10 (25)
    背水一战 Windows 10 (24)
    背水一战 Windows 10 (23)
    背水一战 Windows 10 (22)
    背水一战 Windows 10 (21)
    背水一战 Windows 10 (20)
    背水一战 Windows 10 (19)
    背水一战 Windows 10 (18)
    背水一战 Windows 10 (17)
  • 原文地址:https://www.cnblogs.com/wangxiaonan/p/10702223.html
Copyright © 2011-2022 走看看