]> git.saurik.com Git - wxWidgets.git/blob - tests/streams/tempfile.cpp
cleanup
[wxWidgets.git] / tests / streams / tempfile.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/streams/tempfile.cpp
3 // Purpose: Test wxTempFileOutputStream
4 // Author: Mike Wetherell
5 // RCS-ID: $Id$
6 // Copyright: (c) 2005 Mike Wetherell
7 // Licence: wxWidgets licence
8 ///////////////////////////////////////////////////////////////////////////////
9
10 #include "testprec.h"
11
12 #ifdef __BORLANDC__
13 #pragma hdrstop
14 #endif
15
16 // for all others, include the necessary headers
17 #ifndef WX_PRECOMP
18 #include "wx/wx.h"
19 #endif
20
21 #include "wx/wfstream.h"
22 #include "wx/filename.h"
23 #include "bstream.h"
24
25 #if wxUSE_STREAMS && wxUSE_FILE
26
27
28 ///////////////////////////////////////////////////////////////////////////////
29 // Self deleting test file
30
31 class TestFile
32 {
33 public:
34 TestFile();
35 ~TestFile() { if (wxFileExists(m_name)) wxRemoveFile(m_name); }
36 wxString GetName() const { return m_name; }
37 private:
38 wxString m_name;
39 };
40
41 // Initialise with a test pattern so we can see if the file is replaced
42 //
43 TestFile::TestFile()
44 {
45 wxFile file;
46 m_name = wxFileName::CreateTempFileName(_T("wxtest"), &file);
47 file.Write("Before", 6);
48 }
49
50
51 ///////////////////////////////////////////////////////////////////////////////
52 // The test case
53
54 class tempStream : public CppUnit::TestCase
55 {
56 CPPUNIT_TEST_SUITE(tempStream);
57 CPPUNIT_TEST(DoNothing);
58 CPPUNIT_TEST(Close);
59 CPPUNIT_TEST(Commit);
60 CPPUNIT_TEST(Discard);
61 CPPUNIT_TEST_SUITE_END();
62
63 void DoNothing() { DoTest(DONOTHING, false); }
64 void Close() { DoTest(CLOSE, true); }
65 void Commit() { DoTest(COMMIT, true); }
66 void Discard() { DoTest(DISCARD, false); }
67
68 enum Action { DONOTHING, CLOSE, COMMIT, DISCARD };
69 void DoTest(Action action, bool shouldHaveCommited);
70 };
71
72 // the common test code
73 //
74 void tempStream::DoTest(Action action, bool shouldHaveCommited)
75 {
76 TestFile temp;
77
78 {
79 wxTempFileOutputStream out(temp.GetName());
80 out.Write("Affer", 5);
81 CPPUNIT_ASSERT(out.SeekO(2) == 2);
82 out.Write("t", 1);
83 CPPUNIT_ASSERT(out.IsSeekable());
84 CPPUNIT_ASSERT(out.GetLength() == 5);
85 CPPUNIT_ASSERT(out.TellO() == 3);
86
87 switch (action) {
88 case DONOTHING: break;
89 case COMMIT: out.Commit(); break;
90 case DISCARD: out.Discard(); break;
91 case CLOSE: out.Close();
92 }
93 }
94
95 wxFileInputStream in(temp.GetName());
96 char buf[32];
97 in.Read(buf, sizeof(buf));
98 buf[in.LastRead()] = 0;
99 CPPUNIT_ASSERT(strcmp(buf, shouldHaveCommited ? "After" : "Before") == 0);
100 }
101
102
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)
106
107 #endif // wxUSE_STREAMS && wxUSE_FILE