1.编写测试
本节课次要是教咱们本人编写一些测试代码,不适度依赖于autograder
假如编写一个method:sort(),作用是将数组排序,例如:
{"i", "have", "an", "egg"}
排序后应该是:
{"an", "egg", "have", "i"}
因而咱们能够查看输出的原始数组的每一项与输入后果的每一项是否相等来判断sort()是否正确:
String[] input = {"i", "have", "an", "egg"};
String[] expected = {"an", "egg", "have", "i"};
Sort.sort(input);
for (int i = 0; i < input.length; i += 1) {
if (!input[i].equals(expected[i])) {
System.out.println("Mismatch in position " + i + ", expected: " + expected + ", but got: " + input[i] + ".");
break;
}
然而应用循环和.equals()办法逐项比对十分繁琐,org.junit library能够帮忙咱们更好地实现此项评测工作,只需一行,即可代替循环的整段,应用的是
org.junit.Assert.assertArrayEquals(expected, input)
后果如下:
String[] input = {"i", "have", "an", "egg"};
String[] expected = {"an", "egg", "have", "i"};
Sort.sort(input);
org.junit.Assert.assertArrayEquals(expected, input);
2.抉择排序
本例应用递归实现抉择排序,咱们把抉择排序拆分成3种办法:
- findMinium():找到以后数组中的数值最小项
- Swap():把最小项与数组第一项替换
- Sort():调用前两个函数,例如第一次找到了数组中最小的元素并将其调换到第一项的地位,之后从第二项开始(也就是不用再对第一项排序),反复步骤:递归+贪婪
最终如下:
public class SelectionSort{
public static int findMinium(String[] x,int start){
int minIndex = start;
for(int i = start;i < x.length;i ++){
int cmp = x[i].compareTo(x[minIndex]);
if(cmp < 0){
minIndex = i;
}
}
return minIndex;
}
public static void Swap(String[] x,int a,int b){
String temp = x[a];
x[a] = x[b];
x[b] = temp;
}
public static void Sort(String[] x,int start){
if(start == x.length){
return;
}
int min = findMinium(x,start);
Swap(x,start,min);
Sort(x,start+1);
}
}
3.编写测试方法
对每一种办法编写测试:
1.testFindminium():
public static void testfindMinium(){
String[] input = {"i","have","an","egg"};
int expected = 2;
int actual = SelectionSort.findMinium(input,0);
org.junit.Assert.assertEquals(expected,actual);
}
2.testSwap():
public static void testSwap(){
String[] input = {"i","have","an","egg"};
String[] expected = {"an","have","i","egg"};
int a = 0;
int b = 2;
SelectionSort.Swap(input,a,b);
org.junit.Assert.assertArrayEquals(expected,input);
}
3.testSort():
public static void testSort(){
String[] input = {"i","have","an","egg"};
String[] expected = {"an","egg","have","i"};
SelectionSort.Sort(input,0);
org.junit.Assert.assertArrayEquals(expected,input);
}
编写结束后,咱们须要测试哪一个函数,就在main()中调用该测试方法:
public static void main(String[] args){
testSort();
testfindMinium();
testSwap();
}
留神:
最好不要把多个测试方法同时运行,因为它们其中任何一个运行失败将会导致程序终止,从而在它之后的测试都将不会再运行,也就是说须要每次去手动正文掉一些办法,每次只能运行一个
可见此办法是很繁琐的,上面有优化策略
4.优化 : 应用org.junit.Test
-
在每一个测试方法后面加上正文:
@org.junit.Test
- 而后,删除主办法public static void main()
- 将所有test()改为non-static,也就是去掉static关键字
- 点击上方Tab栏:run–>抉择左右对称红绿三角的run,之后能够看到每个测试方法均被运行
同时,也能够抉择点击左侧竖直标号栏的绿色播放按钮,对你想运行的测试方法单个运行,由此解脱了每次人工正文掉某个不须要运行的办法的懊恼
5.应用org.junit库
每次应用junit来测试debug都要写很多前缀org.junit.Assert….,所以为了缩小工作量,咱们引入junit的包,在Intellij IDEA下,点击
File—>Project Structure
点击library,点击+
找到Intellij IDEA的装置门路,在装置门路下抉择lib文件,找到junit4.java,抉择OK,实现导入
PS:起初才发现cs61b的library-sp18外面有junit.jar包 QAQ
导入之后,能够间接在java程序中import:
import org.junit.Test;
import static org.junit.Assert.*;
之后能够将
org.junit.Test 替换为 Test
org.junit.Assert 去掉
更加不便可读:
import org.junit.Test;
import static org.junit.Assert.*;
public class TestSort{
@Test
public void testSort(){
String[] input = {"i","have","an","egg"};
String[] expected = {"an","egg","have","i"};
SelectionSort.Sort(input,0);
assertArrayEquals(expected,input);
}
@Test
public void testfindMinium(){
String[] input = {"i","have","an","egg"};
int expected = 2;
int actual = SelectionSort.findMinium(input,0);
org.junit.Assert.assertEquals(expected,actual);
}
@Test
public void testSwap(){
String[] input = {"i","have","an","egg"};
String[] expected = {"an","have","i","egg"};
int a = 0;
int b = 2;
SelectionSort.Swap(input,a,b);
assertArrayEquals(expected,input);
}
}
更多junit库的Assert类的办法请参考:
Assert(JUnit API)
发表回复