gpt4 book ai didi

c++ - 通过一些 GTest TEST_F 的 CLion 执行挂起,逐步调试倒退?

转载 作者:太空宇宙 更新时间:2023-11-03 22:52:10 25 4
gpt4 key购买 nike

我正在调试我在 CLion 中使用 C++ 编写的应用程序,并使用 Google Test 对其进行测试。这是我发现的计算机视觉论文的实现,我将 OpenCV 用于其 Mat 类和图像处理函数。

我的三个测试函数的行为非常不稳定;这是一个。

/**
* Create a flat point cloud and flip it horizontally
*/
TEST_F(PointCloudTest, FlipHorizontal) {
// load image & set constants
cv::Mat image = omar::imreadAsGrayDouble(dirname + "uttower.png");
cv::Mat flippedImage = omar::imreadAsGrayDouble(dirname + "uttower_horz.png");
double fLength = 200.0; // for this, the flength is arbitrary as long as it's the same.

// build point cloud
omar::PointCloud pc(image, fLength);

/* Build the other camera pose
* Specifically, rotated pi over Y
* and 2 focal lengths out into Z+ space.
* It's written as 2.001 to test whether small variations affect point placement*/
omar::Pose otherCamera(0.0, 0.0, 2.001, 0.0, M_PI, 0.0);

// flip horizontally
omar::PointCloud newPc = pc.viewPoints(otherCamera);
cv::Mat newImage = newPc.renderToImage(fLength, image.rows, image.cols);
omar::assertMatEquals(flippedImage, newImage, 10 * DBL_EPSILON);
}

GTest 调试系统将提供完成时的输出:

[确定] PointCloudTest.FlipHorizo​​ntal(205 毫秒)

但图标从未更改为表明测试已完成的信号。

image showing OK and ! but stalled wheels for three tests

当我去调试它时,如果我在函数底部设置一个断点,它就会停止,然后每次我按下“Step Over”时,它都会向上 剩下的几行,一行一行,然后写完。结果证明这是所有测试的调试行为。

挂起测试与通过的不同测试仅略有不同。具体来说,该行是 viewPoints 行。相关代码如下:

PointCloud::PointCloud(const PointCloud& other) {
pointsAndIntensities = other.pointsAndIntensities.clone();
}
PointCloud PointCloud::viewPoints(omar::Pose otherCameraPose) {
PointCloud otherPointCloud = PointCloud(*this);
otherPointCloud.transform(otherCameraPose);
return otherPointCloud;
}

void PointCloud::transform(omar::Pose otherCameraPose) {
cv::Mat transformedPoints = otherCameraPose.world2camera(pointsAndIntensities(cv::Range(0, 3), cv::Range::all()));
cv::Mat replaceablePoints(pointsAndIntensities, cv::Range(0, 3));
transformedPoints.copyTo(replaceablePoints);
}

我想了解此排序的原因,但它可能与真正的问题(挂起测试)相关,也可能不相关。从技术上讲,所有三个测试都通过了,但我认为这个问题会在未来造成很多问题。我不知道该怎么做。感谢帮助和跟进问题!

最佳答案

TL;DR:确保以新行结束打印语句。

在描述问题时,我遗漏了一个非常非常重要的细节。我决定删掉我用来从我的描述中调试一些信息的 cout 行。原代码为:

void PointCloud::transform(omar::Pose otherCameraPose) {
//cv::Mat oldPoints(pointsAndIntensities, cv::Range(0, 3));

cv::Mat transformedPoints = otherCameraPose.world2camera(pointsAndIntensities(cv::Range(0, 3), cv::Range::all()));
cv::Mat replaceablePoints(pointsAndIntensities, cv::Range(0, 3));
std::cout << "Transformed: " << transformedPoints.col(3) << std::endl;
std::cout << "Original: " << replaceablePoints.col(3) << std::endl;
transformedPoints.copyTo(replaceablePoints);
std::cout << "Replaced?: " << pointsAndIntensities.col(3);
}

我决定深入研究代码以找到导致挂起的确切行,以防出现内存错误。我追踪到上面的 transform 方法,结果是我在那里的 std::cout 行。我不知道在测试中使用流是否会导致一些问题。事实证明,这是因为我没有以 std::endl 结束我的最后一个 print 语句。

我的直觉是,CLion 依赖于 GTest 的 cout 输出来确定测试是通过还是失败,并且因为 [ OK ] PointCloudTest.FlipHorizo​​ntal (205 ms) 不在新行上,它没有识别出测试已成功完成。

关于c++ - 通过一些 GTest TEST_F 的 CLion 执行挂起,逐步调试倒退?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36667584/

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