unicode.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: tests/strings/unicode.cpp
  3. // Purpose: Unicode unit test
  4. // Author: Vadim Zeitlin, Wlodzimierz ABX Skiba
  5. // Created: 2004-04-28
  6. // Copyright: (c) 2004 Vadim Zeitlin, Wlodzimierz Skiba
  7. ///////////////////////////////////////////////////////////////////////////////
  8. // ----------------------------------------------------------------------------
  9. // headers
  10. // ----------------------------------------------------------------------------
  11. #include "testprec.h"
  12. #ifdef __BORLANDC__
  13. #pragma hdrstop
  14. #endif
  15. #ifndef WX_PRECOMP
  16. #include "wx/wx.h"
  17. #endif // WX_PRECOMP
  18. #include "wx/encconv.h"
  19. // ----------------------------------------------------------------------------
  20. // helper class holding the matching MB and WC strings
  21. // ----------------------------------------------------------------------------
  22. struct StringConversionData
  23. {
  24. // either str or wcs (but not both) may be NULL, this means that the conversion
  25. // to it should fail
  26. StringConversionData(const char *str_, const wchar_t *wcs_, int flags_ = 0)
  27. : str(str_), wcs(wcs_), flags(flags_)
  28. {
  29. }
  30. const char * const str;
  31. const wchar_t * const wcs;
  32. enum
  33. {
  34. TEST_BOTH = 0, // test both str -> wcs and wcs -> str
  35. ONLY_MB2WC = 1 // only test str -> wcs conversion
  36. };
  37. const int flags;
  38. // test that the conversion between str and wcs (subject to flags) succeeds
  39. //
  40. // the first argument is the index in the test array and is used solely for
  41. // diagnostics
  42. void Test(size_t n, wxMBConv& conv) const
  43. {
  44. if ( str )
  45. {
  46. wxWCharBuffer wbuf = conv.cMB2WC(str);
  47. if ( wcs )
  48. {
  49. CPPUNIT_ASSERT_MESSAGE
  50. (
  51. Message(n, "MB2WC failed"),
  52. wbuf.data()
  53. );
  54. CPPUNIT_ASSERT_MESSAGE
  55. (
  56. Message(n, "MB2WC", wbuf, wcs),
  57. wxStrcmp(wbuf, wcs) == 0
  58. );
  59. }
  60. else // conversion is supposed to fail
  61. {
  62. CPPUNIT_ASSERT_MESSAGE
  63. (
  64. Message(n, "MB2WC succeeded"),
  65. !wbuf.data()
  66. );
  67. }
  68. }
  69. if ( wcs && !(flags & ONLY_MB2WC) )
  70. {
  71. wxCharBuffer buf = conv.cWC2MB(wcs);
  72. if ( str )
  73. {
  74. CPPUNIT_ASSERT_MESSAGE
  75. (
  76. Message(n, "WC2MB failed"),
  77. buf.data()
  78. );
  79. CPPUNIT_ASSERT_MESSAGE
  80. (
  81. Message(n, "WC2MB", buf, str),
  82. strcmp(buf, str) == 0
  83. );
  84. }
  85. else
  86. {
  87. CPPUNIT_ASSERT_MESSAGE
  88. (
  89. Message(n, "WC2MB succeeded"),
  90. !buf.data()
  91. );
  92. }
  93. }
  94. }
  95. private:
  96. static std::string
  97. Message(size_t n, const wxString& msg)
  98. {
  99. return std::string(wxString::Format("#%lu: %s", (unsigned long)n, msg));
  100. }
  101. template <typename T>
  102. static std::string
  103. Message(size_t n,
  104. const char *func,
  105. const wxCharTypeBuffer<T>& actual,
  106. const T *expected)
  107. {
  108. return Message(n,
  109. wxString::Format("%s returned \"%s\", expected \"%s\"",
  110. func, actual.data(), expected));
  111. }
  112. };
  113. // ----------------------------------------------------------------------------
  114. // test class
  115. // ----------------------------------------------------------------------------
  116. class UnicodeTestCase : public CppUnit::TestCase
  117. {
  118. public:
  119. UnicodeTestCase();
  120. private:
  121. CPPUNIT_TEST_SUITE( UnicodeTestCase );
  122. CPPUNIT_TEST( ToFromAscii );
  123. CPPUNIT_TEST( ConstructorsWithConversion );
  124. CPPUNIT_TEST( ConversionFixed );
  125. CPPUNIT_TEST( ConversionWithNULs );
  126. CPPUNIT_TEST( ConversionUTF7 );
  127. CPPUNIT_TEST( ConversionUTF8 );
  128. CPPUNIT_TEST( ConversionUTF16 );
  129. CPPUNIT_TEST( ConversionUTF32 );
  130. CPPUNIT_TEST( IsConvOk );
  131. #if wxUSE_UNICODE
  132. CPPUNIT_TEST( Iteration );
  133. #endif
  134. CPPUNIT_TEST_SUITE_END();
  135. void ToFromAscii();
  136. void ConstructorsWithConversion();
  137. void ConversionFixed();
  138. void ConversionWithNULs();
  139. void ConversionUTF7();
  140. void ConversionUTF8();
  141. void ConversionUTF16();
  142. void ConversionUTF32();
  143. void IsConvOk();
  144. #if wxUSE_UNICODE
  145. void Iteration();
  146. #endif
  147. DECLARE_NO_COPY_CLASS(UnicodeTestCase)
  148. };
  149. // register in the unnamed registry so that these tests are run by default
  150. CPPUNIT_TEST_SUITE_REGISTRATION( UnicodeTestCase );
  151. // also include in its own registry so that these tests can be run alone
  152. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( UnicodeTestCase, "UnicodeTestCase" );
  153. UnicodeTestCase::UnicodeTestCase()
  154. {
  155. }
  156. void UnicodeTestCase::ToFromAscii()
  157. {
  158. #define TEST_TO_FROM_ASCII(txt) \
  159. { \
  160. static const char *msg = txt; \
  161. wxString s = wxString::FromAscii(msg); \
  162. CPPUNIT_ASSERT( strcmp( s.ToAscii() , msg ) == 0 ); \
  163. }
  164. TEST_TO_FROM_ASCII( "Hello, world!" );
  165. TEST_TO_FROM_ASCII( "additional \" special \t test \\ component \n :-)" );
  166. }
  167. void UnicodeTestCase::ConstructorsWithConversion()
  168. {
  169. // the string "Déjà" in UTF-8 and wchar_t:
  170. const unsigned char utf8Buf[] = {0x44,0xC3,0xA9,0x6A,0xC3,0xA0,0};
  171. const unsigned char utf8subBuf[] = {0x44,0xC3,0xA9,0x6A,0}; // just "Déj"
  172. const char *utf8 = (char *)utf8Buf;
  173. const char *utf8sub = (char *)utf8subBuf;
  174. wxString s1(utf8, wxConvUTF8);
  175. #if wxUSE_UNICODE
  176. const wchar_t wchar[] = {0x44,0xE9,0x6A,0xE0,0};
  177. CPPUNIT_ASSERT_EQUAL( wchar, s1 );
  178. wxString s2(wchar);
  179. CPPUNIT_ASSERT_EQUAL( wchar, s2 );
  180. CPPUNIT_ASSERT_EQUAL( wxString::FromUTF8(utf8), s2 );
  181. #else
  182. CPPUNIT_ASSERT_EQUAL( utf8, s1 );
  183. #endif
  184. wxString sub(utf8sub, wxConvUTF8); // "Dej" substring
  185. wxString s3(utf8, wxConvUTF8, 4);
  186. CPPUNIT_ASSERT_EQUAL( sub, s3 );
  187. #if wxUSE_UNICODE
  188. wxString s4(wchar, wxConvUTF8, 3);
  189. CPPUNIT_ASSERT_EQUAL( sub, s4 );
  190. // conversion should stop with failure at pos 35
  191. wxString s("\t[pl]open.format.Sformatuj dyskietk\xea=gfloppy %f", wxConvUTF8);
  192. CPPUNIT_ASSERT( s.empty() );
  193. #endif // wxUSE_UNICODE
  194. // test using Unicode strings together with char* strings (this must work
  195. // in ANSI mode as well, of course):
  196. wxString s5("ascii");
  197. CPPUNIT_ASSERT_EQUAL( "ascii", s5 );
  198. s5 += " value";
  199. CPPUNIT_ASSERT( strcmp(s5.mb_str(), "ascii value") == 0 );
  200. CPPUNIT_ASSERT_EQUAL( "ascii value", s5 );
  201. CPPUNIT_ASSERT( s5 != "SomethingElse" );
  202. }
  203. void UnicodeTestCase::ConversionFixed()
  204. {
  205. size_t len;
  206. #if wxUSE_UNICODE
  207. wxConvLibc.cWC2MB(L"", 0, &len);
  208. #else // !wxUSE_UNICODE
  209. wxConvLibc.cMB2WC("", 0, &len);
  210. #endif // wxUSE_UNICODE/!wxUSE_UNICODE
  211. CPPUNIT_ASSERT_EQUAL( 0, len );
  212. #if wxUSE_UNICODE
  213. // check that when we convert a fixed number of characters we obtain the
  214. // expected return value
  215. CPPUNIT_ASSERT_EQUAL( 0, wxConvLibc.ToWChar(NULL, 0, "", 0) );
  216. CPPUNIT_ASSERT_EQUAL( 1, wxConvLibc.ToWChar(NULL, 0, "x", 1) );
  217. CPPUNIT_ASSERT_EQUAL( 2, wxConvLibc.ToWChar(NULL, 0, "x", 2) );
  218. CPPUNIT_ASSERT_EQUAL( 2, wxConvLibc.ToWChar(NULL, 0, "xy", 2) );
  219. #endif // wxUSE_UNICODE
  220. }
  221. void UnicodeTestCase::ConversionWithNULs()
  222. {
  223. #if wxUSE_UNICODE
  224. static const size_t lenNulString = 10;
  225. wxString szTheString(L"The\0String", wxConvLibc, lenNulString);
  226. wxCharBuffer theBuffer = szTheString.mb_str();
  227. CPPUNIT_ASSERT( memcmp(theBuffer.data(), "The\0String",
  228. lenNulString + 1) == 0 );
  229. wxString szTheString2("The\0String", wxConvLocal, lenNulString);
  230. CPPUNIT_ASSERT_EQUAL( lenNulString, szTheString2.length() );
  231. CPPUNIT_ASSERT( wxTmemcmp(szTheString2.c_str(), L"The\0String",
  232. lenNulString + 1) == 0 );
  233. #else // !wxUSE_UNICODE
  234. wxString szTheString("TheString");
  235. szTheString.insert(3, 1, '\0');
  236. wxWCharBuffer theBuffer = szTheString.wc_str(wxConvLibc);
  237. CPPUNIT_ASSERT( memcmp(theBuffer.data(), L"The\0String", 11 * sizeof(wchar_t)) == 0 );
  238. wxString szLocalTheString("TheString");
  239. szLocalTheString.insert(3, 1, '\0');
  240. wxWCharBuffer theLocalBuffer = szLocalTheString.wc_str(wxConvLocal);
  241. CPPUNIT_ASSERT( memcmp(theLocalBuffer.data(), L"The\0String", 11 * sizeof(wchar_t)) == 0 );
  242. #endif // wxUSE_UNICODE/!wxUSE_UNICODE
  243. }
  244. void UnicodeTestCase::ConversionUTF7()
  245. {
  246. static const StringConversionData utf7data[] =
  247. {
  248. // normal fragments
  249. StringConversionData("+AKM-", L"\xa3"),
  250. StringConversionData("+AOk-t+AOk-", L"\xe9t\xe9"),
  251. // this one is an alternative valid encoding of the same string
  252. StringConversionData("+AOk-t+AOk", L"\xe9t\xe9",
  253. StringConversionData::ONLY_MB2WC),
  254. // some special cases
  255. StringConversionData("+-", L"+"),
  256. StringConversionData("+--", L"+-"),
  257. // the following are invalid UTF-7 sequences
  258. StringConversionData("\xa3", NULL),
  259. StringConversionData("+", NULL),
  260. StringConversionData("+~", NULL),
  261. StringConversionData("a+", NULL),
  262. };
  263. for ( size_t n = 0; n < WXSIZEOF(utf7data); n++ )
  264. {
  265. const StringConversionData& d = utf7data[n];
  266. // converting to/from UTF-7 using iconv() currently doesn't work
  267. // because of several problems:
  268. // - GetMBNulLen() doesn't return correct result (iconv converts L'\0'
  269. // to an incomplete and anyhow nonsensical "+AA" string)
  270. // - iconv refuses to convert "+-" (although it converts "+-\n" just
  271. // fine, go figure)
  272. //
  273. // I have no idea how to fix this so just disable the test for now
  274. #if 0
  275. d.Test(n, wxCSConv("utf-7"));
  276. #endif
  277. d.Test(n, wxConvUTF7);
  278. }
  279. }
  280. void UnicodeTestCase::ConversionUTF8()
  281. {
  282. static const StringConversionData utf8data[] =
  283. {
  284. #ifdef wxHAVE_U_ESCAPE
  285. StringConversionData("\xc2\xa3", L"\u00a3"),
  286. #endif
  287. StringConversionData("\xc2", NULL),
  288. };
  289. wxCSConv conv(wxT("utf-8"));
  290. for ( size_t n = 0; n < WXSIZEOF(utf8data); n++ )
  291. {
  292. const StringConversionData& d = utf8data[n];
  293. d.Test(n, conv);
  294. d.Test(n, wxConvUTF8);
  295. }
  296. static const char* const u25a6 = "\xe2\x96\xa6";
  297. wxMBConvUTF8 c(wxMBConvUTF8::MAP_INVALID_UTF8_TO_OCTAL);
  298. CPPUNIT_ASSERT_EQUAL( 2, c.ToWChar(NULL, 0, u25a6, wxNO_LEN) );
  299. CPPUNIT_ASSERT_EQUAL( 0, c.ToWChar(NULL, 0, u25a6, 0) );
  300. CPPUNIT_ASSERT_EQUAL( 1, c.ToWChar(NULL, 0, u25a6, 3) );
  301. CPPUNIT_ASSERT_EQUAL( 2, c.ToWChar(NULL, 0, u25a6, 4) );
  302. }
  303. void UnicodeTestCase::ConversionUTF16()
  304. {
  305. static const StringConversionData utf16data[] =
  306. {
  307. #ifdef wxHAVE_U_ESCAPE
  308. StringConversionData(
  309. "\x04\x1f\x04\x40\x04\x38\x04\x32\x04\x35\x04\x42\0\0",
  310. L"\u041f\u0440\u0438\u0432\u0435\u0442"),
  311. StringConversionData(
  312. "\x01\0\0b\x01\0\0a\x01\0\0r\0\0",
  313. L"\u0100b\u0100a\u0100r"),
  314. #endif
  315. StringConversionData("\0f\0o\0o\0\0", L"foo"),
  316. };
  317. wxCSConv conv(wxFONTENCODING_UTF16BE);
  318. for ( size_t n = 0; n < WXSIZEOF(utf16data); n++ )
  319. {
  320. const StringConversionData& d = utf16data[n];
  321. d.Test(n, conv);
  322. }
  323. // special case: this string has consecutive NULs inside it which don't
  324. // terminate the string, this exposed a bug in our conversion code which
  325. // got confused in this case
  326. size_t len;
  327. conv.cMB2WC("\x01\0\0B\0C" /* A macron BC */, 6, &len);
  328. CPPUNIT_ASSERT_EQUAL( 3, len );
  329. // Another one: verify that the length of the resulting string is computed
  330. // correctly when there is a surrogate in the input.
  331. wxMBConvUTF16BE().cMB2WC("\xd8\x03\xdc\x01\0" /* OLD TURKIC LETTER YENISEI A */, wxNO_LEN, &len);
  332. CPPUNIT_ASSERT_EQUAL( 1, len );
  333. }
  334. void UnicodeTestCase::ConversionUTF32()
  335. {
  336. static const StringConversionData utf32data[] =
  337. {
  338. #ifdef wxHAVE_U_ESCAPE
  339. StringConversionData(
  340. "\0\0\x04\x1f\0\0\x04\x40\0\0\x04\x38\0\0\x04\x32\0\0\x04\x35\0\0\x04\x42\0\0\0\0",
  341. L"\u041f\u0440\u0438\u0432\u0435\u0442"),
  342. #endif
  343. StringConversionData("\0\0\0f\0\0\0o\0\0\0o\0\0\0\0", L"foo"),
  344. };
  345. wxCSConv conv(wxFONTENCODING_UTF32BE);
  346. for ( size_t n = 0; n < WXSIZEOF(utf32data); n++ )
  347. {
  348. const StringConversionData& d = utf32data[n];
  349. d.Test(n, conv);
  350. }
  351. size_t len;
  352. conv.cMB2WC("\0\0\x01\0\0\0\0B\0\0\0C" /* A macron BC */, 12, &len);
  353. CPPUNIT_ASSERT_EQUAL( 3, len );
  354. }
  355. void UnicodeTestCase::IsConvOk()
  356. {
  357. CPPUNIT_ASSERT( wxCSConv(wxFONTENCODING_SYSTEM).IsOk() );
  358. CPPUNIT_ASSERT( wxCSConv("US-ASCII").IsOk() );
  359. CPPUNIT_ASSERT( wxCSConv("UTF-8").IsOk() );
  360. CPPUNIT_ASSERT( !wxCSConv("NoSuchConversion").IsOk() );
  361. #ifdef __WINDOWS__
  362. CPPUNIT_ASSERT( wxCSConv("WINDOWS-437").IsOk() );
  363. #endif
  364. }
  365. #if wxUSE_UNICODE
  366. void UnicodeTestCase::Iteration()
  367. {
  368. // "czech" in Czech ("cestina"):
  369. static const char *textUTF8 = "\304\215e\305\241tina";
  370. static const wchar_t textUTF16[] = {0x10D, 0x65, 0x161, 0x74, 0x69, 0x6E, 0x61, 0};
  371. wxString text(wxString::FromUTF8(textUTF8));
  372. CPPUNIT_ASSERT( wxStrcmp(text.wc_str(), textUTF16) == 0 );
  373. // verify the string was decoded correctly:
  374. {
  375. size_t idx = 0;
  376. for ( wxString::const_iterator i = text.begin(); i != text.end(); ++i, ++idx )
  377. {
  378. CPPUNIT_ASSERT( *i == textUTF16[idx] );
  379. }
  380. }
  381. // overwrite the string with something that is shorter in UTF-8:
  382. {
  383. for ( wxString::iterator i = text.begin(); i != text.end(); ++i )
  384. *i = 'x';
  385. }
  386. // restore the original text now:
  387. {
  388. wxString::iterator end1 = text.end();
  389. wxString::const_iterator end2 = text.end();
  390. size_t idx = 0;
  391. for ( wxString::iterator i = text.begin(); i != text.end(); ++i, ++idx )
  392. {
  393. *i = textUTF16[idx];
  394. CPPUNIT_ASSERT( end1 == text.end() );
  395. CPPUNIT_ASSERT( end2 == text.end() );
  396. }
  397. CPPUNIT_ASSERT( end1 == text.end() );
  398. CPPUNIT_ASSERT( end2 == text.end() );
  399. }
  400. // and verify it again:
  401. {
  402. size_t idx = 0;
  403. for ( wxString::const_iterator i = text.begin(); i != text.end(); ++i, ++idx )
  404. {
  405. CPPUNIT_ASSERT( *i == textUTF16[idx] );
  406. }
  407. }
  408. }
  409. #endif // wxUSE_UNICODE