filetest.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: tests/file/filetest.cpp
  3. // Purpose: wxFile unit test
  4. // Author: Vadim Zeitlin
  5. // Created: 2009-09-12
  6. // Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
  7. ///////////////////////////////////////////////////////////////////////////////
  8. // ----------------------------------------------------------------------------
  9. // headers
  10. // ----------------------------------------------------------------------------
  11. #include "testprec.h"
  12. #ifdef __BORLANDC__
  13. #pragma hdrstop
  14. #endif
  15. #if wxUSE_FILE
  16. #include "wx/file.h"
  17. #include "testfile.h"
  18. // ----------------------------------------------------------------------------
  19. // test class
  20. // ----------------------------------------------------------------------------
  21. class FileTestCase : public CppUnit::TestCase
  22. {
  23. public:
  24. FileTestCase() { }
  25. private:
  26. CPPUNIT_TEST_SUITE( FileTestCase );
  27. CPPUNIT_TEST( ReadAll );
  28. #if wxUSE_UNICODE
  29. CPPUNIT_TEST( RoundTripUTF8 );
  30. CPPUNIT_TEST( RoundTripUTF16 );
  31. CPPUNIT_TEST( RoundTripUTF32 );
  32. #endif // wxUSE_UNICODE
  33. CPPUNIT_TEST( TempFile );
  34. CPPUNIT_TEST_SUITE_END();
  35. void ReadAll();
  36. #if wxUSE_UNICODE
  37. void RoundTripUTF8() { DoRoundTripTest(wxConvUTF8); }
  38. void RoundTripUTF16() { DoRoundTripTest(wxMBConvUTF16()); }
  39. void RoundTripUTF32() { DoRoundTripTest(wxMBConvUTF32()); }
  40. #endif // wxUSE_UNICODE
  41. void DoRoundTripTest(const wxMBConv& conv);
  42. void TempFile();
  43. wxDECLARE_NO_COPY_CLASS(FileTestCase);
  44. };
  45. // ----------------------------------------------------------------------------
  46. // CppUnit macros
  47. // ----------------------------------------------------------------------------
  48. CPPUNIT_TEST_SUITE_REGISTRATION( FileTestCase );
  49. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileTestCase, "FileTestCase" );
  50. // ----------------------------------------------------------------------------
  51. // tests implementation
  52. // ----------------------------------------------------------------------------
  53. void FileTestCase::ReadAll()
  54. {
  55. TestFile tf;
  56. const char* text = "Ream\nde";
  57. {
  58. wxFile fout(tf.GetName(), wxFile::write);
  59. CPPUNIT_ASSERT( fout.IsOpened() );
  60. fout.Write(text, strlen(text));
  61. CPPUNIT_ASSERT( fout.Close() );
  62. }
  63. {
  64. wxFile fin(tf.GetName(), wxFile::read);
  65. CPPUNIT_ASSERT( fin.IsOpened() );
  66. wxString s;
  67. CPPUNIT_ASSERT( fin.ReadAll(&s) );
  68. CPPUNIT_ASSERT_EQUAL( text, s );
  69. }
  70. }
  71. #if wxUSE_UNICODE
  72. void FileTestCase::DoRoundTripTest(const wxMBConv& conv)
  73. {
  74. TestFile tf;
  75. // Explicit length is needed because of the embedded NUL.
  76. const wxString data("Hello\0UTF!", 10);
  77. {
  78. wxFile fout(tf.GetName(), wxFile::write);
  79. CPPUNIT_ASSERT( fout.IsOpened() );
  80. CPPUNIT_ASSERT( fout.Write(data, conv) );
  81. }
  82. {
  83. wxFile fin(tf.GetName(), wxFile::read);
  84. CPPUNIT_ASSERT( fin.IsOpened() );
  85. const ssize_t len = fin.Length();
  86. wxCharBuffer buf(len);
  87. CPPUNIT_ASSERT_EQUAL( len, fin.Read(buf.data(), len) );
  88. wxString dataReadBack(buf, conv, len);
  89. CPPUNIT_ASSERT_EQUAL( data, dataReadBack );
  90. }
  91. }
  92. #endif // wxUSE_UNICODE
  93. void FileTestCase::TempFile()
  94. {
  95. wxTempFile tmpFile;
  96. CPPUNIT_ASSERT( tmpFile.Open(wxT("test2")) && tmpFile.Write(wxT("the answer is 42")) );
  97. CPPUNIT_ASSERT( tmpFile.Commit() );
  98. CPPUNIT_ASSERT( wxRemoveFile(wxT("test2")) );
  99. }
  100. #endif // wxUSE_FILE