GTEST/GMOCK介绍与实战:Gtest Sample10

2023-05-16

文章目录

    • 1.简介
    • 2.用法

1.简介

示例#10展示了如何使用侦听器API来实现基本内存泄漏检查。

2.用法

// This sample shows how to use Google Test listener API to implement
// a primitive leak checker.

#include <stdio.h>
#include <stdlib.h>

#include "gtest/gtest.h"
using ::testing::EmptyTestEventListener;
using ::testing::InitGoogleTest;
using ::testing::Test;
using ::testing::TestEventListeners;
using ::testing::TestInfo;
using ::testing::TestPartResult;
using ::testing::UnitTest;

namespace {
// We will track memory used by this class.
class Water {
 public:
  // Normal Water declarations go here.

  // operator new and operator delete help us control water allocation.
  void* operator new(size_t allocation_size) {
    allocated_++;
    return malloc(allocation_size);
  }

  void operator delete(void* block, size_t /* allocation_size */) {
    allocated_--;
    free(block);
  }

  static int allocated() { return allocated_; }

 private:
  static int allocated_;
};

int Water::allocated_ = 0;

// This event listener monitors how many Water objects are created and
// destroyed by each test, and reports a failure if a test leaks some Water
// objects. It does this by comparing the number of live Water objects at
// the beginning of a test and at the end of a test.
class LeakChecker : public EmptyTestEventListener {
 private:
  // Called before a test starts.
  void OnTestStart(const TestInfo& /* test_info */) override {
    initially_allocated_ = Water::allocated();
  }

  // Called after a test ends.
  void OnTestEnd(const TestInfo& /* test_info */) override {
    int difference = Water::allocated() - initially_allocated_;

    // You can generate a failure in any event handler except
    // OnTestPartResult. Just use an appropriate Google Test assertion to do
    // it.
    EXPECT_LE(difference, 0) << "Leaked " << difference << " unit(s) of Water!";
  }

  int initially_allocated_;
};

TEST(ListenersTest, DoesNotLeak) {
  Water* water = new Water;
  delete water;
}

// This should fail when the --check_for_leaks command line flag is
// specified.
TEST(ListenersTest, LeaksWater) {
  Water* water = new Water;
  EXPECT_TRUE(water != nullptr);
}
}  // namespace

int main(int argc, char** argv) {
  InitGoogleTest(&argc, argv);

  bool check_for_leaks = false;
  if (argc > 1 && strcmp(argv[1], "--check_for_leaks") == 0)
    check_for_leaks = true;
  else
    printf("%s\n",
           "Run this program with --check_for_leaks to enable "
           "custom leak checking in the tests.");

  // If we are given the --check_for_leaks command line flag, installs the
  // leak checker.
  if (check_for_leaks) {
    TestEventListeners& listeners = UnitTest::GetInstance()->listeners();

    // Adds the leak checker to the end of the test event listener list,
    // after the default text output printer and the default XML report
    // generator.
    //
    // The order is important - it ensures that failures generated in the
    // leak checker's OnTestEnd() method are processed by the text and XML
    // printers *before* their OnTestEnd() methods are called, such that
    // they are attributed to the right test. Remember that a listener
    // receives an OnXyzStart event *after* listeners preceding it in the
    // list received that event, and receives an OnXyzEnd event *before*
    // listeners preceding it.
    //
    // We don't need to worry about deleting the new listener later, as
    // Google Test will do it.
    listeners.Append(new LeakChecker);
  }
  return RUN_ALL_TESTS();
}

  • ref:gtest sample
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

GTEST/GMOCK介绍与实战:Gtest Sample10 的相关文章

  • gtest教程(记录小白从0学习gtest的过程)

    gtest使用教程 1 简介 之前对gtest一无所知 最近 找了些相关的资料 学习了下 这里主要记录了学习过程和相关知识点 什么是gtest gtest测试框架是在不同平台上 xff08 Linux xff0c Mac OS X xff0
  • GoogleTest中gMock的使用

    GoogleTest中的gMock是一个库 xff0c 用于创建mock类并使用它们 当你编写原型或测试 prototype or test 时 xff0c 完全依赖真实对象通常是不可行或不明智的 not feasible or wise
  • 【C++中级篇】gtest的使用方法及cmake配置

    个人箴言 xff1a 不积跬步 xff0c 无以至千里 xff1b 不积小流 xff0c 无以成江海 夯实基础 xff0c 成就高楼大厦 前言 xff1a 在一项c 43 43 工程中 xff0c 会存在很多类 xff0c 很多功能方法 x
  • 安装kalibr踩坑1:Could NOT find GTest (missing: GTEST_LIBRARY GTEST_MAIN_LIBRARY)

    Could NOT find GTest missing GTEST LIBRARY GTEST MAIN LIBRARY 因为GTest虽然安装过 xff0c 但是好像找不到头文件 sudo apt get install libgtes
  • GTEST/GMOCK介绍与实战:Gtest Sample9

    文章目录 1 简介2 用法 1 简介 示例 9显示了使用侦听器API修改谷歌Test的控制台输出和使用其反射API来检查测试结果 2 用法 span class token comment This sample shows how to
  • Gtest输出单元测试报告和输出覆盖率报告

    文章目录 1 要求2 生成gtest测试报告3 生成gtest覆盖率报告 1 要求 编译工具 xff1a 选择Cmake xff0c 单元测试使用Gtest 2 生成gtest测试报告 gtest本身仅能输出xml或者json格式的测试报告
  • CLion下的gtest测试

    在mac环境中 xff0c 使用CLion编译的简单gtest程序 一 下载gtest源码 加入工程中 xff1a 二 编写CMakeList txt 文件 在文件中添加头文件和链接库文件 xff0c 并将链接库文件与目标文件进行链接 sp
  • Google Mock

    源码分析 通过 Google Mock Gmock 简单使用和源码分析 简单使用 中的例子 我们发现被mock的相关方法在mock类中已经被重新实现了 否则它们也不会按照我们的期待的行为执行 我们通过阅读源码 来分析整个过程的实现逻辑 转载
  • TDD三定律

    定律一 在编写不能通过的单元测试前 不可编写生产代码 定律二 只可编写刚好无法通过的单元测试 不能编译也算不过 定律三 只可编写刚好足以通过当前失败测试的生产代码 测试代码的要素 可读性 可读性 可读性 重要事说三遍 编写测试用例的模式 或
  • GTest基础学习-05-第5个单元测试-父test fixture和子test fixture的使用

    这篇来学习Gtest官方示例中的第5个 为什么直接跳过第4个 因为第四个是测试一个简单的计数器 看了下单元测试内容 没有新的知识点 就一个TEST 里面使用了连续3 4个EXPECT TRUE断言宏 完全没有任何新的知识点 就不再介绍第4个
  • 遇见gtest--事件

    1 前言 在单元测试中 我们经常需要在某个测试套件 测试用例或者整个测试运行之前进行前置条件设置及检查 或者运行之后对运行结果进行校验等操作 在gtest中 称之为事件机制 gtest将事件按照作用的范围不同进行划分 从大到小总共分为3个层
  • GTest源码剖析(六)——RUN_ALL_TESTS

    GTest源码剖析 RUN ALL TESTS GTest源码剖析RUN ALL TESTS RUN ALL TESTS源码分析 1 UnitTestRun 2 HandleExceptionsInMethodIfSupported 3 U
  • CMake项目使用ctest+gtest进行单元测试

    随着CMake工具越来越强大便捷 越来越多的C C 项目转而使用CMake来进行编译管理 它还提供了用于测试的ctest命令来执行项目中编写的单元测试 本文就以一个实例来介绍如何使用ctest来进行单元测试 一 环境准备 本文实例环境VSC
  • 使用gtest做单元测试

    使用gtest做单元测试 文章目录 使用gtest做单元测试 1 用gtest写测试工程的大致流程 配置gtest头文件及库 gtest的相关概念 TEST与TEST F 断言 事件机制 参考 gtest是一个跨平台的 Liunx Mac
  • Linux下使用gtest对接口进行单元测试

    目录 1 背景 2 gtest 断言 2 1 布尔值判断 2 2 二进制比较 2 3 字符串比较 2 4 浮点数比较 3 实践 3 1 框架使用 3 2 用例编写 3 3 编译运行 4 结论 1 背景 工程中涉及基础接口的设计 为了保证接口
  • Google Mock - GoogleTest(九)

    本文翻译自 https github com google googletest blob master googlemock docs CheatSheet md 一 定义一个模拟类 1 模拟一个正常的类 就是接口类 给 1 2 3 4
  • gtest里面的断言EXPECT_EQ和ASSERT_EQ的区别

    tips 主要用于记录工作中遇到的问题及解决方案 最近刚开始使用gtest 对里面的断言EXPECT EQ和ASSERT EQ的区别有疑惑 故记录下来 以备后续查看 TEST Binary test std string strPath O
  • GTest源码剖析(四)——TEST_P宏

    GTest源码剖析 TEST P宏 GTest源码剖析TEST P宏 TEST P宏用法 TestWithParam 类 1 TestWithParam 类定义 2 WithParamInterface 模版类定义 INSTANTIATE
  • gtest单元测试框架介绍及简单使用

    Gtest介绍 Gtest是Google的一个开源框架 它主要用于写单元测试 检查真自己的程序是否符合预期行为 可在多个平台上使用 包括Linux Mac OS X Windows Cygwin和Symbian 它提供了丰富的断言 致命和非
  • gtest 单元测试工具的基本使用

    gtest 单元测试 gtest 简介 gtest 优点 安装 gtest 测试 demo 总结 gtest 简介 gtest是Google的一套用于编写C 测试的框架 可以运行在很多平台上 包括Linux Mac OS X Windows

随机推荐

  • Pandas groupby 自定义聚合函数

    span class token comment 自定义聚合函数 xff0c span span class token comment n i o 出现次数0 gt 0 span span class token comment n i
  • Tiff超高压缩图片

    使用libtiff库 xff0c 压缩8位深度二值化图片至1位深度 path 61 34 C Users CodeFlag Desktop input 0 sel png 34 Mat img 61 imread path IMREAD C
  • M4C精读:融合多种模态到公共语义空间,使用指针增强多模态变形器来迭代应答TextVQA任务 Iterative Answer Prediction Pointer-Augmented

    M4C精读 融合多种模态到公共语义空间 xff0c 使用指针增强多模态变形器来迭代应答TextVQA任务 Iterative Answer Prediction with Pointer Augmented Multimodal Trans
  • c++ map 总结

    c 43 43 map 总结 头文件 声明 map lt int int gt p 插入 p insert map lt int int gt value type key value 已有覆盖 查找 p find key map lt i
  • 论文解析 DEEP SORT 多目标跟踪 Kalman滤波 数据关联

    论文解析 DEEP SORT 多目标跟踪 Kalman滤波 数据关联 SIMPLE ONLINE AND REALTIME TRACKING WITH A DEEP ASSOCIATION METRIC code点我 ABSTRACT SO
  • 挂载共享盘

    服务端配置 xff1a 安装 nfs xff1a sudo apt get install nfs kernel server host PC创建共享文件夹用于 mount xff0c 假设路径 dir 注意不要将源码放在共享目录配置 nf
  • VScode 格式化代码快捷键、修改快捷键

    前些天发现了一个巨牛的人工智能学习网站 xff0c 通俗易懂 xff0c 风趣幽默 xff0c 忍不住分享一下给大家 点击跳转到教程 1 请看仔细快捷键是 xff1a shift 43 alt 43 F 我是从 eclipse 转的 ide
  • C中printf()的常用输出

    1 输出整数 xff1a d int a 61 10 printf 34 d n 34 a 2 输出无符号数 xff1a u unsigned int b 61 25 printf 34 u n 34 b 3 输出十六进制数 xff1a x
  • Cmake配置工程,管理src和include文件夹,构建最常见的工程目录结构,快速入手cmake,编写CMakeLists.txt文件

    初学CMake的时候 xff0c 常用命令就算用到很熟练 xff0c 但是对于整个工程的管理还会让我们感到吃力 我在这里分享一个常用的工程目录结构 xff0c 对于CMake还不是特别熟练的同学 xff0c 帮助大家快速上手CMake 这里
  • 01、UART串口通信

    文章目录 0 前言1 串行通信的初步认识2 RS232 通信接口3 USB 转串口通信4 IO 口模拟 UART 串口通信5 UART串口通信的基本应用5 1 通信的三种基本类型5 2 UART 模块介绍5 3 UART 串口程序 6 AS
  • USART串口总结2

    前言 xff1a 开始学USART 43 DMA 的时候看到帖子 STM32 UART DMA 实现未知数据长度接收 xff0c 觉得方法妙极了 此下出自此帖子 xff08 整体的思路是这样的 xff0c 一开始设置好DMA 接收 xff0
  • Django实战(自定义模块+Echart Tree+sqlite3)

    主要从以下这些方面依次介绍这次django实战经验 xff1a 数据处理django基本框架搭建sqlite3数据库Json数据Echart树状图Jquery 异步刷新laydate 日期查询前后端交互 数据处理 从本地文件夹中获得所有源文
  • linux内核源码下载地址

    官网链接 xff1a https www kernel org HTTPhttps www kernel org pub GIThttps git kernel org 官网下载经常速度太慢 xff0c 无法下载 xff0c 提供另一个链接
  • Android 网络基础 -- HTTP 请求过程及理解

    本文来自 图解 HTTP xff0c 相关资料与图片均来自于该书 HTTP 通信过程中 xff0c 从客户端到服务端的响应是怎么样的呢 xff1f 这一章 xff0c 我们一起来了解一下 一 HTTP 报文 用于 HTTP 协议交互的信息被
  • Intel RealSense L515&Unreal Engine 4调试记录

    文章目录 前言一 安装与配置1 安装前置条件2 配置 二 编译与运行1 编译2 运行 填坑与测试1 填坑2 测试 前言 Intel RealSense系列推出了适用于Unreal Engine 4的相关插件 xff0c 官网提供了相关示例代
  • Intel RealSense L515 motion的计算与可视化

    文章目录 前言 一 环境准备 二 具体步骤 1 示例下载 2 代码编译 3 填坑 前言 前面的文章介绍了将L515数据映射至UE当中 本篇文章将针对Intel RealSense SDK 2 0 进行姿势的计算与可视化 一 环境准备 Int
  • PELCO-D协议 要点整理

    消息格式 Byte 1Byte 2Byte 3Byte 4Byte 5Byte 6Byte 7Sync ByteAddressCommand 1Command 2Data 1Data 2Checksum The synchronizatio
  • GTEST/GMOCK介绍与实战:Gtest Sample9

    文章目录 1 简介2 用法 1 简介 示例 9显示了使用侦听器API修改谷歌Test的控制台输出和使用其反射API来检查测试结果 2 用法 span class token comment This sample shows how to
  • Gtest输出单元测试报告和输出覆盖率报告

    文章目录 1 要求2 生成gtest测试报告3 生成gtest覆盖率报告 1 要求 编译工具 xff1a 选择Cmake xff0c 单元测试使用Gtest 2 生成gtest测试报告 gtest本身仅能输出xml或者json格式的测试报告
  • GTEST/GMOCK介绍与实战:Gtest Sample10

    文章目录 1 简介2 用法 1 简介 示例 10展示了如何使用侦听器API来实现基本内存泄漏检查 2 用法 span class token comment This sample shows how to use Google Test