googletest:sample8分析

news/2025/3/1 19:42:45/文章来源:https://www.cnblogs.com/fortunely/p/18744840

目录
  • 待测文件
  • 测试文件

待测文件

同sample6,都是prime_tables.h文件

PrimeTable类声明了素数表的一系列接口,包括
1)IsPrime:判断当前输入数n是否为素数;
2)GetNextPrime:找比输入数n大的下一个素数.

文件内容略.

测试文件

如果我们想引进一种新的、改进的PrimeTable实现,结合了PrecalcPrimeTable的速度和OnTheFlyPrimeTable的多功能特性,用于判断一个数是否素数、获取下一个素数. 在内部,实例化了PrecalcPrimeTable, OnTheFlyPrimeTable,在特定情况下只使用更适合的一个. 但在内存不足时,可以在不实例化PreCalculatedPrimeTable的情况下实例化HybridPrimeTable,并且只使用OnTheFlyPrimeTable.

sample8_unittest.cc

// Suppose we want to introduce a new, improved implementation of PrimeTable
// which combines speed of PrecalcPrimeTable and versatility of
// OnTheFlyPrimeTable (see prime_tables.h). Inside it instantiates both
// PrecalcPrimeTable and OnTheFlyPrimeTable and uses the one that is more
// appropriate under the circumstances. But in low memory conditions, it can be
// told to instantiate without PrecalcPrimeTable instance at all and use only
// OnTheFlyPrimeTable.
class HybridPrimeTable : public PrimeTable {public:HybridPrimeTable(bool force_on_the_fly, int max_precalculated): on_the_fly_impl_(new OnTheFlyPrimeTable),precalc_impl_(force_on_the_fly? nullptr: new PreCalculatedPrimeTable(max_precalculated)),max_precalculated_(max_precalculated) {}~HybridPrimeTable() override {delete on_the_fly_impl_;delete precalc_impl_;}bool IsPrime(int n) const override {if (precalc_impl_ != nullptr && n < max_precalculated_)return precalc_impl_->IsPrime(n);elsereturn on_the_fly_impl_->IsPrime(n);}int GetNextPrime(int p) const override {int next_prime = -1;if (precalc_impl_ != nullptr && p < max_precalculated_)next_prime = precalc_impl_->GetNextPrime(p);return next_prime != -1 ? next_prime : on_the_fly_impl_->GetNextPrime(p);}private:OnTheFlyPrimeTable* on_the_fly_impl_;PreCalculatedPrimeTable* precalc_impl_;int max_precalculated_;
};using ::testing::Bool;
using ::testing::Combine;
using ::testing::TestWithParam;
using ::testing::Values;// To test all code paths for HybridPrimeTable we must test it with numbers
// both within and outside PreCalculatedPrimeTable's capacity and also with
// PreCalculatedPrimeTable disabled. We do this by defining fixture which will
// accept different combinations of parameters for instantiating a
// HybridPrimeTable instance.
class PrimeTableTest : public TestWithParam< ::std::tuple<bool, int> > {protected:void SetUp() override {bool force_on_the_fly;int max_precalculated;std::tie(force_on_the_fly, max_precalculated) = GetParam();table_ = new HybridPrimeTable(force_on_the_fly, max_precalculated);}void TearDown() override {delete table_;table_ = nullptr;}HybridPrimeTable* table_;
};

通过testing::TestWithParam<>的模板参数,将是否需要强制使用OnTheFlyPrimeTable (bool)和PreCalculatedPrimeTable (int)表中最大数 传递给PrimeTableTest,进而决定如何实例化HybridPrimeTable.

如何设计test?

跟普通的TEST类似依然可以用HybridPrimeTable提供接口进行测试.
当然,还能在test body中,通过GetParam()获取由INSTANTIATE_TEST_SUITE_P实例化的test parameter.

TEST_P(PrimeTableTest, ReturnsFalseForNonPrimes) {// Inside the test body, you can refer to the test parameter by GetParam().// In this case, the test parameter is a PrimeTable interface pointer which// we can use directly.// Please note that you can also save it in the fixture's SetUp() method// or constructor and use saved copy in the tests.EXPECT_FALSE(table_->IsPrime(-5));EXPECT_FALSE(table_->IsPrime(0));EXPECT_FALSE(table_->IsPrime(1));EXPECT_FALSE(table_->IsPrime(4));EXPECT_FALSE(table_->IsPrime(6));EXPECT_FALSE(table_->IsPrime(100));
}TEST_P(PrimeTableTest, ReturnsTrueForPrimes) {EXPECT_TRUE(table_->IsPrime(2));EXPECT_TRUE(table_->IsPrime(3));EXPECT_TRUE(table_->IsPrime(5));EXPECT_TRUE(table_->IsPrime(7));EXPECT_TRUE(table_->IsPrime(11));EXPECT_TRUE(table_->IsPrime(131));
}TEST_P(PrimeTableTest, CanGetNextPrime) {EXPECT_EQ(2, table_->GetNextPrime(0));EXPECT_EQ(3, table_->GetNextPrime(2));EXPECT_EQ(5, table_->GetNextPrime(3));EXPECT_EQ(7, table_->GetNextPrime(5));EXPECT_EQ(11, table_->GetNextPrime(7));EXPECT_EQ(131, table_->GetNextPrime(128));
}

设计完test,还需要用宏INSTANTIATE_TEST_SUITE_P来实例化"value-parameterized tests",GoogleTest才会针对每个实例进行test.

// In order to run value-parameterized tests, you need to instantiate them,
// or bind them to a list of values which will be used as test parameters.
// You can instantiate them in a different translation module, or even
// instantiate them several times.
//
// Here, we instantiate our tests with a list of parameters. We must combine
// all variations of the boolean flag suppressing PrecalcPrimeTable and some
// meaningful values for tests. We choose a small value (1), and a value that
// will put some of the tested numbers beyond the capability of the
// PrecalcPrimeTable instance and some inside it (10). Combine will produce all
// possible combinations.
INSTANTIATE_TEST_SUITE_P(MeaningfulTestParameters, PrimeTableTest,Combine(Bool(), Values(1, 10)));

注意:这里INSTANTIATE_TEST_SUITE_P第三个参数Combine(Bool(), Values(1, 10))对应的是TestWithParam< ::std::tuple<bool, int> >的模板参数,而sample7对应传的参数是Values(&CreateOnTheFlyPrimeTable, &CreatePreCalculatedPrimeTable<1000>),即工厂方法的函数地址. 而我们用GetParam()获取的,就是这里的第三个参数.

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

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

相关文章

Java学习——数组

数组的定义和声明1.声明数组 int[] array =null;这一步在栈里面压入了一个array;声明的时候数组还不存在 2.创建数组 array=new int[10] ;这一步在堆里面开辟了一个内存 3.给数组元素赋值 for (int i = 0; i < nums.length; i++) { nums[i] = i + 1; System.out.println(nu…

pyenv下载慢的解决方式

pyenv install 不知道为什么会卡住 解决方法就是手动直接下载对应的版本 放到对应的目录 例如这里显示的To C:\Users\28453\.pyenv\pyenv-win\install_cache\python-3.12.9-amd64.exe 我们只需要到 python 官网去下载对应的版本 并放到指定目录 我用的是 pyenv-win 需要下载 …

MongoDB聚合查询

MongoDB聚合查询 创建测试数据 db.student.drop() db.student.insertOne({_id: 1,name: "zhangsan",age: 12,teacher: ["Tom","Jack"]}) db.student.insertOne({_id: 2,name: "lisi",age: 15,teacher: ["Lucy","Tom&qu…

htmx怎么样,光速弃坑

前言 htmx可以说是对html标签的加强,提供了一些额外的标签属性,来获得一些功能,比如ajax局部渲染之类的。 快速自定义事件等等…… 使用? <script src="https://cdn.bootcdn.net/ajax/libs/htmx/2.0.4/htmx.min.js"></script>我想首先使用一下获取&l…

2024春季NOI省选游记

前言 初二,同步赛选手,Day 1 还行,Day 2 拉跨。 Day 1 (时刻有些许偏差) \(8:25\) 公布密码了,居然没有 PDF 密码。 \(8:26\) 开 T1,感觉余数必须要枚举,剩下的可能需要 \(\log\) 找解。 \(8:35\) 开始写二分,没过大样例。 \(8:57\) 调了半天,发现二分是假的。 \(9:13\…

3.1 多元函数的全微分

1 多元函数的全微分 1.1 定义 1.1.1 回顾一元函数的微分 1.1.2 多元函数的定义1.1.3 可微推导连续 但连续不一定可微,证明如下: 若连续一定可微,又一元函数必满足二元函数的性质,又一元函数可微必可导,可推至连续必可导,矛盾 1.2 多元函数可微的必要条件 1.2.1 可微必可导…

React—10—受控组件和非受控组件;高阶组件

一、概念 我的理解是,是否有react提供数据,分为受控组件和非受控组件。 比如input元素,只要绑定了value属性,那么在react中,用户在输入框输入的值不会显示在输入框(react应该做了限制,原生html的input框即使value绑定了值依然可以输入), 这就导致,想改变value的值,必须…

autojs通过vscode写好的文件上传的手机,并打包apk

1.安装vscode 2.下载autojs插件和chinese插件3.启动autojs插件,手机才可以链接 (链接时输入的是手机的ip地址)4.上传到手机5.打包本文来自博客园,作者:六月OvO,转载请注明原文链接:https://www.cnblogs.com/chenlifan/p/18745272

Windows 提权-UAC 绕过

本文通过 Google 翻译 UAC-Bypass – Windows Privilege Escalation 这篇文章所产生,本人仅是对机器翻译中部分表达别扭的字词进行了校正及个别注释补充。导航0 前言 1 场景一:枚举得到存储的管理员凭证(GUI 环境)1.1 使用 netplwiz.exe 帮助主题绕过 UAC2 场景二:枚举得到…

三剑客与正则系列-正则表达式

1.注意事项正则符号都是英文符号,避免使用中文符号 推荐使用grep/egrep命令,默认设置了别名,自动加上颜色 http://nbre.oldboylinux.cn/ 分析正则与正则匹配到的内容. 其他坑.# "" . #‘’ ”“ 。alias grep=grep color=auto alias egrep=egrep color=auto2.符号概述…

【凸优化笔记2】-凸函数、下水平集、范数

转自:https://zhuanlan.zhihu.com/p/102098039 1. 凸函数 1.1 凸函数定义 一个函数𝑓:𝑅𝑛→𝑅是凸的,如果定义域𝑑𝑜𝑚𝑓是凸集,并且对于所有𝑥,𝑦∈𝑑𝑜𝑚𝑓,𝜃∈[0,1],都有: 𝑓(𝜃𝑥+(1−𝜃)𝑦)≤𝜃𝑓(𝑥)+(1−𝜃)𝑓…

使用AI后为什么思考会变得困难?

使用AI后为什么思考会变得困难? 我总结了四篇近期的研究论文,来展示AI是如何以及为什么侵蚀我们的批判性思维能力。作者使用AI制作的图像前言:作者在这篇文章中,借AI技术的崛起,揭示了一场悄然发生的思想博弈。表面上,AI为我们带来了前所未有的效率与便捷,但在无形之中,…