日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用Google Test的一个简单例子

發布時間:2023/12/2 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用Google Test的一个简单例子 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.


0.?引子

?

本例是從?gtest-1.5.0?自帶的?sample?中的?sample1?改寫而來,筆者只添加了一個求?n?的階層的函數,如下。

void?Factorial(int?n,?int?&?result?)

{

????result = 1;

????for?(int?i = 1; i <= n; i++)

????????result *= i;

}

目的是想測試像這樣將返回值放在參數中返回的函數。

對于該函數,添加的單元測試代碼如下。

TEST?(FactorialTest?,?Mytest?)

{

????int?result = 0;

????Factorial?(5, result);

????EXPECT_EQ?(120, result);

}

1.?要測試的代碼

?

要測試的代碼?(Sample.h)?代碼如下。

[c-sharp]?view plaincopy
  • /**?
  • ?*?GoogleTest?test?
  • ?*?platform:?win32,?visual?studio?2005/2010;?Linux,?gcc4.1.2?
  • ?*/??
  • #ifndef?_SAMPLE_H_??
  • #define?_SAMPLE_H_??
  • //?Returns?n!?(the?factorial?of?n).??For?negative?n,?n!?is?defined?to?be?1.??
  • int?Factorial(int?n);??
  • void?Factorial(int?n,?int?&result);??
  • //?Returns?true?iff?n?is?a?prime?number.??
  • bool?IsPrime(int?n);??
  • #endif??
  • 要測試的代碼?(Sample.cpp)?代碼如下。

    [cpp]?view plaincopy
  • /**?
  • ?*?GoogleTest?test?
  • ?*?platform:?win32,?visual?studio?2005/2010;?Linux,?gcc4.1.2?
  • ?*/??
  • #include?"sample.h"??
  • //?Returns?n!?(the?factorial?of?n).??For?negative?n,?n!?is?defined?to?be?1.??
  • int?Factorial(int?n)??
  • {??
  • ????int?result?=?1;??
  • ????for?(int?i?=?1;?i?<=?n;?i++)??
  • ????????result?*=?i;??
  • ????return?result;??
  • }??
  • void?Factorial(int?n,?int?&result)??
  • {??
  • ????result?=?1;??
  • ????for?(int?i?=?1;?i?<=?n;?i++)??
  • ????????result?*=?i;??
  • }??
  • ??
  • //?Returns?true?iff?n?is?a?prime?number.??
  • bool?IsPrime(int?n)??
  • {??
  • ????//?Trivial?case?1:?small?numbers??
  • ????if?(n?<=?1)??
  • ????????return?false;??
  • ????//?Trivial?case?2:?even?numbers??
  • ????if?(n?%?2?==?0)??
  • ????????return?n==2;??
  • ????//?Now,?we?have?that?n?is?odd?and?n?>=?3.??
  • ????//?Try?to?divide?n?by?every?odd?number?i,?starting?from?3??
  • ????for?(int?i?=?3;?;?i?+=?2)??
  • ????{??
  • ????????//?We?only?have?to?try?i?up?to?the?squre?root?of?n??
  • ????????if?(i?>?n/i)??
  • ????????????break;??
  • ????????//?Now,?we?have?i?<=?n/i?<?n.??
  • ????????//?If?n?is?divisible?by?i,?n?is?not?prime.??
  • ????????if?(n?%?i?==?0)??
  • ????????????return?false;??
  • ????}??
  • ????//?n?has?no?integer?factor?in?the?range?(1,?n),?and?thus?is?prime.??
  • ????return?true;??
  • }??
  • 2.?單元測試代碼

    ?

    單元測試代碼?(test.cpp)?如下。

    [cpp]?view plaincopy
  • /**?
  • ?*?GoogleTest?test?
  • ?*?platform:?win32,?visual?studio?2005/2010;?Linux,?gcc4.1.2?
  • ?*/??
  • #include?"sample.h"??
  • #include?<gtest/gtest.h>??
  • //?Step?2.?Use?the?TEST?macro?to?define?your?tests.??
  • //?Tests?Factorial().??
  • //?Tests?factorial?of?negative?numbers.??
  • //?Test?Case?name?is?FactorialTest,?Test?name?is?Negative??
  • TEST(FactorialTest,?Negative)??
  • {??
  • ????EXPECT_EQ(1,?Factorial(-5));??
  • ????EXPECT_EQ(1,?Factorial(-1));??
  • ????EXPECT_TRUE(Factorial(-10)?>?0);??
  • }??
  • //?Tests?factorial?of?0.??
  • TEST(FactorialTest,?Zero)??
  • {??
  • ????EXPECT_EQ(1,?Factorial(0));??
  • }??
  • //?Tests?factorial?of?positive?numbers.??
  • TEST(FactorialTest,?Positive)??
  • {??
  • ????EXPECT_EQ(1,?Factorial(1));??
  • ????EXPECT_EQ(2,?Factorial(2));??
  • ????EXPECT_EQ(6,?Factorial(3));??
  • ????EXPECT_EQ(40320,?Factorial(8));??
  • }??
  • TEST(FactorialTest,?Mytest)??
  • {??
  • ????int?result?=?0;??
  • ????Factorial(5,?result);??
  • ????EXPECT_EQ(120,?result);??
  • }??
  • //?Tests?IsPrime()??
  • //?Tests?negative?input.??
  • TEST(IsPrimeTest,?Negative)??
  • {??
  • ????EXPECT_FALSE(IsPrime(-1));??
  • ????EXPECT_FALSE(IsPrime(-2));??
  • ????EXPECT_FALSE(IsPrime(INT_MIN));??
  • }??
  • //?Tests?some?trivial?cases.??
  • TEST(IsPrimeTest,?Trivial)??
  • {??
  • ????EXPECT_FALSE(IsPrime(0));??
  • ????EXPECT_FALSE(IsPrime(1));??
  • ????EXPECT_TRUE(IsPrime(2));??
  • ????EXPECT_TRUE(IsPrime(3));??
  • }??
  • //?Tests?positive?input.??
  • TEST(IsPrimeTest,?Positive)??
  • {??
  • ????EXPECT_FALSE(IsPrime(4));??
  • ????EXPECT_TRUE(IsPrime(5));??
  • ????EXPECT_FALSE(IsPrime(6));??
  • ????EXPECT_TRUE(IsPrime(23));??
  • }??
  • ??
  • //?Step?3.?Call?RUN_ALL_TESTS()?in?main().??
  • //??
  • //?We?do?this?by?linking?in?src/gtest_main.cc?file,?which?consists?of??
  • //?a?main()?function?which?calls?RUN_ALL_TESTS()?for?us.??
  • //??
  • //?This?runs?all?the?tests?you've?defined,?prints?the?result,?and??
  • //?returns?0?if?successful,?or?1?otherwise.??
  • //??
  • //?Did?you?notice?that?we?didn't?register?the?tests???The??
  • //?RUN_ALL_TESTS()?macro?magically?knows?about?all?the?tests?we??
  • //?defined.??Isn't?this?convenient???
  • 3.?編譯

    ?

    3.1 Linux?平臺

    ?

    makefile?文件,請參考??Linux平臺如何編譯使用Google test寫的單元測試??

    ?

    3.2 Win32?平臺

    ?

    Make.bat?文件,請參考??Win32?平臺如何編譯使用?Google test??寫的單元測試???

    ?

    4.?運行結果

    ?

    4.1 Linux?平臺

    ?

    運行結果如下。

    # ./test

    Running main() from gtest_main.cc

    [==========] Running 7 tests from 2 test cases.

    [----------] Global test environment set-up.

    [----------] 4 tests from FactorialTest

    [ RUN??????] FactorialTest.Negative

    [???????OK ] FactorialTest.Negative (0 ms)

    [ RUN??????] FactorialTest.Zero

    [???????OK ] FactorialTest.Zero (0 ms)

    [ RUN??????] FactorialTest.Positive

    [???????OK ] FactorialTest.Positive (0 ms)

    [ RUN??????] FactorialTest.Mytest

    [???????OK ] FactorialTest.Mytest (0 ms)

    [----------] 4 tests from FactorialTest (0 ms total)

    ?

    [----------] 3 tests from IsPrimeTest

    [ RUN??????] IsPrimeTest.Negative

    [???????OK ] IsPrimeTest.Negative (0 ms)

    [ RUN??????] IsPrimeTest.Trivial

    [???????OK ] IsPrimeTest.Trivial (0 ms)

    [ RUN??????] IsPrimeTest.Positive

    [???????OK ] IsPrimeTest.Positive (0 ms)

    [----------] 3 tests from IsPrimeTest (0 ms total)

    ?

    [----------] Global test environment tear-down

    [==========] 7 tests from 2 test cases ran. (0 ms total)

    [??PASSED??] 7 tests.

    7?個測試均通過。

    ?

    4.2 Win32?平臺

    ?

    運行結果如下。

    ?

    ?

    總結

    以上是生活随笔為你收集整理的使用Google Test的一个简单例子的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。