我们正在使用Boost :: Test对我们的代码进行单元测试(Visual Studio 2010,win32 C ++项目)。由于我们经常使用Ogre::Vector3
个对象(3D空间中的几何向量),我已经编写了这个包装器来轻松测试两个Vector3对象的几乎相等:
static const double EPSILON = 1e-7;
void CHECK_CLOSE(const Ogre::Vector3 & actual, const Ogre::Vector3 & expected, Ogre::Real tolerance) {
if (std::fabs(expected.x) < EPSILON) BOOST_CHECK_SMALL(actual.x, tolerance);
else BOOST_CHECK_CLOSE(actual.x, expected.x, tolerance);
if (std::fabs(expected.y) < EPSILON) BOOST_CHECK_SMALL(actual.y, tolerance);
else BOOST_CHECK_CLOSE(actual.y, expected.y, tolerance);
if (std::fabs(expected.z) < EPSILON) BOOST_CHECK_SMALL(actual.z, tolerance);
else BOOST_CHECK_CLOSE(actual.z, expected.z, tolerance);
}
不幸的是,当其中一个BOOST_CHECK_XXX
调用失败时,它会报告包装函数中的位置(它位于rage_test.cpp中):
../../../src/tests/boost_test/rage_test.cpp(20): error in
"km_bodyposemodifier_combo": difference{0.646806%} between
actual.y{3.01940417} and expected.y{3} exceeds 0.100000001%
我宁愿看到它展开另一个堆栈帧,并报告CHECK_CLOSE
调用的位置。我该怎么做?
答案 0 :(得分:1)
你可以将CHECK_CLOSE
变成一个宏(我知道......宏是邪恶的,但作为调试函数的包装器,自动传递__FILE__
,__LINE__
...
#if defined(DEBUG)
#define CHECK_CLOSE(actual, expected, tolerance) { \
if (std::fabs(expected.x) < EPSILON) BOOST_CHECK_SMALL(actual.x, tolerance); \
else BOOST_CHECK_CLOSE(actual.x, expected.x, tolerance); \
if (std::fabs(expected.y) < EPSILON) BOOST_CHECK_SMALL(actual.y, tolerance); \
else BOOST_CHECK_CLOSE(actual.y, expected.y, tolerance); \
if (std::fabs(expected.z) < EPSILON) BOOST_CHECK_SMALL(actual.z, tolerance); \
else BOOST_CHECK_CLOSE(actual.z, expected.z, tolerance); \
}
#else
#define CHECK_CLOSE(actual, expected, tolerance)
#endif