带有 unsigned char 的 BOOST_CHECK_EQUAL_COLLECTIONS 在不匹配时输出不可打印的字符

发布于 2025-01-05 21:17:17 字数 1445 浏览 2 评论 0原文

有没有办法更改 BOOST_CHECK_EQUAL_COLLECTIONSunsigned char 输出格式?

我正在使用 Boost.Test 1.37.0 验证无符号字符数组中的值:

//  result.Message is a fixed-size unsigned char array
//  result.Length is the length of the data inside result.Message

const unsigned expected_message[] = { 3, 60, 43, 17 };

BOOST_CHECK_EQUAL_COLLECTIONS(
    result.Message,
    result.Message + result.Length,
    expected_message,
    expected_message + sizeof(expected_message) / sizeof(*expected_message) );

并且在不匹配时得到无法打印的字符:

test_foo.cpp(117): error in "test_bar": check { result.Message, result.Message + result.Length } == { expected_message, expected_message + sizeof(expected_message) /  sizeof(*expected_message) } failed. 
Mismatch in a position 1:  != 60
Mismatch in a position 2: < != 43
Mismatch in a position 3:          != 17

我暂时将 expected_message 更改为 unsigned 数组,因此它打印数字而不是字符 - 类似地,我可以将 result.Message 复制到新的 vector 并与之比较:

vector<unsigned> result_message(result.Message, result.Message + result.Length);

这不是很糟糕,但如果可能的话我更愿意与原版进行比较。

在内部,BOOST_CHECK_EQUAL_COLLECTIONS 使用的是我无法访问的临时 stringstream,但这让我对 ostream 格式感到好奇。

我在处理方面和语言环境方面没有太多经验,但我想知道是否可以使用它们以某种方式将各个 unsigned char 打印为数字而不是 ASCII?

Is there any way to change the unsigned char output formatting of BOOST_CHECK_EQUAL_COLLECTIONS?

I'm using Boost.Test 1.37.0 to validate values in an unsigned char array:

//  result.Message is a fixed-size unsigned char array
//  result.Length is the length of the data inside result.Message

const unsigned expected_message[] = { 3, 60, 43, 17 };

BOOST_CHECK_EQUAL_COLLECTIONS(
    result.Message,
    result.Message + result.Length,
    expected_message,
    expected_message + sizeof(expected_message) / sizeof(*expected_message) );

and I get unprintable characters on mismatch:

test_foo.cpp(117): error in "test_bar": check { result.Message, result.Message + result.Length } == { expected_message, expected_message + sizeof(expected_message) /  sizeof(*expected_message) } failed. 
Mismatch in a position 1:  != 60
Mismatch in a position 2: < != 43
Mismatch in a position 3:          != 17

I temporarily changed expected_message to an unsigned array so it prints numbers rather than characters - similarly, I could copy result.Message to a new vector<unsigned> and compare against that:

vector<unsigned> result_message(result.Message, result.Message + result.Length);

which isn't terrible, but I'd prefer to compare against the original if possible.

Internally, BOOST_CHECK_EQUAL_COLLECTIONS is using a temporary stringstream that I can't access, but it got me wondering about ostream formatting.

I don't have a lot of experience dealing with facets and locales, but I'm wondering if I could use them somehow to make individual unsigned chars print as numbers instead of ASCII?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

倾其所爱 2025-01-12 21:17:17

令我惊讶的是,您可以通过在测试文件内的 std 命名空间中为 unsigned char 定义运算符<<来实现此目的案例,test_foo.cpp):

namespace std {

ostream &operator<<( ostream &os, const unsigned char &uc ) {
    return os << static_cast<unsigned>(uc);
}

}

这给出:

Mismatch in a position 0: 4 != 60
Mismatch in a position 1: 60 != 43
Mismatch in a position 2: 9 != 17

Much to my surprise, you can accomplish this by defining operator<< for unsigned char in the std namespace inside your test file (in my case, test_foo.cpp):

namespace std {

ostream &operator<<( ostream &os, const unsigned char &uc ) {
    return os << static_cast<unsigned>(uc);
}

}

This gives:

Mismatch in a position 0: 4 != 60
Mismatch in a position 1: 60 != 43
Mismatch in a position 2: 9 != 17
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文