Linux与Win下使用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) 代码如下。

  1. /** 
  2.  * GoogleTest test 
  3.  * platform: win32, visual studio 2005/2010; Linux, gcc4.1.2 
  4.  */ 
  5. #ifndef _SAMPLE_H_  
  6. #define _SAMPLE_H_   
  7. // Returns n! (the factorial of n).  For negative n, n! is defined to be 1.   
  8. int Factorial(int n);  
  9. void Factorial(int n, int &result);  
  10. // Returns true iff n is a prime number.   
  11. bool IsPrime(int n);  
  12. #endif  

要测试的代码 (Sample.cpp) 代码如下。

  1. /** 
  2.  * GoogleTest test 
  3.  * platform: win32, visual studio 2005/2010; Linux, gcc4.1.2 
  4.  */  
  5. #include "sample.h"   
  6. // Returns n! (the factorial of n).  For negative n, n! is defined to be 1.   
  7. int Factorial(int n)  
  8. {  
  9.     int result = 1;  
  10.     for (int i = 1; i <= n; i++)  
  11.         result *= i;  
  12.     return result;  
  13. }  
  14. void Factorial(int n, int &result)  
  15. {  
  16.     result = 1;  
  17.     for (int i = 1; i <= n; i++)  
  18.         result *= i;  
  19. }  
  20.   
  21. // Returns true iff n is a prime number.   
  22. bool IsPrime(int n)  
  23. {  
  24.     // Trivial case 1: small numbers   
  25.     if (n <= 1)  
  26.         return false;  
  27.     // Trivial case 2: even numbers   
  28.     if (n % 2 == 0)  
  29.         return n==2;  
  30.     // Now, we have that n is odd and n >= 3.   
  31.     // Try to divide n by every odd number i, starting from 3   
  32.     for (int i = 3; ; i += 2)  
  33.     {  
  34.         // We only have to try i up to the squre root of n   
  35.         if (i > n/i)  
  36.             break;  
  37.         // Now, we have i <= n/i < n.   
  38.         // If n is divisible by i, n is not prime.   
  39.         if (n % i == 0)  
  40.             return false;  
  41.     }  
  42.     // n has no integer factor in the range (1, n), and thus is prime.   
  43.     return true;  
  44. }  

2. 单元测试代码

 

单元测试代码 (test.cpp) 如下。

  1. /** 
  2.  * GoogleTest test 
  3.  * platform: win32, visual studio 2005/2010; Linux, gcc4.1.2 
  4.  */  
  5. #include "sample.h"   
  6. #include <gtest/gtest.h>   
  7. // Step 2. Use the TEST macro to define your tests.   
  8. // Tests Factorial().   
  9. // Tests factorial of negative numbers.   
  10. // Test Case name is FactorialTest, Test name is Negative   
  11. TEST(FactorialTest, Negative)  
  12. {  
  13.     EXPECT_EQ(1, Factorial(-5));  
  14.     EXPECT_EQ(1, Factorial(-1));  
  15.     EXPECT_TRUE(Factorial(-10) > 0);  
  16. }  
  17. // Tests factorial of 0.   
  18. TEST(FactorialTest, Zero)  
  19. {  
  20.     EXPECT_EQ(1, Factorial(0));  
  21. }  
  22. // Tests factorial of positive numbers.   
  23. TEST(FactorialTest, Positive)  
  24. {  
  25.     EXPECT_EQ(1, Factorial(1));  
  26.     EXPECT_EQ(2, Factorial(2));  
  27.     EXPECT_EQ(6, Factorial(3));  
  28.     EXPECT_EQ(40320, Factorial(8));  
  29. }  
  30. TEST(FactorialTest, Mytest)  
  31. {  
  32.     int result = 0;  
  33.     Factorial(5, result);  
  34.     EXPECT_EQ(120, result);  
  35. }  
  36. // Tests IsPrime()   
  37. // Tests negative input.   
  38. TEST(IsPrimeTest, Negative)  
  39. {  
  40.     EXPECT_FALSE(IsPrime(-1));  
  41.     EXPECT_FALSE(IsPrime(-2));  
  42.     EXPECT_FALSE(IsPrime(INT_MIN));  
  43. }  
  44. // Tests some trivial cases.   
  45. TEST(IsPrimeTest, Trivial)  
  46. {  
  47.     EXPECT_FALSE(IsPrime(0));  
  48.     EXPECT_FALSE(IsPrime(1));  
  49.     EXPECT_TRUE(IsPrime(2));  
  50.     EXPECT_TRUE(IsPrime(3));  
  51. }  
  52. // Tests positive input.   
  53. TEST(IsPrimeTest, Positive)  
  54. {  
  55.     EXPECT_FALSE(IsPrime(4));  
  56.     EXPECT_TRUE(IsPrime(5));  
  57.     EXPECT_FALSE(IsPrime(6));  
  58.     EXPECT_TRUE(IsPrime(23));  
  59. }  
  60.   
  61. // Step 3. Call RUN_ALL_TESTS() in main().   
  62. //   
  63. // We do this by linking in src/gtest_main.cc file, which consists of   
  64. // a main() function which calls RUN_ALL_TESTS() for us.   
  65. //   
  66. // This runs all the tests you've defined, prints the result, and   
  67. // returns 0 if successful, or 1 otherwise.   
  68. //   
  69. // Did you notice that we didn't register the tests?  The   
  70. // RUN_ALL_TESTS() macro magically knows about all the tests we   
  71. // 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 平台

 

运行结果如下。

 

 

相关内容