gtest学习笔记(二)gtest自带的sample1---Func函数使用

2023-10-29


前言

第一章中已经编译出自带的sample例子,在build/googletest目录下可以看到sample的各种例子的可执行程序。
Google Test 附带了10个单元测试用例,难度由浅及深。
sample1主要演示了如何测试函数

源码学习

  1. sample1由三个部分组成:sample1.h , sample1.cpp , sample1UnitTest.cpp (作者对原始文档进行了命名调整,以便更好的进行说明)
  2. sample1.cpp中撰写了待测试的函数,对应头文件 sample1.h。Factorial( )用于求一个数的阶乘,IsPrime( ) 用于判定一个数是否是素数
#include "sample1.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;
}

// Returns true if and only if 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 square 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;
}
  1. sample01.h 进行了函数声明
#ifndef GOOGLETEST_SAMPLES_SAMPLE1_H_
#define GOOGLETEST_SAMPLES_SAMPLE1_H_

// Returns n! (the factorial of n).  For negative n, n! is defined to be 1.
int Factorial(int n);

// Returns true if and only if n is a prime number.
bool IsPrime(int n);

#endif  // GOOGLETEST_SAMPLES_SAMPLE1_H_
  1. sample1UnitTest.cpp是对应单元测试的代码
#include "sample1.h"

#include <limits.h>

#include "gtest/gtest.h"
namespace {

// Step 2. Use the TEST macro to define your tests.
//
// TEST has two parameters: the test case name and the test name.
// After using the macro, you should define your test logic between a
// pair of braces.  You can use a bunch of macros to indicate the
// success or failure of a test.  EXPECT_TRUE and EXPECT_EQ are
// examples of such macros.  For a complete list, see gtest.h.
//
// <TechnicalDetails>
//
// In Google Test, tests are grouped into test cases.  This is how we
// keep test code organized.  You should put logically related tests
// into the same test case.
//
// The test case name and the test name should both be valid C++
// identifiers.  And you should not use underscore (_) in the names.
//
// Google Test guarantees that each test you define is run exactly
// once, but it makes no guarantee on the order the tests are
// executed.  Therefore, you should write your tests in such a way
// that their results don't depend on their order.
//
// </TechnicalDetails>

// Tests Factorial().

// Tests factorial of negative numbers.
TEST(FactorialTest, Negative) {
  // This test is named "Negative", and belongs to the "FactorialTest"
  // test case.
  EXPECT_EQ(1, Factorial(-5));
  EXPECT_EQ(1, Factorial(-1));
  EXPECT_GT(Factorial(-10), 0);

  // <TechnicalDetails>
  //
  // EXPECT_EQ(expected, actual) is the same as
  //
  //   EXPECT_TRUE((expected) == (actual))
  //
  // except that it will print both the expected value and the actual
  // value when the assertion fails.  This is very helpful for
  // debugging.  Therefore in this case EXPECT_EQ is preferred.
  //
  // On the other hand, EXPECT_TRUE accepts any Boolean expression,
  // and is thus more general.
  //
  // </TechnicalDetails>
}

// 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));
}

// Tests IsPrime()

// Tests negative input.
TEST(IsPrimeTest, Negative) {
  // This test belongs to the IsPrimeTest test case.

  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));
}
}  // namespace

// 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?

utest语法

  1. TEST宏的简单使用
  2. EXPECT_EQ断言宏
  3. EXPECT_GT断言宏
  4. EXPECT_FALSE断言宏
  5. EXPECT_TRUE断言宏

运行

通过命令./sample1 运行sample1例子

~/learn/third_party/01-gtest/googletest-main/build/googletest$ ./sample1_unittest 
Running main() from /home/zhouchen/learn/third_party/01-gtest/googletest-main/googletest/src/gtest_main.cc
[==========] Running 6 tests from 2 test suites.
[----------] Global test environment set-up.
[----------] 3 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)
[----------] 3 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
[==========] 6 tests from 2 test suites ran. (0 ms total)
[  PASSED  ] 6 tests.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

gtest学习笔记(二)gtest自带的sample1---Func函数使用 的相关文章

  • JDK安装及oracle安装

    这里写自定义目录标题 JDK安装 虚拟机win03中安装Oracle JDK安装 1 jdk安装 解压安装包进行安装 记住安装地址 不要安装在含有中文的文件夹 若忘记则win r键 输入cmd 进入命令行模式 输入 where java 查
  • Docker的自定义镜像

    文章目录 五 自定义镜像 五 自定义镜像 回顾一下 什么是镜像 答 镜像是将应用程序及其所需要的系统函数库 环境 依赖 配置打包而成 镜像的结构是分层的 每一层称为一个Layer 其中 最底层包含基本的系统函数库 环境变量 文件系统 的层我
  • 如何使用React脚手架新建一个React项目

    1 react脚手架 xxx脚手架 用来帮助程序员快速创建一个基于xxx库的模板项目 包含了所有需要的配置 语法检查 jsx编译 devServer 下载好了所有相关的依赖 可以直接运行一个简单效果 react提供了一个用于创建react项
  • 狂神说Docker进阶篇笔记

    文章目录 Docker Compase 简介 官方介绍 安装 体验 网络规则 停止 小结 docker compose yaml 规则 实战开源项目 WordPress 实战微服务 小结 Docker Swarm 工作模式 node Ser
  • openjudge 1.6.7 有趣的跳跃

    OpenJudge 07 有趣的跳跃 解题思路 1 有趣的跳跃定义为n个数据 相邻两个相减后得到的绝对值的n 1个数据 经过排序后 正好是从1到n 1 那么首先的步骤为创建一个能容纳n个数据的数组 int a 3005 定义在主函数外 2

随机推荐

  • 前端代码集锦

    HTML中隐藏标签和显示标签的例子 注意登录前后的变化 HTML的代码 div div class login div div
  • cd.ssh bash: cd.ssh: 未找到命令.../没有这个文件或目录

    在linux中设置免密登录的时候 cd ssh 报错说bash cd ssh 未找到命令 原来是我的命令打错了 应该是cd ssh 后面有个 但是我这样输入以后 还是报错 说什么没有这个文件或目录 那你就要先ssh 一下自己的现在的ip地址
  • nodejs+vue+vue-router.路由问题

    路由加载出现警告 页面上路由不起作用 查看源码发现router view标签被解析成 lt gt 由于没有完全编译 修改配置文件vue config js添加如下配置
  • 在linux中,如何增加、修改、删除、暂停和冻结用户名

    在linux中 如何增加 修改 删除 暂停和冻结用户名 在操作增加 修改和删除用户名前 先认识linux中两个最重要的文件 它们就是账号管理最重要文件 etc passwd 与 etc shadow 环境 windows 7 virtual
  • router-view上添加key和data-key的作用

    key属性 用于提供对每个渲染的组件的唯一标识 当路由切换时 如果
  • 2022年12月-电子学会青少年等级考试C语言(一级)真题与解析

    2022年12月软件编程 C语言 等级考试 一级 分数 100 题数 5 时间限制 1000 ms 内存限制 65536 kB 1 加一 题目描述 输入一个整数x 输出这个数加1后的值 即x 1的值 输入 一个整数x 0 x 1000 输出
  • C/S、B/S架构详解,一文带你搞懂

    一 CS BS架构定义 CS架构 Client Server Architecture 是一种分布式计算模型 其中客户端和服务器之间通过网络进行通信 在这种架构中 客户端负责向服务器发送请求 并接收服务器返回的响应 服务器则负责处理客户端的
  • IDEA中 @override报错的解决方法

    IDEA中 override报错的解决方法 参考文章 1 IDEA中 override报错的解决方法 2 https www cnblogs com printN p 6870036 html 备忘一下
  • ETL正则匹配汇总

    一 正则匹配全部汇总 1 匹配中文 u4e00 u9fa5 2 英文字母 a zA Z 3 数字 0 9 4 匹配中文 英文字母和数字及下划线 u4e00 u9fa5 a zA Z0 9 同时判断输入长度 u4e00 u9fa5 a zA
  • 分布式事务有这一篇就够了!

    分布式事务 文章目录 分布式事务 1 基础概念 1 1 什么是事务 1 2 本地事务 1 3 分布式事务 1 4 分布式事务产生的情景 2 分布式事务基础理论 2 1 CAP理论 2 1 1 理解CAP C Consistency A Av
  • 盒子的宽度超出元素的盒子的解决办法box-sizing

    padding用在哪里超出的 box sizing就放在哪 盒子的宽度超出元素的盒子的解决办法 未修改的代码与样式 rpx与px都是单位 如果不了解就当px处理 1 calc 减轻相当于两侧的padding值 不推荐 了解 2 border
  • 计算机专业毕业设计选题原则

    计算机技术变化很快 新技术新观念每年都会涌现很多 计算机专业的毕业设计 是一次非常好的把理论知识结合实践的好机会 所以 选好自己的毕业设计题目 相当重要 我们建议 你要结合自己的职业理想来做 1 如果你将来想从事信息系统类开发 建议选择C
  • SpringBoot项目整合RabbitMQ

    1 简介 消息队列 Message Queue 是分布式系统中常用的组件 它允许不同的应用程序之间通过发送和接收消息进行通信 Spring Boot提供了简单且强大的方式来整合消息队列 其中包括RabbitMQ ActiveMQ Kafka
  • openwrt控制天翼网关定时重启

    由于天翼网关不支持定时重启 另外app里设置定时重启也没有作用 所以查找了一些相关工具和用法 再结合前辈的经验 参考的博客网址 自己最终把该功能实现了 首先需要用winscp登陆到刷了openwrt的路由器中 然后在 usr bin 目录下
  • CC2530学习(一)环境配置

    CC2530F256是一款将各种丰富的功能系统地集成到一片LSI Large Scale Integrated circuit 大规模集成电路 的片上系统 System on Chip 简称SoC 内嵌增强型51内核的单片机 芯片后缀256
  • docker安装redis(镜像安装)

    目录 安装 1 1 安装redis镜像 2 查看redis镜像 3 运行容器 4 查看容器安装成功 安装2 1 安装redis镜像 其中latest是镜像版本 根据程序需要 选择适合的版本 2 新建data和conf两个文件夹 位置随意 3
  • frida native hook简单demo

    记录frida hook native的笔记 整体代码如下 Java perform function console log Inside java perform function function jstring2Str jstrin
  • Basic Level 1027 打印沙漏 (20分)

    题目 本题要求你写个程序把给定的符号打印成沙漏的形状 例如给定17个 要求按下列格式打印 所谓 沙漏形状 是指每行输出奇数个符号 各行符号中心对齐 相邻两行符号数差2 符号数先从大到小顺序递减到1 再从小到大顺序递增 首尾符号数相等 给定任
  • pacmaker与corosync实现高可用集群

    一 pacmaker与corosync的简单介绍 1 pacmaker pacemaker是一个开源的高可用资源管理器 CRM 位于HA集群架构中资源管理 资源代理 RA 这个层次 它不能提供底层心跳信息传递的功能 要想与对方节点通信需要借
  • gtest学习笔记(二)gtest自带的sample1---Func函数使用

    文章目录 前言 源码学习 utest语法 运行 前言 第一章中已经编译出自带的sample例子 在build googletest目录下可以看到sample的各种例子的可执行程序 Google Test 附带了10个单元测试用例 难度由浅及