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

[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.   
  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) 如下。

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

 

运行结果如下。

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/351795.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Java静态方法与非静态方法的泛型

Java中&#xff0c;非静态方法可以使用类的泛型&#xff0c;也可以定义自己的泛型&#xff1b;静态方法由于随着类的加载而加载&#xff0c;不能访问类的泛型&#xff08;因为在创建对象的时候才确定&#xff09;&#xff0c;因此必须定义自己的泛型类型。 详细请参考&#xf…

Android Studio 日志工具

在项目中提供5个方法打印日志 Log.v() 最常见的日志信息 Log.d() 调试信息 Log.i() 用于打印用户操作行为 Log.w()警告潜在风险 Log.e()报错信息 TAG 填入类名就好 msg:要打印的信息 也可以对信息进行过滤 点他弹出自定义的日志过滤器 转载于:https://www.cnblogs.com/feizianq…

jpa加密_使用JPA侦听器的数据库加密

jpa加密最近&#xff0c;我不得不将数据库加密添加到一些字段中&#xff0c;并且发现了很多不好的建议。 建筑问题 最大的问题是建筑。 如果持久性管理器静静地处理您的加密&#xff0c;那么根据定义&#xff0c;您的体系结构将在持久性和安全性设计之间要求紧密而不必要的绑…

同一进程中的线程究竟共享哪些资源

线程共享的环境包括&#xff1a;进程代码段、进程的公有数据(利用这些共享的数据&#xff0c;线程很容易的实现相互之间的通讯)、进程打开的文件描述符、信号的处理器、进程的当前目录和进程用户ID与进程组ID。 进程拥有这许多共性的同时&#xff0c;还拥有自己的个性。有了这些…

物联lot是什么意思_什么是物联网,物联网(lOT)简介

什么是物联网物联网(The Internet of Things&#xff0c;简称IOT)是指通过各种信息传感器、射频识别技术、全球定位系统、红外线感应器、激光扫描器等各种装置与技术&#xff0c;实时采集任何需要监控、 连接、互动的物体或过程&#xff0c;采集其声、光、热、电、力学、化 学、…

Python 位操作运算符

&按位与运算符&#xff1a;参与运算的两个值,如果两个相应位都为1,则该位的结果为1,否则为0(a & b) 输出结果 12 &#xff0c;二进制解释&#xff1a; 0000 1100|按位或运算符&#xff1a;只要对应的二个二进位有一个为1时&#xff0c;结果位就为1。(a | b) 输出结果 6…

JavaOne 2016后续活动

我很高兴今年参加了JavaOne&#xff0c;我可以用一个词概括一下这一经验&#xff1a;Brilliant。 对于我来说&#xff0c;今年与往年相比有很大不同&#xff0c;因为我在周日有一个演讲要共同演讲&#xff0c;而我剩下的一周时间都可以参加会议。 因此&#xff0c;我了解到在Ja…

python对初学者的看法_python学习之道(1)——新手小白对print()函数的理解,Python,之路,一,浅谈...

Python学习之路(一) ——浅谈新手小白对print()函数的理解写在前面笔者目前为在校大四学生(某末流211)&#xff0c;大学生活即将画上终点&#xff0c;然而却还没有真正精通一门语言&#xff0c;很是惭愧。在大学期间参加了各种文体活动&#xff0c;获得了很多次演讲比赛的奖项&…

理解Windows内核模式与用户模式

&#xfeff;&#xfeff;1、基础 运行 Windows 的计算机中的处理器有两个不同模式&#xff1a;“用户模式”和“内核模式”。根据处理器上运行的代码的类型&#xff0c;处理器在两个模式之间切换。应用程序在用户模式下运行&#xff0c;核心操作系统组件在内核模式下运行。多个…

判断使用设备是PC还是phone

<script type"text/javascript"> //如果是手机设备&#xff0c;则.. if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {window.location.href "http://www."; } </script><style type"text/css"> me…

求1+2+3+...+n

题目描述 求123...n&#xff0c;要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句&#xff08;A?B:C&#xff09;。 1、根据基本公式展开&#xff0c;利用java的幂函数代替乘法&#xff0c;利用位移运算代替除法 public class Solution {pub…

1 京东_推荐好友拿好礼 | 每1积分可兑换30元京东电子卡

三季度转推介活动火热进行中该积分最终以被推荐客户9月30日金融资产余额为依据积分兑现时间为活动结束后15个工作日我行会将京东电子卡密码通过短信方式直接发送至推荐客户预留手机号码上(请推荐客户登记真实、准确的电话号码&#xff0c;否则无法兑现积分奖励)尊敬的客户&…

在cmd命令行下编译运行C/C++源文件

一直用java来写程序&#xff0c;java配置好jre路径之后&#xff0c;在cmd下编译运行&#xff0c;很方便。 刚好要给一个舍友改下C程序&#xff0c;想到可不可以像java一样在环境变量里配置好C的编译路径呢&#xff1f; 于是上网搜了一下&#xff0c;得到如下结果&#xff1a; 一…

制作程序化装饰花纹图案_用装饰器设计图案装饰

制作程序化装饰花纹图案装饰图案是广泛使用的结构图案之一。 此模式在运行时动态更改对象的功能&#xff0c;而不会影响对象的现有功能。 简而言之&#xff0c;此模式通过包装将附加功能添加到对象。 问题陈述&#xff1a; 想象一个场景&#xff0c;我们有一个比萨饼&#xff…

10停止nginx命令 win_windows版nginx快速操控神器(重启,关闭)

众所周知,Windows 版本的Nginx 是linux版本的阉割版,但是在开发或者个人测试的时候,还是非常好用的&#xff0c;但是nginx运行的命令自己敲打起来不是那么的方便,因此呢&#xff0c;我们就写了个Bat批处理小程序&#xff0c;封装了这些命令&#xff0c;2.1版本您只需要配置下 N…

学习vi和vim编辑器(8):全局替换(1)

本章学习vi编辑器中的全局替换命令。通过全局替换命令&#xff0c;可以自动替换文件中所有出现过的某个单词。全局替换一般会用到两个ex命令&#xff1a;":g"(global)&#xff0c;":s"(substitute)。 替换命令&#xff1a; 替换命令的语法如下&#xff1…

使用DynamoDBMapper查询DynamoDB项目

在上一篇文章中&#xff0c;我们使用底层Java api在DynamoDB数据库上发出了查询。 使用DynamoDBMapper进行查询非常简单。 使用哈希键发出查询非常简单。 这样的查询的最佳候选者是通过使用电子邮件哈希键进行搜索的Users表。 public User getUser(String email) {User user…

实训二—博客三

学期末总结 经过了一个学期的java学习&#xff0c;使我对java产生了很大的兴趣&#xff0c;虽然我学的并不怎么好&#xff0c;但是我喜欢程序可以成功运行时带来的成就感&#xff0c;我也享受排错的过程。学加练的上课模式我觉得也很棒&#xff0c;给了我们独立思考编程的时间&…

小肚皮最新版本_小肚皮旧版本

00后最炙手可热的社交软件《小肚皮旧版本》等你下载天&#xff0c;在这里独特的社交玩法等你感受&#xff0c;为你带来有趣的玩法乐趣&#xff0c;小肚皮旧版本中丰富的乐趣玩法等你来感受&#xff0c;让你轻松享受到更多的乐趣所在&#xff0c;为你带来不一样的社交乐趣。特色…

小心使用STL中map的[]操作符

一个map就是一个&#xff08;关键码&#xff08;key&#xff09;&#xff0c;值&#xff08;value&#xff09;&#xff09;对偶的序列&#xff0c;它提供基于关键码的快速提取操作。也就是说&#xff0c;可以用下标运算符[]将关键码作为下标去执行查找&#xff0c;并返回对应的…