1
0

testEncoding.cxx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*============================================================================
  2. KWSys - Kitware System Library
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "kwsysPrivate.h"
  11. #if defined(_MSC_VER)
  12. # pragma warning (disable:4786)
  13. #endif
  14. #include KWSYS_HEADER(Encoding.hxx)
  15. #include KWSYS_HEADER(Encoding.h)
  16. #include KWSYS_HEADER(ios/iostream)
  17. #include <locale.h>
  18. #include <string.h>
  19. #include <stdlib.h>
  20. // Work-around CMake dependency scanning limitation. This must
  21. // duplicate the above list of headers.
  22. #if 0
  23. # include "Encoding.hxx.in"
  24. # include "Encoding.h.in"
  25. # include "kwsys_ios_iostream.h.in"
  26. #endif
  27. //----------------------------------------------------------------------------
  28. static const unsigned char helloWorldStrings[][32] =
  29. {
  30. // English
  31. {'H','e','l','l','o',' ','W','o','r','l','d',0},
  32. // Japanese
  33. {0xE3, 0x81, 0x93, 0xE3, 0x82, 0x93, 0xE3, 0x81, 0xAB, 0xE3,
  34. 0x81, 0xA1, 0xE3, 0x81, 0xAF, 0xE4, 0xB8, 0x96, 0xE7, 0x95,
  35. 0x8C, 0},
  36. // Arabic
  37. {0xD9, 0x85, 0xD8, 0xB1, 0xD8, 0xAD, 0xD8, 0xA8, 0xD8, 0xA7,
  38. 0x20, 0xD8, 0xA7, 0xD9, 0x84, 0xD8, 0xB9, 0xD8, 0xA7, 0xD9,
  39. 0x84, 0xD9, 0x85, 0},
  40. // Yiddish
  41. {0xD7, 0x94, 0xD7, 0xA2, 0xD7, 0x9C, 0xD7, 0x90, 0x20, 0xD7,
  42. 0x95, 0xD7, 0x95, 0xD7, 0xA2, 0xD7, 0x9C, 0xD7, 0x98, 0},
  43. // Russian
  44. {0xD0, 0xBF, 0xD1, 0x80, 0xD0, 0xB8, 0xD0, 0xB2, 0xD0, 0xB5,
  45. 0xD1, 0x82, 0x20, 0xD0, 0xBC, 0xD0, 0xB8, 0xD1, 0x80, 0},
  46. // Latin
  47. {0x4D, 0x75, 0x6E, 0x64, 0x75, 0x73, 0x20, 0x73, 0x61, 0x6C,
  48. 0x76, 0x65, 0},
  49. // Swahili
  50. {0x68, 0x75, 0x6A, 0x61, 0x6D, 0x62, 0x6F, 0x20, 0x44, 0x75,
  51. 0x6E, 0x69, 0x61, 0},
  52. // Icelandic
  53. {0x48, 0x61, 0x6C, 0x6C, 0xC3, 0xB3, 0x20, 0x68, 0x65, 0x69,
  54. 0x6D, 0x75, 0x72, 0},
  55. {0}
  56. };
  57. //----------------------------------------------------------------------------
  58. static int testHelloWorldEncoding()
  59. {
  60. int ret = 0;
  61. for(int i=0; helloWorldStrings[i][0] != 0; i++)
  62. {
  63. std::string str = reinterpret_cast<const char*>(helloWorldStrings[i]);
  64. std::cout << str << std::endl;
  65. std::wstring wstr = kwsys::Encoding::ToWide(str);
  66. std::string str2 = kwsys::Encoding::ToNarrow(wstr);
  67. wchar_t* c_wstr = kwsysEncoding_DupToWide(str.c_str());
  68. char* c_str2 = kwsysEncoding_DupToNarrow(c_wstr);
  69. if(!wstr.empty() && (str != str2 || strcmp(c_str2, str.c_str())))
  70. {
  71. std::cout << "converted string was different: " << str2 << std::endl;
  72. std::cout << "converted string was different: " << c_str2 << std::endl;
  73. ret++;
  74. }
  75. free(c_wstr);
  76. free(c_str2);
  77. }
  78. return ret;
  79. }
  80. static int testRobustEncoding()
  81. {
  82. // test that the conversion functions handle invalid
  83. // unicode correctly/gracefully
  84. int ret = 0;
  85. char cstr[] = {(char)-1, 0};
  86. // this conversion could fail
  87. std::wstring wstr = kwsys::Encoding::ToWide(cstr);
  88. wstr = kwsys::Encoding::ToWide(NULL);
  89. if(wstr != L"")
  90. {
  91. const wchar_t* wcstr = wstr.c_str();
  92. std::cout << "ToWide(NULL) returned";
  93. for(size_t i=0; i<wstr.size(); i++)
  94. {
  95. std::cout << " " << std::hex << (int)wcstr[i];
  96. }
  97. std::cout << std::endl;
  98. ret++;
  99. }
  100. wstr = kwsys::Encoding::ToWide("");
  101. if(wstr != L"")
  102. {
  103. const wchar_t* wcstr = wstr.c_str();
  104. std::cout << "ToWide(\"\") returned";
  105. for(size_t i=0; i<wstr.size(); i++)
  106. {
  107. std::cout << " " << std::hex << (int)wcstr[i];
  108. }
  109. std::cout << std::endl;
  110. ret++;
  111. }
  112. #ifdef WIN32
  113. // 16 bit wchar_t - we make an invalid surrogate pair
  114. wchar_t cwstr[] = {0xD801, 0xDA00, 0};
  115. // this conversion could fail
  116. std::string win_str = kwsys::Encoding::ToNarrow(cwstr);
  117. #endif
  118. std::string str = kwsys::Encoding::ToNarrow(NULL);
  119. if(str != "")
  120. {
  121. std::cout << "ToNarrow(NULL) returned " << str << std::endl;
  122. ret++;
  123. }
  124. str = kwsys::Encoding::ToNarrow(L"");
  125. if(wstr != L"")
  126. {
  127. std::cout << "ToNarrow(\"\") returned " << str << std::endl;
  128. ret++;
  129. }
  130. return ret;
  131. }
  132. static int testCommandLineArguments()
  133. {
  134. int status = 0;
  135. char const* argv[2] = {
  136. "./app.exe",
  137. (char const*)helloWorldStrings[1]
  138. };
  139. kwsys::Encoding::CommandLineArguments args(2, argv);
  140. kwsys::Encoding::CommandLineArguments arg2 =
  141. kwsys::Encoding::CommandLineArguments(args);
  142. char const* const* u8_argv = args.argv();
  143. for(int i=0; i<args.argc(); i++)
  144. {
  145. char const* u8_arg = u8_argv[i];
  146. if(strcmp(argv[i], u8_arg) != 0)
  147. {
  148. std::cout << "argv[" << i << "] " << argv[i] << " != "
  149. << u8_arg << std::endl;
  150. status++;
  151. }
  152. }
  153. kwsys::Encoding::CommandLineArguments args3 =
  154. kwsys::Encoding::CommandLineArguments::Main(2, argv);
  155. return status;
  156. }
  157. //----------------------------------------------------------------------------
  158. int testEncoding(int, char*[])
  159. {
  160. const char* loc = setlocale(LC_ALL, "");
  161. if(loc)
  162. {
  163. std::cout << "Locale: " << loc << std::endl;
  164. }
  165. else
  166. {
  167. std::cout << "Locale: None" << std::endl;
  168. }
  169. int ret = 0;
  170. ret |= testHelloWorldEncoding();
  171. ret |= testRobustEncoding();
  172. ret |= testCommandLineArguments();
  173. return ret;
  174. }