带有 unsigned char 的 BOOST_CHECK_EQUAL_COLLECTIONS 在不匹配时输出不可打印的字符
有没有办法更改 BOOST_CHECK_EQUAL_COLLECTIONS
的 unsigned 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 char
s print as numbers instead of ASCII?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
令我惊讶的是,您可以通过在测试文件内的
std
命名空间中为unsigned char
定义运算符<<来实现此目的案例,test_foo.cpp
):这给出:
Much to my surprise, you can accomplish this by defining
operator<<
forunsigned char
in thestd
namespace inside your test file (in my case,test_foo.cpp
):This gives: