tempfile.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: tests/streams/tempfile.cpp
  3. // Purpose: Test wxTempFileOutputStream
  4. // Author: Mike Wetherell
  5. // Copyright: (c) 2005 Mike Wetherell
  6. // Licence: wxWindows licence
  7. ///////////////////////////////////////////////////////////////////////////////
  8. #include "testprec.h"
  9. #ifdef __BORLANDC__
  10. #pragma hdrstop
  11. #endif
  12. // for all others, include the necessary headers
  13. #ifndef WX_PRECOMP
  14. #include "wx/wx.h"
  15. #endif
  16. #include "wx/wfstream.h"
  17. #include "wx/filename.h"
  18. #include "bstream.h"
  19. #if wxUSE_STREAMS && wxUSE_FILE
  20. #include "testfile.h"
  21. ///////////////////////////////////////////////////////////////////////////////
  22. // The test case
  23. class tempStream : public CppUnit::TestCase
  24. {
  25. CPPUNIT_TEST_SUITE(tempStream);
  26. CPPUNIT_TEST(DoNothing);
  27. CPPUNIT_TEST(Close);
  28. CPPUNIT_TEST(Commit);
  29. CPPUNIT_TEST(Discard);
  30. CPPUNIT_TEST_SUITE_END();
  31. void DoNothing() { DoTest(DONOTHING, false); }
  32. void Close() { DoTest(CLOSE, true); }
  33. void Commit() { DoTest(COMMIT, true); }
  34. void Discard() { DoTest(DISCARD, false); }
  35. enum Action { DONOTHING, CLOSE, COMMIT, DISCARD };
  36. void DoTest(Action action, bool shouldHaveCommited);
  37. };
  38. // the common test code
  39. //
  40. void tempStream::DoTest(Action action, bool shouldHaveCommited)
  41. {
  42. TestFile temp;
  43. {
  44. wxTempFileOutputStream out(temp.GetName());
  45. out.Write("Affer", 5);
  46. CPPUNIT_ASSERT(out.SeekO(2) == 2);
  47. out.Write("t", 1);
  48. CPPUNIT_ASSERT(out.IsSeekable());
  49. CPPUNIT_ASSERT(out.GetLength() == 5);
  50. CPPUNIT_ASSERT(out.TellO() == 3);
  51. switch (action) {
  52. case DONOTHING: break;
  53. case COMMIT: out.Commit(); break;
  54. case DISCARD: out.Discard(); break;
  55. case CLOSE: out.Close();
  56. }
  57. }
  58. wxFileInputStream in(temp.GetName());
  59. char buf[32];
  60. in.Read(buf, sizeof(buf));
  61. buf[in.LastRead()] = 0;
  62. CPPUNIT_ASSERT(strcmp(buf, shouldHaveCommited ? "After" : "Before") == 0);
  63. }
  64. // Register the stream sub suite, by using some stream helper macro.
  65. // Note: Don't forget to connect it to the base suite (See: bstream.cpp => StreamCase::suite())
  66. STREAM_TEST_SUBSUITE_NAMED_REGISTRATION(tempStream)
  67. #endif // wxUSE_STREAMS && wxUSE_FILE