我需要为 UTF-8 验证编写单元测试,但我不知道如何在 C++ 中编写不正确的 UTF-8 情况:
TEST(validation, Tests)
{
std::string str = "hello";
EXPECT_TRUE(validate_utf8(str));
// I need incorrect UTF-8 cases
}
如何在 C++ 中编写不正确的 UTF-8 大小写?
最佳答案
您可以使用十六进制形式的 \x
转义序列或八进制形式的 \000
转义序列指定字符串中的各个字节。
例如:
std::string str = "\xD0";
这是不完整的 UTF8。
看看https://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt适用于有效和格式错误的 UTF8 测试用例。
关于用于验证 UTF-8 的 C++ 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70926826/