filestream.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: tests/streams/filestream.cpp
  3. // Purpose: Test wxFileInputStream/wxFileOutputStream
  4. // Author: Hans Van Leemputten
  5. // Copyright: (c) 2004 Hans Van Leemputten
  6. // Licence: wxWindows licence
  7. ///////////////////////////////////////////////////////////////////////////////
  8. // For compilers that support precompilation, includes "wx/wx.h".
  9. // and "wx/cppunit.h"
  10. #include "testprec.h"
  11. #ifdef __BORLANDC__
  12. #pragma hdrstop
  13. #endif
  14. // for all others, include the necessary headers
  15. #ifndef WX_PRECOMP
  16. #include "wx/wx.h"
  17. #endif
  18. #include "wx/wfstream.h"
  19. #include "bstream.h"
  20. #define DATABUFFER_SIZE 1024
  21. static const wxString FILENAME_FILEINSTREAM = wxT("fileinstream.test");
  22. static const wxString FILENAME_FILEOUTSTREAM = wxT("fileoutstream.test");
  23. ///////////////////////////////////////////////////////////////////////////////
  24. // The test case
  25. //
  26. // Try to fully test wxFileInputStream and wxFileOutputStream
  27. class fileStream : public BaseStreamTestCase<wxFileInputStream, wxFileOutputStream>
  28. {
  29. public:
  30. fileStream();
  31. virtual ~fileStream();
  32. CPPUNIT_TEST_SUITE(fileStream);
  33. // Base class stream tests the fileStream supports.
  34. CPPUNIT_TEST(Input_GetSize);
  35. CPPUNIT_TEST(Input_GetC);
  36. CPPUNIT_TEST(Input_Read);
  37. CPPUNIT_TEST(Input_Eof);
  38. CPPUNIT_TEST(Input_LastRead);
  39. CPPUNIT_TEST(Input_CanRead);
  40. CPPUNIT_TEST(Input_SeekI);
  41. CPPUNIT_TEST(Input_TellI);
  42. CPPUNIT_TEST(Input_Peek);
  43. CPPUNIT_TEST(Input_Ungetch);
  44. CPPUNIT_TEST(Output_PutC);
  45. CPPUNIT_TEST(Output_Write);
  46. CPPUNIT_TEST(Output_LastWrite);
  47. CPPUNIT_TEST(Output_SeekO);
  48. CPPUNIT_TEST(Output_TellO);
  49. // Other test specific for File stream test case.
  50. CPPUNIT_TEST_SUITE_END();
  51. protected:
  52. // Add own test here.
  53. private:
  54. // Implement base class functions.
  55. virtual wxFileInputStream *DoCreateInStream();
  56. virtual wxFileOutputStream *DoCreateOutStream();
  57. virtual void DoDeleteOutStream();
  58. private:
  59. wxString GetInFileName() const;
  60. };
  61. fileStream::fileStream()
  62. {
  63. m_bSeekInvalidBeyondEnd = false;
  64. }
  65. fileStream::~fileStream()
  66. {
  67. // Remove the temp test file...
  68. ::wxRemoveFile(FILENAME_FILEINSTREAM);
  69. ::wxRemoveFile(FILENAME_FILEOUTSTREAM);
  70. }
  71. wxFileInputStream *fileStream::DoCreateInStream()
  72. {
  73. wxFileInputStream *pFileInStream = new wxFileInputStream(GetInFileName());
  74. CPPUNIT_ASSERT(pFileInStream->IsOk());
  75. return pFileInStream;
  76. }
  77. wxFileOutputStream *fileStream::DoCreateOutStream()
  78. {
  79. wxFileOutputStream *pFileOutStream = new wxFileOutputStream(FILENAME_FILEOUTSTREAM);
  80. CPPUNIT_ASSERT(pFileOutStream->IsOk());
  81. return pFileOutStream;
  82. }
  83. void fileStream::DoDeleteOutStream()
  84. {
  85. ::wxRemoveFile(FILENAME_FILEOUTSTREAM);
  86. }
  87. wxString fileStream::GetInFileName() const
  88. {
  89. static bool bFileCreated = false;
  90. if (!bFileCreated)
  91. {
  92. // Create the file only once
  93. bFileCreated = true;
  94. // Make sure we have a input file...
  95. char buf[DATABUFFER_SIZE];
  96. wxFileOutputStream out(FILENAME_FILEINSTREAM);
  97. // Init the data buffer.
  98. for (size_t i = 0; i < DATABUFFER_SIZE; i++)
  99. buf[i] = (i % 0xFF);
  100. // Save the data
  101. out.Write(buf, DATABUFFER_SIZE);
  102. }
  103. return FILENAME_FILEINSTREAM;
  104. }
  105. // Register the stream sub suite, by using some stream helper macro.
  106. // Note: Don't forget to connect it to the base suite (See: bstream.cpp => StreamCase::suite())
  107. STREAM_TEST_SUBSUITE_NAMED_REGISTRATION(fileStream)