由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 请教一下怎么写unit test
相关主题
一道Coding面试题目leetcode wordsearch的时间复杂度?
Binary search很靠基本功啊amazon的那道题目
C++ constructor problemInterview questions, Bloomberg
问个题,没思路一个容易记忆的permutation算法
问一个关于c++的很傻的问题,多谢好记(但不是最优)的combination算法
生成一个有重复数的全排列,怎么做比较好one C++ question
C++ Q96: function inheritanceC++ object size一问
问题:从电话号码打出所有单词One C++ question
相关话题的讨论汇总
话题: test话题: int话题: target话题: index话题: cout
进入JobHunting版参与讨论
1 (共1页)
a******e
发帖数: 124
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<<"Array is: "< for(int i=0; i cout< if((i+1)%20==0) cout< }
cout< cout<<"Target: "< cout<<"Expected index: "< cout<<"Output index: "< if(index==output)
cout<<" Pass!"< else{
cout<<" Fail!"< 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<<"All Test Pass!"< else cout<<"Test Fail!"< }
int main(int argc, char **argv){
Run_Test();
}
C***y
发帖数: 2546
2
找一个unit test framework。基本上就是写各种test case作为参数调用要测的函数,
然后拿输出结果和预期的正确结果作比较,不一致的话,test就通过不了

【在 a******e 的大作中提到】
: 以前写程序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;

A*****i
发帖数: 3587
3
有一种东西叫做test driving programming
每次写代码前先写好test case,然后在开始写代码
f**********s
发帖数: 115
4
lz貌似问的就是怎么想test case啊
有好多种方法决定test case应该是什么, 比如要cover boundary, index off by 1之
类好多细节,除此之外还要考虑if else全部的branch coverage etc
有的时候你感觉全部boundary都cover了, 但是去改一下你的loop故意改错,test居然
还能过,就说明test写的不好。有些工具可以自动篡改你的code来验证test, 这种叫pi
test
要是想系统的学,推荐一本书fundation of software testing, by Aditya Mathur
a******e
发帖数: 124
5
对test framework还不太了解,去研究一下

【在 C***y 的大作中提到】
: 找一个unit test framework。基本上就是写各种test case作为参数调用要测的函数,
: 然后拿输出结果和预期的正确结果作比较,不一致的话,test就通过不了

a******e
发帖数: 124
6
还没有太多software development经验,每次测试都是在main函数里简单测试一下。。。
谢谢,以后也提前写test case

【在 A*****i 的大作中提到】
: 有一种东西叫做test driving programming
: 每次写代码前先写好test case,然后在开始写代码

a******e
发帖数: 124
7
我主要想知道unit test有没有固定格式,比如有没有固定的library method, 我看有
些人用assert,
还有就是你说的这个怎么样想test case, 你说的这些很有启发性,多谢指点

pi

【在 f**********s 的大作中提到】
: lz貌似问的就是怎么想test case啊
: 有好多种方法决定test case应该是什么, 比如要cover boundary, index off by 1之
: 类好多细节,除此之外还要考虑if else全部的branch coverage etc
: 有的时候你感觉全部boundary都cover了, 但是去改一下你的loop故意改错,test居然
: 还能过,就说明test写的不好。有些工具可以自动篡改你的code来验证test, 这种叫pi
: test
: 要是想系统的学,推荐一本书fundation of software testing, by Aditya Mathur

1 (共1页)
进入JobHunting版参与讨论
相关主题
One C++ question问一个关于c++的很傻的问题,多谢
one C++ question生成一个有重复数的全排列,怎么做比较好
发个题目给大家复习一下marcoC++ Q96: function inheritance
Why I can't compile this function successfully问题:从电话号码打出所有单词
一道Coding面试题目leetcode wordsearch的时间复杂度?
Binary search很靠基本功啊amazon的那道题目
C++ constructor problemInterview questions, Bloomberg
问个题,没思路一个容易记忆的permutation算法
相关话题的讨论汇总
话题: test话题: int话题: target话题: index话题: cout