使用Google Test实现代码之美中的二进制查找测试
2008-11-09 01:04 ubunoon 阅读(741) 评论(0) 编辑 收藏 举报
Google Test用来改写代码之美中的二进制查找代码(部分),编译后输出让人非常满意。
代码之美中有一个关于二进制查找的代码测试,里面循序渐进的告诉我们如何进行测试,刚刚学习了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)[N], 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.
请按任意键继续. . .
所有的输出都正常显示,显然到目前为止,测试是符合我们的要求的。
// 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)[N], 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.
请按任意键继续. . .
所有的输出都正常显示,显然到目前为止,测试是符合我们的要求的。
/*
*
* Copyright (c) 2011 Ubunoon.
* All rights reserved.
*
* email: netubu#gmail.com replace '#' to '@'
* http://www.cnblogs.com/ubunoon
* 欢迎来邮件定制各类验证码识别,条码识别,图像处理等软件
* 推荐不错的珍珠饰品,欢迎订购 * 宜臣珍珠(淡水好珍珠) */
*
* Copyright (c) 2011 Ubunoon.
* All rights reserved.
*
* email: netubu#gmail.com replace '#' to '@'
* http://www.cnblogs.com/ubunoon
* 欢迎来邮件定制各类验证码识别,条码识别,图像处理等软件
* 推荐不错的珍珠饰品,欢迎订购 * 宜臣珍珠(淡水好珍珠) */