gpt4 book ai didi

c++ - 使用google test和google mock比较 float 组

转载 作者:太空狗 更新时间:2023-10-29 20:58:00 24 4
gpt4 key购买 nike

我是 Google 测试产品的新手,正在试用一些信号处理代码。我试图断言 float 组在一定范围内是相等的,使用 google mock,正如 this question 的答案所建议的那样.我想知道为如下表达式添加一些容错的推荐方法。 . .

EXPECT_THAT(  impulse, testing::ElementsAreArray( std::vector<float>({
0, 0, 0, 1, 1, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
}) ) );

如果数组中的元素值在彼此的 10-8 范围内,我希望测试通过。

最佳答案

这是一种方法。首先在测试范围之外定义一个匹配器。根据文档,匹配器不能在类或函数中定义。 .

MATCHER_P(FloatNearPointwise, tol, "Out of range") {
return (std::get<0>(arg)>std::get<1>(arg)-tol && std::get<0>(arg)<std::get<1>(arg)+tol) ;
}

然后可以与 Pointwise 一起使用 int 测试。 . .

std::vector<float> expected_array({
0, 0, 0, 1, 1, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
});

EXPECT_THAT( impulse, Pointwise( FloatNearPointwise(1e-8), expected_array ) );

但如果有直接使用内置 FloatNear 的解决方案会更整洁。

关于c++ - 使用google test和google mock比较 float 组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28768359/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com