]>
git.saurik.com Git - wxWidgets.git/blob - tests/streams/tempfile.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/streams/tempfile.cpp
3 // Purpose: Test wxTempFileOutputStream
4 // Author: Mike Wetherell
6 // Copyright: (c) 2005 Mike Wetherell
7 // Licence: wxWidgets licence
8 ///////////////////////////////////////////////////////////////////////////////
16 // for all others, include the necessary headers
21 #include "wx/wfstream.h"
22 #include "wx/filename.h"
25 #if wxUSE_STREAMS && wxUSE_FILE
28 ///////////////////////////////////////////////////////////////////////////////
29 // Self deleting test file
35 ~TestFile() { if (wxFileExists(m_name
)) wxRemoveFile(m_name
); }
36 wxString
GetName() const { return m_name
; }
41 // Initialise with a test pattern so we can see if the file is replaced
46 m_name
= wxFileName::CreateTempFileName(wxT("wxtest"), &file
);
47 file
.Write("Before", 6);
51 ///////////////////////////////////////////////////////////////////////////////
54 class tempStream
: public CppUnit::TestCase
56 CPPUNIT_TEST_SUITE(tempStream
);
57 CPPUNIT_TEST(DoNothing
);
60 CPPUNIT_TEST(Discard
);
61 CPPUNIT_TEST_SUITE_END();
63 void DoNothing() { DoTest(DONOTHING
, false); }
64 void Close() { DoTest(CLOSE
, true); }
65 void Commit() { DoTest(COMMIT
, true); }
66 void Discard() { DoTest(DISCARD
, false); }
68 enum Action
{ DONOTHING
, CLOSE
, COMMIT
, DISCARD
};
69 void DoTest(Action action
, bool shouldHaveCommited
);
72 // the common test code
74 void tempStream::DoTest(Action action
, bool shouldHaveCommited
)
79 wxTempFileOutputStream
out(temp
.GetName());
80 out
.Write("Affer", 5);
81 CPPUNIT_ASSERT(out
.SeekO(2) == 2);
83 CPPUNIT_ASSERT(out
.IsSeekable());
84 CPPUNIT_ASSERT(out
.GetLength() == 5);
85 CPPUNIT_ASSERT(out
.TellO() == 3);
88 case DONOTHING
: break;
89 case COMMIT
: out
.Commit(); break;
90 case DISCARD
: out
.Discard(); break;
91 case CLOSE
: out
.Close();
95 wxFileInputStream
in(temp
.GetName());
97 in
.Read(buf
, sizeof(buf
));
98 buf
[in
.LastRead()] = 0;
99 CPPUNIT_ASSERT(strcmp(buf
, shouldHaveCommited
? "After" : "Before") == 0);
103 // Register the stream sub suite, by using some stream helper macro.
104 // Note: Don't forget to connect it to the base suite (See: bstream.cpp => StreamCase::suite())
105 STREAM_TEST_SUBSUITE_NAMED_REGISTRATION(tempStream
)
107 #endif // wxUSE_STREAMS && wxUSE_FILE