ffilestream.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: tests/streams/ffilestream.cpp
  3. // Purpose: Test wxFFileInputStream/wxFFileOutputStream
  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_FFILEINSTREAM = wxT("ffileinstream.test");
  22. static const wxString FILENAME_FFILEOUTSTREAM = wxT("ffileoutstream.test");
  23. ///////////////////////////////////////////////////////////////////////////////
  24. // The test case
  25. //
  26. // Try to fully test wxFFileInputStream and wxFFileOutputStream
  27. class ffileStream : public BaseStreamTestCase<wxFFileInputStream, wxFFileOutputStream>
  28. {
  29. public:
  30. ffileStream();
  31. virtual ~ffileStream();
  32. CPPUNIT_TEST_SUITE(ffileStream);
  33. // Base class stream tests the ffileStream 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 wxFFileInputStream *DoCreateInStream();
  56. virtual wxFFileOutputStream *DoCreateOutStream();
  57. virtual void DoDeleteOutStream();
  58. private:
  59. wxString GetInFileName() const;
  60. };
  61. ffileStream::ffileStream()
  62. {
  63. m_bSeekInvalidBeyondEnd = false;
  64. m_bEofAtLastRead = false;
  65. }
  66. ffileStream::~ffileStream()
  67. {
  68. // Remove the temp test file...
  69. ::wxRemoveFile(FILENAME_FFILEINSTREAM);
  70. ::wxRemoveFile(FILENAME_FFILEOUTSTREAM);
  71. }
  72. wxFFileInputStream *ffileStream::DoCreateInStream()
  73. {
  74. wxFFileInputStream *pFileInStream = new wxFFileInputStream(GetInFileName());
  75. CPPUNIT_ASSERT(pFileInStream->IsOk());
  76. return pFileInStream;
  77. }
  78. wxFFileOutputStream *ffileStream::DoCreateOutStream()
  79. {
  80. wxFFileOutputStream *pFileOutStream = new wxFFileOutputStream(FILENAME_FFILEOUTSTREAM);
  81. CPPUNIT_ASSERT(pFileOutStream->IsOk());
  82. return pFileOutStream;
  83. }
  84. void ffileStream::DoDeleteOutStream()
  85. {
  86. ::wxRemoveFile(FILENAME_FFILEOUTSTREAM);
  87. }
  88. wxString ffileStream::GetInFileName() const
  89. {
  90. static bool bFileCreated = false;
  91. if (!bFileCreated)
  92. {
  93. // Create the file only once
  94. bFileCreated = true;
  95. // Make sure we have a input file...
  96. char buf[DATABUFFER_SIZE];
  97. wxFFileOutputStream out(FILENAME_FFILEINSTREAM);
  98. // Init the data buffer.
  99. for (size_t i = 0; i < DATABUFFER_SIZE; i++)
  100. buf[i] = (i % 0xFF);
  101. // Save the data
  102. out.Write(buf, DATABUFFER_SIZE);
  103. }
  104. return FILENAME_FFILEINSTREAM;
  105. }
  106. // Register the stream sub suite, by using some stream helper macro.
  107. // Note: Don't forget to connect it to the base suite (See: bstream.cpp => StreamCase::suite())
  108. STREAM_TEST_SUBSUITE_NAMED_REGISTRATION(ffileStream)