0. 引子

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. 要测试的代码

```/**
* 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```

```/**
* 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. 单元测试代码

```/**
* 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 平台

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 平台

Technorati 标签: