在Android studio中,测试输出数组中最大子数组的和
1 1 package com.example.tony.testforgit; 2 2 3 3 /** 4 4 * Created by Tony on 2017/3/17. 5 5 */ 6 6 public class Array { 7 7 public int maxArr(int[] arr){ 8 8 int newSum = arr[0]; 9 9 int maxSum = arr[0]; 10 10 for(int i=1;i<arr.length;i++){ 11 11 newSum += arr[i]; 12 12 if(newSum<arr[i]){ 13 13 newSum = arr[i]; 14 14 } 15 15 if(maxSum<newSum){ 16 16 maxSum = newSum; 17 17 } 18 18 } 19 19 return maxSum; 20 20 } 21 21 }
生成测试类,编辑测试用例
1 package com.example.tony.testforgit; 2 3 import org.junit.Before; 4 import org.junit.Test; 5 6 import static org.junit.Assert.*; 7 8 /** 9 * Created by Tony on 2017/3/17. 10 */ 11 public class ArrayTest { 12 private Array array; 13 14 int arr1[] ={1,2,3,-1,2}; 15 16 @Before 17 public void setUp() throws Exception { 18 array = new Array(); 19 } 20 21 @Test 22 public void testMaxArr() throws Exception { 23 assertEquals(7d,array.maxArr(arr1),0); 24 25 26 } 27 }
测试结果:成功