Google单元测试框架gtest之官方sample笔记4--事件监控之内存泄漏测试
sample 10 使用event listener监控Water类的创建和销毁。在Water类中,有一个静态变量allocated,创建一次值加一,销毁一次值减一。为了实现这个功能,重载了new和delete关键字,然后在new和delete函数中,做allocated的增减和记录allocated变量的值。
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;
gtest的event listener能在TEST执行前和执行后调用,然后就可以判断TEST执行完后是否发生泄漏。event listner是非入侵式检测,不需要在TEST里写测试代码,而是在TEST之外执行特定的监控代码。
注册event监听的方法如下,在每个测试前执行OnTestStart,在测试后执行OnTestEnd。计算int difference = Water::allocated() - initially_allocated_;就可以得知是否发生内存泄漏,忘记了删除new的对象。
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_;
};
// 如果检查泄漏,则注册event listener监控
if (check_for_leaks) {
TestEventListeners& listeners = UnitTest::GetInstance()->listeners();
listeners.Append(new LeakChecker);
}
测试代码如下,由于使用的是非入侵式检测,所以TEST函数和普通的测试一样。下面的DoesNotLeak测试无内存泄漏发生,而LeaksWater测试,会发生内存泄漏。
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);
}
不启用内存泄漏测试时的输出结果:

使用内存泄漏测试时的输出结果:

尊重技术文章,转载请注明!
Google单元测试框架gtest之官方sample笔记4--事件监控之内存泄漏测试
Google单元测试框架gtest之官方sample笔记4--事件监控之内存泄漏测试的更多相关文章
- Google单元测试框架gtest之官方sample笔记2--类型参数测试
gtest 提供了类型参数化测试方案,可以测试不同类型的数据接口,比如模板测试.可以定义参数类型列表,按照列表定义的类型,每个测试case都执行一遍. 本例中,定义了2种计算素数的类,一个是实时计算, ...
- Google单元测试框架gtest之官方sample笔记3--值参数化测试
1.7 sample7--接口测试 值参数不限定类型,也可以是类的引用,这就可以实现对类接口的测试,一个基类可以有多个继承类,那么可以测试不同的子类功能,但是只需要写一个测试用例,然后使用参数列表实现 ...
- Google单元测试框架gtest之官方sample笔记1--简单用例
1.0 通用部分 和常见的测试工具一样,gtest提供了单体测试常见的工具和组件.比如判断各种类型的值相等,大于,小于等,管理多个测试的测试组如testsuit下辖testcase,为了方便处理初始化 ...
- Google单元测试框架gtest--值参数测试
测试一个方法,需要较多个参数进行测试,比如最大值.最小值.异常值和正常值.这中间会有较多重复代码工作,而值参数测试就是避免这种重复性工作,并且不会损失测试的便利性和准确性. 如果测试一个函数,需要些各 ...
- C++单元测试框架gtest使用
作用 作为代码编码人员,写完代码,不仅要保证编译通过和运行,还要保证逻辑尽量正确.单元测试是对软件可测试最小单元的检查和校验.单元测试与其他测试不同,单元测试可看作是编码工作的一部分,应该由程序员完成 ...
- 简单易懂的单元测试框架-gtest(一)
简介 gtest是google开源的一个单元测试框架,以其简单易学的特点被广泛使用.该框架以第三方库的方式插入被测代码中.同其他单元测试框架相似,gtest也通过制作测试样例来进行代码测试.同 ...
- 简单易懂的单元测试框架-gtest(二)
简介 事件机制用于在案例运行前后添加一些操作(相当于挂钩函数).目前,gtest提供了三种等级的事件,分别: 全局级,所有案例执行的前后 TestSuite级,某一个案例集的前后 TestCa ...
- Google C++单元测试框架---Gtest框架简介(译文)
一.设置一个新的测试项目 在用google test写测试项目之前,需要先编译gtest到library库并将测试与其链接.我们为一些流行的构建系统提供了构建文件: msvc/ for Visual ...
- 【Linux_Shell 脚本编程学习笔记四、监控系统内存并报警企业案例脚本】
前置知识:awk 参考学习博客:https://www.cnblogs.com/bugingcode/p/8287914.html awk 'BEGIN{ commands } pattern{ co ...
随机推荐
- hdu1217 Arbitrage
Problem Description Arbitrage is the use of discrepancies in currency exchange rates to transform on ...
- CF1465-D. Grime Zoo
CF1465-D. Grime Zoo 题意: 一个长度为n,由\(0,1,?\)这三个字符构成的字符串,字符串中\(01\)子串贡献\(x\)值,\(10\)的子串贡献\(y\)值,现在让你把\(? ...
- [Golang]-8 工作池、速率限制、原子计数器、互斥锁
目录 工作池 速率限制 原子计数器 互斥锁 工作池 在这个例子中,我们将看到如何使用 Go 协程和通道实现一个工作池 . func worker(id int, jobs <-chan int, ...
- codefoeces 864B
B. Polycarp and Letters time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- redis跳表
redis使用跳表作为有序集合的底层实现之一,下面来看下跳表的结构 一.跳表的结构
- HDU4565-数学推导求递推公式+矩阵快速幂
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4565 我们带着这个根号是没法计算的 我们仔细观察一下,(a+sqrt(b))^n用二项式定理展开,我 ...
- 买车交税 All In One
中国买车交税 All In One 消费税 增值税 车辆购置税 车船税 关税 refs xgqfrms 2012-2020 www.cnblogs.com 发布文章使用:只允许注册用户才可以访问! 原 ...
- git & github & git clone & 'git clone' failed with status 128
git & github & git clone & 'git clone' failed with status 128 'git clone' failed with st ...
- css & background & svg
css & background & svg https://developer.mozilla.org/en-US/docs/Web/CSS/background backgroun ...
- Taro API
Taro API Taro 的 API 包括 Taro 内置提供的 API 以及对小程序的端能力 API 的封装. https://taro-docs.jd.com/taro/docs/apis/ab ...