1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- #include <gtest/gtest.h>
- #include "toolkit.h"
- TEST(TestCharUtil, TestCharSize)
- {
- ASSERT_TRUE(sizeof(char) == 1);
- ASSERT_TRUE(sizeof(wchar_t) == 4);
- }
- TEST(TestCharUtil, TestWideChar2Char)
- {
- wchar_t hello[127] = L"Hello World";
- char world[128] = { 0 };
- size_t ret = toolkit_wcs2mbs(hello, world, 128);
- std::cout << "ret len: " << ret << " string:" << world << std::endl;
- ASSERT_TRUE(ret == 11);
- }
- TEST(TestCharUtil, TestChar2WideChar)
- {
- char hello[127] = "Hello World";
- wchar_t world[128] = { 0 };
- size_t ret = toolkit_mbs2wcs(hello, world, 128);
- std::wcout << "ret len: " << ret << " string:" << world << std::endl;
- ASSERT_TRUE(ret == 11);
- }
- TEST(TestCharUtil, TestCapacity)
- {
- char hello[127] = "Hello World";
- size_t ret = toolkit_mbs2wcs(hello, NULL, 0);
- ASSERT_TRUE(ret == 12);
- wchar_t hello2[127] = L"Hello World";
- ret = toolkit_wcs2mbs(hello2, NULL, 0);
- ASSERT_TRUE(ret == 12);
- }
- TEST(TestCharUtil, ExceptionTest)
- {
- char hello[127] = "";
- size_t ret = toolkit_mbs2wcs(hello, NULL, 0);
- ASSERT_TRUE(ret == 0);
- wchar_t hello2[127] = L"";
- ret = toolkit_wcs2mbs(hello2, NULL, 0);
- ASSERT_TRUE(ret == 0);
- }
- TEST(TestCharUtil, EmptyTest)
- {
- wchar_t* wmsg = NULL;
- char* msg = NULL;
- wmsg = (wchar_t*)malloc(sizeof(wchar_t));
- wmsg[0] = L'\0';
- ASSERT_TRUE(wmsg != NULL);
- int n = toolkit_wcs2mbs(wmsg, NULL, 0);
- const int rn = n > 0 ? n : 1;
- ASSERT_TRUE(rn == 1);
- msg = (char*)malloc(rn);
- memset(msg, '\0', sizeof(char) * rn);
- toolkit_wcs2mbs(wmsg, msg, n);
- ASSERT_TRUE(strlen(msg) == 0);
- FREE(wmsg);
- ASSERT_TRUE(msg != NULL);
- FREE(msg);
- }
|