convautotest.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: tests/mbconv/convauto.cpp
  3. // Purpose: wxConvAuto unit test
  4. // Author: Vadim Zeitlin
  5. // Created: 2006-04-04
  6. // Copyright: (c) 2006 Vadim Zeitlin
  7. ///////////////////////////////////////////////////////////////////////////////
  8. // ----------------------------------------------------------------------------
  9. // headers
  10. // ----------------------------------------------------------------------------
  11. #include "testprec.h"
  12. #ifdef __BORLANDC__
  13. #pragma hdrstop
  14. #endif
  15. #if wxUSE_UNICODE
  16. #include "wx/convauto.h"
  17. #include "wx/mstream.h"
  18. #include "wx/txtstrm.h"
  19. // ----------------------------------------------------------------------------
  20. // test class
  21. // ----------------------------------------------------------------------------
  22. class ConvAutoTestCase : public CppUnit::TestCase
  23. {
  24. public:
  25. ConvAutoTestCase() { }
  26. private:
  27. CPPUNIT_TEST_SUITE( ConvAutoTestCase );
  28. CPPUNIT_TEST( Empty );
  29. CPPUNIT_TEST( Short );
  30. CPPUNIT_TEST( None );
  31. CPPUNIT_TEST( UTF32LE );
  32. CPPUNIT_TEST( UTF32BE );
  33. CPPUNIT_TEST( UTF16LE );
  34. CPPUNIT_TEST( UTF16BE );
  35. CPPUNIT_TEST( UTF8 );
  36. CPPUNIT_TEST( StreamUTF8NoBOM );
  37. CPPUNIT_TEST( StreamUTF8 );
  38. CPPUNIT_TEST( StreamUTF16LE );
  39. CPPUNIT_TEST( StreamUTF16BE );
  40. CPPUNIT_TEST( StreamUTF32LE );
  41. CPPUNIT_TEST( StreamUTF32BE );
  42. CPPUNIT_TEST_SUITE_END();
  43. // real test function: check that converting the src multibyte string to
  44. // wide char using wxConvAuto yields wch as the first result
  45. //
  46. // the length of the string may need to be passed explicitly if it has
  47. // embedded NULs, otherwise it's not necessary
  48. void TestFirstChar(const char *src, wchar_t wch, size_t len = wxNO_LEN);
  49. void Empty();
  50. void Short();
  51. void None();
  52. void UTF32LE();
  53. void UTF32BE();
  54. void UTF16LE();
  55. void UTF16BE();
  56. void UTF8();
  57. // test whether two lines of text are converted properly from a stream
  58. void TestTextStream(const char *src,
  59. size_t srclength,
  60. const wxString& line1,
  61. const wxString& line2);
  62. void StreamUTF8NoBOM();
  63. void StreamUTF8();
  64. void StreamUTF16LE();
  65. void StreamUTF16BE();
  66. void StreamUTF32LE();
  67. void StreamUTF32BE();
  68. };
  69. // register in the unnamed registry so that these tests are run by default
  70. CPPUNIT_TEST_SUITE_REGISTRATION(ConvAutoTestCase);
  71. // also include in its own registry so that these tests can be run alone
  72. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(ConvAutoTestCase, "ConvAutoTestCase");
  73. // ----------------------------------------------------------------------------
  74. // tests
  75. // ----------------------------------------------------------------------------
  76. void ConvAutoTestCase::TestFirstChar(const char *src, wchar_t wch, size_t len)
  77. {
  78. wxWCharBuffer wbuf = wxConvAuto().cMB2WC(src, len, NULL);
  79. CPPUNIT_ASSERT( wbuf );
  80. CPPUNIT_ASSERT_EQUAL( wch, *wbuf );
  81. }
  82. void ConvAutoTestCase::Empty()
  83. {
  84. CPPUNIT_ASSERT( !wxConvAuto().cMB2WC("") );
  85. }
  86. void ConvAutoTestCase::Short()
  87. {
  88. TestFirstChar("1", wxT('1'));
  89. }
  90. void ConvAutoTestCase::None()
  91. {
  92. TestFirstChar("Hello world", wxT('H'));
  93. }
  94. void ConvAutoTestCase::UTF32LE()
  95. {
  96. TestFirstChar("\xff\xfe\0\0A\0\0\0", wxT('A'), 8);
  97. }
  98. void ConvAutoTestCase::UTF32BE()
  99. {
  100. TestFirstChar("\0\0\xfe\xff\0\0\0B", wxT('B'), 8);
  101. }
  102. void ConvAutoTestCase::UTF16LE()
  103. {
  104. TestFirstChar("\xff\xfeZ\0", wxT('Z'), 4);
  105. }
  106. void ConvAutoTestCase::UTF16BE()
  107. {
  108. TestFirstChar("\xfe\xff\0Y", wxT('Y'), 4);
  109. }
  110. void ConvAutoTestCase::UTF8()
  111. {
  112. #ifdef wxHAVE_U_ESCAPE
  113. TestFirstChar("\xef\xbb\xbf\xd0\x9f", L'\u041f');
  114. #endif
  115. }
  116. void ConvAutoTestCase::TestTextStream(const char *src,
  117. size_t srclength,
  118. const wxString& line1,
  119. const wxString& line2)
  120. {
  121. wxMemoryInputStream instream(src, srclength);
  122. wxTextInputStream text(instream);
  123. CPPUNIT_ASSERT_EQUAL( line1, text.ReadLine() );
  124. CPPUNIT_ASSERT_EQUAL( line2, text.ReadLine() );
  125. }
  126. // the first line of the teststring used in the following functions is an
  127. // 'a' followed by a Japanese hiragana A (u+3042).
  128. // The second line is a single Greek beta (u+03B2). There is no blank line
  129. // at the end.
  130. namespace
  131. {
  132. const wxString line1 = wxString::FromUTF8("a\xe3\x81\x82");
  133. const wxString line2 = wxString::FromUTF8("\xce\xb2");
  134. } // anonymous namespace
  135. void ConvAutoTestCase::StreamUTF8NoBOM()
  136. {
  137. // currently this test doesn't work because without the BOM wxConvAuto
  138. // decides that the string is in Latin-1 after finding the first (but not
  139. // the two subsequent ones which are part of the same UTF-8 sequence!)
  140. // 8-bit character
  141. //
  142. // FIXME: we need to fix this at wxTextInputStream level, see #11570
  143. #if 0
  144. TestTextStream("\x61\xE3\x81\x82\x0A\xCE\xB2",
  145. 7, line1, line2);
  146. #endif
  147. }
  148. void ConvAutoTestCase::StreamUTF8()
  149. {
  150. TestTextStream("\xEF\xBB\xBF\x61\xE3\x81\x82\x0A\xCE\xB2",
  151. 10, line1, line2);
  152. }
  153. void ConvAutoTestCase::StreamUTF16LE()
  154. {
  155. TestTextStream("\xFF\xFE\x61\x00\x42\x30\x0A\x00\xB2\x03",
  156. 10, line1, line2);
  157. }
  158. void ConvAutoTestCase::StreamUTF16BE()
  159. {
  160. TestTextStream("\xFE\xFF\x00\x61\x30\x42\x00\x0A\x03\xB2",
  161. 10, line1, line2);
  162. }
  163. void ConvAutoTestCase::StreamUTF32LE()
  164. {
  165. TestTextStream("\xFF\xFE\0\0\x61\x00\0\0\x42\x30\0\0\x0A"
  166. "\x00\0\0\xB2\x03\0\0",
  167. 20, line1, line2);
  168. }
  169. void ConvAutoTestCase::StreamUTF32BE()
  170. {
  171. TestTextStream("\0\0\xFE\xFF\0\0\x00\x61\0\0\x30\x42\0\0\x00\x0A"
  172. "\0\0\x03\xB2",
  173. 20, line1, line2);
  174. }
  175. #endif // wxUSE_UNICODE