使用Google测试时,如何打印GLM数学库对象值
当断言失败时,我正在尝试使用Googletest和漂亮的打印来测试一些数学操作。我一直在关注有关漂亮打印的指南,我找到了一个名为printto
的函数。在命名空间中为自定义结构的此功能超载工作:
namespace my_ns {
struct A {
A(uint32_t a_, uint32_t b_) : a(a_), b(b_) {}
uint32_t a = 0, b = 0;
bool operator==(const A &rhs) const { return a == rhs.a && b == rhs.b; }
};
void PrintTo(const test::A &value, std::ostream *out) {
*out << value.a << " - " << value.b;
}
}
TEST(PrettyTest, Test) { EXPECT_EQ(my_ns::A(10, 20), my_ns::A(20, 25)); }
这起作用,我会得到人类可读的期望失败消息:
error: Expected equality of these values:
my_ns::A(10, 20)
Which is: 10 - 20
my_ns::A(20, 25)
Which is: 20 - 25
但是,当我尝试为GLM数学库做同样的操作时,我会继续获得默认字符串,这是一个二进制值:
namespace glm {
void PrintTo(const vec3 &value, std::ostream *out) {
*out << to_string(value);
}
} // namespace glm
TEST_F(PrettyTest, GlmTest) { EXPECT_EQ(glm::vec3(1.0f), glm::vec3(2.0f)); }
以下测试打印:
error: Expected equality of these values:
glm::vec3(1.0f)
Which is: 12-byte object <00-00 80-3F 00-00 80-3F 00-00 80-3F>
glm::vec3(2.0f)
Which is: 12-byte object <00-00 00-40 00-00 00-40 00-00 00-40>
此问题仅存在于GLM中,我无法弄清楚原因是什么原因。我尝试使用不同的名称空间,但无法使其工作。
I am trying to test some math operations with GLM using GoogleTest and pretty print when the assertions fail. I have been following the guides regarding the pretty printing and I found a function named PrintTo
. Overloading this function for a custom structure in a namespace works:
namespace my_ns {
struct A {
A(uint32_t a_, uint32_t b_) : a(a_), b(b_) {}
uint32_t a = 0, b = 0;
bool operator==(const A &rhs) const { return a == rhs.a && b == rhs.b; }
};
void PrintTo(const test::A &value, std::ostream *out) {
*out << value.a << " - " << value.b;
}
}
TEST(PrettyTest, Test) { EXPECT_EQ(my_ns::A(10, 20), my_ns::A(20, 25)); }
This works and I get human readable expect failure messages:
error: Expected equality of these values:
my_ns::A(10, 20)
Which is: 10 - 20
my_ns::A(20, 25)
Which is: 20 - 25
However, when I am trying to do the same for GLM math library, I keep getting the default string, which is a binary value:
namespace glm {
void PrintTo(const vec3 &value, std::ostream *out) {
*out << to_string(value);
}
} // namespace glm
TEST_F(PrettyTest, GlmTest) { EXPECT_EQ(glm::vec3(1.0f), glm::vec3(2.0f)); }
The following test prints:
error: Expected equality of these values:
glm::vec3(1.0f)
Which is: 12-byte object <00-00 80-3F 00-00 80-3F 00-00 80-3F>
glm::vec3(2.0f)
Which is: 12-byte object <00-00 00-40 00-00 00-40 00-00 00-40>
This issue only exists in GLM and I cannot figure out what causes this issue. I tried to use different namespaces but I could not get it to working.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如您从GLM源代码在这里,
vec3
不是一种类型,而是命名空间中的typedefglm
to实际类型,对于某些版本,库中的某些版本位于相同的glm 命名空间(请参阅在这里但是对于其他(旧) - 在
glm ::详细信息
namespace(请参阅在这里)。请注意,ADL不会在关联的名称空间中添加typedefs的命名空间,只有实际类型定义的名称空间,这个问题。因此,由于在您的情况下,ADL无助于
printto
glm
名称空间,因此,对于您的GLM版本进入glm ::详细信息
类似的名称空间:As you can see from glm source code here,
vec3
is not a type, but a typedef in namespaceglm
to the actual type, which for some versions of the library resides in the sameglm
namespace (see here), but for other (older) - inglm::detail
namespace (see here). Note that ADL doesn't add namespaces of typedefs to associated namespaces, only namespaces of actual type definitions, which is discussed inthis question. So, since in your case ADL doesn't help to find
PrintTo
function inglm
namespace, for your version of glm it most probably will do its job if you put function definition intoglm::detail
namespace like this:问题是我有很多测试文件,但是我要应用的自定义仅在一个文件中。我通过删除所有测试并仅保留一项测试来调试此问题。当我这样做时,一切都起作用了。当我添加一个新的测试文件作为编译目标时,打印停止工作。我通过创建一个自定义标头文件并添加该文件中的所有逻辑,并在我的测试中仅包含该文件来解决此问题:
The issue was that I had many test files but the customization that I was applying was only in one file. I debugged this by removing all the tests and keeping only one. As I did that, everything worked. When I added a new test file as compile target, the printing stopped working. I solved this issue by creating a custom header file and adding encapsulating all the logic in that file and including only that file in my tests: