Education service 很强啊# Stock
a*e
1 楼
以前写程序test时候只是在main函数里简单输出一下看看对不对,最近想学一下unit
test,这个有什么格式要求吗(我看有些人用assert())?比如用c++语言想写一个
binary search的unit test该如何写,谢谢!
试着写了一个,不知道这样算不算unit test
#include
using namespace std;
int BinarySearch(int arr[], int size, int target){
int high=size-1;
int low=0;
int mid;
while(low<=high){
mid=low+(high-low)/2;
if(arr[mid]==target){
return mid;
}
else if(arr[mid]>target){
high=mid-1;
}
else{
low=mid+1;
}
}
return -1;
}
bool BinarySearch_Test (int arr[], int n, int target, int index){
bool result=true;
int output=BinarySearch(arr, n, target);
cout<for(int i=0; i cout< if((i+1)%20==0) cout< }
cout< cout<cout<cout<if(index==output)
cout<else{
cout<result=false;
}
cout< return result;
}
void Run_Test(){
int n, target, index;
//test case 1: normal case
int arr1[]={1, 3, 4, 5, 7, 10, 23, 25};
n=8; target=5; index=3;
bool test1=BinarySearch_Test(arr1, n, target, index);
//test case 2: test the first element
int arr2[]={1, 3, 4, 5, 7, 10, 23, 25};
n=8; target=1; index=0;
bool test2=BinarySearch_Test(arr2, n, target, index);
//test case 3: test the last element
int arr3[]={1, 3, 4, 5, 7, 10, 23, 25};
n=8; target=25; index=7;
bool test3=BinarySearch_Test(arr3, n, target, index);
//test case 4: test for large array
int arr4[100];
for(int i=1; i<100; i++){
arr4[i]=arr4[i-1]+1;
}
n=100; target=55; index=55;
bool test4=BinarySearch_Test(arr4, n, target, index);
//test case 5: test for array with repeat elements
int arr5[]={3, 3, 4, 5, 5, 10, 10, 25};
n=8; target=4; index=2;
bool test5=BinarySearch_Test(arr5, n, target, index);
if(test1&&test2&&test3&&test4&&test5) cout<else cout<}
int main(int argc, char **argv){
Run_Test();
}
test,这个有什么格式要求吗(我看有些人用assert())?比如用c++语言想写一个
binary search的unit test该如何写,谢谢!
试着写了一个,不知道这样算不算unit test
#include
using namespace std;
int BinarySearch(int arr[], int size, int target){
int high=size-1;
int low=0;
int mid;
while(low<=high){
mid=low+(high-low)/2;
if(arr[mid]==target){
return mid;
}
else if(arr[mid]>target){
high=mid-1;
}
else{
low=mid+1;
}
}
return -1;
}
bool BinarySearch_Test (int arr[], int n, int target, int index){
bool result=true;
int output=BinarySearch(arr, n, target);
cout<for(int i=0; i
cout<
cout<else{
cout<result=false;
}
cout<
}
void Run_Test(){
int n, target, index;
//test case 1: normal case
int arr1[]={1, 3, 4, 5, 7, 10, 23, 25};
n=8; target=5; index=3;
bool test1=BinarySearch_Test(arr1, n, target, index);
//test case 2: test the first element
int arr2[]={1, 3, 4, 5, 7, 10, 23, 25};
n=8; target=1; index=0;
bool test2=BinarySearch_Test(arr2, n, target, index);
//test case 3: test the last element
int arr3[]={1, 3, 4, 5, 7, 10, 23, 25};
n=8; target=25; index=7;
bool test3=BinarySearch_Test(arr3, n, target, index);
//test case 4: test for large array
int arr4[100];
for(int i=1; i<100; i++){
arr4[i]=arr4[i-1]+1;
}
n=100; target=55; index=55;
bool test4=BinarySearch_Test(arr4, n, target, index);
//test case 5: test for array with repeat elements
int arr5[]={3, 3, 4, 5, 5, 10, 10, 25};
n=8; target=4; index=2;
bool test5=BinarySearch_Test(arr5, n, target, index);
if(test1&&test2&&test3&&test4&&test5) cout<else cout<}
int main(int argc, char **argv){
Run_Test();
}