使用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) 代码如下。
- /**
- * 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) 代码如下。
- /**
- * 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) 如下。
- /**
- * 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的一个简单例子相关推荐
- SAP MM采购定价过程的一个简单例子
SAP MM采购定价过程的一个简单例子 本文以一个简单的例子阐述了SAP MM模块中采购定价的基本原理.本例中,假定采购订单里输入的是含税采购价,然后系统自动计算出物料最终的采购价格(含税价-税额=采 ...
- 一个简单例子:贫血模型or领域模型
转:一个简单例子:贫血模型or领域模型 贫血模型 我们首先用贫血模型来实现.所谓贫血模型就是模型对象之间存在完整的关联(可能存在多余的关联),但是对象除了get和set方外外几乎就没有其它的方法,整个 ...
- KaTex的一个简单例子
在网页中可以用KaTex显示数学公式,KaTex官网是https://katex.org 以下是KaTex的一个简单例子: 下载https://github.com/KaTeX/KaTeX/archi ...
- 单纯形法之人工变量法求解步骤:一个简单例子
文章目录 人工变量法 1. 大M法 1.1. 题目 1.2. 转化为标准型 1.3. 添加人工变量 2. 两阶段法 2.1. 步骤 2.2. 题目 2.2.1. 转化为标准型 2.2.2. 添加人工变 ...
- 初次使用CTex时的一个简单例子
初次使用CTex时的一个简单例子 1.初次使用Ctex时的例子 第一次使用 LATEX,在WinEdt编辑以下代码,然后使用PDFLaTeX生成PDF文档. \documentclass{articl ...
- 协方差检验用在什么地方_通过一个简单例子,通俗讲下协方差分析
内容来自:"小白学统计"微信公众号,感谢作者授权. 临床中经常碰到这种设计:研究对象分为两组,接受不同治疗(如治疗组和安慰组),每组分别在治疗前和治疗后测量观察指标(如血压值).目 ...
- 什么是量子计算机?用一个简单例子来解释
译者:王亮 作者:YK Sugi 原文:http://t.cn/EZAElk0 Hi,大家好! 不久前,我参观了加拿大温哥华的D-Wave Systems公司,这是一家制造前沿量子计算机的公司. ...
- 通过一个简单例子理解 RecyclerView.ItemDecoration
一.前言 RecyclerView 是从5.0推出的 MD 风格的控件.RecyclerView 之前有 ListView.GridView,但是功能很有限,例如 ListView 只能实现垂直方向上 ...
- 词法分析程序 LEX和VC6整合使用的一个简单例子
词法分析的理论知识不少,包括了正规式.正规文法.它们之间的转换以及确定的有穷自动机和不确定的有穷自动机等等... 要自己写一个词法分析器也不会很难,只要给出了最简的有穷自动机,就能很方便实现了,用if ...
最新文章
- 求解单源最短路径的几种算法
- 概率分布,先懂这6个
- Android Studio编译问题-Error:Could not find org.jetbrains.trove4j
- 修改树莓派更新源及设置代理配置
- SAP Cloud for Customer Cloud Application Studio的下载地址
- shell 文件 开头的格式
- Katalon Recorder录制脚本
- foreach 用法例子
- ChengDu University Mental Health Test 需求分析文档
- 计算机编码,二进制编码数字
- 数学建模——蒙特卡罗模型
- 【转载】机器指令计算机
- 上海移动视频客户端 for 8910
- 统计字符号串“aaaabbbccccddfggh”中字母个数或统计最多字母数;
- 前缀和【一维前缀和与二维前缀和】
- python画心形代码大全_还在为 520 发愁吗?教你用 Python 写个表白神器!
- dacom蓝牙耳机怎么重置_蓝牙耳机常见故障处理方法
- Matlab基础——切比雪夫I型滤波器(二)
- 常用深度学习数据集网址
- 1、查询各班各科分数最高的学生学号,姓名,班级名称,科目名称,分数:
热门文章
- jenkins 新手入门安装失败_树莓派安装jenkins,安安静静做个持续集成打包机
- python在材料模拟中的应用_材料模拟python_模拟-python模拟-在不妨碍实现的情况下修补方法...
- fedora如何隐藏顶部状态栏_藏在 iOS 13 里的 9 个隐藏小技巧
- 三层交换机有什么优势?
- 光端机的原理和使用范围
- 网管交换机与非网管交换机的利弊介绍
- 如何分辨PoE工业交换机是否标准供电
- [渝粤教育] 西南科技大学 英语口语 在线考试复习资料
- 【渝粤教育】电大中专品牌管理与推广 (2)作业 题库
- 蓝桥杯基础模块8_2:串口进阶