diff --git a/test/src/test_Encoding.cc b/test/src/test_Encoding.cc index acb79cc..d6deb06 100644 --- a/test/src/test_Encoding.cc +++ b/test/src/test_Encoding.cc @@ -67,3 +67,33 @@ TEST(Encoding_decode, decodes_CP_1252_correctly) { EXPECT_EQ(0x99u, Encoding::decode(Encoding::CP_1252, (const uint8_t *)"\x99")); } + + +TEST(Encoding_encode, encodes_UTF_8_correctly) +{ + struct + { + uint32_t code_point; + const char * expected; + } tests[] = { + {(uint32_t)'%', "%"}, + {0x567u, "\xD5\xA7"}, + {0x9876u, "\xE9\xA1\xB6"}, + {0x12345u, "\xF0\x92\x8D\x85"}, + {0x1234567u, "\xF9\x88\xB4\x95\xA7"}, + {0x12345678u, "\xFC\x92\x8D\x85\x99\xB8"}, + }; + for (unsigned int i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) + { + char buffer[10] = {0}; + EXPECT_EQ(strlen(tests[i].expected), Encoding::encode(tests[i].code_point, Encoding::UTF_8, (uint8_t *)buffer)); + EXPECT_EQ(std::string(tests[i].expected), buffer); + } +} + +TEST(Encoding_encode, encodes_CP_1252_correctly) +{ + char buffer[10] = {0}; + EXPECT_EQ(1, Encoding::encode(0x89u, Encoding::CP_1252, (uint8_t *)buffer)); + EXPECT_EQ(std::string("\x89"), buffer); +}