textstreamtest.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: tests/streams/textstreamtest.cpp
  3. // Purpose: wxTextXXXStream unit test
  4. // Author: Ryan Norton, Vince Harron
  5. // Created: 2004-08-14
  6. // Copyright: (c) 2004 Ryan Norton, (c) 2006 Vince Harron
  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/txtstrm.h"
  19. #include "wx/wfstream.h"
  20. #if wxUSE_LONGLONG
  21. #include "wx/longlong.h"
  22. #endif
  23. #if wxUSE_UNICODE
  24. #include "wx/mstream.h"
  25. #endif // wxUSE_UNICODE
  26. // ----------------------------------------------------------------------------
  27. // test class
  28. // ----------------------------------------------------------------------------
  29. class TextStreamTestCase : public CppUnit::TestCase
  30. {
  31. public:
  32. TextStreamTestCase();
  33. private:
  34. CPPUNIT_TEST_SUITE( TextStreamTestCase );
  35. CPPUNIT_TEST( Endline );
  36. CPPUNIT_TEST( MiscTests );
  37. #if wxUSE_LONGLONG
  38. CPPUNIT_TEST( TestLongLong );
  39. CPPUNIT_TEST( TestLongLong );
  40. #endif // wxUSE_LONGLONG
  41. #if wxUSE_UNICODE
  42. CPPUNIT_TEST( TestUTF8Input );
  43. CPPUNIT_TEST( TestEmbeddedZerosUTF16LEInput );
  44. CPPUNIT_TEST( TestEmbeddedZerosUTF16BEInput );
  45. CPPUNIT_TEST( TestEmbeddedZerosUTF32LEInput );
  46. CPPUNIT_TEST( TestEmbeddedZerosUTF32BEInput );
  47. #endif // wxUSE_UNICODE
  48. CPPUNIT_TEST_SUITE_END();
  49. void Endline();
  50. void MiscTests();
  51. #if wxUSE_LONGLONG
  52. void TestLongLong();
  53. void TestULongLong();
  54. #endif // wxUSE_LONGLONG
  55. #if wxUSE_UNICODE
  56. void TestUTF8Input();
  57. void TestEmbeddedZerosUTF16LEInput();
  58. void TestEmbeddedZerosUTF16BEInput();
  59. void TestEmbeddedZerosUTF32LEInput();
  60. void TestEmbeddedZerosUTF32BEInput();
  61. void TestInput(const wxMBConv& conv,
  62. const void* encodedText,
  63. size_t encodedSize );
  64. #endif // wxUSE_UNICODE
  65. DECLARE_NO_COPY_CLASS(TextStreamTestCase)
  66. };
  67. // register in the unnamed registry so that these tests are run by default
  68. CPPUNIT_TEST_SUITE_REGISTRATION( TextStreamTestCase );
  69. // also include in its own registry so that these tests can be run alone
  70. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TextStreamTestCase, "TextStreamTestCase" );
  71. TextStreamTestCase::TextStreamTestCase()
  72. {
  73. }
  74. #if defined(__WINDOWS__) || defined(__WXPM__)
  75. # define NEWLINE "\r\n"
  76. # define NEWLINELEN 2
  77. #elif defined(__WXMAC__) && !defined(__DARWIN__)
  78. # define NEWLINE "\r"
  79. # define NEWLINELEN 1
  80. #else
  81. # define NEWLINE "\n"
  82. # define NEWLINELEN 1
  83. #endif
  84. void TextStreamTestCase::Endline()
  85. {
  86. wxFileOutputStream* pOutFile = new wxFileOutputStream(wxT("test.txt"));
  87. wxTextOutputStream* pOutText = new wxTextOutputStream(*pOutFile);
  88. *pOutText << wxT("Test text") << endl
  89. << wxT("More Testing Text (There should be newline before this)");
  90. delete pOutText;
  91. delete pOutFile;
  92. wxFileInputStream* pInFile = new wxFileInputStream(wxT("test.txt"));
  93. char szIn[9 + NEWLINELEN];
  94. pInFile->Read(szIn, 9 + NEWLINELEN);
  95. CPPUNIT_ASSERT( memcmp(&szIn[9], NEWLINE, NEWLINELEN) == 0 );
  96. delete pInFile;
  97. }
  98. void TextStreamTestCase::MiscTests()
  99. {
  100. wxString filename = wxT("testdata.fc");
  101. wxFileInputStream fsIn(filename);
  102. if ( !fsIn.IsOk() )
  103. {
  104. return;
  105. }
  106. wxTextInputStream tis(fsIn);
  107. CPPUNIT_ASSERT_EQUAL("# this is the test data file for wxFileConfig tests", tis.ReadLine());
  108. CPPUNIT_ASSERT_EQUAL("value1=one", tis.ReadLine());
  109. CPPUNIT_ASSERT_EQUAL("# a comment here", tis.ReadLine());
  110. CPPUNIT_ASSERT_EQUAL("value2=two", tis.ReadLine());
  111. CPPUNIT_ASSERT_EQUAL("value\\ with\\ spaces\\ inside\\ it=nothing special", tis.ReadLine());
  112. CPPUNIT_ASSERT_EQUAL("path=$PATH", tis.ReadLine());
  113. }
  114. #if wxUSE_LONGLONG
  115. template <typename T>
  116. static void DoTestRoundTrip(const T *values, size_t numValues)
  117. {
  118. {
  119. wxFileOutputStream fileOut(wxT("test.txt"));
  120. wxTextOutputStream textOut(fileOut);
  121. for ( size_t n = 0; n < numValues; n++ )
  122. {
  123. textOut << values[n] << endl;
  124. }
  125. }
  126. {
  127. wxFileInputStream fileIn(wxT("test.txt"));
  128. wxTextInputStream textIn(fileIn);
  129. T value;
  130. for ( size_t n = 0; n < numValues; n++ )
  131. {
  132. textIn >> value;
  133. CPPUNIT_ASSERT( value == values[n] );
  134. }
  135. }
  136. }
  137. void TextStreamTestCase::TestLongLong()
  138. {
  139. static const wxLongLong llvalues[] =
  140. {
  141. 0,
  142. 1,
  143. -1,
  144. 0x12345678l,
  145. -0x12345678l,
  146. wxLL(0x123456789abcdef0),
  147. wxLL(-0x123456789abcdef0),
  148. };
  149. DoTestRoundTrip(llvalues, WXSIZEOF(llvalues));
  150. }
  151. void TextStreamTestCase::TestULongLong()
  152. {
  153. static const wxULongLong ullvalues[] =
  154. {
  155. 0,
  156. 1,
  157. 0x12345678l,
  158. wxULL(0x123456789abcdef0),
  159. };
  160. DoTestRoundTrip(ullvalues, WXSIZEOF(ullvalues));
  161. }
  162. #endif // wxUSE_LONGLONG
  163. #if wxUSE_UNICODE
  164. static const wchar_t txtWchar[4] =
  165. {
  166. 0x0041, // LATIN CAPITAL LETTER A
  167. 0x0100, // A WITH BREVE, LATIN SMALL LETTER
  168. 0x0041, // LATIN CAPITAL LETTER A
  169. 0x0100, // A WITH BREVE, LATIN SMALL LETTER
  170. };
  171. static const unsigned char txtUtf8[6] =
  172. {
  173. 0x41, 0xc4, 0x80, 0x41, 0xc4, 0x80,
  174. };
  175. static const unsigned char txtUtf16le[8] =
  176. {
  177. 0x41, 0x00, 0x00, 0x01, 0x41, 0x00, 0x00, 0x01,
  178. };
  179. static const unsigned char txtUtf16be[8] =
  180. {
  181. 0x00, 0x41, 0x01, 0x00, 0x00, 0x41, 0x01, 0x00,
  182. };
  183. static const unsigned char txtUtf32le[16] =
  184. {
  185. 0x41, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  186. 0x41, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  187. };
  188. static const unsigned char txtUtf32be[16] =
  189. {
  190. 0x00, 0x00, 0x00, 0x41, 0x00, 0x00, 0x01, 0x00,
  191. 0x00, 0x00, 0x00, 0x41, 0x00, 0x00, 0x01, 0x00,
  192. };
  193. void TextStreamTestCase::TestUTF8Input()
  194. {
  195. TestInput(wxConvUTF8, txtUtf8, sizeof(txtUtf8));
  196. TestInput(wxCSConv(wxFONTENCODING_UTF8), txtUtf8, sizeof(txtUtf8));
  197. }
  198. void TextStreamTestCase::TestEmbeddedZerosUTF16LEInput()
  199. {
  200. TestInput(wxMBConvUTF16LE(), txtUtf16le, sizeof(txtUtf16le));
  201. TestInput(wxCSConv(wxFONTENCODING_UTF16LE), txtUtf16le, sizeof(txtUtf16le));
  202. }
  203. void TextStreamTestCase::TestEmbeddedZerosUTF16BEInput()
  204. {
  205. TestInput(wxMBConvUTF16BE(), txtUtf16be, sizeof(txtUtf16be));
  206. TestInput(wxCSConv(wxFONTENCODING_UTF16BE), txtUtf16be, sizeof(txtUtf16be));
  207. }
  208. void TextStreamTestCase::TestEmbeddedZerosUTF32LEInput()
  209. {
  210. TestInput(wxMBConvUTF32LE(), txtUtf32le, sizeof(txtUtf32le));
  211. TestInput(wxCSConv(wxFONTENCODING_UTF32LE), txtUtf32le, sizeof(txtUtf32le));
  212. }
  213. void TextStreamTestCase::TestEmbeddedZerosUTF32BEInput()
  214. {
  215. TestInput(wxMBConvUTF32BE(), txtUtf32be, sizeof(txtUtf32be));
  216. TestInput(wxCSConv(wxFONTENCODING_UTF32BE), txtUtf32be, sizeof(txtUtf32be));
  217. }
  218. void TextStreamTestCase::TestInput(const wxMBConv& conv,
  219. const void *encodedText,
  220. size_t encodedSize)
  221. {
  222. wxMemoryInputStream byteIn(encodedText, encodedSize);
  223. wxTextInputStream textIn(byteIn, wxT("\n"), conv);
  224. wxString temp;
  225. while ( wxChar c = textIn.GetChar() )
  226. {
  227. temp.Append(c);
  228. }
  229. CPPUNIT_ASSERT_EQUAL( WXSIZEOF(txtWchar), temp.length() );
  230. CPPUNIT_ASSERT_EQUAL( 0, memcmp(txtWchar, temp.wc_str(), sizeof(txtWchar)) );
  231. }
  232. #endif // wxUSE_UNICODE