This article describes how to use Gtest for unit testing.
Gtest is the Google Unit testing framework. Very convenient to use.
First, download the gtest (some Google projects include gtest, such as Protobuf), and copy the directory to use.
http://code.google.com/p/googletest/
If the wall, Baidu search, a lot.
Unzip the Gtest.zip and get the gtest.1.x.x directory.
Export Gtest_home= the Directory
Compile:
CD $GTEST _home/make
Make
Run the sample program and familiarize yourself with the gtest style
./sample1_unittes
A static library of Gtest_main.a is also obtained after compiling.
Ln-s Gtest_main.a LIBGTEST.A
Soft links to conform to the GCC Library Reference specification.
After pressing sample1_unittes.cc to write your own test code (TEST.CPP), you are ready to start compiling
g++-i$gtest_home/include-l$gtest_home/make-lgtest Test.cpp-o Test.exe
Generating a test program Test.exe
Combination number calculation Code combination.cpp:
#include <stdio.h>#include<stdlib.h>#include<string.h>#include"Combination.h"LongCombination (Long Base,Long Select) {Longres=0;if(Base<Select||Base<=0)return 0;if(Base==Select)return 1;if(Select>=Base-Select) {res=arrangement (Select+1,Base)/Arrangement (1,Base-Select);}Else{res=arrangement (Base-Select+1,Base)/Arrangement (1,Select);}returnRes;}LongArrangement (LongStartLongend) {if(Start <=0|| Start >end) {return 1;}Longres=start; for(Longi=start+1; i<=end;i++) {res*=i;}returnRes;}
Header file Combination.h:
Long combination (longbase,longselect); Long Arrangement (long start,long
Test Code Gtest_combination.cpp
#include <limits.h>#include"Combination.h"#include<gtest/gtest.h>TEST (arrangement, all) {Expect_eq (1, Arrangement (0,1)); Expect_eq (1, Arrangement (1,1)); Expect_eq (1, Arrangement (1,0)); Expect_eq (1, Arrangement (5,0)); Expect_eq (2, Arrangement (2,2)); Expect_eq ( -, Arrangement (2,4)); Expect_eq ( -, Arrangement (1,5)); Expect_eq (7*8*9, Arrangement (7,9));} TEST (combination, all) {Expect_eq (0, Combination (0,1)); Expect_eq (0, Combination (0,0)); Expect_eq (0, Combination (-1,1)); Expect_eq (0, Combination (1,2)); Expect_eq (1, Combination (1,1)); Expect_eq (2, Combination (2,1)); Expect_eq (3, Combination (3,1)); Expect_eq (1, Combination (2,2)); Expect_eq (3, Combination (3,2)); Expect_eq (1, Combination (3,3)); Expect_eq (1, Combination (4,0)); Expect_eq (4, Combination (4,1)); Expect_eq (6, Combination (4,2)); Expect_eq (4, Combination (4,3)); Expect_eq (1, Combination (4,4)); Expect_eq (0, Combination (4,5)); Expect_eq ( -, Combination (9,3));}
Makefile:
Test=gtest_combination
testobj=$ (TEST). O
tarobj=combination.o
All: $ (TEST)
$ (TEST): $ (testobj) $ (tarobj)
g++-i$ (gtest_home)/include-l$ (gtest_home)/make-lgtest $^-o [email protected]
%.O:%.cpp
g++-c-i$ (gtest_home)/include-g-o [email protected] $<
Compiling and testing:
$make
$gtest _combination
Output Result:
Running main () from gtest_main.cc
[==========] Running 2 Tests from 2 test cases.
[----------] Global test environment set-up.
[----------] 1 test from arrangement
[RUN] Arrangement.all
[OK] Arrangement.all (0 ms)
[----------] 1 test from arrangement (0 ms Total)
[----------] 1 test from combination
[RUN] Combination.all
[OK] Combination.all (0 ms)
[----------] 1 test from combination (0 MS total)
[----------] Global test Environment Tear-down
[==========] 2 tests from 2 test cases ran. (6 ms Total)
[PASSED] 2 tests.
Gtest of C + + unit test--combinatorial number calculation.