note
requirement
- 确定测试哪些函数,并计算出测试用例的个数
- 五个函数,代码行数不少于 10 行
- statements and branches 两种方式
- 使用 Junit 编写 Test suite
- 跑完之后截图 – statement coverage
- 是否完全覆盖
- 跑完之后截图 – branche coverage
- 是否完全覆盖
- 生成报告
- 讨论如下问题
If all the methods in the source code that are tested and 100% All-Branch coverage is achieved, is it still possible for the program to fail?
确定函数
SelectionSort
public <T extends Comparable<T>> T[] sort(T[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
int min = i;
for (int j = i + 1; j < n; j++) {
if (SortUtils.less(arr[j], arr[min])) {
}
}
if (min != i) {
SortUtils.swap(arr, i, min);
}
}
return arr;
}
BubbleSort
public <T extends Comparable<T>> T[] sort(T array[]) {
for (int i = 0, size = array.length; i < size - 1; ++i) {
boolean swapped = false;
for (int j = 0; j < size - 1 - i; ++j) {
swapped = less(array[j], array[j + 1]) && swap(array, j, j + 1);
}
if (!swapped) {
break;
}
}
return array;
}
CocktailShakerSort
public <T extends Comparable<T>> T[] sort(T[] array) {
int length = array.length;
int left = 0;
int right = length - 1;
int swappedLeft, swappedRight;
while (left < right) {
swappedRight = 0;
for (int i = left; i < right; i++) {
if (SortUtils.less(array[i + 1], array[i])) {
SortUtils.swap(array, i, i + 1);
swappedRight = i;
}
}
right = swappedRight;
swappedLeft = length - 1;
for (int j = right; j > left; j--) {
if (SortUtils.less(array[j], array[j - 1])) {
SortUtils.swap(array, j - 1, j);
swappedLeft = j;
}
}
left = swappedLeft;
}
return array;
}
MergeSort
private static <T extends Comparable<T>> void merge(T[] arr, T[] temp, int left, int mid, int right) {
System.arraycopy(arr, left, temp, left, right - left + 1);
int i = left;
int j = mid + 1;
int k = left;
while (i <= mid && j <= right) {
if (temp[i].compareTo(temp[j]) <= 0) {
arr[k++] = temp[i++];
} else {
arr[k++] = temp[j++];
}
}
while (i <= mid) {
arr[k++] = temp[i++];
}
while (j <= right) {
arr[k++] = temp[j++];
}
}
InsertionSort
public <T extends Comparable<T>> T[] sort(T[] array) {
for (int j = 1; j < array.length; j++) {
T key = array[j];
int i = j - 1;
while (i >= 0 && less(key, array[i])) {
array[i + 1] = array[i];
i--;
}
array[i + 1] = key;
}
return array;
}