awtk/tests/utf8_test.cc

49 lines
1.1 KiB
C++
Raw Normal View History

#include "tkc/utf8.h"
2018-02-21 19:36:38 +08:00
#include "gtest/gtest.h"
TEST(Utf8, ascii) {
const char* str = "hello";
const wchar_t* wstr = L"hello";
char res_str[128];
wchar_t res_wstr[128];
ASSERT_EQ(wcscmp(tk_utf8_to_utf16(str, res_wstr, ARRAY_SIZE(res_wstr)), wstr), 0);
ASSERT_EQ(strcmp(tk_utf8_from_utf16(wstr, res_str, ARRAY_SIZE(res_str)), str), 0);
2018-02-21 19:36:38 +08:00
}
2020-04-04 06:38:43 +08:00
static void dump_utf8(const char* str) {
const char* p = str;
log_debug("dump_utf8:%s\n", str);
2020-04-05 09:18:33 +08:00
while (*p) {
2020-04-04 06:38:43 +08:00
log_debug("%02x ", ((int)(*p) & 0xff));
p++;
}
log_debug("\n");
}
static void dump_unicode(const wchar_t* str) {
const wchar_t* p = str;
log_debug("dump_unicode\n");
2020-04-05 09:18:33 +08:00
while (*p) {
2020-04-04 06:38:43 +08:00
log_debug("%04x ", (int)(*p));
p++;
}
log_debug("\n");
}
2018-02-21 19:36:38 +08:00
TEST(Utf8, chinese) {
2020-04-04 06:38:43 +08:00
const char* str = "中文";
const wchar_t* wstr = L"中文";
2018-02-21 19:36:38 +08:00
char res_str[128];
wchar_t res_wstr[128];
2020-04-04 06:38:43 +08:00
dump_utf8(str);
dump_unicode(wstr);
ASSERT_EQ(wcscmp(tk_utf8_to_utf16(str, res_wstr, ARRAY_SIZE(res_wstr)), wstr), 0);
ASSERT_EQ(strcmp(tk_utf8_from_utf16(wstr, res_str, ARRAY_SIZE(res_str)), str), 0);
2020-04-04 06:38:43 +08:00
dump_utf8(res_str);
dump_unicode(res_wstr);
2018-02-21 19:36:38 +08:00
}