test_w2char.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include <gtest/gtest.h>
  2. #include "toolkit.h"
  3. TEST(TestCharUtil, TestCharSize)
  4. {
  5. ASSERT_TRUE(sizeof(char) == 1);
  6. ASSERT_TRUE(sizeof(wchar_t) == 4);
  7. }
  8. TEST(TestCharUtil, TestWideChar2Char)
  9. {
  10. wchar_t hello[127] = L"Hello World";
  11. char world[128] = { 0 };
  12. size_t ret = toolkit_wcs2mbs(hello, world, 128);
  13. std::cout << "ret len: " << ret << " string:" << world << std::endl;
  14. ASSERT_TRUE(ret == 11);
  15. }
  16. TEST(TestCharUtil, TestChar2WideChar)
  17. {
  18. char hello[127] = "Hello World";
  19. wchar_t world[128] = { 0 };
  20. size_t ret = toolkit_mbs2wcs(hello, world, 128);
  21. std::wcout << "ret len: " << ret << " string:" << world << std::endl;
  22. ASSERT_TRUE(ret == 11);
  23. }
  24. TEST(TestCharUtil, TestCapacity)
  25. {
  26. char hello[127] = "Hello World";
  27. size_t ret = toolkit_mbs2wcs(hello, NULL, 0);
  28. ASSERT_TRUE(ret == 12);
  29. wchar_t hello2[127] = L"Hello World";
  30. ret = toolkit_wcs2mbs(hello2, NULL, 0);
  31. ASSERT_TRUE(ret == 12);
  32. }
  33. TEST(TestCharUtil, ExceptionTest)
  34. {
  35. char hello[127] = "";
  36. size_t ret = toolkit_mbs2wcs(hello, NULL, 0);
  37. ASSERT_TRUE(ret == 0);
  38. wchar_t hello2[127] = L"";
  39. ret = toolkit_wcs2mbs(hello2, NULL, 0);
  40. ASSERT_TRUE(ret == 0);
  41. }
  42. TEST(TestCharUtil, EmptyTest)
  43. {
  44. wchar_t* wmsg = NULL;
  45. char* msg = NULL;
  46. wmsg = (wchar_t*)malloc(sizeof(wchar_t));
  47. wmsg[0] = L'\0';
  48. ASSERT_TRUE(wmsg != NULL);
  49. int n = toolkit_wcs2mbs(wmsg, NULL, 0);
  50. const int rn = n > 0 ? n : 1;
  51. ASSERT_TRUE(rn == 1);
  52. msg = (char*)malloc(rn);
  53. memset(msg, '\0', sizeof(char) * rn);
  54. toolkit_wcs2mbs(wmsg, msg, n);
  55. ASSERT_TRUE(strlen(msg) == 0);
  56. FREE(wmsg);
  57. ASSERT_TRUE(msg != NULL);
  58. FREE(msg);
  59. }