锘??xml version="1.0" encoding="utf-8" standalone="yes"?> 棣栧厛瀹炵幇涓や釜Fibonacci鍑芥暟錛岀劧鍚庡榪欎袱涓嚱鏁拌繘琛屾祴璇曪細 Fibonacci_1錛屼嬌鐢ㄥ驚鐜疄鐜幫細 鎵╁睍鍐呭錛?br />1. TEST_T(test_case_name, test_name)錛岀敤浜庡畾涔夎繍琛屾椂闂存祴璇曠敤渚嬨?br />2. TEST_T_SHOWTIME()錛屾墦寮鎵撳嵃嫻嬭瘯鐢ㄤ緥榪愯鏃墮棿鎵撳嵃寮鍏熾?br />3. EXCEPT_TIME(second)鍜孉SSERT_TIME(second)錛屾柇璦錛宻econd涓篸ouble綾誨瀷錛屾祴璇曡繍琛屾椂闂存槸鍚﹀皬浜巗econd銆?/p>
浣跨敤璇存槑錛?br />鍚戞甯鎬嬌鐢ㄤ竴鏍鳳紝鍙槸鍦ㄩ渶瑕佹椂闂存祴璇曟椂include “gtest_e.h”鍗沖彲錛屽綋鐒朵篃寰楁妸鐩稿簲鐨勫簱閾炬帴鍒版墽琛屾枃浠朵腑銆?/p>
鍏蜂綋瀹炵幇錛?br />婧愭枃浠秅test_e.h----
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <regex.h>
int main(int argc, char** argv)

{
char * pattern, *mstr;
int x, z, lno = 0, cflags = REG_EXTENDED;
char ebuf[128], lbuf[256], format[32];
regex_t reg;
regmatch_t pm[10];
const size_t nmatch = 10;
pattern = argv[1];
z = regcomp(®, pattern, cflags);
if (z != 0)
{
regerror(z, ®, ebuf, sizeof(ebuf));
fprintf(stderr, "%s: pattern '%s' \n",ebuf, pattern);
regfree(®);
return 1;
}
while(fgets(lbuf, sizeof(lbuf), stdin))
{
++lno;
if ((z = strlen(lbuf)) > 0 && lbuf[z-1]== '\n') lbuf[z - 1] = 0;
z = regexec(®, lbuf, nmatch, pm, 0);
if (z == REG_NOMATCH)
{
printf("not match\n");
continue;
}
else if (z != 0)
{
regerror(z, ®, ebuf, sizeof(ebuf));
fprintf(stderr, "%s: regcom('%s')\n",
ebuf, lbuf);
return 2;
}
if (pm[0].rm_so!=-1)
printf("%04d: %s\n", lno, lbuf);
for (x = 0; x < nmatch && pm[x].rm_so != -1; ++ x)
{
mstr = strndup(lbuf + pm[x].rm_so, pm[x].rm_eo-pm[x].rm_so);
printf(" $%d: %s\n", x, mstr);
free(mstr);
}
fflush(stdout);
}
regfree(®);
return 0;
}
./testreg "[\[][^\[\]]*[\]][#|$] "
[root@localhost bin]#
not match
[a\]#
0005: [a\]#
$0: [a\]#
./testreg "[\[]+"
a
not match
[
0002: [
$0: [
[root@localhost bin]#
0003: [root@localhost bin]#
$0: [
./testreg "[\]]+"
]
not match
\]
0002: \]
$0: \]
-----------------
./testreg "[]]+"
]]
0001: ]]
$0: ]]
./testreg "[^\[]]+" //瀵筟杞箟錛屼笉瀵筣杞箟
[abc]
0001: [abc]
$0: c]
./testreg "[^\[\]]+" //瀵筟鍜宂杞箟
[abc]
0001: [abc]
$0: c]
./testreg "[^]\[]+" //瀵筟杞箟錛屼笉瀵筣杞箟錛屼笖鎶奭鏀懼湪^鍚庝嬌]涓嶄笌鍓嶉潰鐨刐鍖歸厤
[abc]
0001: [abc]
$0: abc
]]>
鏂囩珷鏉ユ簮:http://blog.csdn.net/volant_hoo/archive/2009/05/11/4167919.aspx
]]>
鏂囩珷鏉ユ簮:http://blog.csdn.net/volant_hoo/archive/2009/05/11/4169081.aspx
]]>
鏂囩珷鏉ユ簮:http://blog.csdn.net/volant_hoo/archive/2009/05/11/4169108.aspx
]]>
鏂囩珷鏉ユ簮:http://blog.csdn.net/volant_hoo/archive/2009/05/11/4169093.aspx
]]>
unsigned int Fibonacci_1(unsigned int n)
{
unsigned int i;
unsigned int f0 = 1, f1 = 1, f2;
for (i = 1; i < n; i++)
{
f2 = f0 + f1;
f0 = f1;
f1 = f2;
}
return f1;
}
Fibonacci_2錛屼嬌鐢ㄩ掑綊瀹炵幇錛?/p>
unsigned int Fibonacci_2(unsigned int n)
{
switch (n)
{
case 0:
return 1;
case 1:
return 1;
default:
return Fibonacci_2(n - 1) + Fibonacci_2(n - 2);
}
}
嫻嬭瘯鐢ㄤ緥錛?/p>
TEST_T(Fibonacci_Recursive, 30)
{
Fibonacci_2(30);
ASSERT_TIME(0.1);
}
TEST_T(Fibonacci_Loop, 30)
{
Fibonacci_1(30);
ASSERT_TIME(0.1);
}
TEST_T(Fibonacci_Recursive, 40)
{
TEST_T_SHOWTIME();
Fibonacci_2(40);
EXCEPT_TIME(0.1);
ASSERT_TIME(1) << "\nUsed too long time!";
}
TEST_T(Fibonacci_Loop, 40)
{
TEST_T_SHOWTIME();
Fibonacci_1(40);
EXCEPT_TIME(0.1);
ASSERT_TIME(1) << "\nUsed too long time!";
}
嫻嬭瘯緇撴灉錛?/p>
[==========] Running 4 tests from 2 test cases.
[----------] Global test environment set-up.
[----------] 2 tests from TIME_Fibonacci_Recursive
[ RUN ] TIME_Fibonacci_Recursive.30
[ OK ] TIME_Fibonacci_Recursive.30
[ RUN ] TIME_Fibonacci_Recursive.40
FibonacciTest.cpp:47: Failure
Failed
Time: running 2.9995(s) > 0.1(s)
FibonacciTest.cpp:48: Failure
Failed
Time: running 2.9995(s) > 1(s)
Used too long time!
[ TIME ] used time: 2.9995(s)
[ FAILED ] TIME_Fibonacci_Recursive.40
[----------] 2 tests from TIME_Fibonacci_Loop
[ RUN ] TIME_Fibonacci_Loop.30
[ OK ] TIME_Fibonacci_Loop.30
[ RUN ] TIME_Fibonacci_Loop.40
[ TIME ] used time: 0(s)
[ OK ] TIME_Fibonacci_Loop.40
[----------] Global test environment tear-down
[==========] 4 tests from 2 test cases ran.
[ PASSED ] 3 tests.
[ FAILED ] 1 test, listed below:
[ FAILED ] TIME_Fibonacci_Recursive.40
]]>
/**
* gtest_e.h
*/
#ifndef GTEST_E_H_
#define GTEST_E_H_
#include "gtest_time.h"
#define TEST_T(test_case_name, test_name)\
GTEST_TEST(TIME_##test_case_name, test_name, ::TimeTest)
#define TEST_T_SHOWTIME() TimeTest_setShowFlag(1)
#define ASSERT_TIME(time) if(TimeTest_setTimePoint() - time > 0) \
FAIL() << "Time: running " << TimeTest_getTime() << "(s) > " << time << "(s)"
#define EXCEPT_TIME(time) if(TimeTest_setTimePoint() - time > 0) \
ADD_FAILURE() << "Time: running " << TimeTest_getTime() << "(s) > " << time << "(s)"
#endif /* GTEST_E_H_ */
/**
* gtest_time.h
*/
#ifndef GTEST_TIME_H_
#define GTEST_TIME_H_
#include <gtest/gtest.h>
class TimeTest: public testing::Test
{
public:
inline void TimeTest_setShowFlag(int flag)
{
show_time_ = flag;
}
inline double TimeTest_getTime()
{
return end_time_ - start_time_;
}
double TimeTest_setTimePoint();
protected:
double start_time_;
double end_time_;
int show_time_;
virtual void SetUp();
virtual void TearDown();
};
#endif /* GTEST_TIME_H_ */
/**
* gtest_time.cpp
*/
#include <iostream>
#include "gtest_time.h"
using namespace std;
#if defined(WIN32)
#include <sys/timeb.h>
double now()
{
struct _timeb current;
_ftime(¤t);
return (((double) current.time) + (1.0 * current.millitm) * 0.000001);
}
#else
double now()
{
struct timeval current;
gettimeofday(¤t, NULL);
return (((double) current.tv_sec) + 1.0e-6 * ((double) current.tv_usec));
}
#endif
void TimeTest::SetUp()
{
start_time_ = now();
end_time_ = 0;
TimeTest_setShowFlag(0);
}
void TimeTest::TearDown()
{
if (show_time_)
{
double used_time = TimeTest_setTimePoint();
cout << "[ TIME ] used time: " << used_time << "(s)"
<< endl;
}
}
double TimeTest::TimeTest_setTimePoint()
{
end_time_ = now();
return TimeTest_getTime();
}
浠ヤ笂鍐呭鍙槸涓涓畝鍗曠殑瀹炵幇錛屾病鏈夎繃澶氱殑嫻嬭瘯錛屼笖鏃墮棿綺懼害涓嶅錛岃宸緝澶с?img src ="http://m.shnenglu.com/volant/aggbug/60201.html" width = "1" height = "1" />
]]>
鏂█錛?br />ASSERT_TRUE(condition); EXPECT_TRUE(condition); condition涓虹湡
ASSERT_FALSE(condition); EXPECT_FALSE(condition); condition涓哄亣
ASSERT_EQ(expected, actual); EXPECT_EQ(expected, actual); expected == actual
ASSERT_NE(val1, val2); EXPECT_NE(val1, val2); val1 != val2
ASSERT_LT(val1, val2); EXPECT_LT(val1, val2); val1 < val2
ASSERT_LE(val1, val2); EXPECT_LE(val1, val2); val1 <= val2
ASSERT_GT(val1, val2); EXPECT_GT(val1, val2); val1 > val2
ASSERT_GE(val1, val2); EXPECT_GE(val1, val2); val1 >= val2
ASSERT_STREQ(expected_str, actual_str); EXPECT_STREQ(expected_str, actual_str); 涓や釜C瀛楃涓叉湁鐩稿悓鐨勫唴瀹?br />ASSERT_STRNE(str1, str2); EXPECT_STRNE(str1, str2); 涓や釜C瀛楃涓叉湁涓嶅悓鐨勫唴瀹?br />ASSERT_STRCASEEQ(expected_str, actual_str); EXPECT_STRCASEEQ(expected_str, actual_str); 涓や釜C瀛楃涓叉湁鐩稿悓鐨勫唴瀹癸紝蹇界暐澶у皬鍐?br />ASSERT_STRCASENE(str1, str2); EXPECT_STRCASENE(str1, str2); 涓や釜C瀛楃涓叉湁涓嶅悓鐨勫唴瀹癸紝蹇界暐澶у皬鍐?br />
澶存枃浠訛細
#include <gtest/gtest.h>
main錛?br /> testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
搴擄細
-lgtest
]]>
鏂囩珷鏉ユ簮:http://blog.csdn.net/volant_hoo/archive/2008/06/02/2502339.aspx
]]>
鏂囩珷鏉ユ簮:http://blog.csdn.net/volant_hoo/archive/2008/04/07/2256611.aspx
]]>
鏂囩珷鏉ユ簮:http://blog.csdn.net/volant_hoo/archive/2008/04/04/2252490.aspx
]]>