使用Google Test实现代码之美中的二进制查找测试
2008-11-09 01:04
453 查看
代码之美中有一个关于二进制查找的代码测试,里面循序渐进的告诉我们如何进行测试,刚刚学习了Google Test的测试方法,那么为什么不修改代码之美中的二进制查找为Google Test的实现呢,这样我们可以用Google Test来练练笔。
BinarySearchTest
// google_test.cpp : 定义控制台应用程序的入口点。
//
#include <gtest/gtest.h>
#include <limits>
inline int GetMidValue(int low, int high)
{
return low+high)>>1 ;
}
template<typename T, size_t N>
int BinarySearch(T (&a)
, T target)
{
int low = 0;
int high = N-1;
while ( low <= high )
{
int mid = GetMidValue(low,high);
T midVal = a[mid];
if ( midVal < target)
{
low = mid + 1;
}
else if ( midVal > target )
{
high = mid -1;
}
else
{
return mid;
}
}
return -1;
}
namespace BinarySearchTest
{
// 在msvc下面,数组个数不允许为0,因此不用进行测试
TEST(BinarySearchTest, SizeOne)
{
int value[1]={100};
ASSERT_EQ(0, BinarySearch(value,100));
}
TEST(BinarySearchTest, SmokeTest)
{
int value[] = {1,4,42,45,33,23,34,78};
ASSERT_EQ(2, BinarySearch(value,42) );
ASSERT_EQ(-1, BinarySearch(value,0) );
}
TEST(BinarySearchTest, Boundary)
{
int value[] = { -324, -3, -1, 0, 42, 99, 101 };
ASSERT_EQ(0, BinarySearch(value, -324) ); // low
ASSERT_EQ(3, BinarySearch(value, 0 ) ); // mid
ASSERT_EQ(6, BinarySearch(value, 101) ); // high
}
TEST(BinarySearchTest, MinAndMaxValue)
{
int value[] = { INT_MIN , -324, -3, -1, 0, 42, 99, 101,INT_MAX };
ASSERT_EQ(0, BinarySearch(value,INT_MIN) );
ASSERT_EQ(8, BinarySearch(value,INT_MAX) );
}
TEST(GetMidValueTest, BoundaryValue)
{
ASSERT_EQ(0, GetMidValue(0,1) );
ASSERT_EQ(1, GetMidValue(0,2) );
ASSERT_EQ(1, GetMidValue(1,1) );
ASSERT_EQ(120000, GetMidValue(110000,130000) );
ASSERT_EQ( (INT_MAX-2), GetMidValue(INT_MAX-2, INT_MAX-1) );
ASSERT_EQ( (INT_MAX-1), GetMidValue(INT_MAX-1, INT_MAX) );
}
}
int main(int argc, _TCHAR* argv[])
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
inline int GetMidValue(int low, int high)
{
return ( (unsigned int)(low+high)>>1 );
}
编译运行,得到如下结果:
[ RUN ] BinarySearchTest.SizeOne
[ OK ] BinarySearchTest.SizeOne
[ RUN ] BinarySearchTest.SmokeTest
[ OK ] BinarySearchTest.SmokeTest
[ RUN ] BinarySearchTest.Boundary
[ OK ] BinarySearchTest.Boundary
[ RUN ] BinarySearchTest.MinAndMaxValue
[ OK ] BinarySearchTest.MinAndMaxValue
[----------] 1 test from GetMidValueTest
[ RUN ] GetMidValueTest.BoundaryValue
e:\project\c++\vs2008\google_test\google_test\google_test.cpp(77): error: Value
of: GetMidValue(2147483647-2, 2147483647-1)
Actual: -3
Expected: (2147483647-2)
Which is: 2147483645
[ FAILED ] GetMidValueTest.BoundaryValue // 这部分将会用红色表示
[----------] Global test environment tear-down
[==========] 5 tests from 2 test cases ran.
[ PASSED ] 4 tests.
[ FAILED ] 1 test, listed below:
[ FAILED ] GetMidValueTest.BoundaryValue
显然测试并没有达到我们的要求,第77行中出现了GetMidValue的错误,看过代码之美的朋友都知道,此处需要一个无符号的整数来进行位移,因此将GetMidValue的代码修改成下面的代码形式:
inline int GetMidValue(int low, int high)
{
return ( (unsigned int)(low+high)>>1) ;
}
运行后得到下面输出
[==========] Running 5 tests from 2 test cases.
[----------] Global test environment set-up.
[----------] 4 tests from BinarySearchTest
[ RUN ] BinarySearchTest.SizeOne
[ OK ] BinarySearchTest.SizeOne
[ RUN ] BinarySearchTest.SmokeTest
[ OK ] BinarySearchTest.SmokeTest
[ RUN ] BinarySearchTest.Boundary
[ OK ] BinarySearchTest.Boundary
[ RUN ] BinarySearchTest.MinAndMaxValue
[ OK ] BinarySearchTest.MinAndMaxValue
[----------] 1 test from GetMidValueTest
[ RUN ] GetMidValueTest.BoundaryValue
[ OK ] GetMidValueTest.BoundaryValue
[----------] Global test environment tear-down
[==========] 5 tests from 2 test cases ran.
[ PASSED ] 5 tests.
请按任意键继续. . .
所有的输出都正常显示,显然到目前为止,测试是符合我们的要求的。
BinarySearchTest
// google_test.cpp : 定义控制台应用程序的入口点。
//
#include <gtest/gtest.h>
#include <limits>
inline int GetMidValue(int low, int high)
{
return low+high)>>1 ;
}
template<typename T, size_t N>
int BinarySearch(T (&a)
, T target)
{
int low = 0;
int high = N-1;
while ( low <= high )
{
int mid = GetMidValue(low,high);
T midVal = a[mid];
if ( midVal < target)
{
low = mid + 1;
}
else if ( midVal > target )
{
high = mid -1;
}
else
{
return mid;
}
}
return -1;
}
namespace BinarySearchTest
{
// 在msvc下面,数组个数不允许为0,因此不用进行测试
TEST(BinarySearchTest, SizeOne)
{
int value[1]={100};
ASSERT_EQ(0, BinarySearch(value,100));
}
TEST(BinarySearchTest, SmokeTest)
{
int value[] = {1,4,42,45,33,23,34,78};
ASSERT_EQ(2, BinarySearch(value,42) );
ASSERT_EQ(-1, BinarySearch(value,0) );
}
TEST(BinarySearchTest, Boundary)
{
int value[] = { -324, -3, -1, 0, 42, 99, 101 };
ASSERT_EQ(0, BinarySearch(value, -324) ); // low
ASSERT_EQ(3, BinarySearch(value, 0 ) ); // mid
ASSERT_EQ(6, BinarySearch(value, 101) ); // high
}
TEST(BinarySearchTest, MinAndMaxValue)
{
int value[] = { INT_MIN , -324, -3, -1, 0, 42, 99, 101,INT_MAX };
ASSERT_EQ(0, BinarySearch(value,INT_MIN) );
ASSERT_EQ(8, BinarySearch(value,INT_MAX) );
}
TEST(GetMidValueTest, BoundaryValue)
{
ASSERT_EQ(0, GetMidValue(0,1) );
ASSERT_EQ(1, GetMidValue(0,2) );
ASSERT_EQ(1, GetMidValue(1,1) );
ASSERT_EQ(120000, GetMidValue(110000,130000) );
ASSERT_EQ( (INT_MAX-2), GetMidValue(INT_MAX-2, INT_MAX-1) );
ASSERT_EQ( (INT_MAX-1), GetMidValue(INT_MAX-1, INT_MAX) );
}
}
int main(int argc, _TCHAR* argv[])
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
inline int GetMidValue(int low, int high)
{
return ( (unsigned int)(low+high)>>1 );
}
编译运行,得到如下结果:
[ RUN ] BinarySearchTest.SizeOne
[ OK ] BinarySearchTest.SizeOne
[ RUN ] BinarySearchTest.SmokeTest
[ OK ] BinarySearchTest.SmokeTest
[ RUN ] BinarySearchTest.Boundary
[ OK ] BinarySearchTest.Boundary
[ RUN ] BinarySearchTest.MinAndMaxValue
[ OK ] BinarySearchTest.MinAndMaxValue
[----------] 1 test from GetMidValueTest
[ RUN ] GetMidValueTest.BoundaryValue
e:\project\c++\vs2008\google_test\google_test\google_test.cpp(77): error: Value
of: GetMidValue(2147483647-2, 2147483647-1)
Actual: -3
Expected: (2147483647-2)
Which is: 2147483645
[ FAILED ] GetMidValueTest.BoundaryValue // 这部分将会用红色表示
[----------] Global test environment tear-down
[==========] 5 tests from 2 test cases ran.
[ PASSED ] 4 tests.
[ FAILED ] 1 test, listed below:
[ FAILED ] GetMidValueTest.BoundaryValue
显然测试并没有达到我们的要求,第77行中出现了GetMidValue的错误,看过代码之美的朋友都知道,此处需要一个无符号的整数来进行位移,因此将GetMidValue的代码修改成下面的代码形式:
inline int GetMidValue(int low, int high)
{
return ( (unsigned int)(low+high)>>1) ;
}
运行后得到下面输出
[==========] Running 5 tests from 2 test cases.
[----------] Global test environment set-up.
[----------] 4 tests from BinarySearchTest
[ RUN ] BinarySearchTest.SizeOne
[ OK ] BinarySearchTest.SizeOne
[ RUN ] BinarySearchTest.SmokeTest
[ OK ] BinarySearchTest.SmokeTest
[ RUN ] BinarySearchTest.Boundary
[ OK ] BinarySearchTest.Boundary
[ RUN ] BinarySearchTest.MinAndMaxValue
[ OK ] BinarySearchTest.MinAndMaxValue
[----------] 1 test from GetMidValueTest
[ RUN ] GetMidValueTest.BoundaryValue
[ OK ] GetMidValueTest.BoundaryValue
[----------] Global test environment tear-down
[==========] 5 tests from 2 test cases ran.
[ PASSED ] 5 tests.
请按任意键继续. . .
所有的输出都正常显示,显然到目前为止,测试是符合我们的要求的。
相关文章推荐
- PHP 向右侧拉菜单实现代码,测试使用中
- 使用WDM驱动实现在NT下读取物理端口,特殊寄存器,物理内存的代码(C++测试代码)
- 追求代码质量: 使用 TestNG-Abbot 实现自动化 GUI 测试
- 使用jscript实现二进制读写脚本代码
- PHP 向右侧拉菜单实现代码,测试使用中
- C语言使用stdlib.h库函数的二分查找和快速排序的实现代码
- 使用jscript实现二进制读写脚本代码
- Google Test(GTest)使用方法和源码解析——私有属性代码测试技术分析
- 使用Ant+JUnit+Cobertura来实现代码覆盖自动测试
- 基于 JUnit 使用 Feed4JUnit 实现数据与代码分离的测试
- ob_start(&quot;ob_gzhandler&quot;)与ob_*配合使用时导致浏览无法识别内容/开启gzip时且测试通过的下载代码
- WebView使用SyntaxHighlighter实现代码高亮
- [C#]使用 C# 代码实现拓扑排序 dotNet Core WEB程序使用 Nginx反向代理 C#里面获得应用程序的当前路径 关于Nginx设置端口号,在Asp.net 获取不到的,解决办法 .Net程序员 初学Ubuntu ,配置Nignix 夜深了,写了个JQuery的省市区三级级联效果
- Java使用异或运算实现简单的加密解密算法实例代码
- 使用Shark自带的Hibernate实现代码替换DODS (2007-10-16 09:32)
- php语言中使用json的技巧及json的实现代码详解
- 查找-c++代码实现及运行实例结果
- 简单使用JQ代码 实现全选 反选 全不选 批删