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
  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) 代码如下。

[cpp] view plaincopy
  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. // Returns true iff n is a prime number.
  21. bool IsPrime(int n)
  22. {
  23. // Trivial case 1: small numbers
  24. if (n <= 1)
  25. return false;
  26. // Trivial case 2: even numbers
  27. if (n % 2 == 0)
  28. return n==2;
  29. // Now, we have that n is odd and n >= 3.
  30. // Try to divide n by every odd number i, starting from 3
  31. for (int i = 3; ; i += 2)
  32. {
  33. // We only have to try i up to the squre root of n
  34. if (i > n/i)
  35. break;
  36. // Now, we have i <= n/i < n.
  37. // If n is divisible by i, n is not prime.
  38. if (n % i == 0)
  39. return false;
  40. }
  41. // n has no integer factor in the range (1, n), and thus is prime.
  42. return true;
  43. }

2. 单元测试代码

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

[cpp] view plaincopy
  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. // Step 3. Call RUN_ALL_TESTS() in main().
  61. //
  62. // We do this by linking in src/gtest_main.cc file, which consists of
  63. // a main() function which calls RUN_ALL_TESTS() for us.
  64. //
  65. // This runs all the tests you've defined, prints the result, and
  66. // returns 0 if successful, or 1 otherwise.
  67. //
  68. // Did you notice that we didn't register the tests?  The
  69. // RUN_ALL_TESTS() macro magically knows about all the tests we
  70. // 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的一个简单例子相关推荐

  1. SAP MM采购定价过程的一个简单例子

    SAP MM采购定价过程的一个简单例子 本文以一个简单的例子阐述了SAP MM模块中采购定价的基本原理.本例中,假定采购订单里输入的是含税采购价,然后系统自动计算出物料最终的采购价格(含税价-税额=采 ...

  2. 一个简单例子:贫血模型or领域模型

    转:一个简单例子:贫血模型or领域模型 贫血模型 我们首先用贫血模型来实现.所谓贫血模型就是模型对象之间存在完整的关联(可能存在多余的关联),但是对象除了get和set方外外几乎就没有其它的方法,整个 ...

  3. KaTex的一个简单例子

    在网页中可以用KaTex显示数学公式,KaTex官网是https://katex.org 以下是KaTex的一个简单例子: 下载https://github.com/KaTeX/KaTeX/archi ...

  4. 单纯形法之人工变量法求解步骤:一个简单例子

    文章目录 人工变量法 1. 大M法 1.1. 题目 1.2. 转化为标准型 1.3. 添加人工变量 2. 两阶段法 2.1. 步骤 2.2. 题目 2.2.1. 转化为标准型 2.2.2. 添加人工变 ...

  5. 初次使用CTex时的一个简单例子

    初次使用CTex时的一个简单例子 1.初次使用Ctex时的例子 第一次使用 LATEX,在WinEdt编辑以下代码,然后使用PDFLaTeX生成PDF文档. \documentclass{articl ...

  6. 协方差检验用在什么地方_通过一个简单例子,通俗讲下协方差分析

    内容来自:"小白学统计"微信公众号,感谢作者授权. 临床中经常碰到这种设计:研究对象分为两组,接受不同治疗(如治疗组和安慰组),每组分别在治疗前和治疗后测量观察指标(如血压值).目 ...

  7. 什么是量子计算机?用一个简单例子来解释

    译者:王亮  作者:YK Sugi  原文:http://t.cn/EZAElk0 Hi,大家好! 不久前,我参观了加拿大温哥华的D-Wave Systems公司,这是一家制造前沿量子计算机的公司. ...

  8. 通过一个简单例子理解 RecyclerView.ItemDecoration

    一.前言 RecyclerView 是从5.0推出的 MD 风格的控件.RecyclerView 之前有 ListView.GridView,但是功能很有限,例如 ListView 只能实现垂直方向上 ...

  9. 词法分析程序 LEX和VC6整合使用的一个简单例子

    词法分析的理论知识不少,包括了正规式.正规文法.它们之间的转换以及确定的有穷自动机和不确定的有穷自动机等等... 要自己写一个词法分析器也不会很难,只要给出了最简的有穷自动机,就能很方便实现了,用if ...

最新文章

  1. 求解单源最短路径的几种算法
  2. 概率分布,先懂这6个
  3. Android Studio编译问题-Error:Could not find org.jetbrains.trove4j
  4. 修改树莓派更新源及设置代理配置
  5. SAP Cloud for Customer Cloud Application Studio的下载地址
  6. shell 文件 开头的格式
  7. Katalon Recorder录制脚本
  8. foreach 用法例子
  9. ChengDu University Mental Health Test 需求分析文档
  10. 计算机编码,二进制编码数字
  11. 数学建模——蒙特卡罗模型
  12. 【转载】机器指令计算机
  13. 上海移动视频客户端 for 8910
  14. 统计字符号串“aaaabbbccccddfggh”中字母个数或统计最多字母数;
  15. 前缀和【一维前缀和与二维前缀和】
  16. python画心形代码大全_还在为 520 发愁吗?教你用 Python 写个表白神器!
  17. dacom蓝牙耳机怎么重置_蓝牙耳机常见故障处理方法
  18. Matlab基础——切比雪夫I型滤波器(二)
  19. 常用深度学习数据集网址
  20. 1、查询各班各科分数最高的学生学号,姓名,班级名称,科目名称,分数:

热门文章

  1. jenkins 新手入门安装失败_树莓派安装jenkins,安安静静做个持续集成打包机
  2. python在材料模拟中的应用_材料模拟python_模拟-python模拟-在不妨碍实现的情况下修补方法...
  3. fedora如何隐藏顶部状态栏_藏在 iOS 13 里的 9 个隐藏小技巧
  4. 三层交换机有什么优势?
  5. 光端机的原理和使用范围
  6. 网管交换机与非网管交换机的利弊介绍
  7. 如何分辨PoE工业交换机是否标准供电
  8. [渝粤教育] 西南科技大学 英语口语 在线考试复习资料
  9. 【渝粤教育】电大中专品牌管理与推广 (2)作业 题库
  10. 蓝桥杯基础模块8_2:串口进阶