使用Google测试时,如何打印GLM数学库对象值

发布于 2025-02-08 05:02:37 字数 1392 浏览 1 评论 0原文

当断言失败时,我正在尝试使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只有影子陪我不离不弃 2025-02-15 05:02:37

如您从GLM源代码在这里vec3不是一种类型,而是命名空间中的typedef glm to实际类型,对于某些版本,库中的某些版本位于相同的glm 命名空间(请参阅在这里但是对于其他(旧) - 在glm ::详细信息 namespace(请参阅在这里)。请注意,ADL不会在关联的名称空间中添加typedefs的命名空间,只有实际类型定义的名称空间,
这个问题。因此,由于在您的情况下,ADL无助于printto
glm名称空间,因此,对于您的GLM版本进入glm ::详细信息类似的名称空间:

namespace glm::detail {
    void PrintTo(const ::glm::vec3 &value, std::ostream *out) {
        *out << ::glm::to_string(value);
    }
} // namespace glm::detail

As you can see from glm source code here, vec3 is not a type, but a typedef in namespace glm to the actual type, which for some versions of the library resides in the same glm namespace (see here), but for other (older) - in glm::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 in
this question. So, since in your case ADL doesn't help to find PrintTo function in glm namespace, for your version of glm it most probably will do its job if you put function definition into glm::detail namespace like this:

namespace glm::detail {
    void PrintTo(const ::glm::vec3 &value, std::ostream *out) {
        *out << ::glm::to_string(value);
    }
} // namespace glm::detail
尐偏执 2025-02-15 05:02:37

问题是我有很多测试文件,但是我要应用的自定义仅在一个文件中。我通过删除所有测试并仅保留一项测试来调试此问题。当我这样做时,一切都起作用了。当我添加一个新的测试文件作为编译目标时,打印停止工作。我通过创建一个自定义标头文件并添加该文件中的所有逻辑,并在我的测试中仅包含该文件来解决此问题:

// Testing.h
#pragma once

namespace glm {

// Covers all vectors
template <length_t L, typename T, qualifier Q>
void PrintTo(const vec<L, T, Q> &value, std::ostream *out) {
  *out << to_string(value);
}

// Covers all matrices
template <length_t C, length_t R, typename T, qualifier Q>
void PrintTo(const mat<C, R, T, Q> &value, std::ostream *out) {
  *out << to_string(value);
}

// Covers all quaternions
template <typename T, qualifier Q>
void PrintTo(const qua<T, Q> &value, std::ostream *out) {
  *out << to_string(value);
}

} // namespace glm

#include <gtest/gtest.h>
#include <gmock/gmock.h>

// ------
// In all my test files

// other includes...
#include "Testing.h"

// test code here 

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:

// Testing.h
#pragma once

namespace glm {

// Covers all vectors
template <length_t L, typename T, qualifier Q>
void PrintTo(const vec<L, T, Q> &value, std::ostream *out) {
  *out << to_string(value);
}

// Covers all matrices
template <length_t C, length_t R, typename T, qualifier Q>
void PrintTo(const mat<C, R, T, Q> &value, std::ostream *out) {
  *out << to_string(value);
}

// Covers all quaternions
template <typename T, qualifier Q>
void PrintTo(const qua<T, Q> &value, std::ostream *out) {
  *out << to_string(value);
}

} // namespace glm

#include <gtest/gtest.h>
#include <gmock/gmock.h>

// ------
// In all my test files

// other includes...
#include "Testing.h"

// test code here 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文