textfiletest.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: tests/textfile/textfile.cpp
  3. // Purpose: wxTextFile unit test
  4. // Author: Vadim Zeitlin
  5. // Created: 2006-03-31
  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_TEXTFILE
  16. #ifndef WX_PRECOMP
  17. #endif // WX_PRECOMP
  18. #include "wx/ffile.h"
  19. #include "wx/textfile.h"
  20. #ifdef __VISUALC__
  21. #define unlink _unlink
  22. #endif
  23. // ----------------------------------------------------------------------------
  24. // test class
  25. // ----------------------------------------------------------------------------
  26. class TextFileTestCase : public CppUnit::TestCase
  27. {
  28. public:
  29. TextFileTestCase()
  30. {
  31. srand((unsigned)time(NULL));
  32. }
  33. virtual void tearDown() { unlink(GetTestFileName()); }
  34. private:
  35. CPPUNIT_TEST_SUITE( TextFileTestCase );
  36. CPPUNIT_TEST( ReadEmpty );
  37. CPPUNIT_TEST( ReadDOS );
  38. CPPUNIT_TEST( ReadDOSLast );
  39. CPPUNIT_TEST( ReadUnix );
  40. CPPUNIT_TEST( ReadUnixLast );
  41. CPPUNIT_TEST( ReadMac );
  42. CPPUNIT_TEST( ReadMacLast );
  43. CPPUNIT_TEST( ReadMixed );
  44. CPPUNIT_TEST( ReadMixedWithFuzzing );
  45. CPPUNIT_TEST( ReadCRCRLF );
  46. #if wxUSE_UNICODE
  47. CPPUNIT_TEST( ReadUTF8 );
  48. CPPUNIT_TEST( ReadUTF16 );
  49. #endif // wxUSE_UNICODE
  50. CPPUNIT_TEST( ReadBig );
  51. CPPUNIT_TEST_SUITE_END();
  52. void ReadEmpty();
  53. void ReadDOS();
  54. void ReadDOSLast();
  55. void ReadUnix();
  56. void ReadUnixLast();
  57. void ReadMac();
  58. void ReadMacLast();
  59. void ReadMixed();
  60. void ReadMixedWithFuzzing();
  61. void ReadCRCRLF();
  62. #if wxUSE_UNICODE
  63. void ReadUTF8();
  64. void ReadUTF16();
  65. #endif // wxUSE_UNICODE
  66. void ReadBig();
  67. // return the name of the test file we use
  68. static const char *GetTestFileName() { return "textfiletest.txt"; }
  69. // create the test file with the given contents
  70. static void CreateTestFile(const char *contents)
  71. {
  72. CreateTestFile(strlen(contents), contents);
  73. }
  74. // create the test file with the given contents (version must be used if
  75. // contents contains NULs)
  76. static void CreateTestFile(size_t len, const char *contents);
  77. DECLARE_NO_COPY_CLASS(TextFileTestCase)
  78. };
  79. // register in the unnamed registry so that these tests are run by default
  80. CPPUNIT_TEST_SUITE_REGISTRATION( TextFileTestCase );
  81. // also include in its own registry so that these tests can be run alone
  82. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TextFileTestCase, "TextFileTestCase" );
  83. void TextFileTestCase::CreateTestFile(size_t len, const char *contents)
  84. {
  85. FILE *f = fopen(GetTestFileName(), "wb");
  86. CPPUNIT_ASSERT( f );
  87. CPPUNIT_ASSERT_EQUAL( len, fwrite(contents, 1, len, f) );
  88. CPPUNIT_ASSERT_EQUAL( 0, fclose(f) );
  89. }
  90. void TextFileTestCase::ReadEmpty()
  91. {
  92. CreateTestFile("");
  93. wxTextFile f;
  94. CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) );
  95. CPPUNIT_ASSERT_EQUAL( (size_t)0, f.GetLineCount() );
  96. }
  97. void TextFileTestCase::ReadDOS()
  98. {
  99. CreateTestFile("foo\r\nbar\r\nbaz");
  100. wxTextFile f;
  101. CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) );
  102. CPPUNIT_ASSERT_EQUAL( (size_t)3, f.GetLineCount() );
  103. CPPUNIT_ASSERT_EQUAL( wxTextFileType_Dos, f.GetLineType(0) );
  104. CPPUNIT_ASSERT_EQUAL( wxTextFileType_None, f.GetLineType(2) );
  105. CPPUNIT_ASSERT_EQUAL( wxString(wxT("bar")), f.GetLine(1) );
  106. CPPUNIT_ASSERT_EQUAL( wxString(wxT("baz")), f.GetLastLine() );
  107. }
  108. void TextFileTestCase::ReadDOSLast()
  109. {
  110. CreateTestFile("foo\r\n");
  111. wxTextFile f;
  112. CPPUNIT_ASSERT( f.Open(GetTestFileName()) );
  113. CPPUNIT_ASSERT_EQUAL( 1, f.GetLineCount() );
  114. CPPUNIT_ASSERT_EQUAL( wxTextFileType_Dos, f.GetLineType(0) );
  115. CPPUNIT_ASSERT_EQUAL( "foo", f.GetFirstLine() );
  116. }
  117. void TextFileTestCase::ReadUnix()
  118. {
  119. CreateTestFile("foo\nbar\nbaz");
  120. wxTextFile f;
  121. CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) );
  122. CPPUNIT_ASSERT_EQUAL( (size_t)3, f.GetLineCount() );
  123. CPPUNIT_ASSERT_EQUAL( wxTextFileType_Unix, f.GetLineType(0) );
  124. CPPUNIT_ASSERT_EQUAL( wxTextFileType_None, f.GetLineType(2) );
  125. CPPUNIT_ASSERT_EQUAL( wxString(wxT("bar")), f.GetLine(1) );
  126. CPPUNIT_ASSERT_EQUAL( wxString(wxT("baz")), f.GetLastLine() );
  127. }
  128. void TextFileTestCase::ReadUnixLast()
  129. {
  130. CreateTestFile("foo\n");
  131. wxTextFile f;
  132. CPPUNIT_ASSERT( f.Open(GetTestFileName()) );
  133. CPPUNIT_ASSERT_EQUAL( 1, f.GetLineCount() );
  134. CPPUNIT_ASSERT_EQUAL( wxTextFileType_Unix, f.GetLineType(0) );
  135. CPPUNIT_ASSERT_EQUAL( "foo", f.GetFirstLine() );
  136. }
  137. void TextFileTestCase::ReadMac()
  138. {
  139. CreateTestFile("foo\rbar\r\rbaz");
  140. wxTextFile f;
  141. CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) );
  142. CPPUNIT_ASSERT_EQUAL( (size_t)4, f.GetLineCount() );
  143. CPPUNIT_ASSERT_EQUAL( wxTextFileType_Mac, f.GetLineType(0) );
  144. CPPUNIT_ASSERT_EQUAL( wxTextFileType_Mac, f.GetLineType(1) );
  145. CPPUNIT_ASSERT_EQUAL( wxTextFileType_Mac, f.GetLineType(2) );
  146. CPPUNIT_ASSERT_EQUAL( wxTextFileType_None, f.GetLineType(3) );
  147. CPPUNIT_ASSERT_EQUAL( wxString(wxT("foo")), f.GetLine(0) );
  148. CPPUNIT_ASSERT_EQUAL( wxString(wxT("bar")), f.GetLine(1) );
  149. CPPUNIT_ASSERT_EQUAL( wxString(wxT("")), f.GetLine(2) );
  150. CPPUNIT_ASSERT_EQUAL( wxString(wxT("baz")), f.GetLastLine() );
  151. }
  152. void TextFileTestCase::ReadMacLast()
  153. {
  154. CreateTestFile("foo\r");
  155. wxTextFile f;
  156. CPPUNIT_ASSERT( f.Open(GetTestFileName()) );
  157. CPPUNIT_ASSERT_EQUAL( 1, f.GetLineCount() );
  158. CPPUNIT_ASSERT_EQUAL( wxTextFileType_Mac, f.GetLineType(0) );
  159. CPPUNIT_ASSERT_EQUAL( "foo", f.GetFirstLine() );
  160. }
  161. void TextFileTestCase::ReadMixed()
  162. {
  163. CreateTestFile("foo\rbar\r\nbaz\n");
  164. wxTextFile f;
  165. CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) );
  166. CPPUNIT_ASSERT_EQUAL( (size_t)3, f.GetLineCount() );
  167. CPPUNIT_ASSERT_EQUAL( wxTextFileType_Mac, f.GetLineType(0) );
  168. CPPUNIT_ASSERT_EQUAL( wxTextFileType_Dos, f.GetLineType(1) );
  169. CPPUNIT_ASSERT_EQUAL( wxTextFileType_Unix, f.GetLineType(2) );
  170. CPPUNIT_ASSERT_EQUAL( wxString(wxT("foo")), f.GetFirstLine() );
  171. CPPUNIT_ASSERT_EQUAL( wxString(wxT("bar")), f.GetLine(1) );
  172. CPPUNIT_ASSERT_EQUAL( wxString(wxT("baz")), f.GetLastLine() );
  173. }
  174. void TextFileTestCase::ReadMixedWithFuzzing()
  175. {
  176. for ( int iteration = 0; iteration < 100; iteration++)
  177. {
  178. // Create a random buffer with lots of newlines. This is intended to catch
  179. // bad parsing in unexpected situations such as the one from ReadCRCRLF()
  180. // (which is so common it deserves a test of its own).
  181. static const char CHOICES[] = {'\r', '\n', 'X'};
  182. const size_t BUF_LEN = 100;
  183. char data[BUF_LEN + 1];
  184. data[0] = 'X';
  185. data[BUF_LEN] = '\0';
  186. unsigned linesCnt = 0;
  187. for ( size_t i = 1; i < BUF_LEN; i++ )
  188. {
  189. char ch = CHOICES[rand() % WXSIZEOF(CHOICES)];
  190. data[i] = ch;
  191. if ( ch == '\r' || (ch == '\n' && data[i-1] != '\r') )
  192. linesCnt++;
  193. }
  194. if (data[BUF_LEN-1] != '\r' && data[BUF_LEN-1] != '\n')
  195. linesCnt++; // last line was unterminated
  196. CreateTestFile(data);
  197. wxTextFile f;
  198. CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) );
  199. CPPUNIT_ASSERT_EQUAL( (size_t)linesCnt, f.GetLineCount() );
  200. }
  201. }
  202. void TextFileTestCase::ReadCRCRLF()
  203. {
  204. // Notepad may create files with CRCRLF line endings (see
  205. // http://stackoverflow.com/questions/6998506/text-file-with-0d-0d-0a-line-breaks).
  206. // Older versions of wx would loose all data when reading such files.
  207. // Test that the data are read, but don't worry about empty lines in between or
  208. // line endings. Also include a longer streak of CRs, because they can
  209. // happen as well.
  210. CreateTestFile("foo\r\r\nbar\r\r\r\nbaz\r\r\n");
  211. wxTextFile f;
  212. CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) );
  213. wxString all;
  214. for ( wxString str = f.GetFirstLine(); !f.Eof(); str = f.GetNextLine() )
  215. all += str;
  216. CPPUNIT_ASSERT_EQUAL( "foobarbaz", all );
  217. }
  218. #if wxUSE_UNICODE
  219. void TextFileTestCase::ReadUTF8()
  220. {
  221. CreateTestFile("\xd0\x9f\n"
  222. "\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82");
  223. wxTextFile f;
  224. CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName()), wxConvUTF8) );
  225. CPPUNIT_ASSERT_EQUAL( (size_t)2, f.GetLineCount() );
  226. CPPUNIT_ASSERT_EQUAL( wxTextFileType_Unix, f.GetLineType(0) );
  227. CPPUNIT_ASSERT_EQUAL( wxTextFileType_None, f.GetLineType(1) );
  228. #ifdef wxHAVE_U_ESCAPE
  229. CPPUNIT_ASSERT_EQUAL( wxString(L"\u041f"), f.GetFirstLine() );
  230. CPPUNIT_ASSERT_EQUAL( wxString(L"\u0440\u0438\u0432\u0435\u0442"),
  231. f.GetLastLine() );
  232. #endif // wxHAVE_U_ESCAPE
  233. }
  234. void TextFileTestCase::ReadUTF16()
  235. {
  236. CreateTestFile(16,
  237. "\x1f\x04\x0d\x00\x0a\x00"
  238. "\x40\x04\x38\x04\x32\x04\x35\x04\x42\x04");
  239. wxTextFile f;
  240. wxMBConvUTF16LE conv;
  241. CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName()), conv) );
  242. CPPUNIT_ASSERT_EQUAL( (size_t)2, f.GetLineCount() );
  243. CPPUNIT_ASSERT_EQUAL( wxTextFileType_Dos, f.GetLineType(0) );
  244. CPPUNIT_ASSERT_EQUAL( wxTextFileType_None, f.GetLineType(1) );
  245. #ifdef wxHAVE_U_ESCAPE
  246. CPPUNIT_ASSERT_EQUAL( wxString(L"\u041f"), f.GetFirstLine() );
  247. CPPUNIT_ASSERT_EQUAL( wxString(L"\u0440\u0438\u0432\u0435\u0442"),
  248. f.GetLastLine() );
  249. #endif // wxHAVE_U_ESCAPE
  250. }
  251. #endif // wxUSE_UNICODE
  252. void TextFileTestCase::ReadBig()
  253. {
  254. static const size_t NUM_LINES = 10000;
  255. {
  256. wxFFile f(GetTestFileName(), "w");
  257. for ( size_t n = 0; n < NUM_LINES; n++ )
  258. {
  259. fprintf(f.fp(), "Line %lu\n", (unsigned long)n + 1);
  260. }
  261. }
  262. wxTextFile f;
  263. CPPUNIT_ASSERT( f.Open(GetTestFileName()) );
  264. CPPUNIT_ASSERT_EQUAL( NUM_LINES, f.GetLineCount() );
  265. CPPUNIT_ASSERT_EQUAL( wxString("Line 1"), f[0] );
  266. CPPUNIT_ASSERT_EQUAL( wxString("Line 999"), f[998] );
  267. CPPUNIT_ASSERT_EQUAL( wxString("Line 1000"), f[999] );
  268. CPPUNIT_ASSERT_EQUAL( wxString::Format("Line %lu", (unsigned long)NUM_LINES),
  269. f[NUM_LINES - 1] );
  270. }
  271. #endif // wxUSE_TEXTFILE